.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/swissmetro/plot_b18b_ordinal_probit.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_b18b_ordinal_probit.py: 18b. Ordinal probit model ========================= Example of an ordinal probit 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:54:37 .. GENERATED FROM PYTHON SOURCE LINES 13-24 .. code-block:: Python from IPython.core.display_functions import display import biogeme.biogeme_logging as blog from biogeme.biogeme import BIOGEME from biogeme.expressions import Beta, OrderedLogProbit from biogeme.results_processing import ( EstimationResults, get_pandas_estimated_parameters, ) .. GENERATED FROM PYTHON SOURCE LINES 25-26 See the data processing script: :ref:`swissmetro_data`. .. GENERATED FROM PYTHON SOURCE LINES 26-31 .. 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 b18b_ordinal_probit.py') .. rst-class:: sphx-glr-script-out .. code-block:: none Example b18b_ordinal_probit.py .. GENERATED FROM PYTHON SOURCE LINES 32-33 Parameters to be estimated .. GENERATED FROM PYTHON SOURCE LINES 33-36 .. 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 37-38 Threshold parameters for the ordered probit. .. GENERATED FROM PYTHON SOURCE LINES 40-41 :math:`\tau_1 \leq 0`. .. GENERATED FROM PYTHON SOURCE LINES 41-43 .. code-block:: Python tau1 = Beta('tau1', -1, None, 0, 0) .. GENERATED FROM PYTHON SOURCE LINES 44-45 :math:`\delta_2 \geq 0`. .. GENERATED FROM PYTHON SOURCE LINES 45-47 .. code-block:: Python delta2 = Beta('delta2', 2, 0, None, 0) .. GENERATED FROM PYTHON SOURCE LINES 48-49 :math:`\tau_2 = \tau_1 + \delta_2` .. GENERATED FROM PYTHON SOURCE LINES 49-51 .. code-block:: Python tau2 = tau1 + delta2 .. GENERATED FROM PYTHON SOURCE LINES 52-53 Utility .. GENERATED FROM PYTHON SOURCE LINES 53-55 .. code-block:: Python utility = b_time * TRAIN_TT_SCALED + b_cost * TRAIN_COST_SCALED .. GENERATED FROM PYTHON SOURCE LINES 56-57 Associate each discrete indicator with an interval. .. GENERATED FROM PYTHON SOURCE LINES 57-65 .. code-block:: Python log_probability = OrderedLogProbit( eta=utility, cutpoints=[tau1, tau2], y=CHOICE, categories=[1, 2, 3], neutral_labels=[], ) .. GENERATED FROM PYTHON SOURCE LINES 66-67 Create the Biogeme object. .. GENERATED FROM PYTHON SOURCE LINES 67-70 .. code-block:: Python the_biogeme = BIOGEME(database, log_probability) the_biogeme.model_name = 'b18b_ordinal_probit' .. rst-class:: sphx-glr-script-out .. code-block:: none Biogeme parameters read from biogeme.toml. .. GENERATED FROM PYTHON SOURCE LINES 71-72 Estimate the parameters. .. GENERATED FROM PYTHON SOURCE LINES 72-79 .. code-block:: Python try: results = EstimationResults.from_yaml_file( filename=f'saved_results/{the_biogeme.model_name}.yaml' ) except FileNotFoundError: results = the_biogeme.estimate() .. rst-class:: sphx-glr-script-out .. code-block:: none *** Initial values of the parameters are obtained from the file __b18b_ordinal_probit.iter Cannot read file __b18b_ordinal_probit.iter. Statement is ignored. Starting values for the algorithm: {} 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 Iter. b_time b_cost tau1 delta2 Function Relgrad Radius Rho 0 0.016 0.68 -0.6 1.7 5.8e+03 0.012 10 0.99 ++ 1 0.018 0.69 -0.6 1.8 5.8e+03 0.0001 1e+02 1 ++ 2 0.018 0.69 -0.6 1.8 5.8e+03 5e-09 1e+02 1 ++ Optimization algorithm has converged. Relative gradient: 5.0035031126210086e-09 Cause of termination: Relative gradient = 5e-09 <= 6.1e-06 Number of function evaluations: 10 Number of gradient evaluations: 7 Number of hessian evaluations: 3 Algorithm: Newton with trust region for simple bound constraints Number of iterations: 3 Proportion of Hessian calculation: 3/3 = 100.0% Optimization time: 0:00:00.356448 Calculate second derivatives and BHHH File b18b_ordinal_probit.html has been generated. File b18b_ordinal_probit.yaml has been generated. .. GENERATED FROM PYTHON SOURCE LINES 80-82 .. code-block:: Python print(results.short_summary()) .. rst-class:: sphx-glr-script-out .. code-block:: none Results for model b18b_ordinal_probit Nbr of parameters: 4 Sample size: 6768 Excluded data: 3960 Final log likelihood: -5789.055 Akaike Information Criterion: 11586.11 Bayesian Information Criterion: 11613.39 .. GENERATED FROM PYTHON SOURCE LINES 83-85 .. 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.018053 0.023389 0.771831 0.440215 1 b_cost 0.687183 0.036818 18.664467 0.000000 2 tau1 -0.604797 0.038571 -15.680045 0.000000 3 delta2 1.754346 0.029600 59.269266 0.000000 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.687 seconds) .. _sphx_glr_download_auto_examples_swissmetro_plot_b18b_ordinal_probit.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_b18b_ordinal_probit.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_b18b_ordinal_probit.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_b18b_ordinal_probit.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_