Cross-nested logit

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

author:

Michel Bierlaire

date:

Wed Nov 1 18:00:33 2023

import pandas as pd
from biogeme.sampling_of_alternatives import (
    SamplingContext,
    ChoiceSetsGeneration,
    GenerateModel,
    generate_segment_size,
)
from biogeme.expressions import Beta
import biogeme.biogeme_logging as blog
import biogeme.biogeme as bio
from biogeme.nests import OneNestForCrossNestedLogit, NestsForCrossNestedLogit
from specification_sampling import V, combined_variables
from compare import compare
from alternatives import (
    alternatives,
    ID_COLUMN,
    partitions,
    all_alternatives,
    asian_and_downtown,
    only_downtown,
    only_asian,
)
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%|          | 23/10000 [00:00<00:45, 219.49it/s]
  0%|          | 46/10000 [00:00<00:45, 220.47it/s]
  1%|          | 69/10000 [00:00<00:44, 224.52it/s]
  1%|          | 92/10000 [00:00<00:43, 226.43it/s]
  1%|          | 115/10000 [00:00<00:43, 226.04it/s]
  1%|▏         | 138/10000 [00:00<00:43, 226.10it/s]
  2%|▏         | 161/10000 [00:00<00:44, 223.61it/s]
  2%|▏         | 184/10000 [00:00<00:44, 219.48it/s]
  2%|▏         | 206/10000 [00:00<00:44, 218.96it/s]
  2%|▏         | 228/10000 [00:01<00:45, 217.11it/s]
  2%|▎         | 250/10000 [00:01<00:45, 216.55it/s]
  3%|▎         | 272/10000 [00:01<00:44, 217.05it/s]
  3%|▎         | 294/10000 [00:01<00:44, 217.08it/s]
  3%|▎         | 317/10000 [00:01<00:43, 220.91it/s]
  3%|▎         | 340/10000 [00:01<00:43, 221.19it/s]
  4%|▎         | 363/10000 [00:01<00:43, 220.08it/s]
  4%|▍         | 386/10000 [00:01<00:43, 221.88it/s]
  4%|▍         | 409/10000 [00:01<00:42, 223.94it/s]
  4%|▍         | 432/10000 [00:01<00:42, 222.87it/s]
  5%|▍         | 455/10000 [00:02<00:42, 223.85it/s]
  5%|▍         | 478/10000 [00:02<00:42, 223.29it/s]
  5%|▌         | 501/10000 [00:02<00:42, 223.44it/s]
  5%|▌         | 524/10000 [00:02<00:42, 221.94it/s]
  5%|▌         | 547/10000 [00:02<00:42, 223.81it/s]
  6%|▌         | 570/10000 [00:02<00:42, 224.47it/s]
  6%|▌         | 593/10000 [00:02<00:41, 224.85it/s]
  6%|▌         | 616/10000 [00:02<00:41, 225.79it/s]
  6%|▋         | 639/10000 [00:02<00:41, 226.09it/s]
  7%|▋         | 662/10000 [00:02<00:41, 226.60it/s]
  7%|▋         | 685/10000 [00:03<00:41, 226.13it/s]
  7%|▋         | 708/10000 [00:03<00:41, 226.62it/s]
  7%|▋         | 731/10000 [00:03<00:40, 226.41it/s]
  8%|▊         | 754/10000 [00:03<00:40, 226.13it/s]
  8%|▊         | 777/10000 [00:03<00:40, 226.43it/s]
  8%|▊         | 800/10000 [00:03<00:40, 224.90it/s]
  8%|▊         | 823/10000 [00:03<00:40, 225.44it/s]
  8%|▊         | 846/10000 [00:03<00:40, 224.69it/s]
  9%|▊         | 869/10000 [00:03<00:41, 222.61it/s]
  9%|▉         | 892/10000 [00:03<00:41, 221.90it/s]
  9%|▉         | 915/10000 [00:04<00:40, 222.81it/s]
  9%|▉         | 938/10000 [00:04<00:40, 222.52it/s]
 10%|▉         | 961/10000 [00:04<00:40, 222.68it/s]
 10%|▉         | 984/10000 [00:04<00:40, 223.61it/s]
 10%|█         | 1007/10000 [00:04<00:40, 223.39it/s]
 10%|█         | 1030/10000 [00:04<00:40, 222.63it/s]
 11%|█         | 1053/10000 [00:04<00:40, 221.26it/s]
 11%|█         | 1076/10000 [00:04<00:40, 221.71it/s]
 11%|█         | 1099/10000 [00:04<00:40, 222.40it/s]
 11%|█         | 1122/10000 [00:05<00:39, 223.25it/s]
 11%|█▏        | 1145/10000 [00:05<00:39, 223.01it/s]
 12%|█▏        | 1168/10000 [00:05<00:39, 223.11it/s]
 12%|█▏        | 1191/10000 [00:05<00:39, 223.23it/s]
 12%|█▏        | 1214/10000 [00:05<00:39, 224.84it/s]
 12%|█▏        | 1237/10000 [00:05<00:45, 194.59it/s]
 13%|█▎        | 1260/10000 [00:05<00:43, 201.89it/s]
 13%|█▎        | 1283/10000 [00:05<00:41, 208.26it/s]
 13%|█▎        | 1306/10000 [00:05<00:40, 213.11it/s]
 13%|█▎        | 1329/10000 [00:06<00:40, 216.55it/s]
 14%|█▎        | 1352/10000 [00:06<00:39, 217.66it/s]
 14%|█▍        | 1375/10000 [00:06<00:39, 219.45it/s]
 14%|█▍        | 1398/10000 [00:06<00:38, 220.98it/s]
 14%|█▍        | 1421/10000 [00:06<00:38, 222.06it/s]
 14%|█▍        | 1444/10000 [00:06<00:38, 222.64it/s]
 15%|█▍        | 1467/10000 [00:06<00:38, 223.12it/s]
 15%|█▍        | 1490/10000 [00:06<00:38, 223.52it/s]
 15%|█▌        | 1513/10000 [00:06<00:37, 223.61it/s]
 15%|█▌        | 1536/10000 [00:06<00:37, 224.16it/s]
 16%|█▌        | 1559/10000 [00:07<00:37, 223.77it/s]
 16%|█▌        | 1582/10000 [00:07<00:37, 224.97it/s]
 16%|█▌        | 1605/10000 [00:07<00:37, 224.17it/s]
 16%|█▋        | 1628/10000 [00:07<00:37, 224.10it/s]
 17%|█▋        | 1651/10000 [00:07<00:37, 224.05it/s]
 17%|█▋        | 1674/10000 [00:07<00:37, 223.23it/s]
 17%|█▋        | 1697/10000 [00:07<00:37, 222.97it/s]
 17%|█▋        | 1720/10000 [00:07<00:37, 223.57it/s]
 17%|█▋        | 1743/10000 [00:07<00:36, 224.48it/s]
 18%|█▊        | 1766/10000 [00:07<00:36, 225.64it/s]
 18%|█▊        | 1790/10000 [00:08<00:36, 228.03it/s]
 18%|█▊        | 1813/10000 [00:08<00:35, 228.06it/s]
 18%|█▊        | 1836/10000 [00:08<00:35, 227.05it/s]
 19%|█▊        | 1859/10000 [00:08<00:35, 227.41it/s]
 19%|█▉        | 1882/10000 [00:08<00:35, 227.28it/s]
 19%|█▉        | 1905/10000 [00:08<00:35, 227.69it/s]
 19%|█▉        | 1928/10000 [00:08<00:35, 228.25it/s]
 20%|█▉        | 1951/10000 [00:08<00:35, 228.19it/s]
 20%|█▉        | 1975/10000 [00:08<00:35, 228.67it/s]
 20%|█▉        | 1998/10000 [00:08<00:35, 228.03it/s]
 20%|██        | 2021/10000 [00:09<00:35, 226.67it/s]
 20%|██        | 2044/10000 [00:09<00:35, 224.29it/s]
 21%|██        | 2067/10000 [00:09<00:35, 223.83it/s]
 21%|██        | 2090/10000 [00:09<00:35, 223.98it/s]
 21%|██        | 2113/10000 [00:09<00:35, 224.91it/s]
 21%|██▏       | 2136/10000 [00:09<00:34, 224.98it/s]
 22%|██▏       | 2159/10000 [00:09<00:35, 223.32it/s]
 22%|██▏       | 2182/10000 [00:09<00:35, 223.22it/s]
 22%|██▏       | 2205/10000 [00:09<00:34, 223.19it/s]
 22%|██▏       | 2228/10000 [00:10<00:34, 224.05it/s]
 23%|██▎       | 2251/10000 [00:10<00:34, 224.09it/s]
 23%|██▎       | 2274/10000 [00:10<00:34, 223.75it/s]
 23%|██▎       | 2297/10000 [00:10<00:34, 222.45it/s]
 23%|██▎       | 2320/10000 [00:10<00:34, 222.73it/s]
 23%|██▎       | 2343/10000 [00:10<00:34, 224.06it/s]
 24%|██▎       | 2366/10000 [00:10<00:33, 224.81it/s]
 24%|██▍       | 2389/10000 [00:10<00:33, 224.91it/s]
 24%|██▍       | 2412/10000 [00:10<00:33, 224.44it/s]
 24%|██▍       | 2435/10000 [00:10<00:33, 224.91it/s]
 25%|██▍       | 2458/10000 [00:11<00:33, 224.43it/s]
 25%|██▍       | 2481/10000 [00:11<00:33, 225.69it/s]
 25%|██▌       | 2504/10000 [00:11<00:33, 224.20it/s]
 25%|██▌       | 2527/10000 [00:11<00:33, 222.84it/s]
 26%|██▌       | 2550/10000 [00:11<00:33, 222.46it/s]
 26%|██▌       | 2573/10000 [00:11<00:33, 223.23it/s]
 26%|██▌       | 2596/10000 [00:11<00:33, 222.77it/s]
 26%|██▌       | 2619/10000 [00:11<00:33, 223.43it/s]
 26%|██▋       | 2642/10000 [00:11<00:32, 223.22it/s]
 27%|██▋       | 2665/10000 [00:12<00:38, 192.39it/s]
 27%|██▋       | 2688/10000 [00:12<00:36, 199.89it/s]
 27%|██▋       | 2711/10000 [00:12<00:35, 206.22it/s]
 27%|██▋       | 2733/10000 [00:12<00:34, 209.97it/s]
 28%|██▊       | 2755/10000 [00:12<00:34, 212.68it/s]
 28%|██▊       | 2778/10000 [00:12<00:33, 215.69it/s]
 28%|██▊       | 2801/10000 [00:12<00:33, 218.15it/s]
 28%|██▊       | 2823/10000 [00:12<00:33, 216.97it/s]
 28%|██▊       | 2846/10000 [00:12<00:32, 219.25it/s]
 29%|██▊       | 2869/10000 [00:12<00:32, 220.94it/s]
 29%|██▉       | 2892/10000 [00:13<00:32, 221.94it/s]
 29%|██▉       | 2915/10000 [00:13<00:31, 223.19it/s]
 29%|██▉       | 2938/10000 [00:13<00:31, 221.98it/s]
 30%|██▉       | 2961/10000 [00:13<00:31, 220.56it/s]
 30%|██▉       | 2984/10000 [00:13<00:31, 219.92it/s]
 30%|███       | 3007/10000 [00:13<00:31, 221.36it/s]
 30%|███       | 3030/10000 [00:13<00:31, 220.47it/s]
 31%|███       | 3053/10000 [00:13<00:31, 219.36it/s]
 31%|███       | 3075/10000 [00:13<00:31, 219.33it/s]
 31%|███       | 3098/10000 [00:13<00:31, 221.47it/s]
 31%|███       | 3121/10000 [00:14<00:31, 221.73it/s]
 31%|███▏      | 3144/10000 [00:14<00:30, 221.92it/s]
 32%|███▏      | 3167/10000 [00:14<00:30, 221.80it/s]
 32%|███▏      | 3190/10000 [00:14<00:30, 221.48it/s]
 32%|███▏      | 3213/10000 [00:14<00:30, 221.31it/s]
 32%|███▏      | 3236/10000 [00:14<00:30, 221.80it/s]
 33%|███▎      | 3259/10000 [00:14<00:30, 222.37it/s]
 33%|███▎      | 3282/10000 [00:14<00:30, 221.63it/s]
 33%|███▎      | 3305/10000 [00:14<00:30, 222.03it/s]
 33%|███▎      | 3328/10000 [00:15<00:29, 222.87it/s]
 34%|███▎      | 3351/10000 [00:15<00:29, 222.91it/s]
 34%|███▎      | 3374/10000 [00:15<00:29, 221.92it/s]
 34%|███▍      | 3397/10000 [00:15<00:29, 222.44it/s]
 34%|███▍      | 3420/10000 [00:15<00:29, 222.86it/s]
 34%|███▍      | 3443/10000 [00:15<00:29, 222.40it/s]
 35%|███▍      | 3466/10000 [00:15<00:29, 221.99it/s]
 35%|███▍      | 3489/10000 [00:15<00:29, 222.49it/s]
 35%|███▌      | 3512/10000 [00:15<00:29, 223.11it/s]
 35%|███▌      | 3535/10000 [00:15<00:28, 223.82it/s]
 36%|███▌      | 3558/10000 [00:16<00:28, 222.85it/s]
 36%|███▌      | 3581/10000 [00:16<00:29, 220.76it/s]
 36%|███▌      | 3604/10000 [00:16<00:28, 221.50it/s]
 36%|███▋      | 3627/10000 [00:16<00:28, 220.75it/s]
 36%|███▋      | 3650/10000 [00:16<00:28, 220.59it/s]
 37%|███▋      | 3673/10000 [00:16<00:28, 222.35it/s]
 37%|███▋      | 3696/10000 [00:16<00:28, 222.88it/s]
 37%|███▋      | 3719/10000 [00:16<00:28, 222.61it/s]
 37%|███▋      | 3742/10000 [00:16<00:28, 221.96it/s]
 38%|███▊      | 3765/10000 [00:16<00:28, 222.37it/s]
 38%|███▊      | 3788/10000 [00:17<00:27, 224.23it/s]
 38%|███▊      | 3811/10000 [00:17<00:27, 222.89it/s]
 38%|███▊      | 3834/10000 [00:17<00:27, 222.38it/s]
 39%|███▊      | 3857/10000 [00:17<00:27, 222.33it/s]
 39%|███▉      | 3880/10000 [00:17<00:27, 222.50it/s]
 39%|███▉      | 3903/10000 [00:17<00:27, 223.33it/s]
 39%|███▉      | 3926/10000 [00:17<00:27, 222.68it/s]
 39%|███▉      | 3949/10000 [00:17<00:27, 223.61it/s]
 40%|███▉      | 3972/10000 [00:17<00:26, 224.05it/s]
 40%|███▉      | 3995/10000 [00:17<00:26, 224.62it/s]
 40%|████      | 4018/10000 [00:18<00:26, 222.99it/s]
 40%|████      | 4041/10000 [00:18<00:26, 222.72it/s]
 41%|████      | 4064/10000 [00:18<00:26, 223.71it/s]
 41%|████      | 4087/10000 [00:18<00:30, 193.54it/s]
 41%|████      | 4110/10000 [00:18<00:29, 201.26it/s]
 41%|████▏     | 4133/10000 [00:18<00:28, 207.79it/s]
 42%|████▏     | 4156/10000 [00:18<00:27, 211.68it/s]
 42%|████▏     | 4179/10000 [00:18<00:27, 215.42it/s]
 42%|████▏     | 4201/10000 [00:18<00:26, 216.32it/s]
 42%|████▏     | 4224/10000 [00:19<00:26, 218.31it/s]
 42%|████▏     | 4247/10000 [00:19<00:26, 219.59it/s]
 43%|████▎     | 4270/10000 [00:19<00:26, 219.42it/s]
 43%|████▎     | 4293/10000 [00:19<00:25, 220.36it/s]
 43%|████▎     | 4316/10000 [00:19<00:25, 221.22it/s]
 43%|████▎     | 4339/10000 [00:19<00:25, 221.77it/s]
 44%|████▎     | 4362/10000 [00:19<00:25, 222.63it/s]
 44%|████▍     | 4385/10000 [00:19<00:25, 222.42it/s]
 44%|████▍     | 4408/10000 [00:19<00:25, 221.60it/s]
 44%|████▍     | 4431/10000 [00:20<00:25, 222.09it/s]
 45%|████▍     | 4454/10000 [00:20<00:24, 222.87it/s]
 45%|████▍     | 4477/10000 [00:20<00:25, 220.36it/s]
 45%|████▌     | 4500/10000 [00:20<00:24, 221.15it/s]
 45%|████▌     | 4523/10000 [00:20<00:24, 221.80it/s]
 45%|████▌     | 4546/10000 [00:20<00:24, 221.92it/s]
 46%|████▌     | 4569/10000 [00:20<00:24, 222.60it/s]
 46%|████▌     | 4592/10000 [00:20<00:24, 223.35it/s]
 46%|████▌     | 4615/10000 [00:20<00:24, 222.86it/s]
 46%|████▋     | 4638/10000 [00:20<00:24, 222.57it/s]
 47%|████▋     | 4661/10000 [00:21<00:23, 222.96it/s]
 47%|████▋     | 4684/10000 [00:21<00:23, 222.57it/s]
 47%|████▋     | 4707/10000 [00:21<00:23, 221.85it/s]
 47%|████▋     | 4730/10000 [00:21<00:23, 222.12it/s]
 48%|████▊     | 4753/10000 [00:21<00:23, 222.55it/s]
 48%|████▊     | 4776/10000 [00:21<00:23, 223.35it/s]
 48%|████▊     | 4799/10000 [00:21<00:23, 223.18it/s]
 48%|████▊     | 4822/10000 [00:21<00:23, 223.02it/s]
 48%|████▊     | 4845/10000 [00:21<00:23, 222.84it/s]
 49%|████▊     | 4868/10000 [00:21<00:23, 222.73it/s]
 49%|████▉     | 4891/10000 [00:22<00:22, 222.25it/s]
 49%|████▉     | 4914/10000 [00:22<00:22, 222.90it/s]
 49%|████▉     | 4937/10000 [00:22<00:22, 223.13it/s]
 50%|████▉     | 4960/10000 [00:22<00:22, 222.75it/s]
 50%|████▉     | 4983/10000 [00:22<00:22, 223.59it/s]
 50%|█████     | 5006/10000 [00:22<00:22, 223.99it/s]
 50%|█████     | 5029/10000 [00:22<00:22, 223.49it/s]
 51%|█████     | 5052/10000 [00:22<00:22, 223.11it/s]
 51%|█████     | 5075/10000 [00:22<00:21, 224.38it/s]
 51%|█████     | 5098/10000 [00:23<00:21, 223.55it/s]
 51%|█████     | 5121/10000 [00:23<00:21, 222.78it/s]
 51%|█████▏    | 5144/10000 [00:23<00:21, 221.67it/s]
 52%|█████▏    | 5167/10000 [00:23<00:21, 222.30it/s]
 52%|█████▏    | 5190/10000 [00:23<00:21, 222.60it/s]
 52%|█████▏    | 5213/10000 [00:23<00:21, 222.06it/s]
 52%|█████▏    | 5236/10000 [00:23<00:21, 221.54it/s]
 53%|█████▎    | 5259/10000 [00:23<00:21, 221.01it/s]
 53%|█████▎    | 5282/10000 [00:23<00:21, 221.27it/s]
 53%|█████▎    | 5305/10000 [00:23<00:21, 221.89it/s]
 53%|█████▎    | 5328/10000 [00:24<00:21, 222.39it/s]
 54%|█████▎    | 5351/10000 [00:24<00:20, 222.25it/s]
 54%|█████▎    | 5374/10000 [00:24<00:20, 222.79it/s]
 54%|█████▍    | 5397/10000 [00:24<00:20, 222.74it/s]
 54%|█████▍    | 5420/10000 [00:24<00:20, 222.63it/s]
 54%|█████▍    | 5443/10000 [00:24<00:20, 222.72it/s]
 55%|█████▍    | 5466/10000 [00:24<00:20, 222.60it/s]
 55%|█████▍    | 5489/10000 [00:24<00:20, 222.38it/s]
 55%|█████▌    | 5512/10000 [00:24<00:23, 192.84it/s]
 55%|█████▌    | 5535/10000 [00:25<00:22, 201.28it/s]
 56%|█████▌    | 5558/10000 [00:25<00:21, 206.72it/s]
 56%|█████▌    | 5581/10000 [00:25<00:20, 211.34it/s]
 56%|█████▌    | 5604/10000 [00:25<00:20, 214.78it/s]
 56%|█████▋    | 5627/10000 [00:25<00:20, 216.91it/s]
 56%|█████▋    | 5650/10000 [00:25<00:19, 218.08it/s]
 57%|█████▋    | 5673/10000 [00:25<00:19, 219.54it/s]
 57%|█████▋    | 5696/10000 [00:25<00:19, 220.18it/s]
 57%|█████▋    | 5719/10000 [00:25<00:19, 220.32it/s]
 57%|█████▋    | 5742/10000 [00:25<00:19, 221.78it/s]
 58%|█████▊    | 5765/10000 [00:26<00:19, 221.73it/s]
 58%|█████▊    | 5788/10000 [00:26<00:19, 221.54it/s]
 58%|█████▊    | 5811/10000 [00:26<00:18, 221.99it/s]
 58%|█████▊    | 5834/10000 [00:26<00:18, 222.86it/s]
 59%|█████▊    | 5857/10000 [00:26<00:18, 223.90it/s]
 59%|█████▉    | 5880/10000 [00:26<00:18, 224.02it/s]
 59%|█████▉    | 5903/10000 [00:26<00:18, 223.91it/s]
 59%|█████▉    | 5926/10000 [00:26<00:18, 223.27it/s]
 59%|█████▉    | 5949/10000 [00:26<00:18, 223.41it/s]
 60%|█████▉    | 5972/10000 [00:26<00:18, 223.08it/s]
 60%|█████▉    | 5995/10000 [00:27<00:17, 223.15it/s]
 60%|██████    | 6018/10000 [00:27<00:17, 223.12it/s]
 60%|██████    | 6041/10000 [00:27<00:17, 223.51it/s]
 61%|██████    | 6064/10000 [00:27<00:17, 224.37it/s]
 61%|██████    | 6087/10000 [00:27<00:17, 225.39it/s]
 61%|██████    | 6110/10000 [00:27<00:17, 224.33it/s]
 61%|██████▏   | 6133/10000 [00:27<00:17, 224.44it/s]
 62%|██████▏   | 6156/10000 [00:27<00:17, 225.04it/s]
 62%|██████▏   | 6179/10000 [00:27<00:16, 225.85it/s]
 62%|██████▏   | 6202/10000 [00:28<00:16, 225.95it/s]
 62%|██████▏   | 6225/10000 [00:28<00:16, 223.51it/s]
 62%|██████▏   | 6248/10000 [00:28<00:16, 223.54it/s]
 63%|██████▎   | 6271/10000 [00:28<00:16, 223.14it/s]
 63%|██████▎   | 6294/10000 [00:28<00:16, 224.76it/s]
 63%|██████▎   | 6317/10000 [00:28<00:16, 224.55it/s]
 63%|██████▎   | 6340/10000 [00:28<00:16, 224.84it/s]
 64%|██████▎   | 6363/10000 [00:28<00:16, 223.84it/s]
 64%|██████▍   | 6386/10000 [00:28<00:16, 223.09it/s]
 64%|██████▍   | 6409/10000 [00:28<00:16, 223.91it/s]
 64%|██████▍   | 6432/10000 [00:29<00:15, 223.98it/s]
 65%|██████▍   | 6455/10000 [00:29<00:15, 224.04it/s]
 65%|██████▍   | 6478/10000 [00:29<00:15, 223.26it/s]
 65%|██████▌   | 6501/10000 [00:29<00:15, 223.17it/s]
 65%|██████▌   | 6524/10000 [00:29<00:15, 222.80it/s]
 65%|██████▌   | 6547/10000 [00:29<00:15, 223.34it/s]
 66%|██████▌   | 6570/10000 [00:29<00:15, 222.79it/s]
 66%|██████▌   | 6593/10000 [00:29<00:15, 224.31it/s]
 66%|██████▌   | 6616/10000 [00:29<00:15, 223.97it/s]
 66%|██████▋   | 6639/10000 [00:29<00:15, 223.42it/s]
 67%|██████▋   | 6662/10000 [00:30<00:14, 224.16it/s]
 67%|██████▋   | 6685/10000 [00:30<00:14, 224.37it/s]
 67%|██████▋   | 6708/10000 [00:30<00:14, 224.27it/s]
 67%|██████▋   | 6731/10000 [00:30<00:14, 224.30it/s]
 68%|██████▊   | 6754/10000 [00:30<00:14, 225.79it/s]
 68%|██████▊   | 6777/10000 [00:30<00:14, 225.79it/s]
 68%|██████▊   | 6801/10000 [00:30<00:14, 227.03it/s]
 68%|██████▊   | 6824/10000 [00:30<00:14, 225.15it/s]
 68%|██████▊   | 6847/10000 [00:30<00:14, 224.52it/s]
 69%|██████▊   | 6870/10000 [00:30<00:13, 224.52it/s]
 69%|██████▉   | 6893/10000 [00:31<00:13, 224.99it/s]
 69%|██████▉   | 6916/10000 [00:31<00:13, 223.93it/s]
 69%|██████▉   | 6939/10000 [00:31<00:15, 192.91it/s]
 70%|██████▉   | 6962/10000 [00:31<00:15, 201.04it/s]
 70%|██████▉   | 6985/10000 [00:31<00:14, 208.33it/s]
 70%|███████   | 7008/10000 [00:31<00:14, 212.83it/s]
 70%|███████   | 7031/10000 [00:31<00:13, 215.76it/s]
 71%|███████   | 7054/10000 [00:31<00:13, 217.24it/s]
 71%|███████   | 7077/10000 [00:31<00:13, 219.88it/s]
 71%|███████   | 7100/10000 [00:32<00:13, 220.77it/s]
 71%|███████   | 7123/10000 [00:32<00:12, 222.55it/s]
 71%|███████▏  | 7146/10000 [00:32<00:12, 222.22it/s]
 72%|███████▏  | 7169/10000 [00:32<00:12, 223.19it/s]
 72%|███████▏  | 7192/10000 [00:32<00:12, 225.14it/s]
 72%|███████▏  | 7215/10000 [00:32<00:12, 223.82it/s]
 72%|███████▏  | 7238/10000 [00:32<00:12, 225.48it/s]
 73%|███████▎  | 7261/10000 [00:32<00:12, 225.09it/s]
 73%|███████▎  | 7284/10000 [00:32<00:12, 224.15it/s]
 73%|███████▎  | 7307/10000 [00:32<00:11, 224.63it/s]
 73%|███████▎  | 7330/10000 [00:33<00:11, 223.98it/s]
 74%|███████▎  | 7353/10000 [00:33<00:11, 224.48it/s]
 74%|███████▍  | 7376/10000 [00:33<00:11, 224.20it/s]
 74%|███████▍  | 7399/10000 [00:33<00:11, 224.30it/s]
 74%|███████▍  | 7422/10000 [00:33<00:11, 224.18it/s]
 74%|███████▍  | 7445/10000 [00:33<00:11, 224.92it/s]
 75%|███████▍  | 7468/10000 [00:33<00:11, 223.84it/s]
 75%|███████▍  | 7491/10000 [00:33<00:11, 222.80it/s]
 75%|███████▌  | 7514/10000 [00:33<00:11, 223.84it/s]
 75%|███████▌  | 7537/10000 [00:34<00:11, 223.79it/s]
 76%|███████▌  | 7560/10000 [00:34<00:10, 224.51it/s]
 76%|███████▌  | 7583/10000 [00:34<00:10, 223.44it/s]
 76%|███████▌  | 7606/10000 [00:34<00:10, 223.16it/s]
 76%|███████▋  | 7629/10000 [00:34<00:10, 223.37it/s]
 77%|███████▋  | 7652/10000 [00:34<00:10, 223.64it/s]
 77%|███████▋  | 7675/10000 [00:34<00:10, 224.55it/s]
 77%|███████▋  | 7698/10000 [00:34<00:10, 224.47it/s]
 77%|███████▋  | 7721/10000 [00:34<00:10, 224.91it/s]
 77%|███████▋  | 7744/10000 [00:34<00:10, 224.65it/s]
 78%|███████▊  | 7767/10000 [00:35<00:09, 223.68it/s]
 78%|███████▊  | 7790/10000 [00:35<00:09, 224.46it/s]
 78%|███████▊  | 7813/10000 [00:35<00:09, 223.81it/s]
 78%|███████▊  | 7836/10000 [00:35<00:09, 223.09it/s]
 79%|███████▊  | 7859/10000 [00:35<00:09, 222.67it/s]
 79%|███████▉  | 7882/10000 [00:35<00:09, 223.89it/s]
 79%|███████▉  | 7905/10000 [00:35<00:09, 224.35it/s]
 79%|███████▉  | 7928/10000 [00:35<00:09, 223.55it/s]
 80%|███████▉  | 7951/10000 [00:35<00:09, 222.15it/s]
 80%|███████▉  | 7974/10000 [00:35<00:09, 222.10it/s]
 80%|███████▉  | 7997/10000 [00:36<00:08, 222.69it/s]
 80%|████████  | 8020/10000 [00:36<00:08, 223.55it/s]
 80%|████████  | 8043/10000 [00:36<00:08, 222.38it/s]
 81%|████████  | 8066/10000 [00:36<00:08, 222.86it/s]
 81%|████████  | 8089/10000 [00:36<00:08, 223.39it/s]
 81%|████████  | 8112/10000 [00:36<00:08, 223.23it/s]
 81%|████████▏ | 8135/10000 [00:36<00:08, 222.73it/s]
 82%|████████▏ | 8158/10000 [00:36<00:08, 222.17it/s]
 82%|████████▏ | 8181/10000 [00:36<00:08, 223.37it/s]
 82%|████████▏ | 8204/10000 [00:37<00:08, 222.44it/s]
 82%|████████▏ | 8227/10000 [00:37<00:07, 222.48it/s]
 82%|████████▎ | 8250/10000 [00:37<00:07, 221.84it/s]
 83%|████████▎ | 8273/10000 [00:37<00:07, 222.09it/s]
 83%|████████▎ | 8296/10000 [00:37<00:07, 221.77it/s]
 83%|████████▎ | 8319/10000 [00:37<00:07, 222.19it/s]
 83%|████████▎ | 8342/10000 [00:37<00:07, 221.44it/s]
 84%|████████▎ | 8365/10000 [00:37<00:08, 189.61it/s]
 84%|████████▍ | 8388/10000 [00:37<00:08, 198.44it/s]
 84%|████████▍ | 8411/10000 [00:37<00:07, 204.96it/s]
 84%|████████▍ | 8434/10000 [00:38<00:07, 210.53it/s]
 85%|████████▍ | 8457/10000 [00:38<00:07, 213.51it/s]
 85%|████████▍ | 8480/10000 [00:38<00:07, 216.71it/s]
 85%|████████▌ | 8503/10000 [00:38<00:06, 219.11it/s]
 85%|████████▌ | 8526/10000 [00:38<00:06, 221.83it/s]
 85%|████████▌ | 8549/10000 [00:38<00:06, 222.59it/s]
 86%|████████▌ | 8572/10000 [00:38<00:06, 221.98it/s]
 86%|████████▌ | 8595/10000 [00:38<00:06, 223.64it/s]
 86%|████████▌ | 8618/10000 [00:38<00:06, 223.71it/s]
 86%|████████▋ | 8641/10000 [00:39<00:06, 223.78it/s]
 87%|████████▋ | 8664/10000 [00:39<00:05, 224.44it/s]
 87%|████████▋ | 8687/10000 [00:39<00:05, 224.28it/s]
 87%|████████▋ | 8710/10000 [00:39<00:05, 224.56it/s]
 87%|████████▋ | 8733/10000 [00:39<00:05, 224.00it/s]
 88%|████████▊ | 8756/10000 [00:39<00:05, 224.20it/s]
 88%|████████▊ | 8779/10000 [00:39<00:05, 224.63it/s]
 88%|████████▊ | 8802/10000 [00:39<00:05, 223.47it/s]
 88%|████████▊ | 8825/10000 [00:39<00:05, 223.62it/s]
 88%|████████▊ | 8848/10000 [00:39<00:05, 223.97it/s]
 89%|████████▊ | 8871/10000 [00:40<00:05, 224.17it/s]
 89%|████████▉ | 8894/10000 [00:40<00:04, 225.27it/s]
 89%|████████▉ | 8917/10000 [00:40<00:04, 225.21it/s]
 89%|████████▉ | 8940/10000 [00:40<00:04, 225.94it/s]
 90%|████████▉ | 8963/10000 [00:40<00:04, 225.31it/s]
 90%|████████▉ | 8986/10000 [00:40<00:04, 224.23it/s]
 90%|█████████ | 9009/10000 [00:40<00:04, 224.82it/s]
 90%|█████████ | 9032/10000 [00:40<00:04, 223.43it/s]
 91%|█████████ | 9055/10000 [00:40<00:04, 222.98it/s]
 91%|█████████ | 9078/10000 [00:40<00:04, 222.12it/s]
 91%|█████████ | 9101/10000 [00:41<00:04, 222.16it/s]
 91%|█████████ | 9124/10000 [00:41<00:03, 221.67it/s]
 91%|█████████▏| 9147/10000 [00:41<00:03, 222.08it/s]
 92%|█████████▏| 9170/10000 [00:41<00:03, 222.62it/s]
 92%|█████████▏| 9193/10000 [00:41<00:03, 222.74it/s]
 92%|█████████▏| 9216/10000 [00:41<00:03, 222.46it/s]
 92%|█████████▏| 9239/10000 [00:41<00:03, 222.90it/s]
 93%|█████████▎| 9262/10000 [00:41<00:03, 223.13it/s]
 93%|█████████▎| 9285/10000 [00:41<00:03, 222.70it/s]
 93%|█████████▎| 9308/10000 [00:42<00:03, 222.09it/s]
 93%|█████████▎| 9331/10000 [00:42<00:03, 222.42it/s]
 94%|█████████▎| 9354/10000 [00:42<00:02, 222.69it/s]
 94%|█████████▍| 9377/10000 [00:42<00:02, 224.10it/s]
 94%|█████████▍| 9400/10000 [00:42<00:02, 223.86it/s]
 94%|█████████▍| 9423/10000 [00:42<00:02, 223.08it/s]
 94%|█████████▍| 9446/10000 [00:42<00:02, 223.09it/s]
 95%|█████████▍| 9469/10000 [00:42<00:02, 222.83it/s]
 95%|█████████▍| 9492/10000 [00:42<00:02, 222.66it/s]
 95%|█████████▌| 9515/10000 [00:42<00:02, 223.17it/s]
 95%|█████████▌| 9538/10000 [00:43<00:02, 223.23it/s]
 96%|█████████▌| 9561/10000 [00:43<00:01, 223.92it/s]
 96%|█████████▌| 9584/10000 [00:43<00:01, 223.96it/s]
 96%|█████████▌| 9607/10000 [00:43<00:01, 223.66it/s]
 96%|█████████▋| 9630/10000 [00:43<00:01, 222.73it/s]
 97%|█████████▋| 9653/10000 [00:43<00:01, 223.30it/s]
 97%|█████████▋| 9676/10000 [00:43<00:01, 222.70it/s]
 97%|█████████▋| 9699/10000 [00:43<00:01, 222.64it/s]
 97%|█████████▋| 9722/10000 [00:43<00:01, 222.87it/s]
 97%|█████████▋| 9745/10000 [00:43<00:01, 222.59it/s]
 98%|█████████▊| 9768/10000 [00:44<00:01, 222.13it/s]
 98%|█████████▊| 9791/10000 [00:44<00:01, 191.10it/s]
 98%|█████████▊| 9814/10000 [00:44<00:00, 200.95it/s]
 98%|█████████▊| 9837/10000 [00:44<00:00, 207.58it/s]
 99%|█████████▊| 9860/10000 [00:44<00:00, 213.05it/s]
 99%|█████████▉| 9883/10000 [00:44<00:00, 216.28it/s]
 99%|█████████▉| 9906/10000 [00:44<00:00, 217.96it/s]
 99%|█████████▉| 9929/10000 [00:44<00:00, 219.52it/s]
100%|█████████▉| 9952/10000 [00:44<00:00, 221.09it/s]
100%|█████████▉| 9975/10000 [00:45<00:00, 223.34it/s]
100%|█████████▉| 9998/10000 [00:45<00:00, 222.98it/s]
100%|██████████| 10000/10000 [00:46<00:00, 214.57it/s]
Define new variables

Defining new variables...:   0%|          | 0/10 [00:00<?, ?it/s]
Defining new variables...:  10%|█         | 1/10 [00:00<00:03,  2.25it/s]
Defining new variables...:  20%|██        | 2/10 [00:00<00:03,  2.07it/s]
Defining new variables...:  30%|███       | 3/10 [00:01<00:03,  2.03it/s]
Defining new variables...:  40%|████      | 4/10 [00:01<00:02,  2.00it/s]
Defining new variables...:  50%|█████     | 5/10 [00:02<00:02,  2.00it/s]
Defining new variables...:  60%|██████    | 6/10 [00:02<00:02,  1.98it/s]
Defining new variables...:  70%|███████   | 7/10 [00:03<00:01,  1.98it/s]
Defining new variables...:  80%|████████  | 8/10 [00:03<00:01,  1.98it/s]
Defining new variables...:  90%|█████████ | 9/10 [00:04<00:00,  1.97it/s]
Defining new variables...: 100%|██████████| 10/10 [00:05<00:00,  1.97it/s]
Defining new variables...: 100%|██████████| 10/10 [00:37<00:00,  3.78s/it]
File cnl_10_63.dat has been created.
logprob = the_model_generation.get_cross_nested_logit()
the_biogeme = bio.BIOGEME(biogeme_database, logprob)
the_biogeme.modelName = 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)}
)
np.float64(-23025.850929942502)

Estimate the parameters.

results = the_biogeme.estimate(recycle=False)
As the model is not too complex, we activate the calculation of second derivatives. If you want to change it, change the name of the algorithm in the TOML file from "automatic" to "simple_bounds"
*** Initial values of the parameters are obtained from the file __cnl_10_63.iter
Cannot read file __cnl_10_63.iter. Statement is ignored.
As the model is not too complex, we activate the calculation of second derivatives. If you want to change it, change the name of the algorithm in the TOML file from "automatic" to "simple_bounds"
Optimization algorithm: hybrid Newton/BFGS with simple bounds [simple_bounds]
** Optimization: Newton with trust region for simple bounds
Iter.    beta_chinese  beta_ethiopian     beta_french     beta_indian   beta_japanese     beta_korean   beta_lebanese   beta_log_dist    beta_mexican      beta_price     beta_rating        mu_asian     mu_downtown     Function    Relgrad   Radius      Rho
    0           -0.19          -0.051            0.18           -0.22            0.08           -0.21          -0.033              -1            0.96            -0.6            0.63               1             1.4      1.8e+04       0.15        1     0.68    +
    1           -0.21          -0.019            0.26           -0.23           0.088           -0.23          -0.018           -0.58            0.97           -0.47            0.81             1.1               1      1.6e+04      0.072       10     0.93   ++
    2           -0.21          -0.019            0.26           -0.23           0.088           -0.23          -0.018           -0.58            0.97           -0.47            0.81             1.1               1      1.6e+04      0.072     0.78    -0.82    -
    3           -0.48            0.27               1           -0.28            0.21           -0.56             0.2           -0.47             1.2           -0.38            0.93             1.3             1.2      1.6e+04      0.023     0.78     0.78    +
    4           -0.34             0.4             1.2          -0.035            0.42           -0.31            0.52           -0.55             1.3           -0.46            0.94             1.1             1.1      1.6e+04     0.0054      7.8        1   ++
    5           -0.36            0.46             1.3          -0.023            0.48           -0.32            0.59           -0.61             1.4           -0.51               1               1               1      1.6e+04     0.0046       78     0.96   ++
    6           -0.36            0.46             1.3          -0.023            0.48           -0.32            0.59           -0.61             1.4           -0.51               1               1               1      1.6e+04    8.3e-05       78        1   ++
Results saved in file cnl_10_63.html
Results saved in file cnl_10_63.pickle
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:           -15815.23
Likelihood ratio test (null):           14421.24
Rho square (null):                      0.313
Rho bar square (null):                  0.313
Akaike Information Criterion:   31656.46
Bayesian Information Criterion: 31750.19
estimated_parameters = results.get_estimated_parameters()
estimated_parameters
Value Rob. Std err Rob. t-test Rob. p-value
beta_chinese -0.355854 0.065737 -5.413305 6.187183e-08
beta_ethiopian 0.470517 0.048806 9.640480 0.000000e+00
beta_french 1.313453 0.063627 20.643022 0.000000e+00
beta_indian -0.014493 0.054403 -0.266405 7.899274e-01
beta_japanese 0.494104 0.052761 9.364955 0.000000e+00
beta_korean -0.318190 0.053328 -5.966692 2.421114e-09
beta_lebanese 0.607425 0.065966 9.208228 0.000000e+00
beta_log_dist -0.621389 0.024518 -25.344483 0.000000e+00
beta_mexican 1.413177 0.055236 25.584268 0.000000e+00
beta_price -0.521240 0.020715 -25.161987 0.000000e+00
beta_rating 1.044178 0.037827 27.604277 0.000000e+00
mu_asian 1.016981 0.037306 27.260280 0.000000e+00
mu_downtown 1.013238 0.028033 36.144043 0.000000e+00


df, msg = compare(estimated_parameters)
print(df)
              Name  True Value  Estimated Value     T-Test
0      beta_rating        0.75         1.044178  -7.777002
1       beta_price       -0.40        -0.521240   5.852654
2     beta_chinese        0.75        -0.355854  16.822418
3    beta_japanese        1.25         0.494104  14.326799
4      beta_korean        0.75        -0.318190  20.030660
5      beta_indian        1.00        -0.014493  18.647743
6      beta_french        0.75         1.313453  -8.855572
7     beta_mexican        1.25         1.413177  -2.954174
8    beta_lebanese        0.75         0.607425   2.161352
9   beta_ethiopian        0.50         0.470517   0.604089
10   beta_log_dist       -0.60        -0.621389   0.872401
11        mu_asian        2.00         1.016981  26.349899
12     mu_downtown        2.00         1.013238  35.199584
print(msg)

Total running time of the script: (4 minutes 31.760 seconds)

Gallery generated by Sphinx-Gallery