.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/swissmetro/plot_b18ordinal_logit.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_swissmetro_plot_b18ordinal_logit.py: Ordinal logit model =================== Example of an ordinal logit model. This is just to illustrate the syntax, as the data are not ordered. But the example assume, for the sake of it, that the alternatives are ordered as 1->2->3 Michel Bierlaire, EPFL Thu Jun 26 2025, 15:52:21 .. GENERATED FROM PYTHON SOURCE LINES 14-22 .. code-block:: Python import biogeme.biogeme_logging as blog from IPython.core.display_functions import display from biogeme.biogeme import BIOGEME from biogeme.expressions import Beta, Elem, log from biogeme.models import ordered_logit from biogeme.results_processing import get_pandas_estimated_parameters .. GENERATED FROM PYTHON SOURCE LINES 23-24 See the data processing script: :ref:`swissmetro_data`. .. GENERATED FROM PYTHON SOURCE LINES 24-29 .. code-block:: Python from swissmetro_data import CHOICE, TRAIN_COST_SCALED, TRAIN_TT_SCALED, database logger = blog.get_screen_logger(level=blog.INFO) logger.info('Example b18ordinal_logit.py') .. rst-class:: sphx-glr-script-out .. code-block:: none Example b18ordinal_logit.py .. GENERATED FROM PYTHON SOURCE LINES 30-31 Parameters to be estimated .. GENERATED FROM PYTHON SOURCE LINES 31-34 .. code-block:: Python b_time = Beta('b_time', 0, None, None, 0) b_cost = Beta('b_cost', 0, None, None, 0) .. GENERATED FROM PYTHON SOURCE LINES 35-36 Threshold parameters for the ordered logit. .. GENERATED FROM PYTHON SOURCE LINES 38-39 :math:`\tau_1 \leq 0`. .. GENERATED FROM PYTHON SOURCE LINES 39-41 .. code-block:: Python tau1 = Beta('tau1', -1, None, 0, 0) .. GENERATED FROM PYTHON SOURCE LINES 42-43 :math:`\delta_2 \geq 0`. .. GENERATED FROM PYTHON SOURCE LINES 43-45 .. code-block:: Python delta2 = Beta('delta2', 2, 0, None, 0) .. GENERATED FROM PYTHON SOURCE LINES 46-47 :math:`\tau_2 = \tau_1 + \delta_2` .. GENERATED FROM PYTHON SOURCE LINES 47-49 .. code-block:: Python tau2 = tau1 + delta2 .. GENERATED FROM PYTHON SOURCE LINES 50-51 Utility. .. GENERATED FROM PYTHON SOURCE LINES 51-53 .. code-block:: Python utility = b_time * TRAIN_TT_SCALED + b_cost * TRAIN_COST_SCALED .. GENERATED FROM PYTHON SOURCE LINES 54-59 Associate each discrete indicator with an interval. 1. :math:`-\infty \to \tau_1`, 2. :math:`\tau_1 \to \tau_2`, 3. :math:`\tau_2 \to +\infty`. .. GENERATED FROM PYTHON SOURCE LINES 59-66 .. code-block:: Python the_probability = ordered_logit( continuous_value=utility, list_of_discrete_values=[1, 2, 3], reference_threshold_parameter=tau1, scale_parameter=1.0, ) .. GENERATED FROM PYTHON SOURCE LINES 67-68 Extract from the dict the formula associated with the observed choice. .. GENERATED FROM PYTHON SOURCE LINES 68-70 .. code-block:: Python the_chosen_proba = Elem(the_probability, CHOICE) .. GENERATED FROM PYTHON SOURCE LINES 71-73 Definition of the model. This is the contribution of each observation to the log likelihood function. .. GENERATED FROM PYTHON SOURCE LINES 73-75 .. code-block:: Python log_probability = log(the_chosen_proba) .. GENERATED FROM PYTHON SOURCE LINES 76-77 Create the Biogeme object. .. GENERATED FROM PYTHON SOURCE LINES 77-80 .. code-block:: Python the_biogeme = BIOGEME(database, log_probability) the_biogeme.model_name = 'b18ordinal_logit' .. rst-class:: sphx-glr-script-out .. code-block:: none Biogeme parameters read from biogeme.toml. .. GENERATED FROM PYTHON SOURCE LINES 81-82 Estimate the parameters .. GENERATED FROM PYTHON SOURCE LINES 82-84 .. code-block:: Python results = the_biogeme.estimate() .. rst-class:: sphx-glr-script-out .. code-block:: none *** Initial values of the parameters are obtained from the file __b18ordinal_logit.iter Parameter values restored from __b18ordinal_logit.iter Starting values for the algorithm: {'b_time': -0.022080790172257413, 'b_cost': 1.2628932870283, 'tau1': -1.0300981224269388, 'tau1_diff_2': 3.233236512522347} As the model is not too complex, we activate the calculation of second derivatives. To change this behavior, modify the algorithm to "simple_bounds" in the TOML file. Optimization algorithm: hybrid Newton/BFGS with simple bounds [simple_bounds] ** Optimization: Newton with trust region for simple bounds Optimization algorithm has converged. Relative gradient: 2.9148755617971546e-06 Cause of termination: Relative gradient = 2.9e-06 <= 6.1e-06 Number of function evaluations: 1 Number of gradient evaluations: 1 Number of hessian evaluations: 0 Algorithm: Newton with trust region for simple bound constraints Number of iterations: 0 Optimization time: 0:00:00.130996 Calculate second derivatives and BHHH File b18ordinal_logit~00.html has been generated. File b18ordinal_logit~00.yaml has been generated. .. GENERATED FROM PYTHON SOURCE LINES 85-87 .. code-block:: Python print(results.short_summary()) .. rst-class:: sphx-glr-script-out .. code-block:: none Results for model b18ordinal_logit Nbr of parameters: 4 Sample size: 6768 Excluded data: 3960 Final log likelihood: -5789.309 Akaike Information Criterion: 11586.62 Bayesian Information Criterion: 11613.9 .. GENERATED FROM PYTHON SOURCE LINES 88-90 .. code-block:: Python pandas_results = get_pandas_estimated_parameters(estimation_results=results) display(pandas_results) .. rst-class:: sphx-glr-script-out .. code-block:: none Name Value Robust std err. Robust t-stat. Robust p-value 0 b_time -0.022081 0.040060 -0.551195 0.5815 1 b_cost 1.262893 0.058542 21.572527 0.0000 2 tau1 -1.030098 0.067967 -15.155781 0.0000 3 tau1_diff_2 3.233237 0.044509 72.642216 0.0000 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.799 seconds) .. _sphx_glr_download_auto_examples_swissmetro_plot_b18ordinal_logit.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_b18ordinal_logit.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_b18ordinal_logit.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_b18ordinal_logit.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_