class BIOGEME: examples of use of each function

This webpage is for programmers who need examples of use of the functions of the class. The examples are designed to illustrate the syntax. They do not correspond to any meaningful model. For examples of models, visit biogeme.epfl.ch.

In [1]:
import datetime
print(datetime.datetime.now())
2019-09-25 13:57:30.637783
In [2]:
import biogeme.version as ver
print(ver.getText())
biogeme 3.2.2 [September 25, 2019]
Version entirely written in Python
Home page: http://biogeme.epfl.ch
Submit questions to https://groups.google.com/d/forum/biogeme
Michel Bierlaire, Transport and Mobility Laboratory, Ecole Polytechnique Fédérale de Lausanne (EPFL)

In [3]:
import biogeme.biogeme as bio
import biogeme.database as db
import pandas as pd
import numpy as np
from biogeme.expressions import *

Definition of a database

In [4]:
df = pd.DataFrame({'Person':[1,1,1,2,2],
                   'Exclude':[0,0,1,0,1],
                   'Variable1':[1,2,3,4,5],
                   'Variable2':[10,20,30,40,50],
                   'Choice':[1,2,3,1,2],
                   'Av1':[0,1,1,1,1],
                   'Av2':[1,1,1,1,1],
                   'Av3':[0,1,1,1,1]})
myData = db.Database('test',df)

Definition of various expressions

In [5]:
Variable1=Variable('Variable1')
Variable2=Variable('Variable2')
beta1 = Beta('beta1',-1.0,-3,3,0)
beta2 = Beta('beta2',2.0,-3,10,0)
likelihood = -beta1**2 * Variable1 - exp(beta2*beta1) * Variable2 - beta2**4
simul = beta1 / Variable1 + beta2 / Variable2
dictOfExpressions = {'loglike':likelihood,'beta1':beta1,'simul':simul}

Creation of the BIOGEME object

In [6]:
myBiogeme = bio.BIOGEME(myData,dictOfExpressions)
myBiogeme.modelName = 'simpleExample'
print(myBiogeme)
simpleExample: database [test]{'loglike': ((((-(beta1(-1.0) ** `2`)) * Variable1) - (exp((beta2(2.0) * beta1(-1.0))) * Variable2)) - (beta2(2.0) ** `4`)), 'beta1': beta1(-1.0), 'simul': ((beta1(-1.0) / Variable1) + (beta2(2.0) / Variable2))}
simpleExample: database [test]{'loglike': ((((-(beta1(-1.0) ** `2`)) * Variable1) - (exp((beta2(2.0) * beta1(-1.0))) * Variable2)) - (beta2(2.0) ** `4`)), 'beta1': beta1(-1.0), 'simul': ((beta1(-1.0) / Variable1) + (beta2(2.0) / Variable2))}

Note that, by default, Biogeme removes the unused variables from the database to optimize space.

In [7]:
myBiogeme.database.data.columns
Out[7]:
Index(['Variable1', 'Variable2'], dtype='object')

calculateInitLikelihood

In [8]:
myBiogeme.calculateInitLikelihood()
Out[8]:
-115.30029248549191

calculateLikelihood

In [9]:
x = myBiogeme.betaInitValues
xplus = [v+1 for v in x]
print(xplus)
[0.0, 3.0]
In [10]:
myBiogeme.calculateLikelihood(xplus)
Out[10]:
-555.0

calculateLikelihoodAndDerivatives

In [11]:
f,g,h,bhhh = myBiogeme.calculateLikelihoodAndDerivatives(xplus,hessian=True,bhhh=True)
print(f'f={f}')
print(f'g={g}')
print(f'h={h}')
print(f'bhhh={bhhh}')
f=-555.0
g=[-450. -540.]
h=[[-1350.  -150.]
 [ -150.  -540.]]
bhhh=[[49500. 48600.]
 [48600. 58320.]]

likelihoodFiniteDifferenceHessian

In [12]:
myBiogeme.likelihoodFiniteDifferenceHessian(xplus)
Out[12]:
array([[-1380.00020229,  -150.        ],
       [ -150.0000451 ,  -540.00005396]])

checkDerivatives

In [13]:
f,g,h,gdiff,hdiff = myBiogeme.checkDerivatives(verbose=True)
In [14]:
print(f'f={f}')
print(f'g={g}')
print(f'h={h}')
print(f'gdiff={gdiff}')
print(f'hdiff={hdiff}')
hdiff
f=-115.30029248549191
g=[ -10.60058497 -139.69970751]
h=[[-111.20116994   20.30029249]
 [  20.30029249 -260.30029249]]
gdiff=[-5.42793187e-06  2.60800035e-05]
hdiff=[[-8.04552171e-06  7.36597983e-09]
 [-1.61387920e-07  2.22928137e-05]]
Out[14]:
array([[-8.04552171e-06,  7.36597983e-09],
       [-1.61387920e-07,  2.22928137e-05]])

estimate

In [15]:
results = myBiogeme.estimate(bootstrap=10)
In [16]:
results.getEstimatedParameters()
Out[16]:
Value Std err t-test p-value Rob. Std err Rob. t-test Rob. p-value Bootstrap[10] Std err Bootstrap t-test Bootstrap p-value
beta1 -1.273264 0.115144 -11.057997 0.0 0.013724 -92.776669 0.0 0.014712 -86.547687 0.0
beta2 1.248769 0.084830 14.720836 0.0 0.059086 21.134794 0.0 0.063728 19.595403 0.0

simulate

In [17]:
# Simulate with the default values for the parameters
simulationWithDefaultBetas = myBiogeme.simulate()
simulationWithDefaultBetas
Out[17]:
loglike beta1 simul
0 -18.353353 -1.0 -0.800000
1 -20.706706 -1.0 -0.400000
2 -23.060058 -1.0 -0.266667
3 -25.413411 -1.0 -0.200000
4 -27.766764 -1.0 -0.160000
In [18]:
# Simulate with the estimated values for the parameters
print(results.getBetaValues())
simulationWithEstimatedBetas = myBiogeme.simulate(results.getBetaValues())
simulationWithEstimatedBetas
{'beta1': -1.2732639841254711, 'beta2': 1.248768808907056}
Out[18]:
loglike beta1 simul
0 -6.092234 -1.273264 -1.148387
1 -9.752666 -1.273264 -0.574194
2 -13.413098 -1.273264 -0.382796
3 -17.073530 -1.273264 -0.287097
4 -20.733962 -1.273264 -0.229677

confidenceIntervals

In [19]:
drawsFromBetas = results.getBetasForSensitivityAnalysis(myBiogeme.freeBetaNames)
left, right = myBiogeme.confidenceIntervals(drawsFromBetas)
left
Out[19]:
loglike beta1 simul
0 -6.620136 -1.298998 -1.185201
1 -10.068725 -1.298998 -0.592600
2 -13.612312 -1.298998 -0.395067
3 -17.548305 -1.298998 -0.296300
4 -21.516123 -1.298998 -0.237040
In [20]:
right
Out[20]:
loglike beta1 simul
0 -5.644849 -1.253359 -1.119912
1 -9.616986 -1.253359 -0.559956
2 -13.414078 -1.253359 -0.373304
3 -16.975655 -1.253359 -0.279978
4 -20.414494 -1.253359 -0.223982