Note
Go to the end to download the full example code.
18. Ordinal probit model¶
Bayesian estimation 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 Mon Nov 17 2025, 16:44:27
from pathlib import Path
from IPython.core.display_functions import display
See the data processing script: Data preparation for Swissmetro.
from swissmetro_data import CHOICE, TRAIN_COST_SCALED, TRAIN_TT_SCALED, database
import biogeme.biogeme_logging as blog
from biogeme.bayesian_estimation import (
BayesianResults,
BayesianResultsSummary,
get_pandas_estimated_parameters,
)
from biogeme.biogeme import BIOGEME
from biogeme.expressions import Beta, OrderedLogProbit
We define a small but positive lower bound
POSITIVE_LOWER_BOUND = 1.0e-5
logger = blog.get_screen_logger(level=blog.INFO)
logger.info('Example b18b_ordinal_probit.py')
Example b18b_ordinal_probit.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 probit.
\(\tau_1 \leq 0\).
tau1 = Beta('tau1', -1, None, 0, 0)
\(\delta_2 \geq 0\).
delta2 = Beta('delta2', 2, POSITIVE_LOWER_BOUND, 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.
log_probability = OrderedLogProbit(
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 = 'b18b_ordinal_probit'
Biogeme parameters read from biogeme.toml.
Estimate the posterior distribution of the parameters, or read the results if already available.
yaml_file = Path('saved_results') / f'{the_biogeme.model_name}.yaml'
try:
summary_results = BayesianResultsSummary.from_yaml_file(filename=yaml_file)
except FileNotFoundError:
results: BayesianResults = the_biogeme.bayesian_estimation()
summary_results = results.to_summary()
print(summary_results.short_summary())
Sample size 6768
Sampler NUTS
Number of chains 4
Number of draws per chain 2000
Total number of draws 8000
Acceptance rate target 0.9
Run time 0:00:53.253262
Posterior predictive log-likelihood (sum of log mean p) -5788.54
Expected log-likelihood E[log L(Y|θ)] -5791.10
Best-draw log-likelihood (posterior upper bound) -5789.07
LOO (Leave-One-Out Cross-Validation) -5793.68
LOO Standard Error 50.23
Effective number of parameters (p_LOO) 5.14
Present the parameter estimates in a pandas table.
pandas_results = get_pandas_estimated_parameters(
estimation_results=summary_results,
)
display(pandas_results)
Name Value (mean) Value (median) ... R hat ESS (bulk) ESS (tail)
0 b_time 0.017629 0.017538 ... 1.001080 3878.434829 4298.319998
1 b_cost 0.687880 0.687826 ... 1.000997 4424.488572 4259.083526
2 tau1 -0.605291 -0.605517 ... 1.001249 4232.775110 4196.469857
3 delta2 1.755013 1.755351 ... 1.000914 4915.619919 4759.706814
[4 rows x 12 columns]
Report the variables stored in the Bayesian estimation results.
display(summary_results.report_stored_variables())
group variable dims shape
0 constant_data CHOICE [obs] [6768]
1 constant_data TRAIN_COST_SCALED [obs] [6768]
2 constant_data TRAIN_TT_SCALED [obs] [6768]
3 log_likelihood _choice [chain, draw, obs] [4, 2000, 6768]
4 posterior b_cost [chain, draw] [4, 2000]
5 posterior b_time [chain, draw] [4, 2000]
6 posterior delta2 [chain, draw] [4, 2000]
7 posterior log_like [chain, draw, obs] [4, 2000, 6768]
8 posterior tau1 [chain, draw] [4, 2000]
9 prior b_cost [chain, draw] [1, 2000]
10 prior b_time [chain, draw] [1, 2000]
11 prior delta2 [chain, draw] [1, 2000]
12 prior log_like [chain, draw, obs] [1, 2000, 6768]
13 prior tau1 [chain, draw] [1, 2000]
14 sample_stats acceptance_rate [chain, draw] [4, 2000]
15 sample_stats diverging [chain, draw] [4, 2000]
16 sample_stats energy [chain, draw] [4, 2000]
17 sample_stats lp [chain, draw] [4, 2000]
18 sample_stats n_steps [chain, draw] [4, 2000]
19 sample_stats step_size [chain, draw] [4, 2000]
20 sample_stats tree_depth [chain, draw] [4, 2000]
Total running time of the script: (0 minutes 1.142 seconds)