.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/bayesian_swissmetro/plot_b01c_logit_simul.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_b01c_logit_simul.py: 1c. Simulation of a logit model (traditional and Bayesian) ========================================================== Example of simulation with a logit model Michel Bierlaire, EPFL Thu Oct 30 2025, 14:03:15 .. GENERATED FROM PYTHON SOURCE LINES 11-21 .. code-block:: Python import pandas as pd from IPython.core.display_functions import display import biogeme.biogeme_logging as blog from biogeme.bayesian_estimation import BayesianResults from biogeme.biogeme import BIOGEME from biogeme.expressions import Beta, Derive from biogeme.models import logit .. GENERATED FROM PYTHON SOURCE LINES 22-23 See the data processing script: :ref:`swissmetro_data`. .. GENERATED FROM PYTHON SOURCE LINES 23-38 .. code-block:: Python from swissmetro_data import ( CAR_AV_SP, CAR_CO_SCALED, CAR_TT, SM_AV, SM_COST_SCALED, SM_TT, TRAIN_AV_SP, TRAIN_COST_SCALED, TRAIN_TT, database, ) logger = blog.get_screen_logger(level=blog.INFO) .. GENERATED FROM PYTHON SOURCE LINES 39-40 Parameters. .. GENERATED FROM PYTHON SOURCE LINES 40-46 .. code-block:: Python asc_car = Beta('asc_car', 0, None, None, 0) asc_train = Beta('asc_train', 0, None, None, 0) asc_sm = Beta('asc_sm', 0, None, None, 1) b_time = Beta('b_time', 0, None, None, 0) b_cost = Beta('b_cost', 0, None, None, 0) .. GENERATED FROM PYTHON SOURCE LINES 47-50 Definition of the utility functions. As we will calculate the derivative with respect to TRAIN_TT, SM_TT and CAR_TT, they must explicitly appear in the model. If not, the derivative will be zero. Therefore, we do not use the `_SCALED` version of the attributes. We explicitly include their definition. .. GENERATED FROM PYTHON SOURCE LINES 50-54 .. code-block:: Python v_train = asc_train + b_time * TRAIN_TT / 100 + b_cost * TRAIN_COST_SCALED v_swissmetro = asc_sm + b_time * SM_TT / 100 + b_cost * SM_COST_SCALED v_car = asc_car + b_time * CAR_TT / 100 + b_cost * CAR_CO_SCALED .. GENERATED FROM PYTHON SOURCE LINES 55-56 Associate utility functions with the numbering of alternatives. .. GENERATED FROM PYTHON SOURCE LINES 56-58 .. code-block:: Python v = {1: v_train, 2: v_swissmetro, 3: v_car} .. GENERATED FROM PYTHON SOURCE LINES 59-60 Associate the availability conditions with the alternatives. .. GENERATED FROM PYTHON SOURCE LINES 60-62 .. code-block:: Python av = {1: TRAIN_AV_SP, 2: SM_AV, 3: CAR_AV_SP} .. GENERATED FROM PYTHON SOURCE LINES 63-65 Choice probability. .. GENERATED FROM PYTHON SOURCE LINES 65-67 .. code-block:: Python prob_train = logit(v, av, 1) .. GENERATED FROM PYTHON SOURCE LINES 68-70 Elasticity. .. GENERATED FROM PYTHON SOURCE LINES 70-72 .. code-block:: Python time_elasticity_train = Derive(prob_train, 'TRAIN_TT') * TRAIN_TT / prob_train .. GENERATED FROM PYTHON SOURCE LINES 73-75 Quantities to be simulated. .. GENERATED FROM PYTHON SOURCE LINES 75-82 .. code-block:: Python simulate = { 'Prob. train': prob_train, 'train time elasticity': time_elasticity_train, 'Value of time': b_time / b_cost, } .. GENERATED FROM PYTHON SOURCE LINES 83-87 Create the Biogeme object. As we simulate the probability for all alternatives, even when one of them is not available, Biogeme may trigger some warnings. .. GENERATED FROM PYTHON SOURCE LINES 87-90 .. code-block:: Python biosim = BIOGEME(database, simulate) biosim.model_name = 'b01c_logit_simul' .. rst-class:: sphx-glr-script-out .. code-block:: none Biogeme parameters read from biogeme.toml. .. GENERATED FROM PYTHON SOURCE LINES 91-92 Retrieve the estimated values of the parameters. .. GENERATED FROM PYTHON SOURCE LINES 92-97 .. code-block:: Python RESULTS_FILE_NAME = 'saved_results/b01a_logit.nc' estimation_results = BayesianResults.from_netcdf(filename=RESULTS_FILE_NAME) betas = estimation_results.get_beta_values() .. rst-class:: sphx-glr-script-out .. code-block:: none Loaded NetCDF file size: 855.3 MB load finished in 4448 ms (4.45 s) Diagnostics computation took 23.5 seconds (cached). .. GENERATED FROM PYTHON SOURCE LINES 98-99 Simulation using the posterior mean of each parameter .. GENERATED FROM PYTHON SOURCE LINES 99-104 .. code-block:: Python print('Simulation using the posterior mean of each parameter') results = biosim.simulate(the_beta_values=betas) display(results) .. rst-class:: sphx-glr-script-out .. code-block:: none Simulation using the posterior mean of each parameter Prob. train train time elasticity Value of time 0 0.167825 -1.192850 1.180578 1 0.184090 -1.075555 1.180578 2 0.142838 -1.426131 1.180578 3 0.161141 -1.105807 1.180578 4 0.139629 -1.431470 1.180578 ... ... ... ... 6763 0.172357 -1.143983 1.180578 6764 0.164421 -1.154953 1.180578 6765 0.149244 -1.175931 1.180578 6766 0.134531 -1.417798 1.180578 6767 0.176696 -1.137986 1.180578 [6768 rows x 3 columns] .. GENERATED FROM PYTHON SOURCE LINES 105-107 Bayesian simulation using the posterior draws .. GENERATED FROM PYTHON SOURCE LINES 107-113 .. code-block:: Python print('Bayesian simulation') bayesian_results = biosim.simulate_bayesian( bayesian_estimation_results=estimation_results, percentage_of_draws_to_use=3 ) with pd.option_context('display.max_columns', None, 'display.expand_frame_repr', False): display(bayesian_results) .. rst-class:: sphx-glr-script-out .. code-block:: none Bayesian simulation Bayesian simulation performed with 3% of the draws, that is 240/8000 draws. Adjust the parameter "percentage_of_draws_to_use" if you need a different number of draws. 0%| | 0/240 [00:00` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_b01c_logit_simul.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_b01c_logit_simul.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_