.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/programmers/plot_results.py"
.. LINE NUMBERS ARE GIVEN BELOW.
.. only:: html
.. note::
:class: sphx-glr-download-link-note
:ref:`Go to the end `
to download the full example code.
.. rst-class:: sphx-glr-example-title
.. _sphx_glr_auto_examples_programmers_plot_results.py:
biogeme.results
===============
Examples of use of several functions.
This is designed for programmers who need examples of use of the
functions of the module. The examples are designed to illustrate the
syntax. They do not correspond to any meaningful model.
:author: Michel Bierlaire
:date: Wed Nov 29 09:44:41 2023
.. GENERATED FROM PYTHON SOURCE LINES 15-24
.. code-block:: Python
import pandas as pd
from biogeme.version import get_text
import biogeme.biogeme as bio
import biogeme.database as db
import biogeme.results as res
from biogeme.expressions import Beta, Variable, exp
.. GENERATED FROM PYTHON SOURCE LINES 25-26
Version of Biogeme.
.. GENERATED FROM PYTHON SOURCE LINES 26-28
.. code-block:: Python
print(get_text())
.. rst-class:: sphx-glr-script-out
.. code-block:: none
biogeme 3.2.14 [2024-08-05]
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)
.. GENERATED FROM PYTHON SOURCE LINES 29-30
Definition of a database
.. GENERATED FROM PYTHON SOURCE LINES 30-44
.. code-block:: Python
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],
}
)
df
.. raw:: html
|
Person |
Exclude |
Variable1 |
Variable2 |
Choice |
Av1 |
Av2 |
Av3 |
0 |
1 |
0 |
1 |
10 |
1 |
0 |
1 |
0 |
1 |
1 |
0 |
2 |
20 |
2 |
1 |
1 |
1 |
2 |
1 |
1 |
3 |
30 |
3 |
1 |
1 |
1 |
3 |
2 |
0 |
4 |
40 |
1 |
1 |
1 |
1 |
4 |
2 |
1 |
5 |
50 |
2 |
1 |
1 |
1 |
.. GENERATED FROM PYTHON SOURCE LINES 45-47
.. code-block:: Python
my_data = db.Database('test', df)
.. GENERATED FROM PYTHON SOURCE LINES 48-49
Definition of various expressions
.. GENERATED FROM PYTHON SOURCE LINES 49-57
.. code-block:: Python
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
dict_of_expressions = {'log_like': likelihood, 'beta1': beta1, 'simul': simul}
.. GENERATED FROM PYTHON SOURCE LINES 58-59
Creation of the BIOGEME object
.. GENERATED FROM PYTHON SOURCE LINES 59-65
.. code-block:: Python
my_biogeme = bio.BIOGEME(my_data, dict_of_expressions)
my_biogeme.modelName = 'simple_example'
my_biogeme.bootstrap_samples = 10
results = my_biogeme.estimate(run_bootstrap=True)
print(results)
.. rst-class:: sphx-glr-script-out
.. code-block:: none
0%| | 0/10 [00:00, ?it/s]
100%|██████████| 10/10 [00:00<00:00, 440.33it/s]
Results for model simple_example
Output file (HTML): simple_example~00.html
Nbr of parameters: 2
Sample size: 5
Excluded data: 0
Init log likelihood: -67.08858
Final log likelihood: -67.06549
Likelihood ratio test (init): 0.04618125
Rho square (init): 0.000344
Rho bar square (init): -0.0295
Akaike Information Criterion: 138.131
Bayesian Information Criterion: 137.3499
Final gradient norm: 3.900312e-07
beta1 : -1.27[0.115 -11.1 0][0.0137 -92.8 0][0.0111 -115 0]
beta2 : 1.25[0.0848 14.7 0][0.0591 21.1 0][0.0478 26.1 0]
('beta2', 'beta1'): 0.00167 0.171 19.3 0 0.000811 1 55.6 0
.. GENERATED FROM PYTHON SOURCE LINES 66-67
Dump results on a file
.. GENERATED FROM PYTHON SOURCE LINES 67-70
.. code-block:: Python
the_pickle_file = results.write_pickle()
print(the_pickle_file)
.. rst-class:: sphx-glr-script-out
.. code-block:: none
simple_example~01.pickle
.. GENERATED FROM PYTHON SOURCE LINES 71-72
Results can be imported from a file previously generated
.. GENERATED FROM PYTHON SOURCE LINES 72-75
.. code-block:: Python
read_results = res.bioResults(pickle_file=the_pickle_file)
print(read_results)
.. rst-class:: sphx-glr-script-out
.. code-block:: none
Results for model simple_example
Output file (HTML): simple_example~00.html
Nbr of parameters: 2
Sample size: 5
Excluded data: 0
Init log likelihood: -67.08858
Final log likelihood: -67.06549
Likelihood ratio test (init): 0.04618125
Rho square (init): 0.000344
Rho bar square (init): -0.0295
Akaike Information Criterion: 138.131
Bayesian Information Criterion: 137.3499
Final gradient norm: 3.900312e-07
beta1 : -1.27[0.115 -11.1 0][0.0137 -92.8 0][0.0111 -115 0]
beta2 : 1.25[0.0848 14.7 0][0.0591 21.1 0][0.0478 26.1 0]
('beta2', 'beta1'): 0.00167 0.171 19.3 0 0.000811 1 55.6 0
.. GENERATED FROM PYTHON SOURCE LINES 76-77
Results can be formatted in LaTeX
.. GENERATED FROM PYTHON SOURCE LINES 77-79
.. code-block:: Python
print(read_results.get_latex())
.. rst-class:: sphx-glr-script-out
.. code-block:: none
%% This file is designed to be included into a LaTeX document
%% See http://www.latex-project.org for information about LaTeX
%% simple_example - Report from biogeme 3.2.14 [2024-08-05]
%% biogeme 3.2.14 [2024-08-05]
%% 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 2024-08-05 20:01:27.541772
%%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 & -67.08858 \\
Final log likelihood & -67.06549 \\
Likelihood ratio test for the init. model & 0.04618125 \\
Rho-square for the init. model & 0.000344 \\
Rho-square-bar for the init. model & -0.0295 \\
Akaike Information Criterion & 138.131 \\
Bayesian Information Criterion & 137.3499 \\
Final gradient norm & 3.9003E-07 \\
Bootstrapping time & 0:00:00.023199 \\
Nbr of threads & 12 \\
Relative gradient & \verb$7.1957442139436635e-09$ \\
Cause of termination & \verb$Relative gradient = 7.2e-09 <= 0.00012$ \\
Number of function evaluations & \verb$3$ \\
Number of gradient evaluations & \verb$3$ \\
Number of hessian evaluations & \verb$2$ \\
Algorithm & \verb$Newton with trust region for simple bound constraints$ \\
Number of iterations & \verb$2$ \\
Proportion of Hessian calculation & \verb$2/2 = 100.0%$ \\
Optimization time & \verb$0:00:00.003449$ \\
\end{tabular}
%%Parameter estimates
\section{Parameter estimates}
\begin{tabular}{lrrrr}
& Value & Rob. Std err & Rob. t-test & Rob. p-value \\
beta1 & -1.27 & 0.0137 & -92.8 & 0.0 \\
beta2 & 1.25 & 0.0591 & 21.1 & 0.0 \\
\end{tabular}
%%Correlation
\section{Correlation}
\begin{tabular}{lrrrrrrrrrrrr}
& 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.00167 & 0.171 & 19.3 & 0.0 & 0.000811 & 1.0 & 55.6 & 0.0 & 0.000531 & 1.0 & 68.6 & 0.0 \\
\end{tabular}
.. GENERATED FROM PYTHON SOURCE LINES 80-81
Results can be formatted in HTML
.. GENERATED FROM PYTHON SOURCE LINES 81-83
.. code-block:: Python
print(read_results.get_html())
.. rst-class:: sphx-glr-script-out
.. code-block:: none
simple_example - Report from biogeme 3.2.14 [2024-08-05]
biogeme 3.2.14 [2024-08-05]
Python package
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 2024-08-05 20:01:27.544784
Report file: | simple_example~00.html |
Database name: | test |
Estimation report
Number of estimated parameters: | 2 |
Sample size: | 5 |
Excluded observations: | 0 |
Init log likelihood: | -67.08858 |
Final log likelihood: | -67.06549 |
Likelihood ratio test for the init. model: | 0.04618125 |
Rho-square for the init. model: | 0.000344 |
Rho-square-bar for the init. model: | -0.0295 |
Akaike Information Criterion: | 138.131 |
Bayesian Information Criterion: | 137.3499 |
Final gradient norm: | 3.9003E-07 |
Bootstrapping time: | 0:00:00.023199 |
Nbr of threads: | 12 |
Relative gradient: | 7.1957442139436635e-09 |
Cause of termination: | Relative gradient = 7.2e-09 <= 0.00012 |
Number of function evaluations: | 3 |
Number of gradient evaluations: | 3 |
Number of hessian evaluations: | 2 |
Algorithm: | Newton with trust region for simple bound constraints |
Number of iterations: | 2 |
Proportion of Hessian calculation: | 2/2 = 100.0% |
Optimization time: | 0:00:00.003449 |
Estimated parameters
Name | Value | Rob. Std err | Rob. t-test | Rob. p-value |
beta1 | -1.27 | 0.0137 | -92.8 | 0 |
beta2 | 1.25 | 0.0591 | 21.1 | 0 |
Correlation of coefficients
Coefficient1 | Coefficient2 | 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.00167 | 0.171 | 19.3 | 0 | 0.000811 | 1 | 55.6 | 0 | 0.000531 | 1 | 68.6 | 0 |
Smallest eigenvalue: 73.054
Largest eigenvalue: 147.802
Condition number: 2.02318
.. GENERATED FROM PYTHON SOURCE LINES 84-85
General statistics, including a suggested format.
.. GENERATED FROM PYTHON SOURCE LINES 85-88
.. code-block:: Python
statistics = read_results.get_general_statistics()
statistics
.. rst-class:: sphx-glr-script-out
.. code-block:: none
{'Number of estimated parameters': GeneralStatistic(value=2, format=''), 'Sample size': GeneralStatistic(value=5, format=''), 'Excluded observations': GeneralStatistic(value=0, format=''), 'Init log likelihood': GeneralStatistic(value=-67.08858110351106, format='.7g'), 'Final log likelihood': GeneralStatistic(value=-67.06549047946356, format='.7g'), 'Likelihood ratio test for the init. model': GeneralStatistic(value=0.04618124809499591, format='.7g'), 'Rho-square for the init. model': GeneralStatistic(value=np.float64(0.00034418113586087706), format='.3g'), 'Rho-square-bar for the init. model': GeneralStatistic(value=np.float64(-0.029467151390522472), format='.3g'), 'Akaike Information Criterion': GeneralStatistic(value=138.13098095892713, format='.7g'), 'Bayesian Information Criterion': GeneralStatistic(value=np.float64(137.34985678379533), format='.7g'), 'Final gradient norm': GeneralStatistic(value=3.9003122659206894e-07, format='.4E'), 'Bootstrapping time': GeneralStatistic(value=datetime.timedelta(microseconds=23199), format=''), 'Nbr of threads': GeneralStatistic(value=12, format='')}
.. GENERATED FROM PYTHON SOURCE LINES 89-90
The suggested format can be used as follows
.. GENERATED FROM PYTHON SOURCE LINES 90-93
.. code-block:: Python
for k, (v, p) in statistics.items():
print(f'{k}:\t{v:{p}}')
.. rst-class:: sphx-glr-script-out
.. code-block:: none
Number of estimated parameters: 2
Sample size: 5
Excluded observations: 0
Init log likelihood: -67.08858
Final log likelihood: -67.06549
Likelihood ratio test for the init. model: 0.04618125
Rho-square for the init. model: 0.000344
Rho-square-bar for the init. model: -0.0295
Akaike Information Criterion: 138.131
Bayesian Information Criterion: 137.3499
Final gradient norm: 3.9003E-07
Bootstrapping time: 0:00:00.023199
Nbr of threads: 12
.. GENERATED FROM PYTHON SOURCE LINES 94-95
This result can be generated directly with the following function
.. GENERATED FROM PYTHON SOURCE LINES 95-97
.. code-block:: Python
print(results.print_general_statistics())
.. rst-class:: sphx-glr-script-out
.. code-block:: none
Number of estimated parameters: 2
Sample size: 5
Excluded observations: 0
Init log likelihood: -67.08858
Final log likelihood: -67.06549
Likelihood ratio test for the init. model: 0.04618125
Rho-square for the init. model: 0.000344
Rho-square-bar for the init. model: -0.0295
Akaike Information Criterion: 138.131
Bayesian Information Criterion: 137.3499
Final gradient norm: 3.9003E-07
Bootstrapping time: 0:00:00.023199
Nbr of threads: 12
.. GENERATED FROM PYTHON SOURCE LINES 98-99
Estimated parameters as pandas dataframe
.. GENERATED FROM PYTHON SOURCE LINES 99-101
.. code-block:: Python
read_results.get_estimated_parameters()
.. raw:: html
|
Value |
Rob. Std err |
Rob. t-test |
Rob. p-value |
beta1 |
-1.273264 |
0.013724 |
-92.776663 |
0.0 |
beta2 |
1.248769 |
0.059086 |
21.134794 |
0.0 |
.. GENERATED FROM PYTHON SOURCE LINES 102-103
Correlation results
.. GENERATED FROM PYTHON SOURCE LINES 103-105
.. code-block:: Python
read_results.get_correlation_results()
.. raw:: html
|
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.597975 |
0.0 |
0.000531 |
0.999983 |
68.644336 |
0.0 |
.. GENERATED FROM PYTHON SOURCE LINES 106-107
Obtain the values of the parameters
.. GENERATED FROM PYTHON SOURCE LINES 107-109
.. code-block:: Python
read_results.get_beta_values()
.. rst-class:: sphx-glr-script-out
.. code-block:: none
{'beta1': np.float64(-1.2732639874991436), 'beta2': np.float64(1.2487688117902658)}
.. GENERATED FROM PYTHON SOURCE LINES 110-111
Obtain the value of one or several specific parameters
.. GENERATED FROM PYTHON SOURCE LINES 111-113
.. code-block:: Python
read_results.get_beta_values(my_betas=['beta2'])
.. rst-class:: sphx-glr-script-out
.. code-block:: none
{'beta2': np.float64(1.2487688117902658)}
.. GENERATED FROM PYTHON SOURCE LINES 114-115
Variance-covariance matrix (Rao-Cramer)
.. GENERATED FROM PYTHON SOURCE LINES 115-117
.. code-block:: Python
read_results.get_var_covar()
.. raw:: html
|
beta1 |
beta2 |
beta1 |
0.013258 |
0.001671 |
beta2 |
0.001671 |
0.007196 |
.. GENERATED FROM PYTHON SOURCE LINES 118-119
Variance-covariance matrix (robust)
.. GENERATED FROM PYTHON SOURCE LINES 119-121
.. code-block:: Python
read_results.get_robust_var_covar()
.. raw:: html
|
beta1 |
beta2 |
beta1 |
0.000188 |
0.000811 |
beta2 |
0.000811 |
0.003491 |
.. GENERATED FROM PYTHON SOURCE LINES 122-123
Variance-covaraince matrix (bootstrap)
.. GENERATED FROM PYTHON SOURCE LINES 123-125
.. code-block:: Python
read_results.get_bootstrap_var_covar()
.. raw:: html
|
beta1 |
beta2 |
beta1 |
0.000123 |
0.000531 |
beta2 |
0.000531 |
0.002289 |
.. GENERATED FROM PYTHON SOURCE LINES 126-129
Draws for sensitivity analysis are generated using
bootstrapping. Any indicator can be generated by the model for each
draw, and its empirical distribution can be investigated.
.. GENERATED FROM PYTHON SOURCE LINES 129-131
.. code-block:: Python
read_results.get_betas_for_sensitivity_analysis(['beta1', 'beta2'], size=10)
.. rst-class:: sphx-glr-script-out
.. code-block:: none
[{'beta1': np.float64(-1.2873325336200956), 'beta2': np.float64(1.1875198317199467)}, {'beta1': np.float64(-1.2823937362836413), 'beta2': np.float64(1.209195603860152)}, {'beta1': np.float64(-1.2690260405244642), 'beta2': np.float64(1.2669668838392414)}, {'beta1': np.float64(-1.2823937362836415), 'beta2': np.float64(1.209195603860152)}, {'beta1': np.float64(-1.257397879951302), 'beta2': np.float64(1.31651208100832)}, {'beta1': np.float64(-1.2873325336200956), 'beta2': np.float64(1.1875198317199467)}, {'beta1': np.float64(-1.2690260405244642), 'beta2': np.float64(1.2669668838392414)}, {'beta1': np.float64(-1.2777126116342759), 'beta2': np.float64(1.229556814073192)}, {'beta1': np.float64(-1.257397879951302), 'beta2': np.float64(1.31651208100832)}, {'beta1': np.float64(-1.2732639874991436), 'beta2': np.float64(1.2487688117902658)}]
.. GENERATED FROM PYTHON SOURCE LINES 132-133
Results can be produced in the ALOGIT F12 format
.. GENERATED FROM PYTHON SOURCE LINES 133-135
.. code-block:: Python
print(read_results.get_f12())
.. rst-class:: sphx-glr-script-out
.. code-block:: none
simple_example
From biogeme 3.2.14 2024-08-05 20:01:27
END
0 beta1 F -1.273263987499e+00 +1.372396830767e-02
0 beta2 F +1.248768811790e+00 +5.908592227624e-02
-1
5 0 0 -6.706549047946e+01
0 0 2024-08-05 20:01:27
100000
.. GENERATED FROM PYTHON SOURCE LINES 136-137
Miscellaneous functions
.. GENERATED FROM PYTHON SOURCE LINES 139-140
Likelihood ratio test. Let's first estimate a constrained model
.. GENERATED FROM PYTHON SOURCE LINES 142-153
.. code-block:: Python
beta2_constrained = Beta('beta2_constrained', 2.0, -3, 10, 1)
likelihood_constrained = (
-(beta1**2) * Variable1
- exp(beta2_constrained * beta1) * Variable2
- beta2_constrained**4
)
my_biogeme_constrained = bio.BIOGEME(my_data, likelihood_constrained)
my_biogeme_constrained.modelName = 'simple_example_constrained'
results_constrained = my_biogeme_constrained.estimate()
print(results_constrained.short_summary())
.. rst-class:: sphx-glr-script-out
.. code-block:: none
Results for model simple_example_constrained
Nbr of parameters: 1
Sample size: 5
Excluded data: 0
Final log likelihood: -114.7702
Akaike Information Criterion: 231.5403
Bayesian Information Criterion: 231.1498
.. GENERATED FROM PYTHON SOURCE LINES 154-155
We can now perform a likelihood ratio test.
.. GENERATED FROM PYTHON SOURCE LINES 155-160
.. code-block:: Python
test_results = results.likelihood_ratio_test(results_constrained, 0.95)
print(test_results.message)
print(f'Statistic: {test_results.statistic}')
print(f'Threshold: {test_results.threshold}')
.. rst-class:: sphx-glr-script-out
.. code-block:: none
H0 can be rejected at level 95.0%
Statistic: 95.40936413216042
Threshold: 0.003932140000019531
.. GENERATED FROM PYTHON SOURCE LINES 161-162
Calculation of the :math:`p`-value
.. GENERATED FROM PYTHON SOURCE LINES 162-164
.. code-block:: Python
res.calc_p_value(1.96)
.. rst-class:: sphx-glr-script-out
.. code-block:: none
np.float64(0.04999579029644097)
.. GENERATED FROM PYTHON SOURCE LINES 165-166
Compilation of results
.. GENERATED FROM PYTHON SOURCE LINES 166-168
.. code-block:: Python
dict_of_results = {'Model A': read_results, 'Model B': the_pickle_file}
.. GENERATED FROM PYTHON SOURCE LINES 169-171
.. code-block:: Python
df = res.compile_estimation_results(dict_of_results)
df
.. rst-class:: sphx-glr-script-out
.. code-block:: none
( Model A Model B
Number of estimated parameters 2 2
Sample size 5 5
Final log likelihood -67.06549 -67.06549
Akaike Information Criterion 138.130981 138.130981
Bayesian Information Criterion 137.349857 137.349857
beta1 (t-test) -1.27 (-92.8) -1.27 (-92.8)
beta2 (t-test) 1.25 (21.1) 1.25 (21.1), {'Model A': 'Model A', 'Model B': 'Model B'})
.. rst-class:: sphx-glr-timing
**Total running time of the script:** (0 minutes 0.066 seconds)
.. _sphx_glr_download_auto_examples_programmers_plot_results.py:
.. only:: html
.. container:: sphx-glr-footer sphx-glr-footer-example
.. container:: sphx-glr-download sphx-glr-download-jupyter
:download:`Download Jupyter notebook: plot_results.ipynb `
.. container:: sphx-glr-download sphx-glr-download-python
:download:`Download Python source code: plot_results.py `
.. container:: sphx-glr-download sphx-glr-download-zip
:download:`Download zipped: plot_results.zip `
.. only:: html
.. rst-class:: sphx-glr-signature
`Gallery generated by Sphinx-Gallery `_