.. 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. :author: Michel Bierlaire, EPFL :date: Wed Apr 12 21:04:33 2023 .. GENERATED FROM PYTHON SOURCE LINES 12-23 .. code-block:: Python import sys import time import pandas as pd from biogeme import models import biogeme.biogeme as bio import biogeme.exceptions as excep import biogeme.results as res from biogeme.data.optima import read_data, normalized_weight from scenarios import scenario .. GENERATED FROM PYTHON SOURCE LINES 24-26 Obtain the specification for the default scenario. The definition of the scenarios is available in :ref:`scenarios`. .. GENERATED FROM PYTHON SOURCE LINES 26-32 .. code-block:: Python V, nests, _, _ = scenario() V_PT = V[0] V_CAR = V[1] V_SM = V[2] .. GENERATED FROM PYTHON SOURCE LINES 33-34 Obtain the expression for the choice probability of each alternative. .. GENERATED FROM PYTHON SOURCE LINES 34-48 .. code-block:: Python prob_PT = models.nested(V, None, nests, 0) prob_CAR = models.nested(V, None, nests, 1) prob_SM = models.nested(V, None, nests, 2) # Read the estimation results from the file try: results = res.bioResults(pickle_file='saved_results/b02estimation.pickle') except excep.BiogemeError: sys.exit( 'Run first the script b02simulation.py ' 'in order to generate the ' 'file b02estimation.pickle.' ) .. GENERATED FROM PYTHON SOURCE LINES 49-50 Read the database .. GENERATED FROM PYTHON SOURCE LINES 50-52 .. code-block:: Python database = read_data() .. GENERATED FROM PYTHON SOURCE LINES 53-56 We now simulate various expressions on the database, and store the results in a Pandas dataframe. %% .. GENERATED FROM PYTHON SOURCE LINES 56-81 .. code-block:: Python start_time = time.time() simulate_formulas = { 'weight': normalized_weight.get_value_c( betas=results.get_beta_values(), database=database, prepare_ids=True ), 'Utility PT': V_PT.get_value_c( betas=results.get_beta_values(), database=database, prepare_ids=True ), 'Utility car': V_CAR.get_value_c( betas=results.get_beta_values(), database=database, prepare_ids=True ), 'Utility SM': V_SM.get_value_c( betas=results.get_beta_values(), database=database, prepare_ids=True ), 'Prob. PT': prob_PT.get_value_c( betas=results.get_beta_values(), database=database, prepare_ids=True ), 'Prob. car': prob_CAR.get_value_c( betas=results.get_beta_values(), database=database, prepare_ids=True ), 'Prob. SM': prob_SM.get_value_c( betas=results.get_beta_values(), database=database, prepare_ids=True ), } .. GENERATED FROM PYTHON SOURCE LINES 82-86 .. code-block:: Python simulated_values = pd.DataFrame.from_dict( simulate_formulas, ) .. GENERATED FROM PYTHON SOURCE LINES 87-92 .. code-block:: Python print( f'--- Execution time with getValue_c: ' f'{time.time() - start_time:.2f} seconds ---' ) .. rst-class:: sphx-glr-script-out .. code-block:: none --- Execution time with getValue_c: 0.47 seconds --- .. GENERATED FROM PYTHON SOURCE LINES 93-97 We now perform the same simulation using Biogeme. The results are identical, but the execution time is faster. Indeed, Biogeme recycles calculations performed for one expression for the other expressions. .. GENERATED FROM PYTHON SOURCE LINES 99-100 A dictionary with the requested expression must be provided to Biogeme .. GENERATED FROM PYTHON SOURCE LINES 100-110 .. 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 111-115 .. code-block:: Python start_time = time.time() the_biogeme = bio.BIOGEME(database, simulate) biogeme_simulation = the_biogeme.simulate(results.get_beta_values()) .. GENERATED FROM PYTHON SOURCE LINES 116-121 .. code-block:: Python print( f'--- Execution time with Biogeme: ' f'{time.time() - start_time:.2f} seconds ---' ) .. rst-class:: sphx-glr-script-out .. code-block:: none --- Execution time with Biogeme: 0.36 seconds --- .. GENERATED FROM PYTHON SOURCE LINES 122-123 Let's print the two results, to show that they are identical .. GENERATED FROM PYTHON SOURCE LINES 125-126 Without Biogeme .. GENERATED FROM PYTHON SOURCE LINES 126-128 .. 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.886023 -0.180894 -0.147283 ... 0.490700 0.508061 0.001239 1 0.861136 -0.415315 0.221097 ... 0.228886 0.574899 0.196216 2 0.861136 -2.154157 -0.035018 ... 0.102446 0.888093 0.009460 3 0.957386 -2.181930 0.055095 ... 0.039047 0.790872 0.170080 4 0.861136 -1.017444 0.025147 ... 0.252618 0.733898 0.013483 ... ... ... ... ... ... ... ... 1901 2.036009 -1.149760 -0.250998 ... 0.289274 0.710676 0.000051 1902 0.861136 -2.117915 -0.378399 ... 0.148251 0.849664 0.002085 1903 0.861136 -0.973017 0.093820 ... 0.187894 0.688689 0.123416 1904 0.957386 -1.166184 0.030280 ... 0.205622 0.747552 0.046825 1905 0.957386 -1.309326 -0.031210 ... 0.198499 0.767021 0.034480 [1906 rows x 7 columns] .. GENERATED FROM PYTHON SOURCE LINES 129-130 With Biogeme .. GENERATED FROM PYTHON SOURCE LINES 130-131 .. 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.886023 -0.180894 -0.147283 ... 0.490700 0.508061 0.001239 2 0.861136 -0.415315 0.221097 ... 0.228886 0.574899 0.196216 3 0.861136 -2.154157 -0.035018 ... 0.102446 0.888093 0.009460 4 0.957386 -2.181930 0.055095 ... 0.039047 0.790872 0.170080 5 0.861136 -1.017444 0.025147 ... 0.252618 0.733898 0.013483 ... ... ... ... ... ... ... ... 2259 2.036009 -1.149760 -0.250998 ... 0.289274 0.710676 0.000051 2261 0.861136 -2.117915 -0.378399 ... 0.148251 0.849664 0.002085 2262 0.861136 -0.973017 0.093820 ... 0.187894 0.688689 0.123416 2263 0.957386 -1.166184 0.030280 ... 0.205622 0.747552 0.046825 2264 0.957386 -1.309326 -0.031210 ... 0.198499 0.767021 0.034480 [1906 rows x 7 columns] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.176 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 `_