Note
Go to the end to download the full example code.
1a. Estimation of a logit model (Bayesian)¶
Three alternatives:
train,
car and,
Swissmetro.
Stated preferences data.
Michel Bierlaire, EPFL Thu Oct 30 2025, 10:15:52
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 loglogit
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,
)
The logger sets the verbosity of Biogeme. By default, Biogeme is quite silent and generates only warnings. To have more information about what it happening behind the scene, the level should be set to blog.INFO.
logger = blog.get_screen_logger(level=blog.DEBUG)
logger.info('Example b01a_logit.py')
[INFO] 2025-12-25 14:43:15,419 Example b01a_logit.py <plot_b01a_logit.py:49>
Parameters to be estimated: alternative specific constants. By default, the prior distribution is normal, possibly truncated if bounds are defined, with the mean defined by the user, and scale parameter 10.
asc_car = Beta('asc_car', 0, None, None, 0)
asc_train = Beta('asc_train', 0, None, None, 0)
The constant associated with Swissmetro is normalized to zero. It does not need to be defined at all. Here, we illustrate the fact that setting the last argument of the Beta function to 1 fixes the parameter to its default value (here, 0).
asc_sm = Beta('asc_sm', 0, None, None, 1)
Coefficients of the attributes. It is useful to set the upper bound to 0 to reflect the prior assumption about the sign of those parameters.
b_time = Beta('b_time', 0, None, 0, 0)
b_cost = Beta('b_cost', 0, None, 0, 0)
Definition of the utility functions.
v_train = asc_train + b_time * TRAIN_TT_SCALED + b_cost * TRAIN_COST_SCALED
v_sm = 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_sm, 3: v_car}
Associate the availability conditions with the alternatives.
av = {1: TRAIN_AV_SP, 2: SM_AV, 3: CAR_AV_SP}
Definition of the model. This is the contribution of each observation to the log likelihood function.
log_probability = loglogit(v, av, CHOICE)
Create the Biogeme object.
the_biogeme = BIOGEME(database, log_probability)
the_biogeme.model_name = 'b01a_logit'
[DEBUG] 2025-12-25 14:43:15,421 READ FILE biogeme.toml : automatic <parameters.py:184>
[INFO] 2025-12-25 14:43:15,421 Default values of the Biogeme parameters are used. <parameters.py:222>
[WARNING] 2025-12-25 14:43:15,423 File biogeme.toml has been created <parameters.py:259>
Estimate the parameters.
results: BayesianResults = the_biogeme.bayesian_estimation()
[INFO] 2025-12-25 14:43:15,423 *** Initial values of the parameters are obtained from the file __b01a_logit.iter <biogeme.py:813>
[INFO] 2025-12-25 14:43:15,424 Cannot read file __b01a_logit.iter. Statement is ignored. <biogeme.py:700>
[INFO] 2025-12-25 14:43:15,424 Starting values for the algorithm: {} <biogeme.py:824>
/Users/bierlair/MyFiles/github/biogeme/src/biogeme/biogeme.py:832: UserWarning: Note: JAX currently sees 1 CPU device. To parallelize across CPU devices, set XLA_FLAGS as above and restart Python/Jupyter.
macOS / Linux (bash/zsh):
export XLA_FLAGS="--xla_force_host_platform_device_count=<number_of_cores>"
Jupyter (new cell, before `import jax`):
%env XLA_FLAGS="--xla_force_host_platform_device_count=<number_of_cores>"
warning_cpu_devices()
[INFO] 2025-12-25 14:43:15,425 Detected CPU devices: 1 | System logical cores: 12
Current XLA_FLAGS: (none set)
Platform: Darwin 24.6.0 | Python: 3.13.1
<biogeme.py:833>
[INFO] 2025-12-25 14:43:15,474 Auto sampling: JAX available (devices=1, platforms=cpu) → numpyro/vectorized <sampling_strategy.py:130>
/Users/bierlair/MyFiles/github/biogeme/src/biogeme/biogeme.py:859: UserWarning: The effect of Potentials on other parameters is ignored during prior predictive sampling. This is likely to lead to invalid or biased predictive samples.
pm.sample_prior_predictive(
0%| | 0/4000 [00:00<?, ?it/s]
warmup: 0%| | 1/4000 [00:01<1:16:08, 1.14s/it]
warmup: 0%| | 14/4000 [00:01<04:22, 15.21it/s]
warmup: 1%| | 22/4000 [00:01<02:56, 22.51it/s]
warmup: 1%| | 33/4000 [00:01<01:51, 35.48it/s]
warmup: 1%| | 41/4000 [00:01<01:38, 40.26it/s]
warmup: 1%|▏ | 52/4000 [00:01<01:13, 53.57it/s]
warmup: 2%|▏ | 63/4000 [00:01<01:01, 64.53it/s]
warmup: 2%|▏ | 78/4000 [00:01<00:48, 81.52it/s]
warmup: 2%|▏ | 93/4000 [00:02<00:40, 97.07it/s]
warmup: 3%|▎ | 105/4000 [00:02<00:47, 81.81it/s]
warmup: 3%|▎ | 115/4000 [00:02<00:49, 77.93it/s]
warmup: 3%|▎ | 124/4000 [00:02<00:54, 71.73it/s]
warmup: 3%|▎ | 136/4000 [00:02<00:47, 80.87it/s]
warmup: 4%|▎ | 149/4000 [00:02<00:41, 92.37it/s]
warmup: 4%|▍ | 160/4000 [00:02<00:43, 88.26it/s]
warmup: 4%|▍ | 170/4000 [00:03<00:52, 72.69it/s]
warmup: 5%|▍ | 182/4000 [00:03<00:46, 81.73it/s]
warmup: 5%|▍ | 194/4000 [00:03<00:42, 90.37it/s]
warmup: 5%|▌ | 207/4000 [00:03<00:38, 99.25it/s]
warmup: 5%|▌ | 219/4000 [00:03<00:36, 103.48it/s]
warmup: 6%|▌ | 230/4000 [00:03<00:36, 104.13it/s]
warmup: 6%|▌ | 243/4000 [00:03<00:34, 108.47it/s]
warmup: 6%|▋ | 256/4000 [00:03<00:32, 113.71it/s]
warmup: 7%|▋ | 268/4000 [00:03<00:32, 113.98it/s]
warmup: 7%|▋ | 280/4000 [00:04<00:38, 97.10it/s]
warmup: 7%|▋ | 291/4000 [00:04<00:43, 85.74it/s]
warmup: 8%|▊ | 301/4000 [00:04<00:41, 89.05it/s]
warmup: 8%|▊ | 311/4000 [00:04<00:43, 85.15it/s]
warmup: 8%|▊ | 321/4000 [00:04<00:42, 87.58it/s]
warmup: 8%|▊ | 333/4000 [00:04<00:38, 95.47it/s]
warmup: 9%|▊ | 346/4000 [00:04<00:35, 104.31it/s]
warmup: 9%|▉ | 359/4000 [00:04<00:33, 109.44it/s]
warmup: 9%|▉ | 371/4000 [00:05<00:32, 110.71it/s]
warmup: 10%|▉ | 383/4000 [00:05<00:33, 109.55it/s]
warmup: 10%|▉ | 396/4000 [00:05<00:31, 115.16it/s]
warmup: 10%|█ | 410/4000 [00:05<00:29, 120.59it/s]
warmup: 11%|█ | 423/4000 [00:05<00:29, 119.74it/s]
warmup: 11%|█ | 436/4000 [00:05<00:29, 122.63it/s]
warmup: 11%|█ | 449/4000 [00:05<00:28, 123.15it/s]
warmup: 12%|█▏ | 462/4000 [00:05<00:30, 117.93it/s]
warmup: 12%|█▏ | 474/4000 [00:05<00:33, 104.05it/s]
warmup: 12%|█▏ | 487/4000 [00:06<00:32, 109.73it/s]
warmup: 12%|█▏ | 499/4000 [00:06<00:32, 108.63it/s]
warmup: 13%|█▎ | 511/4000 [00:06<00:33, 104.60it/s]
warmup: 13%|█▎ | 522/4000 [00:06<00:35, 96.86it/s]
warmup: 13%|█▎ | 537/4000 [00:06<00:32, 108.19it/s]
warmup: 14%|█▍ | 552/4000 [00:06<00:28, 119.13it/s]
warmup: 14%|█▍ | 567/4000 [00:06<00:27, 126.20it/s]
warmup: 15%|█▍ | 581/4000 [00:06<00:26, 128.79it/s]
warmup: 15%|█▍ | 595/4000 [00:06<00:26, 126.12it/s]
warmup: 15%|█▌ | 610/4000 [00:07<00:25, 131.44it/s]
warmup: 16%|█▌ | 624/4000 [00:07<00:26, 128.92it/s]
warmup: 16%|█▌ | 640/4000 [00:07<00:24, 136.65it/s]
warmup: 16%|█▋ | 654/4000 [00:07<00:24, 133.92it/s]
warmup: 17%|█▋ | 668/4000 [00:07<00:25, 128.77it/s]
warmup: 17%|█▋ | 682/4000 [00:07<00:25, 129.23it/s]
warmup: 17%|█▋ | 698/4000 [00:07<00:24, 136.64it/s]
warmup: 18%|█▊ | 713/4000 [00:07<00:23, 138.88it/s]
warmup: 18%|█▊ | 728/4000 [00:07<00:23, 140.75it/s]
warmup: 19%|█▊ | 743/4000 [00:08<00:23, 139.31it/s]
warmup: 19%|█▉ | 757/4000 [00:08<00:23, 136.22it/s]
warmup: 19%|█▉ | 771/4000 [00:08<00:24, 133.98it/s]
warmup: 20%|█▉ | 786/4000 [00:08<00:23, 135.69it/s]
warmup: 20%|██ | 801/4000 [00:08<00:23, 138.57it/s]
warmup: 20%|██ | 816/4000 [00:08<00:22, 140.84it/s]
warmup: 21%|██ | 831/4000 [00:08<00:22, 138.36it/s]
warmup: 21%|██ | 845/4000 [00:08<00:22, 137.24it/s]
warmup: 21%|██▏ | 859/4000 [00:08<00:22, 137.95it/s]
warmup: 22%|██▏ | 873/4000 [00:09<00:26, 118.13it/s]
warmup: 22%|██▏ | 886/4000 [00:09<00:29, 104.01it/s]
warmup: 22%|██▎ | 900/4000 [00:09<00:28, 109.03it/s]
warmup: 23%|██▎ | 912/4000 [00:09<00:27, 111.23it/s]
warmup: 23%|██▎ | 924/4000 [00:09<00:27, 112.23it/s]
warmup: 23%|██▎ | 939/4000 [00:09<00:25, 121.34it/s]
warmup: 24%|██▍ | 954/4000 [00:09<00:23, 127.93it/s]
warmup: 24%|██▍ | 970/4000 [00:09<00:22, 135.86it/s]
warmup: 25%|██▍ | 986/4000 [00:09<00:21, 139.81it/s]
warmup: 25%|██▌ | 1001/4000 [00:10<00:22, 132.29it/s]
warmup: 25%|██▌ | 1015/4000 [00:10<00:22, 131.28it/s]
warmup: 26%|██▌ | 1029/4000 [00:10<00:23, 127.33it/s]
warmup: 26%|██▌ | 1044/4000 [00:10<00:22, 130.25it/s]
warmup: 26%|██▋ | 1058/4000 [00:10<00:22, 129.40it/s]
warmup: 27%|██▋ | 1075/4000 [00:10<00:21, 137.54it/s]
warmup: 27%|██▋ | 1092/4000 [00:10<00:20, 143.68it/s]
warmup: 28%|██▊ | 1108/4000 [00:10<00:19, 146.76it/s]
warmup: 28%|██▊ | 1123/4000 [00:10<00:19, 144.03it/s]
warmup: 28%|██▊ | 1138/4000 [00:11<00:20, 141.30it/s]
warmup: 29%|██▉ | 1153/4000 [00:11<00:21, 130.73it/s]
warmup: 29%|██▉ | 1168/4000 [00:11<00:21, 134.59it/s]
warmup: 30%|██▉ | 1182/4000 [00:11<00:21, 132.70it/s]
warmup: 30%|███ | 1200/4000 [00:11<00:19, 142.66it/s]
warmup: 30%|███ | 1217/4000 [00:11<00:18, 146.82it/s]
warmup: 31%|███ | 1232/4000 [00:11<00:19, 139.16it/s]
warmup: 31%|███ | 1247/4000 [00:11<00:21, 129.69it/s]
warmup: 32%|███▏ | 1261/4000 [00:12<00:21, 127.52it/s]
warmup: 32%|███▏ | 1277/4000 [00:12<00:20, 133.21it/s]
warmup: 32%|███▏ | 1292/4000 [00:12<00:19, 136.48it/s]
warmup: 33%|███▎ | 1306/4000 [00:12<00:20, 133.10it/s]
warmup: 33%|███▎ | 1321/4000 [00:12<00:19, 134.52it/s]
warmup: 33%|███▎ | 1337/4000 [00:12<00:19, 140.14it/s]
warmup: 34%|███▍ | 1352/4000 [00:12<00:18, 141.32it/s]
warmup: 34%|███▍ | 1369/4000 [00:12<00:17, 148.08it/s]
warmup: 35%|███▍ | 1384/4000 [00:12<00:19, 137.10it/s]
warmup: 35%|███▍ | 1398/4000 [00:12<00:19, 136.33it/s]
warmup: 35%|███▌ | 1414/4000 [00:13<00:18, 139.49it/s]
warmup: 36%|███▌ | 1429/4000 [00:13<00:18, 138.27it/s]
warmup: 36%|███▌ | 1443/4000 [00:13<00:18, 137.02it/s]
warmup: 36%|███▋ | 1458/4000 [00:13<00:18, 139.12it/s]
warmup: 37%|███▋ | 1474/4000 [00:13<00:17, 143.64it/s]
warmup: 37%|███▋ | 1489/4000 [00:13<00:17, 140.29it/s]
warmup: 38%|███▊ | 1505/4000 [00:13<00:17, 145.61it/s]
warmup: 38%|███▊ | 1522/4000 [00:13<00:16, 151.28it/s]
warmup: 38%|███▊ | 1538/4000 [00:13<00:16, 148.57it/s]
warmup: 39%|███▉ | 1556/4000 [00:14<00:15, 155.90it/s]
warmup: 39%|███▉ | 1572/4000 [00:14<00:16, 145.72it/s]
warmup: 40%|███▉ | 1588/4000 [00:14<00:16, 148.91it/s]
warmup: 40%|████ | 1604/4000 [00:14<00:15, 151.30it/s]
warmup: 40%|████ | 1620/4000 [00:14<00:15, 150.17it/s]
warmup: 41%|████ | 1636/4000 [00:14<00:15, 151.90it/s]
warmup: 41%|████▏ | 1653/4000 [00:14<00:15, 155.11it/s]
warmup: 42%|████▏ | 1669/4000 [00:14<00:15, 148.30it/s]
warmup: 42%|████▏ | 1684/4000 [00:14<00:16, 144.54it/s]
warmup: 42%|████▏ | 1699/4000 [00:15<00:16, 140.91it/s]
warmup: 43%|████▎ | 1715/4000 [00:15<00:15, 143.12it/s]
warmup: 43%|████▎ | 1732/4000 [00:15<00:15, 147.39it/s]
warmup: 44%|████▎ | 1747/4000 [00:15<00:15, 146.77it/s]
warmup: 44%|████▍ | 1762/4000 [00:15<00:15, 146.02it/s]
warmup: 44%|████▍ | 1777/4000 [00:15<00:16, 135.90it/s]
warmup: 45%|████▍ | 1792/4000 [00:15<00:16, 136.68it/s]
warmup: 45%|████▌ | 1806/4000 [00:15<00:16, 131.62it/s]
warmup: 46%|████▌ | 1820/4000 [00:15<00:16, 132.49it/s]
warmup: 46%|████▌ | 1834/4000 [00:16<00:17, 126.73it/s]
warmup: 46%|████▌ | 1849/4000 [00:16<00:16, 132.89it/s]
warmup: 47%|████▋ | 1864/4000 [00:16<00:15, 135.44it/s]
warmup: 47%|████▋ | 1880/4000 [00:16<00:15, 139.41it/s]
warmup: 47%|████▋ | 1895/4000 [00:16<00:15, 136.90it/s]
warmup: 48%|████▊ | 1909/4000 [00:16<00:15, 136.03it/s]
warmup: 48%|████▊ | 1926/4000 [00:16<00:14, 145.27it/s]
warmup: 49%|████▊ | 1942/4000 [00:16<00:13, 147.67it/s]
warmup: 49%|████▉ | 1957/4000 [00:16<00:14, 145.03it/s]
warmup: 49%|████▉ | 1972/4000 [00:17<00:18, 112.17it/s]
warmup: 50%|████▉ | 1985/4000 [00:17<00:22, 91.08it/s]
warmup: 50%|████▉ | 1997/4000 [00:17<00:20, 95.50it/s]
sample: 50%|█████ | 2008/4000 [00:17<00:20, 96.50it/s]
sample: 50%|█████ | 2019/4000 [00:17<00:20, 97.27it/s]
sample: 51%|█████ | 2030/4000 [00:17<00:19, 100.32it/s]
sample: 51%|█████ | 2041/4000 [00:17<00:19, 102.75it/s]
sample: 51%|█████▏ | 2054/4000 [00:17<00:18, 107.52it/s]
sample: 52%|█████▏ | 2066/4000 [00:18<00:17, 109.71it/s]
sample: 52%|█████▏ | 2078/4000 [00:18<00:17, 111.76it/s]
sample: 52%|█████▏ | 2091/4000 [00:18<00:16, 113.87it/s]
sample: 53%|█████▎ | 2103/4000 [00:18<00:16, 115.50it/s]
sample: 53%|█████▎ | 2115/4000 [00:18<00:16, 111.17it/s]
sample: 53%|█████▎ | 2128/4000 [00:18<00:16, 114.72it/s]
sample: 54%|█████▎ | 2140/4000 [00:18<00:16, 111.33it/s]
sample: 54%|█████▍ | 2152/4000 [00:18<00:16, 112.28it/s]
sample: 54%|█████▍ | 2164/4000 [00:18<00:16, 109.93it/s]
sample: 54%|█████▍ | 2176/4000 [00:19<00:16, 111.15it/s]
sample: 55%|█████▍ | 2188/4000 [00:19<00:16, 110.60it/s]
sample: 55%|█████▌ | 2200/4000 [00:19<00:15, 112.54it/s]
sample: 55%|█████▌ | 2212/4000 [00:19<00:16, 109.99it/s]
sample: 56%|█████▌ | 2224/4000 [00:19<00:15, 111.58it/s]
sample: 56%|█████▌ | 2238/4000 [00:19<00:14, 118.40it/s]
sample: 56%|█████▋ | 2251/4000 [00:19<00:14, 120.72it/s]
sample: 57%|█████▋ | 2264/4000 [00:19<00:14, 120.25it/s]
sample: 57%|█████▋ | 2277/4000 [00:19<00:14, 121.76it/s]
sample: 57%|█████▋ | 2290/4000 [00:19<00:14, 119.21it/s]
sample: 58%|█████▊ | 2302/4000 [00:20<00:14, 114.61it/s]
sample: 58%|█████▊ | 2315/4000 [00:20<00:14, 117.72it/s]
sample: 58%|█████▊ | 2327/4000 [00:20<00:14, 115.51it/s]
sample: 58%|█████▊ | 2339/4000 [00:20<00:14, 113.98it/s]
sample: 59%|█████▉ | 2351/4000 [00:20<00:14, 112.91it/s]
sample: 59%|█████▉ | 2363/4000 [00:20<00:14, 113.88it/s]
sample: 59%|█████▉ | 2375/4000 [00:20<00:14, 109.72it/s]
sample: 60%|█████▉ | 2388/4000 [00:20<00:14, 114.11it/s]
sample: 60%|██████ | 2400/4000 [00:20<00:13, 114.46it/s]
sample: 60%|██████ | 2413/4000 [00:21<00:13, 115.80it/s]
sample: 61%|██████ | 2426/4000 [00:21<00:13, 117.09it/s]
sample: 61%|██████ | 2438/4000 [00:21<00:13, 116.57it/s]
sample: 61%|██████▏ | 2450/4000 [00:21<00:13, 112.91it/s]
sample: 62%|██████▏ | 2463/4000 [00:21<00:13, 115.41it/s]
sample: 62%|██████▏ | 2475/4000 [00:21<00:13, 115.20it/s]
sample: 62%|██████▏ | 2487/4000 [00:21<00:13, 112.31it/s]
sample: 62%|██████▏ | 2499/4000 [00:21<00:13, 114.29it/s]
sample: 63%|██████▎ | 2511/4000 [00:21<00:13, 112.98it/s]
sample: 63%|██████▎ | 2523/4000 [00:22<00:13, 113.04it/s]
sample: 63%|██████▎ | 2535/4000 [00:22<00:13, 111.78it/s]
sample: 64%|██████▎ | 2547/4000 [00:22<00:12, 112.72it/s]
sample: 64%|██████▍ | 2560/4000 [00:22<00:12, 116.42it/s]
sample: 64%|██████▍ | 2572/4000 [00:22<00:12, 117.44it/s]
sample: 65%|██████▍ | 2584/4000 [00:22<00:12, 114.71it/s]
sample: 65%|██████▍ | 2596/4000 [00:22<00:12, 114.51it/s]
sample: 65%|██████▌ | 2608/4000 [00:22<00:12, 114.21it/s]
sample: 66%|██████▌ | 2620/4000 [00:22<00:11, 115.64it/s]
sample: 66%|██████▌ | 2634/4000 [00:22<00:11, 119.78it/s]
sample: 66%|██████▌ | 2646/4000 [00:23<00:11, 117.94it/s]
sample: 66%|██████▋ | 2658/4000 [00:23<00:11, 115.97it/s]
sample: 67%|██████▋ | 2670/4000 [00:23<00:12, 106.35it/s]
sample: 67%|██████▋ | 2681/4000 [00:23<00:12, 107.08it/s]
sample: 67%|██████▋ | 2693/4000 [00:23<00:11, 109.27it/s]
sample: 68%|██████▊ | 2705/4000 [00:23<00:11, 110.72it/s]
sample: 68%|██████▊ | 2717/4000 [00:23<00:11, 110.18it/s]
sample: 68%|██████▊ | 2730/4000 [00:23<00:11, 110.84it/s]
sample: 69%|██████▊ | 2742/4000 [00:23<00:11, 110.28it/s]
sample: 69%|██████▉ | 2754/4000 [00:24<00:11, 109.87it/s]
sample: 69%|██████▉ | 2766/4000 [00:24<00:11, 109.57it/s]
sample: 69%|██████▉ | 2779/4000 [00:24<00:10, 114.21it/s]
sample: 70%|██████▉ | 2791/4000 [00:24<00:10, 111.10it/s]
sample: 70%|███████ | 2803/4000 [00:24<00:10, 111.98it/s]
sample: 70%|███████ | 2815/4000 [00:24<00:10, 114.13it/s]
sample: 71%|███████ | 2827/4000 [00:24<00:10, 110.22it/s]
sample: 71%|███████ | 2839/4000 [00:24<00:10, 111.15it/s]
sample: 71%|███████▏ | 2851/4000 [00:24<00:10, 110.92it/s]
sample: 72%|███████▏ | 2863/4000 [00:25<00:10, 112.04it/s]
sample: 72%|███████▏ | 2875/4000 [00:25<00:09, 113.15it/s]
sample: 72%|███████▏ | 2888/4000 [00:25<00:09, 116.79it/s]
sample: 72%|███████▎ | 2900/4000 [00:25<00:09, 114.76it/s]
sample: 73%|███████▎ | 2912/4000 [00:25<00:09, 113.34it/s]
sample: 73%|███████▎ | 2925/4000 [00:25<00:09, 115.27it/s]
sample: 73%|███████▎ | 2937/4000 [00:25<00:09, 110.08it/s]
sample: 74%|███████▎ | 2949/4000 [00:25<00:09, 108.86it/s]
sample: 74%|███████▍ | 2961/4000 [00:25<00:09, 109.06it/s]
sample: 74%|███████▍ | 2975/4000 [00:26<00:08, 115.53it/s]
sample: 75%|███████▍ | 2989/4000 [00:26<00:08, 120.39it/s]
sample: 75%|███████▌ | 3003/4000 [00:26<00:08, 123.51it/s]
sample: 75%|███████▌ | 3016/4000 [00:26<00:07, 125.10it/s]
sample: 76%|███████▌ | 3030/4000 [00:26<00:07, 128.19it/s]
sample: 76%|███████▌ | 3044/4000 [00:26<00:07, 129.88it/s]
sample: 76%|███████▋ | 3058/4000 [00:26<00:07, 126.72it/s]
sample: 77%|███████▋ | 3072/4000 [00:26<00:07, 129.66it/s]
sample: 77%|███████▋ | 3086/4000 [00:26<00:07, 126.84it/s]
sample: 78%|███████▊ | 3100/4000 [00:27<00:07, 127.88it/s]
sample: 78%|███████▊ | 3113/4000 [00:27<00:06, 127.16it/s]
sample: 78%|███████▊ | 3127/4000 [00:27<00:07, 124.35it/s]
sample: 78%|███████▊ | 3140/4000 [00:27<00:06, 123.37it/s]
sample: 79%|███████▉ | 3154/4000 [00:27<00:06, 126.95it/s]
sample: 79%|███████▉ | 3168/4000 [00:27<00:06, 128.64it/s]
sample: 80%|███████▉ | 3182/4000 [00:27<00:06, 131.87it/s]
sample: 80%|███████▉ | 3196/4000 [00:27<00:06, 126.31it/s]
sample: 80%|████████ | 3209/4000 [00:27<00:06, 121.00it/s]
sample: 81%|████████ | 3222/4000 [00:28<00:06, 118.03it/s]
sample: 81%|████████ | 3234/4000 [00:28<00:06, 117.20it/s]
sample: 81%|████████ | 3247/4000 [00:28<00:06, 119.06it/s]
sample: 81%|████████▏ | 3259/4000 [00:28<00:06, 117.38it/s]
sample: 82%|████████▏ | 3271/4000 [00:28<00:06, 115.95it/s]
sample: 82%|████████▏ | 3284/4000 [00:28<00:06, 118.68it/s]
sample: 82%|████████▏ | 3296/4000 [00:28<00:05, 118.41it/s]
sample: 83%|████████▎ | 3308/4000 [00:28<00:05, 118.17it/s]
sample: 83%|████████▎ | 3320/4000 [00:28<00:05, 117.87it/s]
sample: 83%|████████▎ | 3332/4000 [00:28<00:05, 117.71it/s]
sample: 84%|████████▎ | 3344/4000 [00:29<00:05, 117.93it/s]
sample: 84%|████████▍ | 3356/4000 [00:29<00:05, 113.75it/s]
sample: 84%|████████▍ | 3369/4000 [00:29<00:05, 115.46it/s]
sample: 85%|████████▍ | 3382/4000 [00:29<00:05, 117.79it/s]
sample: 85%|████████▍ | 3394/4000 [00:29<00:05, 115.04it/s]
sample: 85%|████████▌ | 3407/4000 [00:29<00:05, 116.30it/s]
sample: 86%|████████▌ | 3420/4000 [00:29<00:04, 117.52it/s]
sample: 86%|████████▌ | 3432/4000 [00:29<00:04, 118.19it/s]
sample: 86%|████████▌ | 3444/4000 [00:29<00:04, 117.95it/s]
sample: 86%|████████▋ | 3456/4000 [00:30<00:04, 116.85it/s]
sample: 87%|████████▋ | 3469/4000 [00:30<00:04, 119.64it/s]
sample: 87%|████████▋ | 3483/4000 [00:30<00:04, 122.36it/s]
sample: 87%|████████▋ | 3496/4000 [00:30<00:04, 116.28it/s]
sample: 88%|████████▊ | 3508/4000 [00:30<00:04, 114.04it/s]
sample: 88%|████████▊ | 3520/4000 [00:30<00:04, 112.05it/s]
sample: 88%|████████▊ | 3532/4000 [00:30<00:04, 109.78it/s]
sample: 89%|████████▊ | 3545/4000 [00:30<00:04, 112.83it/s]
sample: 89%|████████▉ | 3557/4000 [00:30<00:03, 113.40it/s]
sample: 89%|████████▉ | 3570/4000 [00:30<00:03, 115.02it/s]
sample: 90%|████████▉ | 3582/4000 [00:31<00:03, 111.66it/s]
sample: 90%|████████▉ | 3594/4000 [00:31<00:03, 112.46it/s]
sample: 90%|█████████ | 3606/4000 [00:31<00:03, 113.43it/s]
sample: 90%|█████████ | 3618/4000 [00:31<00:03, 107.90it/s]
sample: 91%|█████████ | 3630/4000 [00:31<00:03, 109.89it/s]
sample: 91%|█████████ | 3642/4000 [00:31<00:03, 112.34it/s]
sample: 91%|█████████▏| 3656/4000 [00:31<00:02, 118.12it/s]
sample: 92%|█████████▏| 3668/4000 [00:31<00:02, 116.14it/s]
sample: 92%|█████████▏| 3680/4000 [00:31<00:02, 111.93it/s]
sample: 92%|█████████▏| 3692/4000 [00:32<00:02, 110.38it/s]
sample: 93%|█████████▎| 3704/4000 [00:32<00:02, 112.40it/s]
sample: 93%|█████████▎| 3716/4000 [00:32<00:02, 113.64it/s]
sample: 93%|█████████▎| 3728/4000 [00:32<00:02, 114.41it/s]
sample: 94%|█████████▎| 3740/4000 [00:32<00:02, 112.72it/s]
sample: 94%|█████████▍| 3752/4000 [00:32<00:02, 114.39it/s]
sample: 94%|█████████▍| 3765/4000 [00:32<00:02, 116.19it/s]
sample: 94%|█████████▍| 3779/4000 [00:32<00:01, 120.17it/s]
sample: 95%|█████████▍| 3792/4000 [00:32<00:01, 119.88it/s]
sample: 95%|█████████▌| 3804/4000 [00:33<00:01, 118.24it/s]
sample: 95%|█████████▌| 3817/4000 [00:33<00:01, 119.95it/s]
sample: 96%|█████████▌| 3829/4000 [00:33<00:01, 119.94it/s]
sample: 96%|█████████▌| 3841/4000 [00:33<00:01, 116.85it/s]
sample: 96%|█████████▋| 3853/4000 [00:33<00:01, 116.46it/s]
sample: 97%|█████████▋| 3867/4000 [00:33<00:01, 120.37it/s]
sample: 97%|█████████▋| 3880/4000 [00:33<00:01, 116.36it/s]
sample: 97%|█████████▋| 3894/4000 [00:33<00:00, 121.47it/s]
sample: 98%|█████████▊| 3907/4000 [00:33<00:00, 119.26it/s]
sample: 98%|█████████▊| 3919/4000 [00:34<00:00, 115.50it/s]
sample: 98%|█████████▊| 3932/4000 [00:34<00:00, 116.77it/s]
sample: 99%|█████████▊| 3944/4000 [00:34<00:00, 114.85it/s]
sample: 99%|█████████▉| 3957/4000 [00:34<00:00, 117.91it/s]
sample: 99%|█████████▉| 3969/4000 [00:34<00:00, 117.29it/s]
sample: 100%|█████████▉| 3981/4000 [00:34<00:00, 113.76it/s]
sample: 100%|█████████▉| 3993/4000 [00:34<00:00, 112.21it/s]
sample: 100%|██████████| 4000/4000 [00:34<00:00, 115.17it/s]
posterior_predictive_loglike finished in 294 ms
expected_log_likelihood finished in 11 ms
best_draw_log_likelihood finished in 10 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 639 ms
waic finished in 639 ms
loo_res finished in 7580 ms (7.58 s)
loo finished in 7580 ms (7.58 s)
[INFO] 2025-12-25 14:44:28,380 Diagnostics computation took 22.9 seconds (cached). <bayesian_results.py:235>
[INFO] 2025-12-25 14:44:29,340 File b01a_logit.html has been generated. <html_output.py:943>
[INFO] 2025-12-25 14:44:29,340 Save simulation results on b01a_logit.nc <raw_bayesian_results.py:155>
[INFO] 2025-12-25 14:44:54,756 Saved Bayesian results (posterior + metadata) to b01a_logit.nc <raw_bayesian_results.py:207>
print(results.short_summary())
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:00:41.437931
Posterior predictive log-likelihood (sum of log mean p) -5329.78
Expected log-likelihood E[log L(Y|θ)] -5333.27
Best-draw log-likelihood (posterior upper bound) -5331.27
WAIC (Widely Applicable Information Criterion) -5336.78
WAIC Standard Error 59.63
Effective number of parameters (p_WAIC) 7.00
LOO (Leave-One-Out Cross-Validation) -5336.79
LOO Standard Error 59.63
Effective number of parameters (p_LOO) 7.01
Get the results in a pandas table
pandas_results = get_pandas_estimated_parameters(
estimation_results=results,
)
display(pandas_results)
Name Value (mean) Value (median) ... R hat ESS (bulk) ESS (tail)
0 asc_train -0.700918 -0.700670 ... 1.000130 3402.152245 4601.166818
1 asc_car -0.154128 -0.153721 ... 1.000899 3564.651200 4448.207029
2 b_time -1.278959 -1.278735 ... 0.999974 3360.898307 4439.786854
3 b_cost -1.084059 -1.083901 ... 1.000379 4775.294477 4711.471226
[4 rows x 12 columns]
Describe the draws stored in the PyMC report.
display(results.report_stored_variables())
group variable dims shape
0 constant_data CAR_AV_SP (obs,) (6768,)
1 constant_data CAR_CO_SCALED (obs,) (6768,)
2 constant_data CAR_TT_SCALED (obs,) (6768,)
3 constant_data CHOICE (obs,) (6768,)
4 constant_data SM_AV (obs,) (6768,)
5 constant_data SM_COST_SCALED (obs,) (6768,)
6 constant_data SM_TT_SCALED (obs,) (6768,)
7 constant_data TRAIN_AV_SP (obs,) (6768,)
8 constant_data TRAIN_COST_SCALED (obs,) (6768,)
9 constant_data TRAIN_TT_SCALED (obs,) (6768,)
10 log_likelihood _choice (chain, draw, obs) (4, 2000, 6768)
11 posterior asc_car (chain, draw) (4, 2000)
12 posterior asc_train (chain, draw) (4, 2000)
13 posterior b_cost (chain, draw) (4, 2000)
14 posterior b_time (chain, draw) (4, 2000)
15 posterior log_like (chain, draw, obs) (4, 2000, 6768)
16 prior asc_car (chain, draw) (1, 2000)
17 prior asc_train (chain, draw) (1, 2000)
18 prior b_cost (chain, draw) (1, 2000)
19 prior b_time (chain, draw) (1, 2000)
20 prior log_like (chain, draw, obs) (1, 2000, 6768)
21 sample_stats acceptance_rate (chain, draw) (4, 2000)
22 sample_stats diverging (chain, draw) (4, 2000)
23 sample_stats energy (chain, draw) (4, 2000)
24 sample_stats lp (chain, draw) (4, 2000)
25 sample_stats n_steps (chain, draw) (4, 2000)
26 sample_stats step_size (chain, draw) (4, 2000)
27 sample_stats tree_depth (chain, draw) (4, 2000)
Total running time of the script: (1 minutes 39.642 seconds)