Cross-nested logit

Estimation of a cross-nested logit model using sampling of alternatives.

Michel Bierlaire Sat Jul 26 2025, 14:57:17

import pandas as pd
from IPython.core.display_functions import display

import biogeme.biogeme_logging as blog
from alternatives import (
    ID_COLUMN,
    all_alternatives,
    alternatives,
    asian_and_downtown,
    only_asian,
    only_downtown,
    partitions,
)
from biogeme.biogeme import BIOGEME
from biogeme.expressions import Beta
from biogeme.nests import NestsForCrossNestedLogit, OneNestForCrossNestedLogit
from biogeme.results_processing import get_pandas_estimated_parameters
from biogeme.sampling_of_alternatives import (
    ChoiceSetsGeneration,
    GenerateModel,
    SamplingContext,
    generate_segment_size,
)
from compare import compare
from specification_sampling import V, combined_variables
logger = blog.get_screen_logger(level=blog.INFO)
PARTITION = 'downtown'
MEV_PARTITION = 'uniform_asian_or_downtown'
SAMPLE_SIZE = 10  # out of 100 alternatives
SAMPLE_SIZE_MEV = 63  # out of 63 alternatives
CHOICE_COLUMN = 'cnl_3'
MODEL_NAME = f'cnl_{SAMPLE_SIZE}_{SAMPLE_SIZE_MEV}'
FILE_NAME = f'{MODEL_NAME}.dat'
the_partition = partitions.get(PARTITION)
if the_partition is None:
    raise ValueError(f'Unknown partition: {PARTITION}')
segment_sizes = list(
    generate_segment_size(SAMPLE_SIZE, the_partition.number_of_segments())
)

We use all alternatives in the nest.

mev_partition = partitions.get(MEV_PARTITION)
if mev_partition is None:
    raise ValueError(f'Unknown partition: {MEV_PARTITION}')
mev_segment_sizes = [
    SAMPLE_SIZE_MEV,
]

Nests

Downtown

mu_downtown = Beta('mu_downtown', 1, 1, None, 0)
downtown_alpha_dict = {i: 0.5 for i in asian_and_downtown} | {
    i: 1 for i in only_downtown
}
downtown_nest = OneNestForCrossNestedLogit(
    nest_param=mu_downtown, dict_of_alpha=downtown_alpha_dict, name='downtown'
)

Asian

mu_asian = Beta('mu_asian', 1, 1, None, 0)
asian_alpha_dict = {i: 0.5 for i in asian_and_downtown} | {i: 1.0 for i in only_asian}
asian_nest = OneNestForCrossNestedLogit(
    nest_param=mu_asian, dict_of_alpha=asian_alpha_dict, name='asian'
)

cnl_nests = NestsForCrossNestedLogit(
    choice_set=all_alternatives,
    tuple_of_nests=(downtown_nest, asian_nest),
)
The following elements do not appear in any nest and are assumed each to be alone in a separate nest: {2, 4, 5, 6, 8, 10, 12, 16, 19, 21, 23, 25, 26, 28, 29, 35, 39, 41, 42, 43, 44, 52, 53, 58, 59, 62, 64, 65, 66, 67, 69, 71, 73, 82, 85, 95, 96}. If it is not the intention, check the assignment of alternatives to nests.
observations = pd.read_csv('obs_choice.dat')
context = SamplingContext(
    the_partition=the_partition,
    sample_sizes=segment_sizes,
    individuals=observations,
    choice_column=CHOICE_COLUMN,
    alternatives=alternatives,
    id_column=ID_COLUMN,
    biogeme_file_name=FILE_NAME,
    utility_function=V,
    combined_variables=combined_variables,
    mev_partition=mev_partition,
    mev_sample_sizes=mev_segment_sizes,
    cnl_nests=cnl_nests,
)
logger.info(context.reporting())
Size of the choice set: 100
Main partition: 2 segment(s) of size 46, 54
Main sample: 10: 5/46, 5/54
Nbr of MEV alternatives: 63
MEV partition: 1 segment(s) of size 63
MEV sample: 63: 63/63
the_data_generation = ChoiceSetsGeneration(context=context)
the_model_generation = GenerateModel(context=context)
biogeme_database = the_data_generation.sample_and_merge(recycle=False)
Generating 10 + 63 alternatives for 10000 observations

  0%|          | 0/10000 [00:00<?, ?it/s]
  0%|          | 22/10000 [00:00<00:45, 217.50it/s]
  0%|          | 44/10000 [00:00<00:46, 216.05it/s]
  1%|          | 66/10000 [00:00<00:45, 215.98it/s]
  1%|          | 88/10000 [00:00<00:45, 215.64it/s]
  1%|          | 110/10000 [00:00<00:45, 216.21it/s]
  1%|▏         | 132/10000 [00:00<00:45, 216.76it/s]
  2%|▏         | 154/10000 [00:00<00:45, 217.74it/s]
  2%|▏         | 176/10000 [00:00<00:45, 218.04it/s]
  2%|▏         | 198/10000 [00:00<00:45, 215.91it/s]
  2%|▏         | 220/10000 [00:01<00:45, 215.30it/s]
  2%|▏         | 242/10000 [00:01<00:45, 215.02it/s]
  3%|▎         | 264/10000 [00:01<00:45, 214.60it/s]
  3%|▎         | 286/10000 [00:01<00:45, 214.76it/s]
  3%|▎         | 308/10000 [00:01<00:45, 215.20it/s]
  3%|▎         | 330/10000 [00:01<00:45, 214.88it/s]
  4%|▎         | 352/10000 [00:01<00:44, 214.76it/s]
  4%|▎         | 374/10000 [00:01<00:44, 215.48it/s]
  4%|▍         | 396/10000 [00:01<00:44, 215.71it/s]
  4%|▍         | 418/10000 [00:01<00:44, 216.60it/s]
  4%|▍         | 440/10000 [00:02<00:44, 216.51it/s]
  5%|▍         | 462/10000 [00:02<00:43, 217.15it/s]
  5%|▍         | 484/10000 [00:02<00:43, 217.39it/s]
  5%|▌         | 506/10000 [00:02<00:43, 217.20it/s]
  5%|▌         | 528/10000 [00:02<00:44, 212.89it/s]
  6%|▌         | 550/10000 [00:02<00:45, 209.52it/s]
  6%|▌         | 572/10000 [00:02<00:44, 211.30it/s]
  6%|▌         | 594/10000 [00:02<00:44, 213.07it/s]
  6%|▌         | 616/10000 [00:02<00:43, 213.38it/s]
  6%|▋         | 638/10000 [00:02<00:43, 214.38it/s]
  7%|▋         | 660/10000 [00:03<00:43, 214.72it/s]
  7%|▋         | 682/10000 [00:03<00:43, 215.01it/s]
  7%|▋         | 704/10000 [00:03<00:43, 215.30it/s]
  7%|▋         | 726/10000 [00:03<00:43, 215.20it/s]
  7%|▋         | 748/10000 [00:03<00:42, 215.64it/s]
  8%|▊         | 770/10000 [00:03<00:42, 215.82it/s]
  8%|▊         | 792/10000 [00:03<00:42, 216.00it/s]
  8%|▊         | 814/10000 [00:03<00:42, 216.32it/s]
  8%|▊         | 836/10000 [00:03<00:42, 216.24it/s]
  9%|▊         | 858/10000 [00:03<00:42, 215.22it/s]
  9%|▉         | 880/10000 [00:04<00:42, 213.85it/s]
  9%|▉         | 902/10000 [00:04<00:42, 214.15it/s]
  9%|▉         | 924/10000 [00:04<00:42, 215.40it/s]
  9%|▉         | 946/10000 [00:04<00:42, 214.48it/s]
 10%|▉         | 968/10000 [00:04<00:41, 215.69it/s]
 10%|▉         | 990/10000 [00:04<00:41, 215.80it/s]
 10%|█         | 1012/10000 [00:04<00:41, 216.48it/s]
 10%|█         | 1034/10000 [00:04<00:41, 217.17it/s]
 11%|█         | 1056/10000 [00:04<00:41, 217.08it/s]
 11%|█         | 1078/10000 [00:05<00:40, 217.62it/s]
 11%|█         | 1100/10000 [00:05<00:41, 215.61it/s]
 11%|█         | 1122/10000 [00:05<00:41, 215.59it/s]
 11%|█▏        | 1144/10000 [00:05<00:41, 215.04it/s]
 12%|█▏        | 1166/10000 [00:05<00:40, 215.76it/s]
 12%|█▏        | 1188/10000 [00:05<00:40, 215.44it/s]
 12%|█▏        | 1210/10000 [00:05<00:40, 215.17it/s]
 12%|█▏        | 1232/10000 [00:05<00:40, 215.76it/s]
 13%|█▎        | 1254/10000 [00:05<00:40, 215.65it/s]
 13%|█▎        | 1276/10000 [00:05<00:40, 216.07it/s]
 13%|█▎        | 1298/10000 [00:06<00:40, 216.20it/s]
 13%|█▎        | 1320/10000 [00:06<00:40, 214.47it/s]
 13%|█▎        | 1342/10000 [00:06<00:40, 215.41it/s]
 14%|█▎        | 1364/10000 [00:06<00:40, 215.56it/s]
 14%|█▍        | 1386/10000 [00:06<00:39, 216.31it/s]
 14%|█▍        | 1408/10000 [00:06<00:39, 216.21it/s]
 14%|█▍        | 1430/10000 [00:06<00:39, 216.22it/s]
 15%|█▍        | 1452/10000 [00:06<00:39, 216.59it/s]
 15%|█▍        | 1474/10000 [00:06<00:39, 216.33it/s]
 15%|█▍        | 1496/10000 [00:06<00:39, 216.74it/s]
 15%|█▌        | 1518/10000 [00:07<00:39, 213.94it/s]
 15%|█▌        | 1540/10000 [00:07<00:39, 213.94it/s]
 16%|█▌        | 1562/10000 [00:07<00:39, 214.31it/s]
 16%|█▌        | 1584/10000 [00:07<00:39, 214.77it/s]
 16%|█▌        | 1606/10000 [00:07<00:38, 215.27it/s]
 16%|█▋        | 1628/10000 [00:07<00:38, 215.81it/s]
 16%|█▋        | 1650/10000 [00:07<00:38, 216.34it/s]
 17%|█▋        | 1672/10000 [00:07<00:38, 216.82it/s]
 17%|█▋        | 1694/10000 [00:07<00:38, 217.28it/s]
 17%|█▋        | 1716/10000 [00:07<00:38, 217.63it/s]
 17%|█▋        | 1738/10000 [00:08<00:38, 217.22it/s]
 18%|█▊        | 1760/10000 [00:08<00:37, 217.42it/s]
 18%|█▊        | 1782/10000 [00:08<00:37, 217.90it/s]
 18%|█▊        | 1804/10000 [00:08<00:37, 217.43it/s]
 18%|█▊        | 1826/10000 [00:08<00:37, 217.32it/s]
 18%|█▊        | 1848/10000 [00:08<00:37, 217.79it/s]
 19%|█▊        | 1871/10000 [00:08<00:37, 218.47it/s]
 19%|█▉        | 1894/10000 [00:08<00:37, 219.01it/s]
 19%|█▉        | 1916/10000 [00:08<00:36, 219.24it/s]
 19%|█▉        | 1938/10000 [00:08<00:36, 219.24it/s]
 20%|█▉        | 1960/10000 [00:09<00:37, 213.08it/s]
 20%|█▉        | 1982/10000 [00:09<00:37, 214.23it/s]
 20%|██        | 2004/10000 [00:09<00:37, 215.27it/s]
 20%|██        | 2026/10000 [00:09<00:37, 213.40it/s]
 20%|██        | 2048/10000 [00:09<00:37, 213.95it/s]
 21%|██        | 2070/10000 [00:09<00:37, 212.67it/s]
 21%|██        | 2092/10000 [00:09<00:37, 212.56it/s]
 21%|██        | 2114/10000 [00:09<00:36, 214.32it/s]
 21%|██▏       | 2136/10000 [00:09<00:36, 215.45it/s]
 22%|██▏       | 2158/10000 [00:10<00:36, 215.23it/s]
 22%|██▏       | 2180/10000 [00:10<00:36, 214.19it/s]
 22%|██▏       | 2202/10000 [00:10<00:36, 215.33it/s]
 22%|██▏       | 2224/10000 [00:10<00:36, 215.35it/s]
 22%|██▏       | 2246/10000 [00:10<00:35, 215.73it/s]
 23%|██▎       | 2268/10000 [00:10<00:35, 216.31it/s]
 23%|██▎       | 2290/10000 [00:10<00:35, 215.88it/s]
 23%|██▎       | 2312/10000 [00:10<00:35, 217.00it/s]
 23%|██▎       | 2334/10000 [00:10<00:35, 217.36it/s]
 24%|██▎       | 2356/10000 [00:10<00:35, 217.67it/s]
 24%|██▍       | 2378/10000 [00:11<00:35, 216.67it/s]
 24%|██▍       | 2400/10000 [00:11<00:35, 212.14it/s]
 24%|██▍       | 2422/10000 [00:11<00:35, 213.69it/s]
 24%|██▍       | 2444/10000 [00:11<00:35, 214.48it/s]
 25%|██▍       | 2466/10000 [00:11<00:35, 211.91it/s]
 25%|██▍       | 2488/10000 [00:11<00:35, 212.20it/s]
 25%|██▌       | 2510/10000 [00:11<00:35, 213.86it/s]
 25%|██▌       | 2532/10000 [00:11<00:34, 214.64it/s]
 26%|██▌       | 2554/10000 [00:11<00:34, 214.69it/s]
 26%|██▌       | 2576/10000 [00:11<00:34, 214.80it/s]
 26%|██▌       | 2598/10000 [00:12<00:34, 214.58it/s]
 26%|██▌       | 2620/10000 [00:12<00:34, 215.48it/s]
 26%|██▋       | 2642/10000 [00:12<00:34, 216.29it/s]
 27%|██▋       | 2664/10000 [00:12<00:33, 216.48it/s]
 27%|██▋       | 2686/10000 [00:12<00:33, 217.17it/s]
 27%|██▋       | 2708/10000 [00:12<00:33, 217.42it/s]
 27%|██▋       | 2730/10000 [00:12<00:33, 217.63it/s]
 28%|██▊       | 2752/10000 [00:12<00:33, 217.76it/s]
 28%|██▊       | 2774/10000 [00:12<00:33, 216.98it/s]
 28%|██▊       | 2796/10000 [00:12<00:33, 216.75it/s]
 28%|██▊       | 2818/10000 [00:13<00:33, 216.39it/s]
 28%|██▊       | 2840/10000 [00:13<00:33, 216.63it/s]
 29%|██▊       | 2862/10000 [00:13<00:32, 217.47it/s]
 29%|██▉       | 2884/10000 [00:13<00:32, 217.31it/s]
 29%|██▉       | 2906/10000 [00:13<00:32, 217.72it/s]
 29%|██▉       | 2928/10000 [00:13<00:32, 218.20it/s]
 30%|██▉       | 2950/10000 [00:13<00:32, 218.67it/s]
 30%|██▉       | 2973/10000 [00:13<00:32, 219.18it/s]
 30%|██▉       | 2995/10000 [00:13<00:32, 218.25it/s]
 30%|███       | 3017/10000 [00:13<00:31, 218.27it/s]
 30%|███       | 3039/10000 [00:14<00:31, 217.65it/s]
 31%|███       | 3061/10000 [00:14<00:31, 217.92it/s]
 31%|███       | 3083/10000 [00:14<00:31, 218.06it/s]
 31%|███       | 3105/10000 [00:14<00:31, 217.66it/s]
 31%|███▏      | 3127/10000 [00:14<00:31, 216.57it/s]
 31%|███▏      | 3149/10000 [00:14<00:31, 216.80it/s]
 32%|███▏      | 3171/10000 [00:14<00:31, 217.13it/s]
 32%|███▏      | 3193/10000 [00:14<00:31, 217.54it/s]
 32%|███▏      | 3215/10000 [00:14<00:31, 217.17it/s]
 32%|███▏      | 3237/10000 [00:14<00:31, 217.42it/s]
 33%|███▎      | 3259/10000 [00:15<00:31, 216.86it/s]
 33%|███▎      | 3281/10000 [00:15<00:30, 217.32it/s]
 33%|███▎      | 3303/10000 [00:15<00:30, 217.72it/s]
 33%|███▎      | 3325/10000 [00:15<00:30, 217.36it/s]
 33%|███▎      | 3347/10000 [00:15<00:30, 217.95it/s]
 34%|███▎      | 3369/10000 [00:15<00:30, 217.87it/s]
 34%|███▍      | 3392/10000 [00:15<00:30, 218.80it/s]
 34%|███▍      | 3415/10000 [00:15<00:30, 219.03it/s]
 34%|███▍      | 3437/10000 [00:15<00:29, 218.80it/s]
 35%|███▍      | 3459/10000 [00:16<00:29, 218.18it/s]
 35%|███▍      | 3481/10000 [00:16<00:29, 217.62it/s]
 35%|███▌      | 3503/10000 [00:16<00:29, 218.05it/s]
 35%|███▌      | 3525/10000 [00:16<00:29, 217.88it/s]
 35%|███▌      | 3547/10000 [00:16<00:29, 216.50it/s]
 36%|███▌      | 3569/10000 [00:16<00:29, 217.30it/s]
 36%|███▌      | 3591/10000 [00:16<00:29, 217.51it/s]
 36%|███▌      | 3613/10000 [00:16<00:29, 218.11it/s]
 36%|███▋      | 3635/10000 [00:16<00:29, 218.15it/s]
 37%|███▋      | 3657/10000 [00:16<00:29, 217.62it/s]
 37%|███▋      | 3679/10000 [00:17<00:29, 217.03it/s]
 37%|███▋      | 3701/10000 [00:17<00:29, 216.27it/s]
 37%|███▋      | 3723/10000 [00:17<00:28, 216.55it/s]
 37%|███▋      | 3745/10000 [00:17<00:28, 216.79it/s]
 38%|███▊      | 3767/10000 [00:17<00:28, 217.23it/s]
 38%|███▊      | 3789/10000 [00:17<00:28, 216.09it/s]
 38%|███▊      | 3811/10000 [00:17<00:28, 215.45it/s]
 38%|███▊      | 3833/10000 [00:17<00:28, 215.82it/s]
 39%|███▊      | 3855/10000 [00:17<00:28, 215.85it/s]
 39%|███▉      | 3877/10000 [00:17<00:28, 215.92it/s]
 39%|███▉      | 3899/10000 [00:18<00:28, 216.04it/s]
 39%|███▉      | 3921/10000 [00:18<00:27, 217.12it/s]
 39%|███▉      | 3943/10000 [00:18<00:27, 217.31it/s]
 40%|███▉      | 3965/10000 [00:18<00:27, 217.67it/s]
 40%|███▉      | 3987/10000 [00:18<00:27, 217.82it/s]
 40%|████      | 4009/10000 [00:18<00:27, 217.39it/s]
 40%|████      | 4031/10000 [00:18<00:27, 217.75it/s]
 41%|████      | 4053/10000 [00:18<00:27, 217.94it/s]
 41%|████      | 4075/10000 [00:18<00:27, 217.29it/s]
 41%|████      | 4097/10000 [00:18<00:27, 217.11it/s]
 41%|████      | 4119/10000 [00:19<00:27, 215.56it/s]
 41%|████▏     | 4141/10000 [00:19<00:27, 216.19it/s]
 42%|████▏     | 4163/10000 [00:19<00:26, 216.81it/s]
 42%|████▏     | 4185/10000 [00:19<00:26, 216.45it/s]
 42%|████▏     | 4207/10000 [00:19<00:26, 216.99it/s]
 42%|████▏     | 4229/10000 [00:19<00:26, 217.16it/s]
 43%|████▎     | 4251/10000 [00:19<00:26, 217.69it/s]
 43%|████▎     | 4273/10000 [00:19<00:26, 218.14it/s]
 43%|████▎     | 4295/10000 [00:19<00:26, 218.58it/s]
 43%|████▎     | 4317/10000 [00:19<00:26, 218.51it/s]
 43%|████▎     | 4339/10000 [00:20<00:25, 218.09it/s]
 44%|████▎     | 4361/10000 [00:20<00:25, 218.27it/s]
 44%|████▍     | 4383/10000 [00:20<00:25, 218.53it/s]
 44%|████▍     | 4405/10000 [00:20<00:25, 217.98it/s]
 44%|████▍     | 4427/10000 [00:20<00:25, 218.09it/s]
 44%|████▍     | 4449/10000 [00:20<00:25, 217.52it/s]
 45%|████▍     | 4471/10000 [00:20<00:25, 217.66it/s]
 45%|████▍     | 4493/10000 [00:20<00:25, 217.32it/s]
 45%|████▌     | 4515/10000 [00:20<00:25, 216.19it/s]
 45%|████▌     | 4537/10000 [00:20<00:25, 216.00it/s]
 46%|████▌     | 4559/10000 [00:21<00:25, 215.59it/s]
 46%|████▌     | 4581/10000 [00:21<00:25, 215.97it/s]
 46%|████▌     | 4603/10000 [00:21<00:24, 216.70it/s]
 46%|████▋     | 4625/10000 [00:21<00:24, 216.48it/s]
 46%|████▋     | 4647/10000 [00:21<00:24, 214.98it/s]
 47%|████▋     | 4669/10000 [00:21<00:24, 214.45it/s]
 47%|████▋     | 4691/10000 [00:21<00:24, 214.98it/s]
 47%|████▋     | 4713/10000 [00:21<00:24, 214.96it/s]
 47%|████▋     | 4735/10000 [00:21<00:24, 214.20it/s]
 48%|████▊     | 4757/10000 [00:22<00:24, 213.30it/s]
 48%|████▊     | 4779/10000 [00:22<00:24, 211.17it/s]
 48%|████▊     | 4801/10000 [00:22<00:24, 211.67it/s]
 48%|████▊     | 4823/10000 [00:22<00:24, 211.89it/s]
 48%|████▊     | 4845/10000 [00:22<00:24, 210.56it/s]
 49%|████▊     | 4867/10000 [00:22<00:24, 210.17it/s]
 49%|████▉     | 4889/10000 [00:22<00:24, 211.26it/s]
 49%|████▉     | 4911/10000 [00:22<00:23, 212.29it/s]
 49%|████▉     | 4933/10000 [00:22<00:23, 212.28it/s]
 50%|████▉     | 4955/10000 [00:22<00:23, 213.63it/s]
 50%|████▉     | 4977/10000 [00:23<00:23, 214.22it/s]
 50%|████▉     | 4999/10000 [00:23<00:23, 215.42it/s]
 50%|█████     | 5021/10000 [00:23<00:23, 216.34it/s]
 50%|█████     | 5043/10000 [00:23<00:22, 215.73it/s]
 51%|█████     | 5065/10000 [00:23<00:22, 216.23it/s]
 51%|█████     | 5087/10000 [00:23<00:22, 215.43it/s]
 51%|█████     | 5109/10000 [00:23<00:22, 216.45it/s]
 51%|█████▏    | 5131/10000 [00:23<00:22, 216.58it/s]
 52%|█████▏    | 5153/10000 [00:23<00:22, 216.38it/s]
 52%|█████▏    | 5175/10000 [00:23<00:22, 216.58it/s]
 52%|█████▏    | 5197/10000 [00:24<00:22, 217.12it/s]
 52%|█████▏    | 5219/10000 [00:24<00:21, 217.85it/s]
 52%|█████▏    | 5241/10000 [00:24<00:21, 218.22it/s]
 53%|█████▎    | 5263/10000 [00:24<00:21, 216.09it/s]
 53%|█████▎    | 5285/10000 [00:24<00:21, 216.19it/s]
 53%|█████▎    | 5307/10000 [00:24<00:21, 216.43it/s]
 53%|█████▎    | 5329/10000 [00:24<00:21, 216.86it/s]
 54%|█████▎    | 5351/10000 [00:24<00:21, 216.86it/s]
 54%|█████▎    | 5373/10000 [00:24<00:21, 215.97it/s]
 54%|█████▍    | 5395/10000 [00:24<00:21, 215.99it/s]
 54%|█████▍    | 5417/10000 [00:25<00:21, 215.02it/s]
 54%|█████▍    | 5439/10000 [00:25<00:21, 216.47it/s]
 55%|█████▍    | 5461/10000 [00:25<00:20, 217.44it/s]
 55%|█████▍    | 5483/10000 [00:25<00:20, 215.38it/s]
 55%|█████▌    | 5505/10000 [00:25<00:20, 215.96it/s]
 55%|█████▌    | 5527/10000 [00:25<00:20, 214.93it/s]
 55%|█████▌    | 5549/10000 [00:25<00:20, 215.86it/s]
 56%|█████▌    | 5571/10000 [00:25<00:20, 216.53it/s]
 56%|█████▌    | 5593/10000 [00:25<00:20, 216.05it/s]
 56%|█████▌    | 5615/10000 [00:25<00:20, 216.58it/s]
 56%|█████▋    | 5637/10000 [00:26<00:20, 216.72it/s]
 57%|█████▋    | 5659/10000 [00:26<00:19, 217.19it/s]
 57%|█████▋    | 5681/10000 [00:26<00:19, 217.53it/s]
 57%|█████▋    | 5703/10000 [00:26<00:19, 216.17it/s]
 57%|█████▋    | 5725/10000 [00:26<00:19, 216.70it/s]
 57%|█████▋    | 5747/10000 [00:26<00:19, 216.46it/s]
 58%|█████▊    | 5769/10000 [00:26<00:19, 216.55it/s]
 58%|█████▊    | 5791/10000 [00:26<00:19, 216.69it/s]
 58%|█████▊    | 5813/10000 [00:26<00:19, 216.03it/s]
 58%|█████▊    | 5835/10000 [00:27<00:19, 216.39it/s]
 59%|█████▊    | 5857/10000 [00:27<00:19, 215.56it/s]
 59%|█████▉    | 5879/10000 [00:27<00:19, 216.47it/s]
 59%|█████▉    | 5901/10000 [00:27<00:18, 217.20it/s]
 59%|█████▉    | 5923/10000 [00:27<00:18, 217.02it/s]
 59%|█████▉    | 5945/10000 [00:27<00:18, 217.64it/s]
 60%|█████▉    | 5967/10000 [00:27<00:18, 217.42it/s]
 60%|█████▉    | 5989/10000 [00:27<00:18, 217.72it/s]
 60%|██████    | 6011/10000 [00:27<00:18, 217.36it/s]
 60%|██████    | 6033/10000 [00:27<00:18, 218.02it/s]
 61%|██████    | 6055/10000 [00:28<00:18, 217.51it/s]
 61%|██████    | 6077/10000 [00:28<00:18, 217.38it/s]
 61%|██████    | 6099/10000 [00:28<00:17, 217.95it/s]
 61%|██████    | 6121/10000 [00:28<00:17, 217.60it/s]
 61%|██████▏   | 6143/10000 [00:28<00:17, 217.83it/s]
 62%|██████▏   | 6165/10000 [00:28<00:17, 218.22it/s]
 62%|██████▏   | 6187/10000 [00:28<00:17, 218.61it/s]
 62%|██████▏   | 6209/10000 [00:28<00:17, 218.09it/s]
 62%|██████▏   | 6231/10000 [00:28<00:17, 216.94it/s]
 63%|██████▎   | 6253/10000 [00:28<00:17, 216.74it/s]
 63%|██████▎   | 6275/10000 [00:29<00:17, 216.97it/s]
 63%|██████▎   | 6297/10000 [00:29<00:17, 217.28it/s]
 63%|██████▎   | 6319/10000 [00:29<00:16, 217.52it/s]
 63%|██████▎   | 6341/10000 [00:29<00:16, 217.54it/s]
 64%|██████▎   | 6363/10000 [00:29<00:16, 217.55it/s]
 64%|██████▍   | 6385/10000 [00:29<00:16, 216.91it/s]
 64%|██████▍   | 6407/10000 [00:29<00:16, 212.35it/s]
 64%|██████▍   | 6429/10000 [00:29<00:16, 213.53it/s]
 65%|██████▍   | 6451/10000 [00:29<00:16, 213.71it/s]
 65%|██████▍   | 6473/10000 [00:29<00:16, 215.42it/s]
 65%|██████▍   | 6495/10000 [00:30<00:16, 215.84it/s]
 65%|██████▌   | 6517/10000 [00:30<00:16, 216.33it/s]
 65%|██████▌   | 6539/10000 [00:30<00:15, 216.94it/s]
 66%|██████▌   | 6561/10000 [00:30<00:15, 217.24it/s]
 66%|██████▌   | 6583/10000 [00:30<00:15, 217.36it/s]
 66%|██████▌   | 6605/10000 [00:30<00:15, 217.04it/s]
 66%|██████▋   | 6627/10000 [00:30<00:15, 217.12it/s]
 66%|██████▋   | 6649/10000 [00:30<00:15, 217.32it/s]
 67%|██████▋   | 6671/10000 [00:30<00:15, 217.22it/s]
 67%|██████▋   | 6693/10000 [00:30<00:15, 217.65it/s]
 67%|██████▋   | 6715/10000 [00:31<00:15, 217.32it/s]
 67%|██████▋   | 6737/10000 [00:31<00:14, 217.72it/s]
 68%|██████▊   | 6759/10000 [00:31<00:14, 217.32it/s]
 68%|██████▊   | 6781/10000 [00:31<00:14, 216.02it/s]
 68%|██████▊   | 6803/10000 [00:31<00:14, 213.31it/s]
 68%|██████▊   | 6825/10000 [00:31<00:15, 210.37it/s]
 68%|██████▊   | 6847/10000 [00:31<00:14, 212.54it/s]
 69%|██████▊   | 6869/10000 [00:31<00:14, 214.66it/s]
 69%|██████▉   | 6891/10000 [00:31<00:14, 214.42it/s]
 69%|██████▉   | 6913/10000 [00:31<00:14, 215.67it/s]
 69%|██████▉   | 6935/10000 [00:32<00:14, 215.79it/s]
 70%|██████▉   | 6957/10000 [00:32<00:14, 217.04it/s]
 70%|██████▉   | 6979/10000 [00:32<00:13, 217.50it/s]
 70%|███████   | 7001/10000 [00:32<00:13, 216.32it/s]
 70%|███████   | 7023/10000 [00:32<00:13, 214.41it/s]
 70%|███████   | 7045/10000 [00:32<00:13, 214.80it/s]
 71%|███████   | 7067/10000 [00:32<00:13, 215.69it/s]
 71%|███████   | 7089/10000 [00:32<00:13, 216.56it/s]
 71%|███████   | 7111/10000 [00:32<00:13, 215.70it/s]
 71%|███████▏  | 7133/10000 [00:32<00:13, 216.50it/s]
 72%|███████▏  | 7155/10000 [00:33<00:13, 216.44it/s]
 72%|███████▏  | 7177/10000 [00:33<00:13, 216.92it/s]
 72%|███████▏  | 7199/10000 [00:33<00:12, 217.26it/s]
 72%|███████▏  | 7221/10000 [00:33<00:12, 216.82it/s]
 72%|███████▏  | 7243/10000 [00:33<00:12, 217.60it/s]
 73%|███████▎  | 7265/10000 [00:33<00:12, 216.42it/s]
 73%|███████▎  | 7287/10000 [00:33<00:12, 216.64it/s]
 73%|███████▎  | 7309/10000 [00:33<00:12, 216.81it/s]
 73%|███████▎  | 7331/10000 [00:33<00:12, 216.77it/s]
 74%|███████▎  | 7353/10000 [00:34<00:12, 217.30it/s]
 74%|███████▍  | 7375/10000 [00:34<00:12, 216.83it/s]
 74%|███████▍  | 7397/10000 [00:34<00:11, 217.15it/s]
 74%|███████▍  | 7419/10000 [00:34<00:11, 217.13it/s]
 74%|███████▍  | 7441/10000 [00:34<00:11, 216.82it/s]
 75%|███████▍  | 7463/10000 [00:34<00:11, 217.07it/s]
 75%|███████▍  | 7485/10000 [00:34<00:11, 217.73it/s]
 75%|███████▌  | 7507/10000 [00:34<00:11, 218.40it/s]
 75%|███████▌  | 7529/10000 [00:34<00:11, 218.25it/s]
 76%|███████▌  | 7551/10000 [00:34<00:11, 218.25it/s]
 76%|███████▌  | 7573/10000 [00:35<00:11, 218.59it/s]
 76%|███████▌  | 7595/10000 [00:35<00:11, 218.38it/s]
 76%|███████▌  | 7617/10000 [00:35<00:10, 218.31it/s]
 76%|███████▋  | 7639/10000 [00:35<00:10, 218.10it/s]
 77%|███████▋  | 7661/10000 [00:35<00:10, 218.24it/s]
 77%|███████▋  | 7683/10000 [00:35<00:10, 216.15it/s]
 77%|███████▋  | 7705/10000 [00:35<00:10, 216.68it/s]
 77%|███████▋  | 7727/10000 [00:35<00:10, 217.55it/s]
 77%|███████▋  | 7749/10000 [00:35<00:10, 217.33it/s]
 78%|███████▊  | 7772/10000 [00:35<00:10, 218.17it/s]
 78%|███████▊  | 7794/10000 [00:36<00:10, 218.24it/s]
 78%|███████▊  | 7816/10000 [00:36<00:09, 218.41it/s]
 78%|███████▊  | 7838/10000 [00:36<00:09, 218.10it/s]
 79%|███████▊  | 7860/10000 [00:36<00:09, 216.71it/s]
 79%|███████▉  | 7882/10000 [00:36<00:09, 216.65it/s]
 79%|███████▉  | 7904/10000 [00:36<00:09, 216.90it/s]
 79%|███████▉  | 7926/10000 [00:36<00:09, 217.47it/s]
 79%|███████▉  | 7948/10000 [00:36<00:09, 218.04it/s]
 80%|███████▉  | 7970/10000 [00:36<00:09, 216.91it/s]
 80%|███████▉  | 7992/10000 [00:36<00:09, 217.04it/s]
 80%|████████  | 8014/10000 [00:37<00:09, 215.57it/s]
 80%|████████  | 8036/10000 [00:37<00:09, 215.85it/s]
 81%|████████  | 8058/10000 [00:37<00:08, 215.97it/s]
 81%|████████  | 8080/10000 [00:37<00:08, 215.87it/s]
 81%|████████  | 8102/10000 [00:37<00:08, 215.38it/s]
 81%|████████  | 8124/10000 [00:37<00:08, 215.89it/s]
 81%|████████▏ | 8146/10000 [00:37<00:08, 216.87it/s]
 82%|████████▏ | 8168/10000 [00:37<00:08, 217.63it/s]
 82%|████████▏ | 8190/10000 [00:37<00:08, 217.80it/s]
 82%|████████▏ | 8212/10000 [00:37<00:08, 218.27it/s]
 82%|████████▏ | 8234/10000 [00:38<00:08, 216.85it/s]
 83%|████████▎ | 8256/10000 [00:38<00:08, 215.16it/s]
 83%|████████▎ | 8278/10000 [00:38<00:07, 215.63it/s]
 83%|████████▎ | 8300/10000 [00:38<00:07, 215.20it/s]
 83%|████████▎ | 8322/10000 [00:38<00:07, 215.64it/s]
 83%|████████▎ | 8344/10000 [00:38<00:07, 215.66it/s]
 84%|████████▎ | 8366/10000 [00:38<00:07, 216.85it/s]
 84%|████████▍ | 8388/10000 [00:38<00:07, 217.75it/s]
 84%|████████▍ | 8410/10000 [00:38<00:07, 217.50it/s]
 84%|████████▍ | 8432/10000 [00:38<00:07, 217.99it/s]
 85%|████████▍ | 8454/10000 [00:39<00:07, 217.54it/s]
 85%|████████▍ | 8476/10000 [00:39<00:06, 217.91it/s]
 85%|████████▍ | 8498/10000 [00:39<00:06, 218.28it/s]
 85%|████████▌ | 8520/10000 [00:39<00:06, 216.13it/s]
 85%|████████▌ | 8542/10000 [00:39<00:06, 214.00it/s]
 86%|████████▌ | 8564/10000 [00:39<00:06, 209.64it/s]
 86%|████████▌ | 8586/10000 [00:39<00:06, 210.16it/s]
 86%|████████▌ | 8608/10000 [00:39<00:06, 212.76it/s]
 86%|████████▋ | 8630/10000 [00:39<00:06, 213.32it/s]
 87%|████████▋ | 8652/10000 [00:40<00:06, 213.12it/s]
 87%|████████▋ | 8674/10000 [00:40<00:06, 213.51it/s]
 87%|████████▋ | 8696/10000 [00:40<00:06, 214.58it/s]
 87%|████████▋ | 8718/10000 [00:40<00:05, 215.24it/s]
 87%|████████▋ | 8740/10000 [00:40<00:05, 216.18it/s]
 88%|████████▊ | 8762/10000 [00:40<00:05, 216.55it/s]
 88%|████████▊ | 8784/10000 [00:40<00:05, 216.81it/s]
 88%|████████▊ | 8806/10000 [00:40<00:05, 215.95it/s]
 88%|████████▊ | 8828/10000 [00:40<00:05, 215.62it/s]
 88%|████████▊ | 8850/10000 [00:40<00:05, 216.61it/s]
 89%|████████▊ | 8872/10000 [00:41<00:05, 216.99it/s]
 89%|████████▉ | 8894/10000 [00:41<00:05, 217.49it/s]
 89%|████████▉ | 8916/10000 [00:41<00:05, 215.79it/s]
 89%|████████▉ | 8938/10000 [00:41<00:04, 216.37it/s]
 90%|████████▉ | 8960/10000 [00:41<00:04, 217.33it/s]
 90%|████████▉ | 8982/10000 [00:41<00:04, 216.76it/s]
 90%|█████████ | 9004/10000 [00:41<00:04, 216.51it/s]
 90%|█████████ | 9026/10000 [00:41<00:04, 217.34it/s]
 90%|█████████ | 9048/10000 [00:41<00:04, 216.29it/s]
 91%|█████████ | 9070/10000 [00:41<00:04, 216.37it/s]
 91%|█████████ | 9092/10000 [00:42<00:04, 216.14it/s]
 91%|█████████ | 9114/10000 [00:42<00:04, 216.58it/s]
 91%|█████████▏| 9136/10000 [00:42<00:03, 216.61it/s]
 92%|█████████▏| 9158/10000 [00:42<00:03, 217.20it/s]
 92%|█████████▏| 9180/10000 [00:42<00:03, 217.71it/s]
 92%|█████████▏| 9202/10000 [00:42<00:03, 217.11it/s]
 92%|█████████▏| 9224/10000 [00:42<00:03, 217.53it/s]
 92%|█████████▏| 9246/10000 [00:42<00:03, 218.22it/s]
 93%|█████████▎| 9268/10000 [00:42<00:03, 218.18it/s]
 93%|█████████▎| 9290/10000 [00:42<00:03, 218.25it/s]
 93%|█████████▎| 9312/10000 [00:43<00:03, 215.98it/s]
 93%|█████████▎| 9334/10000 [00:43<00:03, 216.77it/s]
 94%|█████████▎| 9356/10000 [00:43<00:02, 216.86it/s]
 94%|█████████▍| 9378/10000 [00:43<00:02, 217.11it/s]
 94%|█████████▍| 9401/10000 [00:43<00:02, 218.03it/s]
 94%|█████████▍| 9423/10000 [00:43<00:02, 218.07it/s]
 94%|█████████▍| 9445/10000 [00:43<00:02, 218.30it/s]
 95%|█████████▍| 9467/10000 [00:43<00:02, 217.88it/s]
 95%|█████████▍| 9489/10000 [00:43<00:02, 217.77it/s]
 95%|█████████▌| 9511/10000 [00:43<00:02, 217.69it/s]
 95%|█████████▌| 9533/10000 [00:44<00:02, 215.59it/s]
 96%|█████████▌| 9555/10000 [00:44<00:02, 216.41it/s]
 96%|█████████▌| 9577/10000 [00:44<00:01, 216.28it/s]
 96%|█████████▌| 9599/10000 [00:44<00:01, 216.09it/s]
 96%|█████████▌| 9621/10000 [00:44<00:01, 217.16it/s]
 96%|█████████▋| 9643/10000 [00:44<00:01, 216.48it/s]
 97%|█████████▋| 9665/10000 [00:44<00:01, 217.25it/s]
 97%|█████████▋| 9687/10000 [00:44<00:01, 217.69it/s]
 97%|█████████▋| 9709/10000 [00:44<00:01, 218.09it/s]
 97%|█████████▋| 9731/10000 [00:44<00:01, 218.32it/s]
 98%|█████████▊| 9753/10000 [00:45<00:01, 217.04it/s]
 98%|█████████▊| 9775/10000 [00:45<00:01, 216.98it/s]
 98%|█████████▊| 9797/10000 [00:45<00:00, 217.60it/s]
 98%|█████████▊| 9819/10000 [00:45<00:00, 217.05it/s]
 98%|█████████▊| 9841/10000 [00:45<00:00, 217.84it/s]
 99%|█████████▊| 9863/10000 [00:45<00:00, 217.85it/s]
 99%|█████████▉| 9885/10000 [00:45<00:00, 218.26it/s]
 99%|█████████▉| 9907/10000 [00:45<00:00, 218.54it/s]
 99%|█████████▉| 9929/10000 [00:45<00:00, 217.49it/s]
100%|█████████▉| 9951/10000 [00:45<00:00, 216.95it/s]
100%|█████████▉| 9973/10000 [00:46<00:00, 214.86it/s]
100%|█████████▉| 9995/10000 [00:46<00:00, 215.54it/s]
100%|██████████| 10000/10000 [00:47<00:00, 210.31it/s]
Define new variables

Defining new variables...:   0%|          | 0/10 [00:00<?, ?it/s]
Defining new variables...:  20%|██        | 2/10 [00:00<00:00, 12.81it/s]
Defining new variables...:  40%|████      | 4/10 [00:00<00:00, 13.15it/s]
Defining new variables...:  60%|██████    | 6/10 [00:00<00:00, 13.18it/s]
Defining new variables...:  80%|████████  | 8/10 [00:00<00:00, 13.33it/s]
Defining new variables...: 100%|██████████| 10/10 [00:00<00:00, 13.55it/s]
Defining new variables...: 100%|██████████| 10/10 [00:05<00:00,  1.84it/s]
File cnl_10_63.dat has been created.
logprob = the_model_generation.get_cross_nested_logit()
the_biogeme = BIOGEME(biogeme_database, logprob)
the_biogeme.model_name = MODEL_NAME
Biogeme parameters read from biogeme.toml.

Calculate the null log likelihood for reporting.

the_biogeme.calculate_null_loglikelihood(
    {i: 1 for i in range(context.total_sample_size)}
)
-23025.850929940458

Estimate the parameters.

results = the_biogeme.estimate(recycle=False)
*** Initial values of the parameters are obtained from the file __cnl_10_63.iter
Cannot read file __cnl_10_63.iter. Statement is ignored.
Starting values for the algorithm: {}
As the model is not too complex, we activate the calculation of second derivatives. To change this behavior, modify the algorithm to "simple_bounds" in the TOML file.
Optimization algorithm: hybrid Newton/BFGS with simple bounds [simple_bounds]
** Optimization: Newton with trust region for simple bounds
Iter.     beta_rating      beta_price    beta_chinese   beta_japanese     beta_korean     beta_indian     beta_french    beta_mexican   beta_lebanese  beta_ethiopian   beta_log_dist     mu_downtown        mu_asian     Function    Relgrad   Radius      Rho
    0            0.56           -0.54          -0.082             0.2           -0.07          -0.089            0.09            0.75          -0.062          -0.083              -1             1.3             1.3      1.6e+04       0.15       10     0.94   ++
    1            0.79           -0.42            0.65             1.5            0.74             1.1             0.9             1.3            0.76            0.54            -0.5             1.5             1.8      1.4e+04      0.056    1e+02     0.94   ++
    2            0.75           -0.43            0.78             1.2            0.81             1.1            0.79             1.3            0.74            0.55            -0.6             1.8             2.1      1.4e+04      0.017    1e+03        1   ++
    3            0.74           -0.42            0.75             1.3            0.79             1.1            0.77             1.3            0.75            0.55           -0.59             1.9             2.1      1.4e+04     0.0028    1e+04      1.1   ++
    4            0.74           -0.42            0.74             1.3            0.79             1.1            0.76             1.3            0.75            0.56           -0.59             1.9             2.1      1.4e+04    0.00011    1e+05        1   ++
    5            0.74           -0.42            0.74             1.3            0.79             1.1            0.76             1.3            0.75            0.56           -0.59             1.9             2.1      1.4e+04    1.8e-07    1e+05        1   ++
Optimization algorithm has converged.
Relative gradient: 1.8195295724071037e-07
Cause of termination: Relative gradient = 1.8e-07 <= 6.1e-06
Number of function evaluations: 19
Number of gradient evaluations: 13
Number of hessian evaluations: 6
Algorithm: Newton with trust region for simple bound constraints
Number of iterations: 6
Proportion of Hessian calculation: 6/6 = 100.0%
Optimization time: 0:11:45.697735
Calculate second derivatives and BHHH
File cnl_10_63.html has been generated.
File cnl_10_63.yaml has been generated.
print(results.short_summary())
Results for model cnl_10_63
Nbr of parameters:              13
Sample size:                    10000
Excluded data:                  0
Null log likelihood:            -23025.85
Final log likelihood:           -14014.06
Likelihood ratio test (null):           18023.57
Rho square (null):                      0.391
Rho bar square (null):                  0.391
Akaike Information Criterion:   28054.13
Bayesian Information Criterion: 28147.86
estimated_parameters = get_pandas_estimated_parameters(estimation_results=results)
display(estimated_parameters)
              Name     Value  Robust std err.  Robust t-stat.  Robust p-value
0      beta_rating  0.736527         0.014947       49.277321             0.0
1       beta_price -0.423968         0.012658      -33.493402             0.0
2     beta_chinese  0.738785         0.062006       11.914783             0.0
3    beta_japanese  1.281334         0.046413       27.607232             0.0
4      beta_korean  0.787133         0.053694       14.659536             0.0
5      beta_indian  1.078120         0.053553       20.131981             0.0
6      beta_french  0.763229         0.049590       15.390741             0.0
7     beta_mexican  1.259324         0.030265       41.609970             0.0
8    beta_lebanese  0.749781         0.049632       15.106678             0.0
9   beta_ethiopian  0.555532         0.041136       13.504778             0.0
10   beta_log_dist -0.585414         0.012764      -45.864380             0.0
11     mu_downtown  1.933860         0.030348       63.722869             0.0
12        mu_asian  2.112963         0.068147       31.006151             0.0
df, msg = compare(estimated_parameters)
print(df)
              Name  True Value  Estimated Value    T-Test
0      beta_rating        0.75         0.736527  0.901436
1       beta_price       -0.40        -0.423968  1.893473
2     beta_chinese        0.75         0.738785  0.180866
3    beta_japanese        1.25         1.281334 -0.675117
4      beta_korean        0.75         0.787133 -0.691563
5      beta_indian        1.00         1.078120 -1.458748
6      beta_french        0.75         0.763229 -0.266764
7     beta_mexican        1.25         1.259324 -0.308091
8    beta_lebanese        0.75         0.749781  0.004411
9   beta_ethiopian        0.50         0.555532 -1.349970
10   beta_log_dist       -0.60        -0.585414 -1.142722
11        mu_asian        2.00         2.112963 -1.657644
12     mu_downtown        2.00         1.933860  2.179381
print(msg)

Total running time of the script: (27 minutes 58.395 seconds)

Gallery generated by Sphinx-Gallery