.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/indicators/plot_b03simulation.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_indicators_plot_b03simulation.py: Simulation of a choice model ============================ We use an estimated model to perform various simulations. Michel Bierlaire, EPFL Sat Jun 28 2025, 16:56:26 .. GENERATED FROM PYTHON SOURCE LINES 11-24 .. code-block:: Python import sys import time import pandas as pd from scenarios import scenario from biogeme.biogeme import BIOGEME from biogeme.data.optima import normalized_weight, read_data from biogeme.jax_calculator import get_value_c from biogeme.models import nested from biogeme.results_processing import EstimationResults .. GENERATED FROM PYTHON SOURCE LINES 25-27 Obtain the specification for the default scenario. The definition of the scenarios is available in ``scenarios.py``. .. GENERATED FROM PYTHON SOURCE LINES 27-33 .. code-block:: Python v, nests, _, _ = scenario() v_pt = v[0] v_car = v[1] v_sm = v[2] .. GENERATED FROM PYTHON SOURCE LINES 34-35 Obtain the expression for the choice probability of each alternative. .. GENERATED FROM PYTHON SOURCE LINES 35-51 .. code-block:: Python prob_pt = nested(v, None, nests, 0) prob_car = nested(v, None, nests, 1) prob_sm = nested(v, None, nests, 2) # Read the estimation results from the file try: results = EstimationResults.from_yaml_file( filename='saved_results/b02estimation.yaml' ) except FileNotFoundError: sys.exit( 'Run first the script plot_b02estimation.py ' 'in order to generate the ' 'file b02estimation.yaml.' ) .. GENERATED FROM PYTHON SOURCE LINES 52-53 Read the database .. GENERATED FROM PYTHON SOURCE LINES 53-55 .. code-block:: Python database = read_data() .. GENERATED FROM PYTHON SOURCE LINES 56-59 We now simulate various expressions on the database, and store the results in a Pandas dataframe. %% .. GENERATED FROM PYTHON SOURCE LINES 59-115 .. code-block:: Python start_time = time.time() simulate_formulas = { 'weight': get_value_c( expression=normalized_weight, betas=results.get_beta_values(), database=database, numerically_safe=False, use_jit=True, ), 'Utility PT': get_value_c( expression=v_pt, betas=results.get_beta_values(), database=database, numerically_safe=False, use_jit=True, ), 'Utility car': get_value_c( expression=v_car, betas=results.get_beta_values(), database=database, numerically_safe=False, use_jit=True, ), 'Utility SM': get_value_c( expression=v_sm, betas=results.get_beta_values(), database=database, numerically_safe=False, use_jit=True, ), 'Prob. PT': get_value_c( expression=prob_pt, betas=results.get_beta_values(), database=database, numerically_safe=False, use_jit=True, ), 'Prob. car': get_value_c( expression=prob_car, betas=results.get_beta_values(), database=database, numerically_safe=False, use_jit=True, ), 'Prob. SM': get_value_c( expression=prob_sm, betas=results.get_beta_values(), database=database, numerically_safe=False, use_jit=True, ), } simulated_values = pd.DataFrame.from_dict(simulate_formulas) end_time = time.time() .. GENERATED FROM PYTHON SOURCE LINES 116-118 .. code-block:: Python print(f'--- Execution time without Biogeme: {end_time - start_time:.2f} seconds ---') .. rst-class:: sphx-glr-script-out .. code-block:: none --- Execution time without Biogeme: 0.40 seconds --- .. GENERATED FROM PYTHON SOURCE LINES 119-123 We now perform the same simulation using Biogeme. The results are identical, but the syntax is simpler and the execution time is a little bit faster. Indeed, Biogeme recycles calculations performed for one expression for the other expressions. .. GENERATED FROM PYTHON SOURCE LINES 125-126 A dictionary with the requested expression must be provided to Biogeme .. GENERATED FROM PYTHON SOURCE LINES 126-136 .. code-block:: Python simulate = { 'weight': normalized_weight, 'Utility PT': v_pt, 'Utility car': v_car, 'Utility SM': v_sm, 'Prob. PT': prob_pt, 'Prob. car': prob_car, 'Prob. SM': prob_sm, } .. GENERATED FROM PYTHON SOURCE LINES 137-143 .. code-block:: Python start_time = time.time() the_biogeme = BIOGEME(database, simulate) the_betas = results.get_beta_values() biogeme_simulation = the_biogeme.simulate(results.get_beta_values()) end_time = time.time() .. GENERATED FROM PYTHON SOURCE LINES 144-148 .. code-block:: Python print( f'--- Execution time with Biogeme: {time.time() - start_time:.2f} seconds ---' ) .. rst-class:: sphx-glr-script-out .. code-block:: none --- Execution time with Biogeme: 0.19 seconds --- .. GENERATED FROM PYTHON SOURCE LINES 149-150 Let's print the two results, to show that they are identical .. GENERATED FROM PYTHON SOURCE LINES 152-153 Without Biogeme .. GENERATED FROM PYTHON SOURCE LINES 153-155 .. code-block:: Python print(simulated_values) .. rst-class:: sphx-glr-script-out .. code-block:: none weight Utility PT Utility car ... Prob. PT Prob. car Prob. SM 0 0.893779 -0.234895 -0.156281 ... 0.479432 0.519164 0.001404 1 0.868674 -0.442324 0.196006 ... 0.241053 0.560874 0.198073 2 0.868674 -2.021453 -0.047998 ... 0.119892 0.875041 0.005067 3 0.965766 -2.293246 0.027488 ... 0.051242 0.813494 0.135264 4 0.868674 -1.010867 0.008470 ... 0.259615 0.729612 0.010774 ... ... ... ... ... ... ... ... 1894 2.053830 -1.156823 -0.256048 ... 0.288868 0.711088 0.000044 1895 0.868674 -2.145009 -0.412530 ... 0.149700 0.849006 0.001294 1896 0.868674 -0.998190 0.065744 ... 0.205937 0.688198 0.105866 1897 0.965766 -1.145826 0.009640 ... 0.222027 0.742198 0.035775 1898 0.965766 -1.292910 -0.048872 ... 0.211590 0.763149 0.025261 [1899 rows x 7 columns] .. GENERATED FROM PYTHON SOURCE LINES 156-157 With Biogeme .. GENERATED FROM PYTHON SOURCE LINES 157-158 .. code-block:: Python print(biogeme_simulation) .. rst-class:: sphx-glr-script-out .. code-block:: none weight Utility PT Utility car ... Prob. PT Prob. car Prob. SM 0 0.893779 -0.234895 -0.156281 ... 0.479432 0.519164 0.001404 1 0.868674 -0.442324 0.196006 ... 0.241053 0.560874 0.198073 2 0.868674 -2.021453 -0.047998 ... 0.119892 0.875041 0.005067 3 0.965766 -2.293246 0.027488 ... 0.051242 0.813494 0.135264 4 0.868674 -1.010867 0.008470 ... 0.259615 0.729612 0.010774 ... ... ... ... ... ... ... ... 1894 2.053830 -1.156823 -0.256048 ... 0.288868 0.711088 0.000044 1895 0.868674 -2.145009 -0.412530 ... 0.149700 0.849006 0.001294 1896 0.868674 -0.998190 0.065744 ... 0.205937 0.688198 0.105866 1897 0.965766 -1.145826 0.009640 ... 0.222027 0.742198 0.035775 1898 0.965766 -1.292910 -0.048872 ... 0.211590 0.763149 0.025261 [1899 rows x 7 columns] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.730 seconds) .. _sphx_glr_download_auto_examples_indicators_plot_b03simulation.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_b03simulation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_b03simulation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_b03simulation.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_