Note
Go to the end to download the full example code.
10. Nested logit model normalized from bottomΒΆ
- Example of a nested logit model where the normalization is done at the
bottom level.
Michel Bierlaire, EPFL Sat Jun 21 2025, 16:31:18
from IPython.core.display_functions import display
import biogeme.biogeme_logging as blog
from biogeme.biogeme import BIOGEME
from biogeme.expressions import Beta
from biogeme.models import lognested_mev_mu
from biogeme.nests import NestsForNestedLogit, OneNestForNestedLogit
from biogeme.results_processing import (
EstimationResults,
get_pandas_estimated_parameters,
)
See the data processing script: Data preparation for Swissmetro.
from swissmetro_data import (
CAR_AV_SP,
CAR_CO_SCALED,
CAR_TT_SCALED,
CHOICE,
SM_AV,
SM_COST_SCALED,
SM_TT_SCALED,
TRAIN_AV_SP,
TRAIN_COST_SCALED,
TRAIN_TT_SCALED,
database,
)
logger = blog.get_screen_logger(level=blog.INFO)
logger.info('Example b10_nested_bottom.py')
Example b10_nested_bottom.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)
This is the scale parameter of the choice model. It is usually normalized to one. In this example, we normalize the nest parameter instead, and estimate the scale parameter for the model. If the lower bound is set to zero, the model cannot be evaluated. Therefore, we set the lower bound to a small number, strictly larger than zero.
scale_parameter = Beta('scale_parameter', 0.5, 0.000001, 1.0, 0)
Definition of the utility functions
v_train = asc_train + b_time * TRAIN_TT_SCALED + b_cost * TRAIN_COST_SCALED
v_swissmetro = asc_sm + b_time * SM_TT_SCALED + b_cost * SM_COST_SCALED
v_car = asc_car + b_time * CAR_TT_SCALED + b_cost * CAR_CO_SCALED
Associate utility functions with the numbering of alternatives.
v = {1: v_train, 2: v_swissmetro, 3: v_car}
Associate the availability conditions with the alternatives.
av = {1: TRAIN_AV_SP, 2: SM_AV, 3: CAR_AV_SP}
Definition of nests. Only the non trivial nests must be defined. A trivial nest is a nest containing exactly one alternative. The nest parameter is normalized to 1.
nest_parameter = 1.0
existing = OneNestForNestedLogit(
nest_param=nest_parameter, list_of_alternatives=[1, 3], name='existing'
)
nests = NestsForNestedLogit(choice_set=list(v), tuple_of_nests=(existing,))
The following elements do not appear in any nest and are assumed each to be alone in a separate nest: {2}. If it is not the intention, check the assignment of alternatives to nests.
Definition of the model. This is the contribution of each observation to the log likelihood function. The choice model is a nested logit, with availability conditions, where the scale parameter mu is explicitly involved.
log_probability = lognested_mev_mu(v, av, nests, CHOICE, scale_parameter)
Create the Biogeme object.
the_biogeme = BIOGEME(database, log_probability)
the_biogeme.model_name = 'b10_nested_bottom'
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 __b10_nested_bottom.iter
Cannot read file __b10_nested_bottom.iter. Statement is ignored.
Starting values for the algorithm: {}
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 scale_parameter asc_car Function Relgrad Radius Rho
0 -1 -1 -1 1e-06 1 5.8e+03 0.039 1 0.23 +
1 -0.88 -1.2 -1.3 1 0.62 5.8e+03 0.15 1 0.15 +
2 -0.45 -1.5 -1.1 0.87 -0.38 5.4e+03 0.044 1 0.47 +
3 -0.45 -1.5 -1.1 0.87 -0.38 5.4e+03 0.044 0.44 -1 -
4 -0.89 -1.5 -1.6 0.44 0.06 5.3e+03 0.028 0.44 0.3 +
5 -0.89 -1.5 -1.6 0.44 0.06 5.3e+03 0.028 0.22 -0.69 -
6 -1.1 -1.8 -1.4 0.65 -0.16 5.3e+03 0.029 0.22 0.15 +
7 -0.9 -1.7 -1.4 0.45 -0.38 5.3e+03 0.015 0.22 0.29 +
8 -0.93 -1.7 -1.6 0.57 -0.28 5.2e+03 0.0044 0.22 0.66 +
9 -0.93 -1.7 -1.6 0.57 -0.28 5.2e+03 0.0044 0.11 -1 -
10 -0.93 -1.7 -1.6 0.57 -0.28 5.2e+03 0.0044 0.054 -0.11 -
11 -0.98 -1.7 -1.6 0.52 -0.23 5.2e+03 0.0049 0.054 0.33 +
12 -0.98 -1.8 -1.6 0.54 -0.28 5.2e+03 0.0029 0.054 0.51 +
13 -0.98 -1.8 -1.6 0.54 -0.28 5.2e+03 0.0029 0.027 -0.34 -
14 -0.98 -1.8 -1.6 0.54 -0.28 5.2e+03 0.0029 0.014 0.041 -
15 -0.97 -1.8 -1.6 0.53 -0.28 5.2e+03 0.0024 0.014 0.69 +
16 -0.98 -1.8 -1.6 0.52 -0.29 5.2e+03 0.0023 0.14 0.92 ++
17 -0.98 -1.8 -1.6 0.52 -0.29 5.2e+03 0.0023 0.068 -1 -
18 -0.98 -1.8 -1.6 0.52 -0.29 5.2e+03 0.0023 0.034 -1.3 -
19 -0.98 -1.8 -1.6 0.52 -0.29 5.2e+03 0.0023 0.017 -0.034 -
20 -1 -1.8 -1.6 0.51 -0.28 5.2e+03 0.0024 0.017 0.46 +
21 -1 -1.8 -1.7 0.52 -0.29 5.2e+03 0.0017 0.017 0.71 +
22 -1 -1.8 -1.7 0.5 -0.3 5.2e+03 0.0018 0.017 0.6 +
23 -1 -1.8 -1.7 0.5 -0.31 5.2e+03 0.0011 0.17 0.95 ++
24 -1 -1.8 -1.7 0.5 -0.31 5.2e+03 0.0011 0.085 -1.2 -
25 -1.1 -1.8 -1.8 0.48 -0.32 5.2e+03 0.0018 0.085 0.24 +
26 -1.1 -1.8 -1.8 0.48 -0.32 5.2e+03 0.0018 0.043 -1.2 -
27 -1 -1.9 -1.8 0.49 -0.36 5.2e+03 0.0017 0.043 0.12 +
28 -1 -1.9 -1.8 0.49 -0.36 5.2e+03 0.0017 0.021 -0.0035 -
29 -1.1 -1.9 -1.8 0.48 -0.35 5.2e+03 0.0009 0.021 0.59 +
30 -1.1 -1.9 -1.8 0.48 -0.35 5.2e+03 0.0009 0.011 -0.19 -
31 -1.1 -1.8 -1.8 0.49 -0.35 5.2e+03 0.00053 0.011 0.59 +
32 -1.1 -1.8 -1.8 0.49 -0.35 5.2e+03 0.00053 0.0053 -0.048 -
33 -1.1 -1.8 -1.8 0.49 -0.35 5.2e+03 0.00027 0.0053 0.49 +
34 -1 -1.8 -1.8 0.49 -0.34 5.2e+03 0.00016 0.0053 0.4 +
35 -1 -1.8 -1.8 0.49 -0.34 5.2e+03 0.00016 0.0027 -0.33 -
36 -1.1 -1.8 -1.8 0.49 -0.34 5.2e+03 5.5e-05 0.0027 0.64 +
37 -1.1 -1.8 -1.8 0.49 -0.34 5.2e+03 5.5e-05 0.0013 -2.7 -
38 -1.1 -1.8 -1.8 0.49 -0.34 5.2e+03 5.5e-05 0.00067 -0.37 -
39 -1.1 -1.8 -1.8 0.49 -0.34 5.2e+03 5.9e-05 0.00067 0.62 +
40 -1.1 -1.8 -1.8 0.49 -0.34 5.2e+03 5.9e-05 0.00033 -0.59 -
41 -1.1 -1.8 -1.8 0.49 -0.34 5.2e+03 7.6e-05 0.00033 0.19 +
42 -1.1 -1.8 -1.8 0.49 -0.34 5.2e+03 1.2e-05 0.0033 0.92 ++
43 -1.1 -1.8 -1.8 0.49 -0.34 5.2e+03 1.2e-05 0.0017 -7.5 -
44 -1.1 -1.8 -1.8 0.49 -0.34 5.2e+03 1.2e-05 0.00083 -1.5 -
45 -1.1 -1.8 -1.8 0.49 -0.34 5.2e+03 1.2e-05 0.00042 -0.06 -
46 -1.1 -1.8 -1.8 0.49 -0.34 5.2e+03 4.1e-06 0.00042 0.83 -
Optimization algorithm has converged.
Relative gradient: 4.075657253042217e-06
Cause of termination: Relative gradient = 4.1e-06 <= 6.1e-06
Number of function evaluations: 100
Number of gradient evaluations: 53
Number of hessian evaluations: 0
Algorithm: BFGS with trust region for simple bound constraints
Number of iterations: 47
Proportion of Hessian calculation: 0/26 = 0.0%
Optimization time: 0:00:00.417002
Calculate second derivatives and BHHH
File b10_nested_bottom.html has been generated.
File b10_nested_bottom.yaml has been generated.
print(results.short_summary())
Results for model b10_nested_bottom
Nbr of parameters: 5
Sample size: 6768
Excluded data: 3960
Final log likelihood: -5236.9
Akaike Information Criterion: 10483.8
Bayesian Information Criterion: 10517.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 asc_train -1.051457 0.164958 -6.374090 1.840521e-10
1 b_time -1.845921 0.225646 -8.180598 2.220446e-16
2 b_cost -1.759568 0.149300 -11.785451 0.000000e+00
3 scale_parameter 0.486866 0.038917 12.510471 0.000000e+00
4 asc_car -0.343211 0.118811 -2.888721 3.868117e-03
Total running time of the script: (0 minutes 1.522 seconds)