Note
Go to the end to download the full example code.
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
import biogeme.biogeme_logging as blog
import biogeme.biogeme as bio
from biogeme.models import ordered_logit
from biogeme.expressions import Beta, log, Elem
See the data processing script: Data preparation for Swissmetro.
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')
Example b18ordinal_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.
U = B_TIME * TRAIN_TT_SCALED + B_COST * TRAIN_COST_SCALED
Associate each discrete indicator with an interval.
\(-\infty \to \tau_1\),
\(\tau_1 \to \tau_2\),
\(\tau_2 \to +\infty\).
the_proba = ordered_logit(
continuous_value=U,
list_of_discrete_values=[1, 2, 3],
tau_parameter=tau1,
)
Extract from the dict the formula associated with the observed choice.
the_chosen_proba = Elem(the_proba, CHOICE)
Definition of the model. This is the contribution of each observation to the log likelihood function.
logprob = log(the_chosen_proba)
Create the Biogeme object.
the_biogeme = bio.BIOGEME(database, logprob)
the_biogeme.modelName = 'b18ordinal_logit'
Biogeme parameters read from biogeme.toml.
Estimate the parameters
results = the_biogeme.estimate()
As the model is not too complex, we activate the calculation of second derivatives. If you want to change it, change the name of the algorithm in the TOML file from "automatic" to "simple_bounds"
*** Initial values of the parameters are obtained from the file __b18ordinal_logit.iter
Cannot read file __b18ordinal_logit.iter. Statement is ignored.
As the model is not too complex, we activate the calculation of second derivatives. If you want to change it, change the name of the algorithm in the TOML file from "automatic" to "simple_bounds"
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
print(results.short_summary())
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
pandas_results = results.get_estimated_parameters()
pandas_results
Total running time of the script: (0 minutes 0.095 seconds)