.. 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_processing ========================== 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. Michel Bierlaire Mon Oct 7 18:46:39 2024 .. GENERATED FROM PYTHON SOURCE LINES 15-35 .. code-block:: Python import pandas as pd from IPython.core.display_functions import display from biogeme.biogeme import BIOGEME from biogeme.database import Database from biogeme.expressions import Beta, Variable, exp from biogeme.results_processing import ( EstimateVarianceCovariance, EstimationResults, calc_p_value, compile_estimation_results, get_f12, get_html_estimated_parameters, get_latex_estimated_parameters, get_pandas_correlation_results, get_pandas_estimated_parameters, ) from biogeme.version import get_text .. GENERATED FROM PYTHON SOURCE LINES 36-37 Version of Biogeme. .. GENERATED FROM PYTHON SOURCE LINES 37-39 .. code-block:: Python print(get_text()) .. rst-class:: sphx-glr-script-out .. code-block:: none biogeme 3.3.1 [2025-09-03] 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 40-41 Definition of a database .. GENERATED FROM PYTHON SOURCE LINES 41-55 .. 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], } ) display(df) .. rst-class:: sphx-glr-script-out .. code-block:: none 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 56-58 .. code-block:: Python my_data = Database('test', df) .. GENERATED FROM PYTHON SOURCE LINES 59-60 Definition of various expressions .. GENERATED FROM PYTHON SOURCE LINES 60-68 .. 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 69-70 Creation of the BIOGEME object .. GENERATED FROM PYTHON SOURCE LINES 70-75 .. code-block:: Python my_biogeme = BIOGEME(my_data, dict_of_expressions, bootstrap_samples=100) my_biogeme.model_name = 'simple_example' results = my_biogeme.estimate(run_bootstrap=True) print(results) .. rst-class:: sphx-glr-script-out .. code-block:: none Bootstraps: 0%| | 0/100 [00:00 IdNameValueBootstrap std err.Bootstrap t-stat.Bootstrap p-value 0beta1-1.270.014-91.20 1beta21.250.060920.50 .. GENERATED FROM PYTHON SOURCE LINES 95-96 General statistics, including a suggested format. .. GENERATED FROM PYTHON SOURCE LINES 96-99 .. code-block:: Python statistics = read_results.get_general_statistics() display(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.06549', 'Final log likelihood': '-67.06549', 'Likelihood ratio test for the init. model': '-0', 'Rho-square for the init. model': '0', 'Rho-square-bar for the init. model': '-0.0298', 'Akaike Information Criterion': '138.131', 'Bayesian Information Criterion': '137.3499', 'Final gradient norm': '1.1521E-07', 'Bootstrapping time': '0:00:07.518484'} .. GENERATED FROM PYTHON SOURCE LINES 100-101 Estimated parameters as pandas dataframe .. GENERATED FROM PYTHON SOURCE LINES 101-104 .. code-block:: Python pandas_parameters = get_pandas_estimated_parameters(estimation_results=read_results) display(pandas_parameters) .. rst-class:: sphx-glr-script-out .. code-block:: none Name Value Bootstrap std err. Bootstrap t-stat. Bootstrap p-value 0 beta1 -1.273264 0.013957 -91.229031 0.0 1 beta2 1.248769 0.060945 20.489987 0.0 .. GENERATED FROM PYTHON SOURCE LINES 105-106 Correlation results .. GENERATED FROM PYTHON SOURCE LINES 106-109 .. code-block:: Python pandas_correlation = get_pandas_correlation_results(estimation_results=read_results) display(pandas_correlation) .. rst-class:: sphx-glr-script-out .. code-block:: none First parameter Second parameter ... Bootstrap t-stat. Bootstrap p-value 0 beta2 beta1 ... 53.669506 0.0 [1 rows x 6 columns] .. GENERATED FROM PYTHON SOURCE LINES 110-111 Obtain the values of the parameters .. GENERATED FROM PYTHON SOURCE LINES 111-114 .. code-block:: Python beta_values = read_results.get_beta_values() display(beta_values) .. rst-class:: sphx-glr-script-out .. code-block:: none {'beta1': -1.273263987213694, 'beta2': 1.2487688099301162} .. GENERATED FROM PYTHON SOURCE LINES 115-116 Obtain the value of one or several specific parameters .. GENERATED FROM PYTHON SOURCE LINES 116-119 .. code-block:: Python some_beta_values = read_results.get_beta_values(my_betas=['beta2']) display(some_beta_values) .. rst-class:: sphx-glr-script-out .. code-block:: none {'beta2': 1.2487688099301162} .. GENERATED FROM PYTHON SOURCE LINES 120-121 Variance-covariance matrix (Rao-Cramer) .. GENERATED FROM PYTHON SOURCE LINES 121-125 .. code-block:: Python rao_cramer = read_results.get_variance_covariance_matrix( variance_covariance_type=EstimateVarianceCovariance.RAO_CRAMER ) display(rao_cramer) .. rst-class:: sphx-glr-script-out .. code-block:: none [[0.01325818 0.00167146] [0.00167146 0.00719613]] .. GENERATED FROM PYTHON SOURCE LINES 126-127 Variance-covariance matrix (robust) .. GENERATED FROM PYTHON SOURCE LINES 127-132 .. code-block:: Python robust = read_results.get_variance_covariance_matrix( variance_covariance_type=EstimateVarianceCovariance.ROBUST ) display(robust) .. rst-class:: sphx-glr-script-out .. code-block:: none [[0.00018835 0.00081089] [0.00081089 0.00349115]] .. GENERATED FROM PYTHON SOURCE LINES 133-134 Variance-covariance matrix (bootstrap) .. GENERATED FROM PYTHON SOURCE LINES 134-139 .. code-block:: Python bootstrap = read_results.get_variance_covariance_matrix( variance_covariance_type=EstimateVarianceCovariance.BOOTSTRAP ) display(bootstrap) .. rst-class:: sphx-glr-script-out .. code-block:: none [[0.00019479 0.00085044] [0.00085044 0.00371433]] .. GENERATED FROM PYTHON SOURCE LINES 140-143 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 143-145 .. code-block:: Python read_results.get_betas_for_sensitivity_analysis(['beta1', 'beta2'], size=10) .. rst-class:: sphx-glr-script-out .. code-block:: none [{'beta1': -1.282393736283639, 'beta2': 1.209195603860136}, {'beta1': -1.282393736283639, 'beta2': 1.209195603860136}, {'beta1': -1.282393736283639, 'beta2': 1.209195603860136}, {'beta1': -1.2611085813010015, 'beta2': 1.3007517002098443}, {'beta1': -1.273263987213694, 'beta2': 1.2487688099301162}, {'beta1': -1.282393736283639, 'beta2': 1.209195603860136}, {'beta1': -1.273263987213694, 'beta2': 1.2487688099301162}, {'beta1': -1.2611085813010015, 'beta2': 1.3007517002098443}, {'beta1': -1.2573978799513064, 'beta2': 1.3165120810083486}, {'beta1': -1.2981035069734936, 'beta2': 1.1393434553785933}, {'beta1': -1.243923633974187, 'beta2': 1.373502065868922}, {'beta1': -1.3170661906585204, 'beta2': 1.0497320768282947}, {'beta1': -1.2690260405244644, 'beta2': 1.2669668838392423}, {'beta1': -1.2611085813405174, 'beta2': 1.3007516144406395}, {'beta1': -1.282393736283639, 'beta2': 1.209195603860136}, {'beta1': -1.287332492839996, 'beta2': 1.1875193084801916}, {'beta1': -1.273263987213694, 'beta2': 1.2487688099301162}, {'beta1': -1.2649797742013058, 'beta2': 1.2842631765105266}, {'beta1': -1.2649797742013058, 'beta2': 1.2842631765105266}, {'beta1': -1.269026040455536, 'beta2': 1.2669668836379888}, {'beta1': -1.2777126116342759, 'beta2': 1.2295568140731905}, {'beta1': -1.2873325027193596, 'beta2': 1.1875194143603178}, {'beta1': -1.2573978799513064, 'beta2': 1.3165120810083486}, {'beta1': -1.2873324919847893, 'beta2': 1.187519300827712}, {'beta1': -1.2573978799513064, 'beta2': 1.3165120810083486}, {'beta1': -1.2649797727418584, 'beta2': 1.284263172308228}, {'beta1': -1.2690260405244644, 'beta2': 1.2669668838392423}, {'beta1': -1.2504079893334659, 'beta2': 1.3461108405007625}, {'beta1': -1.2649797742013058, 'beta2': 1.2842631765105266}, {'beta1': -1.269026040455536, 'beta2': 1.2669668836379888}, {'beta1': -1.2777126116342759, 'beta2': 1.2295568140731905}, {'beta1': -1.2873325027193596, 'beta2': 1.1875194143603178}, {'beta1': -1.298103506705179, 'beta2': 1.1393434542527645}, {'beta1': -1.282393821462994, 'beta2': 1.2091960928148573}, {'beta1': -1.3103131424440955, 'beta2': 1.0825801152308525}, {'beta1': -1.2732639874634077, 'beta2': 1.2487688091207687}, {'beta1': -1.298103506705179, 'beta2': 1.1393434542527645}, {'beta1': -1.2690260404854072, 'beta2': 1.2669668816538486}, {'beta1': -1.2573978799513064, 'beta2': 1.3165120810083486}, {'beta1': -1.2649797727418584, 'beta2': 1.284263172308228}, {'beta1': -1.2690260405244644, 'beta2': 1.2669668838392423}, {'beta1': -1.2504079893334659, 'beta2': 1.3461108405007625}, {'beta1': -1.273263987213694, 'beta2': 1.2487688099301162}, {'beta1': -1.2649797742013058, 'beta2': 1.2842631765105266}, {'beta1': -1.2777126116342759, 'beta2': 1.2295568140731905}, {'beta1': -1.292558011774817, 'beta2': 1.1643229811019848}, {'beta1': -1.2690260405244644, 'beta2': 1.2669668838392423}, {'beta1': -1.2925579238204843, 'beta2': 1.1643222008081031}, {'beta1': -1.282393736283639, 'beta2': 1.209195603860136}, {'beta1': -1.282393736283639, 'beta2': 1.209195603860136}, {'beta1': -1.298103506705179, 'beta2': 1.1393434542527645}, {'beta1': -1.2573978838405362, 'beta2': 1.3165118203767316}, {'beta1': -1.2690260405244644, 'beta2': 1.2669668838392423}, {'beta1': -1.2611085813405174, 'beta2': 1.3007516144406395}, {'beta1': -1.2925578214686664, 'beta2': 1.164322217510277}, {'beta1': -1.2823937457787886, 'beta2': 1.2091956298296247}, {'beta1': -1.298103506705179, 'beta2': 1.1393434542527645}, {'beta1': -1.2690260404854072, 'beta2': 1.2669668816538486}, {'beta1': -1.2538348107653448, 'beta2': 1.331612380127896}, {'beta1': -1.2732639365473521, 'beta2': 1.2487701544564327}, {'beta1': -1.2649797742013058, 'beta2': 1.2842631765105266}, {'beta1': -1.2649797742013058, 'beta2': 1.2842631765105266}, {'beta1': -1.2777126116342759, 'beta2': 1.2295568140731905}, {'beta1': -1.273263987607468, 'beta2': 1.248768812102508}, {'beta1': -1.2611085813010015, 'beta2': 1.3007517002098443}, {'beta1': -1.26497977397441, 'beta2': 1.2842631509636993}, {'beta1': -1.2573978799513064, 'beta2': 1.3165120810083486}, {'beta1': -1.2873324919847893, 'beta2': 1.187519300827712}, {'beta1': -1.2538348107653448, 'beta2': 1.331612380127896}, {'beta1': -1.2732639365473521, 'beta2': 1.2487701544564327}, {'beta1': -1.2925578214686664, 'beta2': 1.164322217510277}, {'beta1': -1.2732640875536856, 'beta2': 1.248769699775698}, {'beta1': -1.273263987213694, 'beta2': 1.2487688099301162}, {'beta1': -1.2690260405244644, 'beta2': 1.2669668838392423}, {'beta1': -1.2538348107653448, 'beta2': 1.331612380127896}, {'beta1': -1.2732639365473521, 'beta2': 1.2487701544564327}, {'beta1': -1.2649797742013058, 'beta2': 1.2842631765105266}, {'beta1': -1.2823937666074936, 'beta2': 1.2091966201417774}, {'beta1': -1.273263987213694, 'beta2': 1.2487688099301162}, {'beta1': -1.287332533620085, 'beta2': 1.1875198317198776}, {'beta1': -1.2649797742013058, 'beta2': 1.2842631765105266}, {'beta1': -1.2573978830118717, 'beta2': 1.3165118359003405}, {'beta1': -1.273263987213694, 'beta2': 1.2487688099301162}, {'beta1': -1.2925578214686664, 'beta2': 1.164322217510277}, {'beta1': -1.2649797742013058, 'beta2': 1.2842631765105266}, {'beta1': -1.2873324919775402, 'beta2': 1.1875193007862883}, {'beta1': -1.2690260405244644, 'beta2': 1.2669668838392423}, {'beta1': -1.2823937485848418, 'beta2': 1.2091958966297875}, {'beta1': -1.2690260405244644, 'beta2': 1.2669668838392423}, {'beta1': -1.2573978810083073, 'beta2': 1.3165119010872348}, {'beta1': -1.273263987213694, 'beta2': 1.2487688099301162}, {'beta1': -1.2649797742013058, 'beta2': 1.2842631765105266}, {'beta1': -1.2777126116342759, 'beta2': 1.2295568140731905}, {'beta1': -1.273263987607468, 'beta2': 1.248768812102508}, {'beta1': -1.282393736283639, 'beta2': 1.209195603860136}, {'beta1': -1.2777126118096789, 'beta2': 1.229556814569854}, {'beta1': -1.282393736283639, 'beta2': 1.209195603860136}, {'beta1': -1.2777126118096789, 'beta2': 1.229556814569854}, {'beta1': -1.3103131424440955, 'beta2': 1.0825801152308525}, {'beta1': -1.257397883841623, 'beta2': 1.316511820405938}] .. GENERATED FROM PYTHON SOURCE LINES 146-147 Results can be produced in the ALOGIT F12 format .. GENERATED FROM PYTHON SOURCE LINES 147-149 .. code-block:: Python print(get_f12(estimation_results=read_results)) .. rst-class:: sphx-glr-script-out .. code-block:: none simple_example From biogeme 3.3.1 2025-09-03 02:00:59 END 0 beta1 F -1.273263987214e+00 +1.395678510435e-02 0 beta2 F +1.248768809930e+00 +6.094531842799e-02 -1 5 0 0 -6.706549047946e+01 0 0 2025-09-03 02:00:59 99981 .. GENERATED FROM PYTHON SOURCE LINES 150-151 Miscellaneous functions .. GENERATED FROM PYTHON SOURCE LINES 153-154 Likelihood ratio test. Let's first estimate a constrained model .. GENERATED FROM PYTHON SOURCE LINES 156-167 .. 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 = BIOGEME(my_data, likelihood_constrained) my_biogeme_constrained.model_name = 'simple_example_constrained' results_constrained = my_biogeme_constrained.estimate() print(results_constrained.short_summary()) .. rst-class:: sphx-glr-script-out .. code-block:: none Bootstraps: 0%| | 0/100 [00:07` .. 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 `_