class bioResults: 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-12-29 21:16:14.673595
In [2]:
import biogeme.version as ver
print(ver.getText())
biogeme 3.2.5 [2019-12-29]
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 biogeme.results as res
import pandas as pd
import numpy as np
from biogeme.expressions import Beta, Variable, exp

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'
results = myBiogeme.estimate(bootstrap=10)
print(results)
Results for model [simpleExample]
Output file (HTML):			simpleExample~00.html
Nbr of parameters:		2
Sample size:			5
Excluded data:			0
Init log likelihood:		-115.3003
Final log likelihood:		-67.06549
Likelihood ratio test:		96.4696
Rho square:			0.418
Rho bar square:			0.401
Akaike Information Criterion:	138.131
Bayesian Information Criterion:	137.3499
Final gradient norm:		2.764804e-07
beta1          : -1.27[0.115 -11.1 0][0.0137 -92.8 0][0.012 -106 0]
beta2          : 1.25[0.0848 14.7 0][0.0591 21.1 0][0.0523 23.9 0]
('beta2', 'beta1'):	0.00167	0.171	19.3	0	0.000811	1	55.6	0

Dump results on a file

In [7]:
f = results.writePickle()
print(f)
simpleExample~01.pickle

Results can be imported from a file previously generated

In [8]:
readResults = res.bioResults(pickleFile=f)
print(readResults)
Results for model [simpleExample]
Output file (HTML):			simpleExample~00.html
Nbr of parameters:		2
Sample size:			5
Excluded data:			0
Init log likelihood:		-115.3003
Final log likelihood:		-67.06549
Likelihood ratio test:		96.4696
Rho square:			0.418
Rho bar square:			0.401
Akaike Information Criterion:	138.131
Bayesian Information Criterion:	137.3499
Final gradient norm:		2.764804e-07
beta1          : -1.27[0.115 -11.1 0][0.0137 -92.8 0][0.012 -106 0]
beta2          : 1.25[0.0848 14.7 0][0.0591 21.1 0][0.0523 23.9 0]
('beta2', 'beta1'):	0.00167	0.171	19.3	0	0.000811	1	55.6	0

Results can be formatted in LaTeX

In [9]:
print(readResults.getLaTeX())
%% This file is designed to be included into a LaTeX document
%% See http://www.latex-project.org/ for information about LaTeX
%% biogeme 3.2.5 [2019-12-29]
%% 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)

%% This file has automatically been generated on 2019-12-29 21:16:15.396491</p>

%%Database name: test

%% General statistics
\section{General statistics}
\begin{tabular}{ll}
Number of estimated parameters & 2 \\
Sample size & 5 \\
Excluded observations & 0 \\
Init log likelihood & -115.3003 \\
Final log likelihood & -67.06549 \\
Likelihood ratio test for the init. model & 96.4696 \\
Rho-square for the init. model & 0.418 \\
Rho-square-bar for the init. model & 0.401 \\
Akaike Information Criterion & 138.131 \\
Bayesian Information Criterion & 137.3499 \\
Final gradient norm & 2.7648E-07 \\
Diagnostic & b'CONVERGENCE: NORM\_OF\_PROJECTED\_GRADIENT\_<=\_PGTOL' \\
Database readings & 8 \\
Iterations & 5 \\
Optimization time & 0:00:00.003452 \\
Bootstrapping time & 0:00:00.030784 \\
Nbr of threads & 8 \\
\end{tabular}

%%Parameter estimates
\section{Parameter estimates}
\begin{tabular}{lrrrrrrrrrr}
\toprule
{} &  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 \\
\midrule
beta1 &  -1.27 &    0.115 &   -11.1 &      0.0 &        0.0137 &        -92.8 &           0.0 &                  0.012 &            -106.0 &                0.0 \\
beta2 &   1.25 &   0.0848 &    14.7 &      0.0 &        0.0591 &         21.1 &           0.0 &                 0.0523 &              23.9 &                0.0 \\
\bottomrule
\end{tabular}

%%Correlation
\section{Correlation}
\begin{tabular}{lrrrrrrrrrrrr}
\toprule
{} &  Covariance &  Correlation &  t-test &  p-value &  Rob. cov. &  Rob. corr. &  Rob. t-test &  Rob. p-value &  Boot. cov. &  Boot. corr. &  Boot. t-test &  Boot. p-value \\
\midrule
beta2-beta1 &     0.00167 &        0.171 &    19.3 &      0.0 &   0.000811 &         1.0 &         55.6 &           0.0 &    0.000628 &          1.0 &          62.5 &            0.0 \\
\bottomrule
\end{tabular}

Results can be formatted in HTML

In [10]:
print(readResults.getHtml())
<html><head><script src='http://transp-or.epfl.ch/biogeme/sorttable.js'></script><meta http-equiv='Content-Type' content='text/html; charset=utf-8' /><title>simpleExample - Report from biogeme 3.2.5 [2019-12-29]</title><meta name='keywords' content='biogeme, discrete choice, random utility'><meta name='description' content='Report from biogeme 3.2.5 [2019-12-29]'><meta name='author' content='Michel Bierlaire'><style type=text/css>.biostyle	{font-size:10.0pt;	font-weight:400;	font-style:normal;	font-family:Courier;}.boundstyle	{font-size:10.0pt;	font-weight:400;	font-style:normal;	font-family:Courier;        color:red}</style></head><body bgcolor='#ffffff'><p>biogeme 3.2.5 [2019-12-29]</p>
<p><a href='https://www.python.org/' target='_blank'>Python</a> package</p>
<p>Home page: <a href='http://biogeme.epfl.ch' target='_blank'>http://biogeme.epfl.ch</a></p>
<p>Submit questions to <a href='https://groups.google.com/d/forum/biogeme' target='_blank'>https://groups.google.com/d/forum/biogeme</a></p>
<p><a href='http://people.epfl.ch/michel.bierlaire'>Michel Bierlaire</a>, <a href='http://transp-or.epfl.ch'>Transport and Mobility Laboratory</a>, <a href='http://www.epfl.ch'>Ecole Polytechnique F&#233;d&#233;rale de Lausanne (EPFL)</a></p>
<p>This file has automatically been generated on 2019-12-29 21:16:15.421544</p>
<p>If you drag this HTML file into the Calc application of <a href='http://www.openoffice.org/' target='_blank'>OpenOffice</a>, or the spreadsheet of <a href='https://www.libreoffice.org/' target='_blank'>LibreOffice</a>, you will be able to perform additional calculations.</p>
<table>
<tr class=biostyle><td align=right><strong>Report file</strong>:	</td><td>simpleExample~00.html</td></tr>
<tr class=biostyle><td align=right><strong>Database name</strong>:	</td><td>test</td></tr>
</table>
<h1>Estimation report</h1>
<table border='0'>
<tr class=biostyle><td align=right ><strong>Number of estimated parameters</strong>: </td> <td>2</td></tr>
<tr class=biostyle><td align=right ><strong>Sample size</strong>: </td> <td>5</td></tr>
<tr class=biostyle><td align=right ><strong>Excluded observations</strong>: </td> <td>0</td></tr>
<tr class=biostyle><td align=right ><strong>Init log likelihood</strong>: </td> <td>-115.3003</td></tr>
<tr class=biostyle><td align=right ><strong>Final log likelihood</strong>: </td> <td>-67.06549</td></tr>
<tr class=biostyle><td align=right ><strong>Likelihood ratio test for the init. model</strong>: </td> <td>96.4696</td></tr>
<tr class=biostyle><td align=right ><strong>Rho-square for the init. model</strong>: </td> <td>0.418</td></tr>
<tr class=biostyle><td align=right ><strong>Rho-square-bar for the init. model</strong>: </td> <td>0.401</td></tr>
<tr class=biostyle><td align=right ><strong>Akaike Information Criterion</strong>: </td> <td>138.131</td></tr>
<tr class=biostyle><td align=right ><strong>Bayesian Information Criterion</strong>: </td> <td>137.3499</td></tr>
<tr class=biostyle><td align=right ><strong>Final gradient norm</strong>: </td> <td>2.7648E-07</td></tr>
<tr class=biostyle><td align=right ><strong>Diagnostic</strong>: </td> <td>b'CONVERGENCE: NORM_OF_PROJECTED_GRADIENT_<=_PGTOL'</td></tr>
<tr class=biostyle><td align=right ><strong>Database readings</strong>: </td> <td>8</td></tr>
<tr class=biostyle><td align=right ><strong>Iterations</strong>: </td> <td>5</td></tr>
<tr class=biostyle><td align=right ><strong>Optimization time</strong>: </td> <td>0:00:00.003452</td></tr>
<tr class=biostyle><td align=right ><strong>Bootstrapping time</strong>: </td> <td>0:00:00.030784</td></tr>
<tr class=biostyle><td align=right ><strong>Nbr of threads</strong>: </td> <td>8</td></tr>
</table>
<h1>Estimated parameters</h1>
<p><font size='-1'>Click on the headers of the columns to sort the table  [<a href='http://www.kryogenix.org/code/browser/sorttable/' target='_blank'>Credits</a>]</font></p>
<table border='1' class='sortable'>
<tr class=biostyle><th>Name</th><th>Value</th><th>Std err</th><th>t-test</th><th>p-value</th><th>Rob. Std err</th><th>Rob. t-test</th><th>Rob. p-value</th><th>Bootstrap[10] Std err</th><th>Bootstrap t-test</th><th>Bootstrap p-value</th></tr>
<tr class=biostyle><td>beta1</td><td>-1.27</td><td>0.115</td><td>-11.1</td><td>0</td><td>0.0137</td><td>-92.8</td><td>0</td><td>0.012</td><td>-106</td><td>0</td></tr>
<tr class=biostyle><td>beta2</td><td>1.25</td><td>0.0848</td><td>14.7</td><td>0</td><td>0.0591</td><td>21.1</td><td>0</td><td>0.0523</td><td>23.9</td><td>0</td></tr>
</table>
<h2>Correlation of coefficients</h2>
<p><font size='-1'>Click on the headers of the columns to sort the table [<a href='http://www.kryogenix.org/code/browser/sorttable/' target='_blank'>Credits</a>]</font></p>
<table border='1' class='sortable'>
<tr class=biostyle><th>Coefficient1</th><th>Coefficient2</th><th>Covariance</th><th>Correlation</th><th>t-test</th><th>p-value</th><th>Rob. cov.</th><th>Rob. corr.</th><th>Rob. t-test</th><th>Rob. p-value</th><th>Boot. cov.</th><th>Boot. corr.</th><th>Boot. t-test</th><th>Boot. p-value</th></tr>
<tr class=biostyle><td>beta2</td><td>beta1</td><td>0.00167</td><td>0.171</td><td>19.3</td><td>0</td><td>0.000811</td><td>1</td><td>55.6</td><td>0</td><td>0.000628</td><td>1</td><td>62.5</td><td>0</td></tr>
</table>
<p>Smallest eigenvalue: 73.054</p>
<p>Smallest singular value: 73.054</p>
</html>

General statistics

In [11]:
readResults.getGeneralStatistics()
Out[11]:
{'Number of estimated parameters': (2, ''),
 'Sample size': (5, ''),
 'Excluded observations': (0, ''),
 'Init log likelihood': (-115.30029248549191, '.7g'),
 'Final log likelihood': (-67.06549047946353, '.7g'),
 'Likelihood ratio test for the init. model': (96.46960401205675, '.7g'),
 'Rho-square for the init. model': (0.41834067343842773, '.3g'),
 'Rho-square-bar for the init. model': (0.40099466366788306, '.3g'),
 'Akaike Information Criterion': (138.13098095892707, '.7g'),
 'Bayesian Information Criterion': (137.34985678379527, '.7g'),
 'Final gradient norm': (2.764804119682062e-07, '.4E'),
 'Diagnostic': (b'CONVERGENCE: NORM_OF_PROJECTED_GRADIENT_<=_PGTOL', ''),
 'Database readings': (8, ''),
 'Iterations': (5, ''),
 'Optimization time': (datetime.timedelta(microseconds=3452), ''),
 'Bootstrapping time': (datetime.timedelta(microseconds=30784), ''),
 'Nbr of threads': (8, '')}

Estimated parameters as pandas dataframe

In [12]:
readResults.getEstimatedParameters()
Out[12]:
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.012001 -106.097521 0.0
beta2 1.248769 0.084830 14.720836 0.0 0.059086 21.134794 0.0 0.052321 23.867541 0.0

Correlation results

In [13]:
readResults.getCorrelationResults()
Out[13]:
Covariance Correlation t-test p-value Rob. cov. Rob. corr. Rob. t-test Rob. p-value Boot. cov. Boot. corr. Boot. t-test Boot. p-value
beta2-beta1 0.001671 0.171121 19.280039 0.0 0.000811 1.0 55.597974 0.0 0.000628 0.999959 62.549546 0.0

Obtain the values of the parameters

In [14]:
readResults.getBetaValues()
Out[14]:
{'beta1': -1.2732639841254711, 'beta2': 1.248768808907056}
In [15]:
readResults.getBetaValues(myBetas=['beta2'])
Out[15]:
{'beta2': 1.248768808907056}

Variance-covariance matrix (Rao-Cramer)

In [16]:
readResults.getVarCovar()
Out[16]:
beta1 beta2
beta1 0.0132582 0.00167146
beta2 0.00167146 0.00719613

Variance-covariance matrix (robust)

In [17]:
readResults.getRobustVarCovar()
Out[17]:
beta1 beta2
beta1 0.000188347 0.000810893
beta2 0.000810893 0.00349115

Variance-covaraince matrix (bootstrap)

In [18]:
readResults.getBootstrapVarCovar()
Out[18]:
beta1 beta2
beta1 0.000144021 0.00062787
beta2 0.00062787 0.00273747

Betas for sensitivity analysis

In [19]:
readResults.getBetasForSensitivityAnalysis(['beta1','beta2'],size=10)
Out[19]:
array([[-1.30088868,  1.12983598],
       [-1.27781994,  1.22915401],
       [-1.26942213,  1.26530918],
       [-1.28371165,  1.2037884 ],
       [-1.29288058,  1.16431328],
       [-1.2940093 ,  1.15945381],
       [-1.2837423 ,  1.20365643],
       [-1.23678827,  1.40580803],
       [-1.24447614,  1.37270939],
       [-1.28172835,  1.21232711]])
In [ ]: