Note
Go to the end to download the full example code.
Binary logit modelΒΆ
Example of a binary logit model. Two alternatives: Train and Car.
Michel Bierlaire, EPFL Sat Jun 28 2025, 12:42:27
from IPython.core.display_functions import display
from biogeme.biogeme import BIOGEME
from biogeme.expressions import Beta
from biogeme.models import loglogit
from biogeme.results_processing import get_pandas_estimated_parameters
See the data processing script: Data preparation for Swissmetro (binary choice).
from swissmetro_binary import (
CAR_AV_SP,
CAR_CO_SCALED,
CAR_TT_SCALED,
CHOICE,
TRAIN_AV_SP,
TRAIN_COST_SCALED,
TRAIN_TT_SCALED,
database,
)
Parameters to be estimated.
asc_car = Beta('asc_car', 0, None, None, 0)
b_time_car = Beta('b_time_car', 0, None, None, 0)
b_time_train = Beta('b_time_train', 0, None, None, 0)
b_cost_car = Beta('b_cost_car', 0, None, None, 0)
b_cost_train = Beta('b_cost_train', 0, None, None, 0)
Definition of the utility functions. We estimate a binary logit model. There are only two alternatives.
v_train = b_time_train * TRAIN_TT_SCALED + b_cost_train * TRAIN_COST_SCALED
v_car = asc_car + b_time_car * CAR_TT_SCALED + b_cost_car * CAR_CO_SCALED
Associate utility functions with the numbering of alternatives.
v = {1: v_train, 3: v_car}
Associate the availability conditions with the alternatives.
av = {1: TRAIN_AV_SP, 3: CAR_AV_SP}
Definition of the model. This is the contribution of each observation to the log likelihood function.
log_probability = loglogit(v, av, CHOICE)
Create the Biogeme object
the_biogeme = BIOGEME(database, log_probability)
the_biogeme.model_name = 'b23logit'
Estimate the parameters
results = the_biogeme.estimate()
print(results.short_summary())
Results for model b23logit
Nbr of parameters: 5
Sample size: 2232
Excluded data: 8496
Final log likelihood: -872.9052
Akaike Information Criterion: 1755.81
Bayesian Information Criterion: 1784.364
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_train -1.134867 0.210637 -5.387777 7.133434e-08
1 b_cost_train -2.393364 0.272021 -8.798468 0.000000e+00
2 asc_car -0.896101 0.178268 -5.026696 4.990013e-07
3 b_time_car -0.383847 0.310672 -1.235537 2.166306e-01
4 b_cost_car -1.088054 0.295942 -3.676576 2.363851e-04
Total running time of the script: (0 minutes 1.716 seconds)