10. Nested logit model normalized from bottom

Bayesian estimation of a nested logit model where the normalization is done at the

bottom level.

Michel Bierlaire, EPFL Mon Nov 03 2025, 20:07:02

from IPython.core.display_functions import display

import biogeme.biogeme_logging as blog
from biogeme.bayesian_estimation import BayesianResults, get_pandas_estimated_parameters
from biogeme.biogeme import BIOGEME
from biogeme.expressions import Beta
from biogeme.models import lognested_mev_mu
from biogeme.nests import NestsForNestedLogit, OneNestForNestedLogit

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 b10nested_bottom.py')
Example b10nested_bottom.py

The scale parameters must stay away from zero. We define a small but positive lower bound

POSITIVE_LOWER_BOUND = 1.0e-5

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, 0, 0)
b_cost = Beta('b_cost', 0, None, 0, 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.

scale_parameter = Beta('scale_parameter', 0.5, POSITIVE_LOWER_BOUND, 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 = BayesianResults.from_netcdf(
        filename=f'saved_results/{the_biogeme.model_name}.nc'
    )
except FileNotFoundError:
    results = the_biogeme.bayesian_estimation()
Loaded NetCDF file size: 855.7 MB
load finished in 4356 ms (4.36 s)
print(results.short_summary())
posterior_predictive_loglike finished in 234 ms
expected_log_likelihood finished in 11 ms
best_draw_log_likelihood finished in 11 ms
/Users/bierlair/python_envs/venv313/lib/python3.13/site-packages/arviz/stats/stats.py:1667: UserWarning: For one or more samples the posterior variance of the log predictive densities exceeds 0.4. This could be indication of WAIC starting to fail.
See http://arxiv.org/abs/1507.04544 for details
  warnings.warn(
waic_res finished in 618 ms
waic finished in 618 ms
loo_res finished in 7234 ms (7.23 s)
loo finished in 7234 ms (7.23 s)
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:01:06.198744
Posterior predictive log-likelihood (sum of log mean p)  -5234.16
Expected log-likelihood E[log L(Y|θ)]                    -5239.36
Best-draw log-likelihood (posterior upper bound)         -5236.94
WAIC (Widely Applicable Information Criterion)           -5244.61
WAIC Standard Error                                      62.44
Effective number of parameters (p_WAIC)                  10.44
LOO (Leave-One-Out Cross-Validation)                     -5244.66
LOO Standard Error                                       62.46
Effective number of parameters (p_LOO)                   10.49
pandas_results = get_pandas_estimated_parameters(estimation_results=results)
display(pandas_results)
Diagnostics computation took 22.8 seconds (cached).
              Name  Value (mean)  ...   ESS (bulk)   ESS (tail)
0        asc_train     -1.057208  ...  3217.971743  4084.104963
1          asc_car     -0.347291  ...  3342.393391  4167.286220
2           b_time     -1.850609  ...  4085.167014  5166.466353
3           b_cost     -1.765100  ...  4048.977940  4850.762497
4  scale_parameter      0.485079  ...  3691.634650  4293.053828

[5 rows x 12 columns]

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

Gallery generated by Sphinx-Gallery