Nested logit

Estimation of a nested logit model using sampling of alternatives.

Michel Bierlaire Sat Jul 26 2025, 13:01:22

import pandas as pd
from alternatives import ID_COLUMN, all_alternatives, alternatives, asian, partitions
from compare import compare
from IPython.core.display_functions import display
from specification_sampling import V, combined_variables

import biogeme.biogeme_logging as blog
from biogeme.biogeme import BIOGEME
from biogeme.expressions import Beta
from biogeme.nests import NestsForNestedLogit, OneNestForNestedLogit
from biogeme.results_processing import (
    EstimationResults,
    get_pandas_estimated_parameters,
)
from biogeme.sampling_of_alternatives import (
    ChoiceSetsGeneration,
    GenerateModel,
    SamplingContext,
    generate_segment_size,
)
from biogeme.tools import timeit
logger = blog.get_screen_logger(level=blog.INFO)
SAMPLE_SIZE = 20  # out of 100
SAMPLE_SIZE_MEV = 33  # out of 33
CHOICE_COLUMN = 'nested_0'
PARTITION = 'downtown'
MEV_PARTITION = 'uniform_asian'
MODEL_NAME = f'nested_{PARTITION}_{SAMPLE_SIZE}'
FILE_NAME = f'{MODEL_NAME}.dat'
the_partition = partitions.get(PARTITION)
if the_partition is None:
    raise ValueError(f'Unknown partition: {PARTITION}')
segment_sizes = 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]
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,
)
logger.info(context.reporting())
Size of the choice set: 100
Main partition: 2 segment(s) of size 46, 54
Main sample: 20: 10/46, 10/54
Nbr of MEV alternatives: 33
MEV partition: 1 segment(s) of size 33
MEV sample: 33: 33/33
the_data_generation = ChoiceSetsGeneration(context=context)
the_model_generation = GenerateModel(context=context)
biogeme_database = the_data_generation.sample_and_merge(recycle=False)
Generating 20 + 33 alternatives for 10000 observations

  0%|          | 0/10000 [00:00<?, ?it/s]
  0%|          | 41/10000 [00:00<00:24, 409.29it/s]
  1%|          | 84/10000 [00:00<00:23, 417.38it/s]
  1%|▏         | 129/10000 [00:00<00:22, 432.17it/s]
  2%|▏         | 173/10000 [00:00<00:22, 430.07it/s]
  2%|▏         | 217/10000 [00:00<00:23, 424.62it/s]
  3%|▎         | 260/10000 [00:00<00:23, 422.23it/s]
  3%|▎         | 303/10000 [00:00<00:23, 421.07it/s]
  3%|▎         | 346/10000 [00:00<00:22, 420.67it/s]
  4%|▍         | 389/10000 [00:00<00:22, 419.38it/s]
  4%|▍         | 431/10000 [00:01<00:22, 419.44it/s]
  5%|▍         | 474/10000 [00:01<00:22, 419.84it/s]
  5%|▌         | 517/10000 [00:01<00:22, 420.15it/s]
  6%|▌         | 560/10000 [00:01<00:22, 420.38it/s]
  6%|▌         | 603/10000 [00:01<00:22, 420.83it/s]
  6%|▋         | 646/10000 [00:01<00:22, 419.76it/s]
  7%|▋         | 688/10000 [00:01<00:22, 419.01it/s]
  7%|▋         | 730/10000 [00:01<00:22, 418.55it/s]
  8%|▊         | 772/10000 [00:01<00:22, 401.54it/s]
  8%|▊         | 813/10000 [00:01<00:23, 385.20it/s]
  9%|▊         | 852/10000 [00:02<00:24, 369.21it/s]
  9%|▉         | 890/10000 [00:02<00:25, 356.34it/s]
  9%|▉         | 926/10000 [00:02<00:25, 354.72it/s]
 10%|▉         | 962/10000 [00:02<00:25, 353.89it/s]
 10%|▉         | 998/10000 [00:02<00:25, 352.90it/s]
 10%|█         | 1034/10000 [00:02<00:25, 353.31it/s]
 11%|█         | 1070/10000 [00:02<00:25, 354.10it/s]
 11%|█         | 1106/10000 [00:02<00:25, 354.28it/s]
 11%|█▏        | 1142/10000 [00:02<00:25, 353.31it/s]
 12%|█▏        | 1178/10000 [00:03<00:25, 352.56it/s]
 12%|█▏        | 1214/10000 [00:03<00:24, 353.16it/s]
 12%|█▎        | 1250/10000 [00:03<00:24, 352.97it/s]
 13%|█▎        | 1286/10000 [00:03<00:24, 352.93it/s]
 13%|█▎        | 1322/10000 [00:03<00:24, 353.16it/s]
 14%|█▎        | 1358/10000 [00:03<00:24, 353.42it/s]
 14%|█▍        | 1394/10000 [00:03<00:24, 354.11it/s]
 14%|█▍        | 1430/10000 [00:03<00:25, 334.16it/s]
 15%|█▍        | 1466/10000 [00:03<00:25, 339.63it/s]
 15%|█▌        | 1501/10000 [00:03<00:24, 342.61it/s]
 15%|█▌        | 1537/10000 [00:04<00:24, 345.15it/s]
 16%|█▌        | 1573/10000 [00:04<00:24, 346.99it/s]
 16%|█▌        | 1609/10000 [00:04<00:24, 348.27it/s]
 17%|█▋        | 1651/10000 [00:04<00:22, 367.42it/s]
 17%|█▋        | 1693/10000 [00:04<00:21, 381.24it/s]
 17%|█▋        | 1735/10000 [00:04<00:21, 390.63it/s]
 18%|█▊        | 1777/10000 [00:04<00:20, 397.63it/s]
 18%|█▊        | 1819/10000 [00:04<00:20, 403.15it/s]
 19%|█▊        | 1861/10000 [00:04<00:20, 405.93it/s]
 19%|█▉        | 1903/10000 [00:04<00:19, 408.27it/s]
 19%|█▉        | 1945/10000 [00:05<00:19, 409.72it/s]
 20%|█▉        | 1987/10000 [00:05<00:19, 410.86it/s]
 20%|██        | 2029/10000 [00:05<00:19, 410.83it/s]
 21%|██        | 2071/10000 [00:05<00:19, 411.84it/s]
 21%|██        | 2113/10000 [00:05<00:19, 412.82it/s]
 22%|██▏       | 2155/10000 [00:05<00:18, 413.50it/s]
 22%|██▏       | 2197/10000 [00:05<00:18, 413.45it/s]
 22%|██▏       | 2239/10000 [00:05<00:18, 414.66it/s]
 23%|██▎       | 2281/10000 [00:05<00:18, 414.79it/s]
 23%|██▎       | 2323/10000 [00:05<00:18, 415.03it/s]
 24%|██▎       | 2365/10000 [00:06<00:18, 416.18it/s]
 24%|██▍       | 2407/10000 [00:06<00:18, 416.70it/s]
 24%|██▍       | 2449/10000 [00:06<00:18, 416.39it/s]
 25%|██▍       | 2491/10000 [00:06<00:18, 416.42it/s]
 25%|██▌       | 2533/10000 [00:06<00:17, 415.55it/s]
 26%|██▌       | 2575/10000 [00:06<00:17, 415.99it/s]
 26%|██▌       | 2617/10000 [00:06<00:17, 415.94it/s]
 27%|██▋       | 2659/10000 [00:06<00:17, 416.25it/s]
 27%|██▋       | 2701/10000 [00:06<00:17, 415.87it/s]
 27%|██▋       | 2743/10000 [00:06<00:17, 415.91it/s]
 28%|██▊       | 2785/10000 [00:07<00:17, 410.72it/s]
 28%|██▊       | 2827/10000 [00:07<00:18, 391.42it/s]
 29%|██▊       | 2867/10000 [00:07<00:19, 375.20it/s]
 29%|██▉       | 2905/10000 [00:07<00:19, 368.24it/s]
 29%|██▉       | 2942/10000 [00:07<00:20, 338.99it/s]
 30%|██▉       | 2978/10000 [00:07<00:20, 342.09it/s]
 30%|███       | 3013/10000 [00:07<00:20, 341.84it/s]
 30%|███       | 3048/10000 [00:07<00:21, 323.68it/s]
 31%|███       | 3081/10000 [00:08<00:22, 311.92it/s]
 31%|███       | 3113/10000 [00:08<00:22, 305.78it/s]
 31%|███▏      | 3144/10000 [00:08<00:22, 301.62it/s]
 32%|███▏      | 3175/10000 [00:08<00:22, 298.71it/s]
 32%|███▏      | 3205/10000 [00:08<00:22, 295.71it/s]
 32%|███▏      | 3235/10000 [00:08<00:23, 293.36it/s]
 33%|███▎      | 3265/10000 [00:08<00:23, 291.61it/s]
 33%|███▎      | 3295/10000 [00:08<00:23, 290.87it/s]
 33%|███▎      | 3325/10000 [00:08<00:23, 289.93it/s]
 34%|███▎      | 3354/10000 [00:08<00:23, 288.87it/s]
 34%|███▍      | 3383/10000 [00:09<00:22, 288.67it/s]
 34%|███▍      | 3412/10000 [00:09<00:22, 288.53it/s]
 34%|███▍      | 3441/10000 [00:09<00:22, 288.38it/s]
 35%|███▍      | 3470/10000 [00:09<00:22, 288.49it/s]
 35%|███▍      | 3499/10000 [00:09<00:22, 287.98it/s]
 35%|███▌      | 3528/10000 [00:09<00:22, 288.26it/s]
 36%|███▌      | 3557/10000 [00:09<00:22, 288.24it/s]
 36%|███▌      | 3587/10000 [00:09<00:22, 288.95it/s]
 36%|███▌      | 3616/10000 [00:09<00:22, 288.82it/s]
 36%|███▋      | 3645/10000 [00:09<00:22, 288.37it/s]
 37%|███▋      | 3674/10000 [00:10<00:21, 288.38it/s]
 37%|███▋      | 3703/10000 [00:10<00:21, 288.07it/s]
 37%|███▋      | 3732/10000 [00:10<00:21, 288.38it/s]
 38%|███▊      | 3761/10000 [00:10<00:21, 288.69it/s]
 38%|███▊      | 3790/10000 [00:10<00:21, 288.79it/s]
 38%|███▊      | 3819/10000 [00:10<00:21, 288.93it/s]
 39%|███▊      | 3853/10000 [00:10<00:20, 302.84it/s]
 39%|███▉      | 3889/10000 [00:10<00:19, 318.67it/s]
 39%|███▉      | 3925/10000 [00:10<00:18, 329.01it/s]
 40%|███▉      | 3961/10000 [00:10<00:17, 335.74it/s]
 40%|███▉      | 3997/10000 [00:11<00:17, 341.27it/s]
 40%|████      | 4033/10000 [00:11<00:17, 344.66it/s]
 41%|████      | 4069/10000 [00:11<00:17, 347.45it/s]
 41%|████      | 4105/10000 [00:11<00:16, 349.62it/s]
 41%|████▏     | 4141/10000 [00:11<00:16, 352.01it/s]
 42%|████▏     | 4177/10000 [00:11<00:16, 352.38it/s]
 42%|████▏     | 4213/10000 [00:11<00:16, 352.51it/s]
 42%|████▏     | 4249/10000 [00:11<00:16, 353.51it/s]
 43%|████▎     | 4285/10000 [00:11<00:17, 332.99it/s]
 43%|████▎     | 4319/10000 [00:12<00:17, 321.06it/s]
 44%|████▎     | 4353/10000 [00:12<00:17, 324.10it/s]
 44%|████▍     | 4387/10000 [00:12<00:17, 327.93it/s]
 44%|████▍     | 4420/10000 [00:12<00:21, 257.24it/s]
 44%|████▍     | 4449/10000 [00:12<00:24, 228.28it/s]
 45%|████▍     | 4476/10000 [00:12<00:23, 236.63it/s]
 45%|████▌     | 4505/10000 [00:12<00:22, 249.07it/s]
 45%|████▌     | 4534/10000 [00:12<00:21, 258.96it/s]
 46%|████▌     | 4563/10000 [00:13<00:20, 266.61it/s]
 46%|████▌     | 4592/10000 [00:13<00:19, 272.65it/s]
 46%|████▌     | 4621/10000 [00:13<00:19, 277.44it/s]
 46%|████▋     | 4650/10000 [00:13<00:19, 280.95it/s]
 47%|████▋     | 4679/10000 [00:13<00:18, 283.34it/s]
 47%|████▋     | 4709/10000 [00:13<00:18, 285.43it/s]
 47%|████▋     | 4738/10000 [00:13<00:18, 286.70it/s]
 48%|████▊     | 4768/10000 [00:13<00:18, 287.81it/s]
 48%|████▊     | 4798/10000 [00:13<00:18, 288.88it/s]
 48%|████▊     | 4827/10000 [00:13<00:17, 288.29it/s]
 49%|████▊     | 4856/10000 [00:14<00:17, 287.70it/s]
 49%|████▉     | 4886/10000 [00:14<00:17, 288.34it/s]
 49%|████▉     | 4915/10000 [00:14<00:17, 288.47it/s]
 49%|████▉     | 4944/10000 [00:14<00:17, 288.68it/s]
 50%|████▉     | 4973/10000 [00:14<00:17, 289.06it/s]
 50%|█████     | 5002/10000 [00:14<00:17, 288.70it/s]
 50%|█████     | 5031/10000 [00:14<00:17, 288.90it/s]
 51%|█████     | 5061/10000 [00:14<00:17, 289.55it/s]
 51%|█████     | 5091/10000 [00:14<00:16, 290.25it/s]
 51%|█████     | 5121/10000 [00:14<00:17, 284.09it/s]
 52%|█████▏    | 5150/10000 [00:15<00:17, 283.91it/s]
 52%|█████▏    | 5179/10000 [00:15<00:16, 284.97it/s]
 52%|█████▏    | 5208/10000 [00:15<00:16, 285.98it/s]
 52%|█████▏    | 5237/10000 [00:15<00:16, 286.87it/s]
 53%|█████▎    | 5266/10000 [00:15<00:16, 287.66it/s]
 53%|█████▎    | 5295/10000 [00:15<00:16, 288.32it/s]
 53%|█████▎    | 5324/10000 [00:15<00:16, 288.44it/s]
 54%|█████▎    | 5355/10000 [00:15<00:15, 294.37it/s]
 54%|█████▍    | 5391/10000 [00:15<00:14, 312.19it/s]
 54%|█████▍    | 5426/10000 [00:15<00:14, 323.35it/s]
 55%|█████▍    | 5462/10000 [00:16<00:13, 332.08it/s]
 55%|█████▍    | 5498/10000 [00:16<00:13, 337.72it/s]
 55%|█████▌    | 5534/10000 [00:16<00:13, 341.99it/s]
 56%|█████▌    | 5570/10000 [00:16<00:12, 345.26it/s]
 56%|█████▌    | 5606/10000 [00:16<00:12, 347.56it/s]
 56%|█████▋    | 5642/10000 [00:16<00:12, 349.27it/s]
 57%|█████▋    | 5678/10000 [00:16<00:12, 351.11it/s]
 57%|█████▋    | 5714/10000 [00:16<00:12, 352.93it/s]
 57%|█████▊    | 5750/10000 [00:16<00:12, 353.38it/s]
 58%|█████▊    | 5786/10000 [00:16<00:11, 352.90it/s]
 58%|█████▊    | 5823/10000 [00:17<00:11, 356.00it/s]
 59%|█████▊    | 5865/10000 [00:17<00:11, 373.97it/s]
 59%|█████▉    | 5907/10000 [00:17<00:10, 386.52it/s]
 59%|█████▉    | 5946/10000 [00:17<00:10, 370.14it/s]
 60%|█████▉    | 5988/10000 [00:17<00:10, 382.65it/s]
 60%|██████    | 6030/10000 [00:17<00:10, 391.90it/s]
 61%|██████    | 6072/10000 [00:17<00:09, 399.86it/s]
 61%|██████    | 6114/10000 [00:17<00:09, 405.10it/s]
 62%|██████▏   | 6156/10000 [00:17<00:09, 408.33it/s]
 62%|██████▏   | 6198/10000 [00:17<00:09, 409.77it/s]
 62%|██████▏   | 6240/10000 [00:18<00:09, 411.07it/s]
 63%|██████▎   | 6282/10000 [00:18<00:09, 412.41it/s]
 63%|██████▎   | 6324/10000 [00:18<00:08, 413.38it/s]
 64%|██████▎   | 6366/10000 [00:18<00:08, 414.16it/s]
 64%|██████▍   | 6408/10000 [00:18<00:08, 414.71it/s]
 65%|██████▍   | 6455/10000 [00:18<00:08, 430.21it/s]
 65%|██████▌   | 6503/10000 [00:18<00:07, 444.44it/s]
 66%|██████▌   | 6552/10000 [00:18<00:07, 455.92it/s]
 66%|██████▌   | 6601/10000 [00:18<00:07, 463.25it/s]
 66%|██████▋   | 6649/10000 [00:19<00:07, 467.88it/s]
 67%|██████▋   | 6697/10000 [00:19<00:07, 471.41it/s]
 67%|██████▋   | 6746/10000 [00:19<00:06, 474.08it/s]
 68%|██████▊   | 6795/10000 [00:19<00:06, 476.28it/s]
 68%|██████▊   | 6844/10000 [00:19<00:06, 477.61it/s]
 69%|██████▉   | 6893/10000 [00:19<00:06, 478.61it/s]
 69%|██████▉   | 6942/10000 [00:19<00:06, 479.35it/s]
 70%|██████▉   | 6991/10000 [00:19<00:06, 480.16it/s]
 70%|███████   | 7040/10000 [00:19<00:06, 479.84it/s]
 71%|███████   | 7088/10000 [00:19<00:06, 478.67it/s]
 71%|███████▏  | 7137/10000 [00:20<00:05, 479.17it/s]
 72%|███████▏  | 7186/10000 [00:20<00:05, 480.86it/s]
 72%|███████▏  | 7235/10000 [00:20<00:05, 482.33it/s]
 73%|███████▎  | 7284/10000 [00:20<00:05, 480.83it/s]
 73%|███████▎  | 7333/10000 [00:20<00:05, 479.93it/s]
 74%|███████▍  | 7382/10000 [00:20<00:05, 480.13it/s]
 74%|███████▍  | 7431/10000 [00:20<00:05, 461.17it/s]
 75%|███████▍  | 7480/10000 [00:20<00:05, 467.15it/s]
 75%|███████▌  | 7528/10000 [00:20<00:05, 470.77it/s]
 76%|███████▌  | 7576/10000 [00:20<00:05, 472.48it/s]
 76%|███████▌  | 7624/10000 [00:21<00:05, 474.61it/s]
 77%|███████▋  | 7673/10000 [00:21<00:04, 476.43it/s]
 77%|███████▋  | 7722/10000 [00:21<00:04, 478.05it/s]
 78%|███████▊  | 7771/10000 [00:21<00:04, 478.67it/s]
 78%|███████▊  | 7826/10000 [00:21<00:04, 499.53it/s]
 79%|███████▉  | 7894/10000 [00:21<00:03, 551.22it/s]
 80%|███████▉  | 7961/10000 [00:21<00:03, 585.97it/s]
 80%|████████  | 8028/10000 [00:21<00:03, 610.62it/s]
 81%|████████  | 8096/10000 [00:21<00:03, 628.75it/s]
 82%|████████▏ | 8163/10000 [00:21<00:02, 640.06it/s]
 82%|████████▏ | 8229/10000 [00:22<00:02, 644.15it/s]
 83%|████████▎ | 8295/10000 [00:22<00:02, 647.40it/s]
 84%|████████▎ | 8361/10000 [00:22<00:02, 650.05it/s]
 84%|████████▍ | 8427/10000 [00:22<00:02, 652.76it/s]
 85%|████████▍ | 8494/10000 [00:22<00:02, 655.17it/s]
 86%|████████▌ | 8561/10000 [00:22<00:02, 657.06it/s]
 86%|████████▋ | 8628/10000 [00:22<00:02, 659.81it/s]
 87%|████████▋ | 8695/10000 [00:22<00:01, 661.54it/s]
 88%|████████▊ | 8762/10000 [00:22<00:01, 662.69it/s]
 88%|████████▊ | 8829/10000 [00:22<00:01, 662.59it/s]
 89%|████████▉ | 8896/10000 [00:30<00:36, 30.65it/s]
 89%|████████▉ | 8949/10000 [00:30<00:26, 40.25it/s]
 90%|█████████ | 9015/10000 [00:30<00:17, 56.83it/s]
 91%|█████████ | 9082/10000 [00:30<00:11, 79.38it/s]
 91%|█████████▏| 9149/10000 [00:30<00:07, 108.88it/s]
 92%|█████████▏| 9217/10000 [00:30<00:05, 146.79it/s]
 93%|█████████▎| 9285/10000 [00:30<00:03, 192.85it/s]
 94%|█████████▎| 9353/10000 [00:30<00:02, 246.15it/s]
 94%|█████████▍| 9421/10000 [00:30<00:01, 304.64it/s]
 95%|█████████▍| 9488/10000 [00:30<00:01, 363.79it/s]
 96%|█████████▌| 9555/10000 [00:31<00:01, 420.49it/s]
 96%|█████████▌| 9622/10000 [00:31<00:00, 472.36it/s]
 97%|█████████▋| 9689/10000 [00:31<00:00, 517.37it/s]
 98%|█████████▊| 9756/10000 [00:31<00:00, 554.36it/s]
 98%|█████████▊| 9823/10000 [00:31<00:00, 583.82it/s]
 99%|█████████▉| 9890/10000 [00:31<00:00, 606.71it/s]
100%|█████████▉| 9957/10000 [00:31<00:00, 623.16it/s]
100%|██████████| 10000/10000 [00:32<00:00, 304.61it/s]
Define new variables

Defining new variables...:   0%|          | 0/20 [00:00<?, ?it/s]
Defining new variables...:  10%|█         | 2/20 [00:00<00:01, 13.99it/s]
Defining new variables...:  20%|██        | 4/20 [00:00<00:01, 13.56it/s]
Defining new variables...:  30%|███       | 6/20 [00:00<00:01, 13.51it/s]
Defining new variables...:  40%|████      | 8/20 [00:00<00:00, 13.68it/s]
Defining new variables...:  50%|█████     | 10/20 [00:00<00:00, 13.47it/s]
Defining new variables...:  60%|██████    | 12/20 [00:00<00:00, 13.58it/s]
Defining new variables...:  70%|███████   | 14/20 [00:01<00:00, 13.75it/s]
Defining new variables...:  80%|████████  | 16/20 [00:01<00:00, 13.60it/s]
Defining new variables...:  90%|█████████ | 18/20 [00:01<00:00, 13.65it/s]
Defining new variables...: 100%|██████████| 20/20 [00:01<00:00, 13.75it/s]
Defining new variables...: 100%|██████████| 20/20 [00:05<00:00,  3.92it/s]
File nested_downtown_20.dat has been created.

Definition of the nest.

mu_asian = Beta('mu_asian', 1.0, 1.0, None, 0)
nest_asian = OneNestForNestedLogit(
    nest_param=mu_asian, list_of_alternatives=asian, name='asian'
)
nests = NestsForNestedLogit(
    choice_set=all_alternatives,
    tuple_of_nests=(nest_asian,),
)
The following elements do not appear in any nest and are assumed each to be alone in a separate nest: {2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16, 19, 20, 21, 22, 23, 24, 25, 26, 28, 29, 30, 32, 35, 36, 38, 39, 41, 42, 43, 44, 46, 48, 49, 52, 53, 54, 56, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 69, 71, 73, 74, 75, 77, 82, 83, 84, 85, 86, 88, 90, 93, 95, 96, 97, 99}. If it is not the intention, check the assignment of alternatives to nests.
log_probability = the_model_generation.get_nested_logit(nests)
the_biogeme = BIOGEME(biogeme_database, log_probability)
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)}
)
-29957.32273553991

Estimate the parameters.

try:
    results = EstimationResults.from_yaml_file(
        filename=f'saved_results/{the_biogeme.model_name}.yaml'
    )
except FileNotFoundError:
    with timeit(f'Estimate of model {the_biogeme.model_name}'):
        results = the_biogeme.estimate()
print(results.short_summary())
Results for model nested_downtown_20
Nbr of parameters:              12
Sample size:                    10000
Excluded data:                  0
Null log likelihood:            -29957.32
Final log likelihood:           -22988.55
Likelihood ratio test (null):           13937.55
Rho square (null):                      0.233
Rho bar square (null):                  0.232
Akaike Information Criterion:   46001.09
Bayesian Information Criterion: 46087.62
parameters_tables = get_pandas_estimated_parameters(estimation_results=results)
estimated_parameters = parameters_tables['Estimated parameters']
display(estimated_parameters)
              Name     Value  Robust std err.  Robust t-stat.  Robust p-value
0      beta_rating  0.767500         0.015274       50.248559             0.0
1       beta_price -0.402806         0.012340      -32.641652             0.0
2     beta_chinese  0.649011         0.070900        9.153916             0.0
3    beta_japanese  1.213496         0.053821       22.546799             0.0
4      beta_korean  0.662360         0.061760       10.724798             0.0
5      beta_indian  0.938903         0.063494       14.787377             0.0
6      beta_french  0.729052         0.049069       14.857732             0.0
7     beta_mexican  1.193994         0.029053       41.096749             0.0
8    beta_lebanese  0.738656         0.049844       14.819238             0.0
9   beta_ethiopian  0.493428         0.040229       12.265577             0.0
10   beta_log_dist -0.600718         0.012805      -46.913354             0.0
11        mu_asian  2.008861         0.058474       34.355009             0.0
df, msg = compare(estimated_parameters)
print(df)
              Name  True Value  Estimated Value    T-Test
0      beta_rating        0.75         0.767500 -1.145712
1       beta_price       -0.40        -0.402806  0.227386
2     beta_chinese        0.75         0.649011  1.424398
3    beta_japanese        1.25         1.213496  0.678244
4      beta_korean        0.75         0.662360  1.419052
5      beta_indian        1.00         0.938903  0.962250
6      beta_french        0.75         0.729052  0.426912
7     beta_mexican        1.25         1.193994  1.927709
8    beta_lebanese        0.75         0.738656  0.227592
9   beta_ethiopian        0.50         0.493428  0.163373
10   beta_log_dist       -0.60        -0.600718  0.056103
11        mu_asian        2.00         2.008861 -0.151546
print(msg)
Parameters not estimated: ['mu_downtown']

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

Gallery generated by Sphinx-Gallery