.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/bayesian_swissmetro/plot_b23a_binary_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_b23a_binary_logit.py: 23a. Binary logit model ======================= Bayesian estimation of a binary logit model. Two alternatives: Train and Car. Michel Bierlaire, EPFL Tue Nov 18 2025, 18:42:42 .. GENERATED FROM PYTHON SOURCE LINES 13-20 .. code-block:: Python from IPython.core.display_functions import display 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 21-22 See the data processing script: :ref:`swissmetro_binary`. .. GENERATED FROM PYTHON SOURCE LINES 22-33 .. code-block:: Python from swissmetro_binary import ( CAR_AV_SP, CAR_CO_SCALED, CAR_TT_SCALED, CHOICE, TRAIN_AV_SP, TRAIN_COST_SCALED, TRAIN_TT_SCALED, database, ) .. GENERATED FROM PYTHON SOURCE LINES 34-35 Parameters to be estimated. .. GENERATED FROM PYTHON SOURCE LINES 35-41 .. code-block:: Python asc_car = Beta('asc_car', 0, None, None, 0) b_time_car = Beta('b_time_car', 0, None, 0, 0) b_time_train = Beta('b_time_train', 0, None, 0, 0) b_cost_car = Beta('b_cost_car', 0, None, 0, 0) b_cost_train = Beta('b_cost_train', 0, None, 0, 0) .. GENERATED FROM PYTHON SOURCE LINES 42-44 Definition of the utility functions. We estimate a binary logit model. There are only two alternatives. .. GENERATED FROM PYTHON SOURCE LINES 44-47 .. code-block:: Python v_train = b_time_train * TRAIN_TT_SCALED + b_cost_train * TRAIN_COST_SCALED v_car = asc_car + b_time_car * CAR_TT_SCALED + b_cost_car * CAR_CO_SCALED .. GENERATED FROM PYTHON SOURCE LINES 48-49 Associate utility functions with the numbering of alternatives. .. GENERATED FROM PYTHON SOURCE LINES 49-51 .. code-block:: Python v = {1: v_train, 3: v_car} .. GENERATED FROM PYTHON SOURCE LINES 52-53 Associate the availability conditions with the alternatives. .. GENERATED FROM PYTHON SOURCE LINES 53-55 .. code-block:: Python av = {1: TRAIN_AV_SP, 3: CAR_AV_SP} .. GENERATED FROM PYTHON SOURCE LINES 56-58 Definition of the model. This is the contribution of each observation to the log likelihood function. .. GENERATED FROM PYTHON SOURCE LINES 58-60 .. code-block:: Python log_probability = loglogit(v, av, CHOICE) .. GENERATED FROM PYTHON SOURCE LINES 61-62 Create the Biogeme object .. GENERATED FROM PYTHON SOURCE LINES 62-65 .. code-block:: Python the_biogeme = BIOGEME(database, log_probability) the_biogeme.model_name = 'b23a_binary_logit' .. GENERATED FROM PYTHON SOURCE LINES 66-67 Estimate the parameters. .. GENERATED FROM PYTHON SOURCE LINES 67-73 .. code-block:: Python try: results = BayesianResults.from_netcdf( filename=f'saved_results/{the_biogeme.model_name}.nc' ) except FileNotFoundError: results = the_biogeme.bayesian_estimation() .. rst-class:: sphx-glr-script-out .. code-block:: none /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() /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` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_b23a_binary_logit.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_b23a_binary_logit.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_