18a. 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

from IPython.core.display_functions import display

import biogeme.biogeme_logging as blog
from biogeme.biogeme import BIOGEME
from biogeme.expressions import Beta, OrderedLogLogit
from biogeme.results_processing import (
    EstimationResults,
    get_pandas_estimated_parameters,
)

See the data processing script: Data preparation for Swissmetro.

from swissmetro_data import CHOICE, TRAIN_COST_SCALED, TRAIN_TT_SCALED, database

logger = blog.get_screen_logger(level=blog.INFO)
logger.info('Example b18a_ordinal_logit.py')
Example b18a_ordinal_logit.py

Parameters to be estimated

b_time = Beta('b_time', 0, None, None, 0)
b_cost = Beta('b_cost', 0, None, None, 0)

Threshold parameters for the ordered logit.

\(\tau_1 \leq 0\).

tau1 = Beta('tau1', -1, None, 0, 0)

\(\delta_2 \geq 0\).

delta2 = Beta('delta2', 2, 0, None, 0)

\(\tau_2 = \tau_1 + \delta_2\)

tau2 = tau1 + delta2

Utility.

utility = b_time * TRAIN_TT_SCALED + b_cost * TRAIN_COST_SCALED

Associate each discrete indicator with an interval.

  1. \(-\infty \to \tau_1\),

  2. \(\tau_1 \to \tau_2\),

  3. \(\tau_2 \to +\infty\).

log_probability = OrderedLogLogit(
    eta=utility,
    cutpoints=[tau1, tau2],
    y=CHOICE,
    categories=[1, 2, 3],
    neutral_labels=[],
)

Create the Biogeme object.

the_biogeme = BIOGEME(database, log_probability)
the_biogeme.model_name = 'b18a_ordinal_logit'
Biogeme parameters read from biogeme.toml.

Estimate the parameters.

try:
    results = EstimationResults.from_yaml_file(
        filename=f'saved_results/{the_biogeme.model_name}.yaml'
    )
except FileNotFoundError:
    results = the_biogeme.estimate()
*** Initial values of the parameters are obtained from the file __b18a_ordinal_logit.iter
Cannot read file __b18a_ordinal_logit.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.0044            0.97           -0.91             2.7      5.9e+03      0.058       10      1.1   ++
    1          -0.022             1.2              -1             3.1      5.8e+03     0.0071    1e+02      1.1   ++
    2          -0.022             1.3              -1             3.2      5.8e+03    0.00013    1e+03        1   ++
    3          -0.022             1.3              -1             3.2      5.8e+03    4.6e-08    1e+03        1   ++
Optimization algorithm has converged.
Relative gradient: 4.580772034607553e-08
Cause of termination: Relative gradient = 4.6e-08 <= 6.1e-06
Number of function evaluations: 13
Number of gradient evaluations: 9
Number of hessian evaluations: 4
Algorithm: Newton with trust region for simple bound constraints
Number of iterations: 4
Proportion of Hessian calculation: 4/4 = 100.0%
Optimization time: 0:00:00.440946
Calculate second derivatives and BHHH
File b18a_ordinal_logit.html has been generated.
File b18a_ordinal_logit.yaml has been generated.
print(results.short_summary())
Results for model b18a_ordinal_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
pandas_results = get_pandas_estimated_parameters(estimation_results=results)
display(pandas_results)
     Name     Value  Robust std err.  Robust t-stat.  Robust p-value
0  b_time -0.022080         0.040060       -0.551179        0.581511
1  b_cost  1.262900         0.058542       21.572537        0.000000
2    tau1 -1.030101         0.067968      -15.155756        0.000000
3  delta2  3.193022         0.046336       68.909624        0.000000

Total running time of the script: (0 minutes 1.002 seconds)

Gallery generated by Sphinx-Gallery