.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/bayesian_swissmetro/plot_b01a_logit.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_bayesian_swissmetro_plot_b01a_logit.py: 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 .. GENERATED FROM PYTHON SOURCE LINES 17-29 .. code-block:: Python 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 .. GENERATED FROM PYTHON SOURCE LINES 30-31 See the data processing script: :ref:`swissmetro_data`. .. GENERATED FROM PYTHON SOURCE LINES 31-45 .. code-block:: Python 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, ) .. GENERATED FROM PYTHON SOURCE LINES 46-48 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`. .. GENERATED FROM PYTHON SOURCE LINES 48-52 .. code-block:: Python logger = blog.get_screen_logger(level=blog.DEBUG) logger.info('Example b01a_logit.py') .. rst-class:: sphx-glr-script-out .. code-block:: none [INFO] 2025-12-25 14:43:15,419 Example b01a_logit.py .. GENERATED FROM PYTHON SOURCE LINES 53-56 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. .. GENERATED FROM PYTHON SOURCE LINES 56-59 .. code-block:: Python asc_car = Beta('asc_car', 0, None, None, 0) asc_train = Beta('asc_train', 0, None, None, 0) .. GENERATED FROM PYTHON SOURCE LINES 60-63 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). .. GENERATED FROM PYTHON SOURCE LINES 63-65 .. code-block:: Python asc_sm = Beta('asc_sm', 0, None, None, 1) .. GENERATED FROM PYTHON SOURCE LINES 66-68 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. .. GENERATED FROM PYTHON SOURCE LINES 68-72 .. code-block:: Python b_time = Beta('b_time', 0, None, 0, 0) b_cost = Beta('b_cost', 0, None, 0, 0) .. GENERATED FROM PYTHON SOURCE LINES 73-74 Definition of the utility functions. .. GENERATED FROM PYTHON SOURCE LINES 74-78 .. code-block:: Python 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 .. GENERATED FROM PYTHON SOURCE LINES 79-80 Associate utility functions with the numbering of alternatives. .. GENERATED FROM PYTHON SOURCE LINES 80-82 .. code-block:: Python v = {1: v_train, 2: v_sm, 3: v_car} .. GENERATED FROM PYTHON SOURCE LINES 83-84 Associate the availability conditions with the alternatives. .. GENERATED FROM PYTHON SOURCE LINES 84-86 .. code-block:: Python av = {1: TRAIN_AV_SP, 2: SM_AV, 3: CAR_AV_SP} .. GENERATED FROM PYTHON SOURCE LINES 87-89 Definition of the model. This is the contribution of each observation to the log likelihood function. .. GENERATED FROM PYTHON SOURCE LINES 89-91 .. code-block:: Python log_probability = loglogit(v, av, CHOICE) .. GENERATED FROM PYTHON SOURCE LINES 92-93 Create the Biogeme object. .. GENERATED FROM PYTHON SOURCE LINES 93-96 .. code-block:: Python the_biogeme = BIOGEME(database, log_probability) the_biogeme.model_name = 'b01a_logit' .. rst-class:: sphx-glr-script-out .. code-block:: none [DEBUG] 2025-12-25 14:43:15,421 READ FILE biogeme.toml : automatic [INFO] 2025-12-25 14:43:15,421 Default values of the Biogeme parameters are used. [WARNING] 2025-12-25 14:43:15,423 File biogeme.toml has been created .. GENERATED FROM PYTHON SOURCE LINES 97-98 Estimate the parameters. .. GENERATED FROM PYTHON SOURCE LINES 98-100 .. code-block:: Python results: BayesianResults = the_biogeme.bayesian_estimation() .. rst-class:: sphx-glr-script-out .. code-block:: none [INFO] 2025-12-25 14:43:15,423 *** Initial values of the parameters are obtained from the file __b01a_logit.iter [INFO] 2025-12-25 14:43:15,424 Cannot read file __b01a_logit.iter. Statement is ignored. [INFO] 2025-12-25 14:43:15,424 Starting values for the algorithm: {} /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=" Jupyter (new cell, before `import jax`): %env XLA_FLAGS="--xla_force_host_platform_device_count=" 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 [INFO] 2025-12-25 14:43:15,474 Auto sampling: JAX available (devices=1, platforms=cpu) → numpyro/vectorized /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 [INFO] 2025-12-25 14:44:29,340 File b01a_logit.html has been generated. [INFO] 2025-12-25 14:44:29,340 Save simulation results on b01a_logit.nc [INFO] 2025-12-25 14:44:54,756 Saved Bayesian results (posterior + metadata) to b01a_logit.nc .. GENERATED FROM PYTHON SOURCE LINES 101-103 .. code-block:: Python print(results.short_summary()) .. rst-class:: sphx-glr-script-out .. code-block:: none 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 .. GENERATED FROM PYTHON SOURCE LINES 104-105 Get the results in a pandas table .. GENERATED FROM PYTHON SOURCE LINES 105-110 .. code-block:: Python pandas_results = get_pandas_estimated_parameters( estimation_results=results, ) display(pandas_results) .. rst-class:: sphx-glr-script-out .. code-block:: none 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] .. GENERATED FROM PYTHON SOURCE LINES 111-112 Describe the draws stored in the PyMC report. .. GENERATED FROM PYTHON SOURCE LINES 112-113 .. code-block:: Python display(results.report_stored_variables()) .. rst-class:: sphx-glr-script-out .. code-block:: none 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) .. rst-class:: sphx-glr-timing **Total running time of the script:** (1 minutes 39.642 seconds) .. _sphx_glr_download_auto_examples_bayesian_swissmetro_plot_b01a_logit.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_b01a_logit.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_b01a_logit.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_b01a_logit.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_