Cross-nested logitΒΆ

Example of a cross-nested logit model with two nests:

  • one with existing alternatives (car and train),

  • one with public transportation alternatives (train and Swissmetro)

author:

Michel Bierlaire, EPFL

date:

Sun Apr 9 18:06:44 2023

import biogeme.biogeme_logging as blog
import biogeme.biogeme as bio
from biogeme import models
from biogeme.expressions import Beta
from biogeme.nests import OneNestForCrossNestedLogit, NestsForCrossNestedLogit

See the data processing script: Data preparation for Swissmetro.

from swissmetro_data import (
    database,
    CHOICE,
    SM_AV,
    CAR_AV_SP,
    TRAIN_AV_SP,
    TRAIN_TT_SCALED,
    TRAIN_COST_SCALED,
    SM_TT_SCALED,
    SM_COST_SCALED,
    CAR_TT_SCALED,
    CAR_CO_SCALED,
)

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

Parameters to be estimated.

ASC_CAR = Beta('ASC_CAR', 0, None, None, 0)
ASC_TRAIN = Beta('ASC_TRAIN', 0, None, None, 0)
ASC_SM = Beta('ASC_SM', 0, None, None, 1)
B_TIME = Beta('B_TIME', 0, None, None, 0)
B_COST = Beta('B_COST', 0, None, None, 0)
MU_EXISTING = Beta('MU_EXISTING', 1, 1, 5, 0)
MU_PUBLIC = Beta('MU_PUBLIC', 1, 1, 5, 0)

Nest membership parameters.

ALPHA_EXISTING = Beta('ALPHA_EXISTING', 0.5, 0, 1, 0)
ALPHA_PUBLIC = 1 - ALPHA_EXISTING

Definition of the utility functions

V1 = ASC_TRAIN + B_TIME * TRAIN_TT_SCALED + B_COST * TRAIN_COST_SCALED
V2 = ASC_SM + B_TIME * SM_TT_SCALED + B_COST * SM_COST_SCALED
V3 = ASC_CAR + B_TIME * CAR_TT_SCALED + B_COST * CAR_CO_SCALED

Associate utility functions with the numbering of alternatives

V = {1: V1, 2: V2, 3: V3}

Associate the availability conditions with the alternatives

av = {1: TRAIN_AV_SP, 2: SM_AV, 3: CAR_AV_SP}

Definition of nests.

nest_existing = OneNestForCrossNestedLogit(
    nest_param=MU_EXISTING,
    dict_of_alpha={1: ALPHA_EXISTING, 2: 0.0, 3: 1.0},
    name='existing',
)

nest_public = OneNestForCrossNestedLogit(
    nest_param=MU_PUBLIC, dict_of_alpha={1: ALPHA_PUBLIC, 2: 1.0, 3: 0.0}, name='public'
)

nests = NestsForCrossNestedLogit(
    choice_set=[1, 2, 3], tuple_of_nests=(nest_existing, nest_public)
)

The choice model is a cross-nested logit, with availability conditions.

logprob = models.logcnl(V, av, nests, CHOICE)

Create the Biogeme object

the_biogeme = bio.BIOGEME(database, logprob)
the_biogeme.modelName = 'b11cnl'
Biogeme parameters read from biogeme.toml.
/Users/bierlair/MyFiles/github/biogeme/docs/source/examples/swissmetro/plot_b11cnl_biogeme3.2.14.py:96: DeprecationWarning: 'modelName' is deprecated. Please use 'model_name' instead.
  the_biogeme.modelName = 'b11cnl'

Estimate the parameters.

results = the_biogeme.estimate()
*** Initial values of the parameters are obtained from the file __b11cnl.iter
Parameter values restored from __b11cnl.iter
Starting values for the algorithm: {'asc_train': -0.03125, 'b_time_train': -0.03125, 'b_cost': -0.03125, 'b_headway_train': -0.03125, 'ga_train': 0.03125, 'alpha_existing': 0.5, 'existing_nest_parameter': 1.03125, 'b_time_swissmetro': 0.03125, 'b_headway_swissmetro': 0.03125, 'ga_swissmetro': 0.03125, 'asc_car': -0.03125, 'b_time_car': -0.03125, 'public_nest_parameter': 1.0}
As the model is rather complex, we cancel the calculation of second derivatives. If you want to control the parameters, change the algorithm from "automatic" to "simple_bounds" in the TOML file.
Optimization algorithm: hybrid Newton/BFGS with simple bounds [simple_bounds]
** Optimization: BFGS with trust region for simple bounds
Iter.       ASC_TRAIN          B_TIME          B_COST  ALPHA_EXISTING     MU_EXISTING         ASC_CAR       MU_PUBLIC     Function    Relgrad   Radius      Rho
    0               0               0               0             0.5               1               0               1        7e+03       0.27      0.5        0    -
    1               0               0               0             0.5               1               0               1        7e+03       0.27     0.25        0    -
    2               0               0               0             0.5               1               0               1        7e+03       0.27     0.12        0    -
    3               0               0               0             0.5               1               0               1        7e+03       0.27    0.062        0    -
    4               0               0               0             0.5               1               0               1        7e+03       0.27    0.031        0    -
    5               0               0               0             0.5               1               0               1        7e+03       0.27    0.016        0    -
    6               0               0               0             0.5               1               0               1        7e+03       0.27   0.0078        0    -
    7               0               0               0             0.5               1               0               1        7e+03       0.27   0.0039        0    -
    8               0               0               0             0.5               1               0               1        7e+03       0.27    0.002        0    -
    9               0               0               0             0.5               1               0               1        7e+03       0.27  0.00098        0    -
   10               0               0               0             0.5               1               0               1        7e+03       0.27  0.00049        0    -
   11               0               0               0             0.5               1               0               1        7e+03       0.27  0.00024        0    -
   12               0               0               0             0.5               1               0               1        7e+03       0.27  0.00012        0    -
   13               0               0               0             0.5               1               0               1        7e+03       0.27  6.1e-05        0    -
   14               0               0               0             0.5               1               0               1        7e+03       0.27  3.1e-05        0    -
   15               0               0               0             0.5               1               0               1        7e+03       0.27  1.5e-05        0    -
   16               0               0               0             0.5               1               0               1        7e+03       0.27  7.6e-06        0    -
   17               0               0               0             0.5               1               0               1        7e+03       0.27  3.8e-06        0    -
   18               0               0               0             0.5               1               0               1        7e+03       0.27  1.9e-06        0    -
   19               0               0               0             0.5               1               0               1        7e+03       0.27  9.5e-07        0    -
   20               0               0               0             0.5               1               0               1        7e+03       0.27  4.8e-07        0    -
   21               0               0               0             0.5               1               0               1        7e+03       0.27  2.4e-07        0    -
   22               0               0               0             0.5               1               0               1        7e+03       0.27  1.2e-07        0    -
   23               0               0               0             0.5               1               0               1        7e+03       0.27    6e-08        0    -
   24               0               0               0             0.5               1               0               1        7e+03       0.27    3e-08        0    -
   25               0               0               0             0.5               1               0               1        7e+03       0.27  1.5e-08        0    -
Optimization algorithm has *not* converged.
Algorithm: BFGS with trust region for simple bound constraints
Cause of termination: Trust region is too small: 1.4901161193847656e-08
Number of iterations: 26
Proportion of Hessian calculation: 0/1 = 0.0%
Optimization time: 0:00:05.088397
Calculate second derivatives and BHHH
It seems that the optimization algorithm did not converge. Therefore, the results may not correspond to the maximum likelihood estimator. Check the specification of the model, or the criteria for convergence of the algorithm.
File b11cnl~02.html has been generated.
File b11cnl~02.yaml has been generated.
print(results.short_summary())
Results for model b11cnl
Nbr of parameters:              7
Sample size:                    6768
Excluded data:                  3960
Final log likelihood:           -6964.663
Akaike Information Criterion:   13943.33
Bayesian Information Criterion: 13991.07
pandas_results = results.get_estimated_parameters()
pandas_results
/Users/bierlair/MyFiles/github/biogeme/docs/source/examples/swissmetro/plot_b11cnl_biogeme3.2.14.py:106: DeprecationWarning: get_estimated_parameters is deprecated. Use get_pandas_estimated_parameters(estimation_results=my_results) instead
  pandas_results = results.get_estimated_parameters()
Name Value Robust std err. Robust t-stat. Robust p-value Active bound
0 ASC_TRAIN 0.0 0.032065 0.000000 1.0 False
1 B_TIME 0.0 0.035002 0.000000 1.0 False
2 B_COST 0.0 0.041720 0.000000 1.0 False
3 ALPHA_EXISTING 0.5 0.054714 9.138463 0.0 False
4 MU_EXISTING 1.0 0.024179 41.358850 0.0 True
5 ASC_CAR 0.0 0.035479 0.000000 1.0 False
6 MU_PUBLIC 1.0 0.038077 26.262430 0.0 True


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

Gallery generated by Sphinx-Gallery