.. 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 :author: Michel Bierlaire, EPFL :date: Mon Apr 10 12:15:28 2023 .. GENERATED FROM PYTHON SOURCE LINES 14-20 .. code-block:: default import biogeme.biogeme_logging as blog import biogeme.biogeme as bio from biogeme.models import ordered_logit from biogeme.expressions import Beta, log, Elem .. GENERATED FROM PYTHON SOURCE LINES 21-22 See the data processing script: :ref:`swissmetro_data`. .. GENERATED FROM PYTHON SOURCE LINES 22-32 .. code-block:: default from swissmetro_data import ( database, CHOICE, TRAIN_TT_SCALED, TRAIN_COST_SCALED, ) 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 33-34 Parameters to be estimated .. GENERATED FROM PYTHON SOURCE LINES 34-37 .. code-block:: default B_TIME = Beta('B_TIME', 0, None, None, 0) B_COST = Beta('B_COST', 0, None, None, 0) .. GENERATED FROM PYTHON SOURCE LINES 38-39 Threshold parameters for the ordered logit. .. GENERATED FROM PYTHON SOURCE LINES 41-42 :math:`\tau_1 \leq 0`. .. GENERATED FROM PYTHON SOURCE LINES 42-44 .. code-block:: default tau1 = Beta('tau1', -1, None, 0, 0) .. GENERATED FROM PYTHON SOURCE LINES 45-46 :math:`\delta_2 \geq 0`. .. GENERATED FROM PYTHON SOURCE LINES 46-48 .. code-block:: default delta2 = Beta('delta2', 2, 0, None, 0) .. GENERATED FROM PYTHON SOURCE LINES 49-50 :math:`\tau_2 = \tau_1 + \delta_2` .. GENERATED FROM PYTHON SOURCE LINES 50-52 .. code-block:: default tau2 = tau1 + delta2 .. GENERATED FROM PYTHON SOURCE LINES 53-54 Utility. .. GENERATED FROM PYTHON SOURCE LINES 54-56 .. code-block:: default U = B_TIME * TRAIN_TT_SCALED + B_COST * TRAIN_COST_SCALED .. GENERATED FROM PYTHON SOURCE LINES 57-62 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 62-68 .. code-block:: default the_proba = ordered_logit( continuous_value=U, list_of_discrete_values=[1, 2, 3], tau_parameter=tau1, ) .. GENERATED FROM PYTHON SOURCE LINES 69-70 Extract from the dict the formula associated with the observed choice. .. GENERATED FROM PYTHON SOURCE LINES 70-72 .. code-block:: default the_chosen_proba = Elem(the_proba, CHOICE) .. GENERATED FROM PYTHON SOURCE LINES 73-75 Definition of the model. This is the contribution of each observation to the log likelihood function. .. GENERATED FROM PYTHON SOURCE LINES 75-77 .. code-block:: default logprob = log(the_chosen_proba) .. GENERATED FROM PYTHON SOURCE LINES 78-79 Create the Biogeme object. .. GENERATED FROM PYTHON SOURCE LINES 79-82 .. code-block:: default the_biogeme = bio.BIOGEME(database, logprob) the_biogeme.modelName = 'b18ordinal_logit' .. rst-class:: sphx-glr-script-out .. code-block:: none File biogeme.toml has been parsed. .. GENERATED FROM PYTHON SOURCE LINES 83-84 Estimate the parameters .. GENERATED FROM PYTHON SOURCE LINES 84-86 .. code-block:: default 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 Cannot read file __b18ordinal_logit.iter. Statement is ignored. Optimization algorithm: hybrid Newton/BFGS with simple bounds [simple_bounds] ** Optimization: Newton with trust region for simple bounds Iter. B_COST B_TIME tau1 tau1_diff_2 Function Relgrad Radius Rho 0 0.71 -0.0041 -0.55 1.7 6.6e+03 0.27 10 1.3 ++ 1 1 -0.02 -0.82 2.6 5.9e+03 0.12 1e+02 1.2 ++ 2 1.2 -0.024 -0.99 3.1 5.8e+03 0.023 1e+03 1.1 ++ 3 1.3 -0.022 -1 3.2 5.8e+03 0.00097 1e+04 1 ++ 4 1.3 -0.022 -1 3.2 5.8e+03 2e-06 1e+04 1 ++ Results saved in file b18ordinal_logit.html Results saved in file b18ordinal_logit.pickle .. GENERATED FROM PYTHON SOURCE LINES 87-89 .. code-block:: default 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 90-92 .. code-block:: default pandas_results = results.getEstimatedParameters() pandas_results .. raw:: html
Value Rob. Std err Rob. t-test Rob. p-value
B_COST 1.262893 0.058542 21.572527 0.0000
B_TIME -0.022081 0.040060 -0.551195 0.5815
tau1 -1.030098 0.067967 -15.155781 0.0000
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.084 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-python :download:`Download Python source code: plot_b18ordinal_logit.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_b18ordinal_logit.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_