.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/tutorials/plot_b04_estimation_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_tutorials_plot_b04_estimation_results.py: Estimation results ================== The estimation results are saved by Biogeme so that they can be re-used without the need for re-estimating the model Michel Bierlaire, EPFL Sun Jun 15 2025, 07:36:09 .. GENERATED FROM PYTHON SOURCE LINES 11-31 .. code-block:: Python from IPython.core.display_functions import display from biogeme.biogeme import BIOGEME from biogeme.models import loglogit from biogeme.results_processing import ( EstimationResults, get_f12, get_html_estimated_parameters, get_latex_estimated_parameters, get_pandas_estimated_parameters, ) from tutorial_data import biogeme_database, choice from tutorial_model import utilities log_choice_probability = loglogit(utilities, None, choice) biogeme_object = BIOGEME(biogeme_database, log_choice_probability) biogeme_object.calculate_null_loglikelihood(avail={0: 1, 1: 1}) .. rst-class:: sphx-glr-script-out .. code-block:: none -14.556090791758852 .. GENERATED FROM PYTHON SOURCE LINES 32-34 The name of the model as provided below determines the name of the files that are generated by Biogeme. Note that the estimation results are not stored, in a variable as we illustrate how they can be imported from a file. .. GENERATED FROM PYTHON SOURCE LINES 34-37 .. code-block:: Python biogeme_object.model_name = 'example' _ = biogeme_object.estimate() .. GENERATED FROM PYTHON SOURCE LINES 38-51 After estimation, three files have been created in the working directory: - `__example.iter`: during the course of the estimation algorithm, Biogeme saves in this file the best solution found so far. If the estimation is interrupted for any reason, it allows to restart it without losing the progress made so far. When the estimation starts, Biogeme looks for a file with that name. If it exists, it uses the values stored in the file as starting point of the estimation algorithm. - `example.html`: the estimation results are reported in a convenient HTML format in this file, that can be opened in any browser. - `example.yaml`: the raw estimation results are stored in this file, so that they can be imported instead of being re-calculated. .. GENERATED FROM PYTHON SOURCE LINES 51-53 .. code-block:: Python imported_results = EstimationResults.from_yaml_file(filename='example.yaml') .. GENERATED FROM PYTHON SOURCE LINES 54-56 The object `imported_results` is identical to thw object `results` generated by the estimation procedure, and can be used in the exact same way. .. GENERATED FROM PYTHON SOURCE LINES 58-59 Summary of the estimation results .. GENERATED FROM PYTHON SOURCE LINES 59-62 .. code-block:: Python print(imported_results.short_summary()) .. rst-class:: sphx-glr-script-out .. code-block:: none Results for model example Nbr of parameters: 2 Sample size: 21 Excluded data: 0 Null log likelihood: -14.55609 Final log likelihood: -6.166042 Likelihood ratio test (null): 16.7801 Rho square (null): 0.576 Rho bar square (null): 0.439 Akaike Information Criterion: 16.33208 Bayesian Information Criterion: 18.42113 .. GENERATED FROM PYTHON SOURCE LINES 63-64 Various other functions are available in order to access the estimation results. .. GENERATED FROM PYTHON SOURCE LINES 66-67 A dict containing the general statistics .. GENERATED FROM PYTHON SOURCE LINES 67-69 .. code-block:: Python print(imported_results.get_general_statistics()) .. rst-class:: sphx-glr-script-out .. code-block:: none {'Number of estimated parameters': '2', 'Sample size': '21', 'Excluded observations': '0', 'Null log likelihood': '-14.55609', 'Init log likelihood': '-6.166042', 'Final log likelihood': '-6.166042', 'Likelihood ratio test for the null model': '16.7801', 'Rho-square for the null model': '0.576', 'Rho-square-bar for the null model': '0.439', 'Likelihood ratio test for the init. model': '-0', 'Rho-square for the init. model': '0', 'Rho-square-bar for the init. model': '-0.324', 'Akaike Information Criterion': '16.33208', 'Bayesian Information Criterion': '18.42113', 'Final gradient norm': '4.6082E-06', 'Bootstrapping time': 'None'} .. GENERATED FROM PYTHON SOURCE LINES 70-71 They can be accessed one by one as well .. GENERATED FROM PYTHON SOURCE LINES 71-87 .. code-block:: Python print(f'Number of parameter: {imported_results.number_of_parameters}') print(f'Sample size: {imported_results.sample_size}') print(f'Excluded observations: {imported_results.number_of_excluded_data}') print(f'Init log likelihood: {imported_results.initial_log_likelihood}') print(f'Final log likelihood: {imported_results.final_loglikelihood}') print( f'Likelihood ratio test for the init. model: {imported_results.likelihood_ratio_init}' ) print( f'Likelihood ratio test for the null model: {imported_results.likelihood_ratio_null}' ) print(f'Akaike Information Criterion: {imported_results.akaike_information_criterion}') print( f'Bayesian Information Criterion: {imported_results.bayesian_information_criterion}' ) .. rst-class:: sphx-glr-script-out .. code-block:: none Number of parameter: 2 Sample size: 21 Excluded observations: 0 Init log likelihood: -6.1660422124237275 Final log likelihood: -6.1660422124237275 Likelihood ratio test for the init. model: -0.0 Likelihood ratio test for the null model: 16.780097158670248 Akaike Information Criterion: 16.332084424847455 Bayesian Information Criterion: 18.4211293002943 .. GENERATED FROM PYTHON SOURCE LINES 88-89 The estimated values of the parameters can be retrieved in a dict. This is particularly useful when using simulation. .. GENERATED FROM PYTHON SOURCE LINES 89-91 .. code-block:: Python print(imported_results.get_beta_values()) .. rst-class:: sphx-glr-script-out .. code-block:: none {'asc_car': -0.23757284960890077, 'b_time': -0.05310981573115624} .. GENERATED FROM PYTHON SOURCE LINES 92-93 The value, the standard error and the t-test can also be extracted for each parameter .. GENERATED FROM PYTHON SOURCE LINES 93-97 .. code-block:: Python print(imported_results.get_parameter_value(parameter_name='b_time')) print(imported_results.get_parameter_std_err(parameter_name='b_time')) print(imported_results.get_parameter_t_test(parameter_name='b_time')) .. rst-class:: sphx-glr-script-out .. code-block:: none -0.05310981573115624 0.021671520295531748 -2.4506732802731177 .. GENERATED FROM PYTHON SOURCE LINES 98-99 The results can also be exported in various formats .. GENERATED FROM PYTHON SOURCE LINES 101-102 As a pandas data frame .. GENERATED FROM PYTHON SOURCE LINES 102-105 .. code-block:: Python pandas_frame = get_pandas_estimated_parameters(estimation_results=imported_results) display(pandas_frame) .. rst-class:: sphx-glr-script-out .. code-block:: none Name Value Robust std err. Robust t-stat. Robust p-value 0 asc_car -0.237573 0.805174 -0.295058 0.767950 1 b_time -0.053110 0.021672 -2.450673 0.014259 .. GENERATED FROM PYTHON SOURCE LINES 106-107 In HTML format .. GENERATED FROM PYTHON SOURCE LINES 107-110 .. code-block:: Python html_code = get_html_estimated_parameters(estimation_results=imported_results) print(html_code) .. rst-class:: sphx-glr-script-out .. code-block:: none
IdNameValueRobust std err.Robust t-stat.Robust p-value
0asc_car-0.2380.805-0.2950.768
1b_time-0.05310.0217-2.450.0143
.. GENERATED FROM PYTHON SOURCE LINES 111-112 In LaTeX format .. GENERATED FROM PYTHON SOURCE LINES 112-115 .. code-block:: Python latex_code = get_latex_estimated_parameters(estimation_results=imported_results) print(latex_code) .. rst-class:: sphx-glr-script-out .. code-block:: none \begin{tabular}{rlr@{.}lr@{.}lr@{.}lr@{.}l} & & \multicolumn{2}{l}{} & \multicolumn{2}{l}{Robust} & \multicolumn{4}{l}{} \\ Parameter & & \multicolumn{2}{l}{Coeff.} & \multicolumn{2}{l}{Asympt.} & \multicolumn{4}{l}{} \\ number & Description & \multicolumn{2}{l}{estimate} & \multicolumn{2}{l}{std. error} & \multicolumn{2}{l}{$t$-stat} & \multicolumn{2}{l}{$p$-value} \\ \hline 0 & asc\_car & -0&238 & 0&805 & -0&295 & 0&768 \\ 1 & b\_time & -0&0531 & 0&0217 & -2&45 & 0&0143 \\ \end{tabular} .. GENERATED FROM PYTHON SOURCE LINES 116-117 In Alogit F12 format .. GENERATED FROM PYTHON SOURCE LINES 117-119 .. code-block:: Python f12_format = get_f12(estimation_results=imported_results) print(f12_format) .. rst-class:: sphx-glr-script-out .. code-block:: none example From biogeme 3.3.1 2025-09-03 05:23:42 END 0 asc_car F -2.375728496089e-01 +8.051736984869e-01 0 b_time F -5.310981573116e-02 +2.167152029553e-02 -1 21 0 -1.455609079176e+01 -6.166042212424e+00 0 0 2025-09-03 05:23:42 61834 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.587 seconds) .. _sphx_glr_download_auto_examples_tutorials_plot_b04_estimation_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_b04_estimation_results.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_b04_estimation_results.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_b04_estimation_results.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_