.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/swissmetro/plot_b19_individual_level_parameters.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_swissmetro_plot_b19_individual_level_parameters.py: 19. Calculation of individual level parameters ============================================== Calculation of the individual level parameters for the model defined in :ref:`plot_b05normal_mixture`. Michel Bierlaire, EPFL Thu Jun 26 2025, 15:55:41 .. GENERATED FROM PYTHON SOURCE LINES 13-19 .. code-block:: Python import sys from IPython.core.display_functions import display from pandas.core.interchange.dataframe_protocol import DataFrame .. GENERATED FROM PYTHON SOURCE LINES 20-21 See the data processing script: :ref:`swissmetro_data`. .. GENERATED FROM PYTHON SOURCE LINES 21-40 .. 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, ) from biogeme.biogeme import BIOGEME from biogeme.expressions import Beta, Draws, MonteCarlo from biogeme.models import logit from biogeme.results_processing import EstimationResults .. GENERATED FROM PYTHON SOURCE LINES 41-42 Parameters. The initial value is irrelevant. .. GENERATED FROM PYTHON SOURCE LINES 42-46 .. code-block:: Python asc_car = Beta('asc_car', 0, None, None, 0) asc_train = Beta('asc_train', 0, None, None, 0) b_cost = Beta('b_cost', 0, None, None, 0) .. GENERATED FROM PYTHON SOURCE LINES 47-49 Define a random parameter, normally distributed, designed to be used for Monte-Carlo simulation. .. GENERATED FROM PYTHON SOURCE LINES 49-53 .. code-block:: Python b_time = Beta('b_time', 0, None, None, 0) b_time_s = Beta('b_time_s', 1, None, None, 0) b_time_rnd = b_time + b_time_s * Draws('b_time_rnd', 'NORMAL') .. GENERATED FROM PYTHON SOURCE LINES 54-55 Retrieve estimation results .. GENERATED FROM PYTHON SOURCE LINES 55-61 .. code-block:: Python result_file_name = 'saved_results/b05a_normal_mixture.yaml' try: the_estimation_results = EstimationResults.from_yaml_file(filename=result_file_name) except FileNotFoundError: print(f'File {result_file_name} is not available.') sys.exit() .. GENERATED FROM PYTHON SOURCE LINES 62-63 Definition of the utility functions. .. GENERATED FROM PYTHON SOURCE LINES 63-67 .. code-block:: Python v_train = asc_train + b_time_rnd * TRAIN_TT_SCALED + b_cost * TRAIN_COST_SCALED v_swissmetro = b_time_rnd * SM_TT_SCALED + b_cost * SM_COST_SCALED v_car = asc_car + b_time_rnd * CAR_TT_SCALED + b_cost * CAR_CO_SCALED .. GENERATED FROM PYTHON SOURCE LINES 68-69 Associate utility functions with the numbering of alternatives. .. GENERATED FROM PYTHON SOURCE LINES 69-71 .. code-block:: Python v = {1: v_train, 2: v_swissmetro, 3: v_car} .. GENERATED FROM PYTHON SOURCE LINES 72-73 Associate the availability conditions with the alternatives. .. GENERATED FROM PYTHON SOURCE LINES 73-75 .. code-block:: Python av = {1: TRAIN_AV_SP, 2: SM_AV, 3: CAR_AV_SP} .. GENERATED FROM PYTHON SOURCE LINES 76-77 Conditional on b_time_rnd, we have a logit model (called the kernel). .. GENERATED FROM PYTHON SOURCE LINES 77-79 .. code-block:: Python prob_chosen = logit(v, av, CHOICE) .. GENERATED FROM PYTHON SOURCE LINES 80-81 Numerator and denominator of the formula for individual parameters. .. GENERATED FROM PYTHON SOURCE LINES 81-84 .. code-block:: Python numerator = MonteCarlo(b_time_rnd * prob_chosen) denominator = MonteCarlo(prob_chosen) .. GENERATED FROM PYTHON SOURCE LINES 85-91 .. code-block:: Python simulate = { 'Numerator': numerator, 'Denominator': denominator, 'Choice': CHOICE, } .. GENERATED FROM PYTHON SOURCE LINES 92-97 .. code-block:: Python biosim = BIOGEME(database, simulate, number_of_draws=10_000) sim: DataFrame = biosim.simulate(the_estimation_results.get_beta_values()) sim['Individual-level parameters'] = sim['Numerator'] / sim['Denominator'] display(sim) .. rst-class:: sphx-glr-script-out .. code-block:: none Numerator Denominator Choice Individual-level parameters 0 -1.717219 0.637238 2.0 -2.694783 1 -1.782196 0.665118 2.0 -2.679519 2 -1.660481 0.609341 2.0 -2.725043 3 -1.090839 0.438512 2.0 -2.487591 4 -1.661974 0.635237 2.0 -2.616306 ... ... ... ... ... 6763 -0.214293 0.161875 1.0 -1.323815 6764 -0.199393 0.162637 1.0 -1.225997 6765 -0.170228 0.145554 1.0 -1.169520 6766 -0.117869 0.134167 1.0 -0.878518 6767 -0.228289 0.171748 1.0 -1.329209 [6768 rows x 4 columns] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 29.210 seconds) .. _sphx_glr_download_auto_examples_swissmetro_plot_b19_individual_level_parameters.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_b19_individual_level_parameters.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_b19_individual_level_parameters.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_b19_individual_level_parameters.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_