.. 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-22 .. code-block:: default 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 optima_data import database, normalized_weight from scenarios import scenario .. GENERATED FROM PYTHON SOURCE LINES 23-25 Obtain the specification for the default scenario. The definition of the scenarios is available in :ref:`scenarios`. .. GENERATED FROM PYTHON SOURCE LINES 25-31 .. code-block:: default V, nests, _, _ = scenario() V_PT = V[0] V_CAR = V[1] V_SM = V[2] .. GENERATED FROM PYTHON SOURCE LINES 32-33 Obtain the expression for the choice probability of each alternative. .. GENERATED FROM PYTHON SOURCE LINES 33-47 .. code-block:: default 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(pickleFile='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 48-50 We now simulate various expressions on the database, and store the results in a Pandas dataframe. .. GENERATED FROM PYTHON SOURCE LINES 52-77 .. code-block:: default start_time = time.time() simulate_formulas = { 'weight': normalized_weight.getValue_c( betas=results.getBetaValues(), database=database, prepareIds=True ), 'Utility PT': V_PT.getValue_c( betas=results.getBetaValues(), database=database, prepareIds=True ), 'Utility car': V_CAR.getValue_c( betas=results.getBetaValues(), database=database, prepareIds=True ), 'Utility SM': V_SM.getValue_c( betas=results.getBetaValues(), database=database, prepareIds=True ), 'Prob. PT': prob_PT.getValue_c( betas=results.getBetaValues(), database=database, prepareIds=True ), 'Prob. car': prob_CAR.getValue_c( betas=results.getBetaValues(), database=database, prepareIds=True ), 'Prob. SM': prob_SM.getValue_c( betas=results.getBetaValues(), database=database, prepareIds=True ), } .. GENERATED FROM PYTHON SOURCE LINES 78-82 .. code-block:: default simulated_values = pd.DataFrame.from_dict( simulate_formulas, ) .. GENERATED FROM PYTHON SOURCE LINES 83-88 .. code-block:: default 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.32 seconds --- .. GENERATED FROM PYTHON SOURCE LINES 89-93 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 95-96 A dictionary with the requested expression must be provided to Biogeme .. GENERATED FROM PYTHON SOURCE LINES 96-106 .. code-block:: default 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 107-111 .. code-block:: default start_time = time.time() the_biogeme = bio.BIOGEME(database, simulate) biogeme_simulation = the_biogeme.simulate(results.getBetaValues()) .. GENERATED FROM PYTHON SOURCE LINES 112-117 .. code-block:: default 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.42 seconds --- .. GENERATED FROM PYTHON SOURCE LINES 118-119 Let's print the two results, to show that they are identical .. GENERATED FROM PYTHON SOURCE LINES 121-122 Without Biogeme .. GENERATED FROM PYTHON SOURCE LINES 122-124 .. code-block:: default 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.245379 -0.156238 ... 0.476807 0.521791 0.001402 1 0.861136 -0.451576 0.198134 ... 0.238681 0.563268 0.198051 2 0.861136 -2.027748 -0.047179 ... 0.119136 0.875824 0.005040 3 0.957386 -2.290720 0.030607 ... 0.051136 0.814110 0.134754 4 0.861136 -1.022414 0.009467 ... 0.257192 0.732059 0.010749 ... ... ... ... ... ... ... ... 1901 2.036009 -1.172087 -0.256681 ... 0.285872 0.714085 0.000043 1902 0.861136 -2.141291 -0.408666 ... 0.149688 0.849040 0.001272 1903 0.861136 -0.996681 0.068780 ... 0.205901 0.689083 0.105016 1904 0.957386 -1.157102 0.010095 ... 0.219850 0.744274 0.035876 1905 0.957386 -1.306555 -0.048721 ... 0.209149 0.765468 0.025383 [1906 rows x 7 columns] .. GENERATED FROM PYTHON SOURCE LINES 125-126 With Biogeme .. GENERATED FROM PYTHON SOURCE LINES 126-127 .. code-block:: default 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.245379 -0.156238 ... 0.476807 0.521791 0.001402 2 0.861136 -0.451576 0.198134 ... 0.238681 0.563268 0.198051 3 0.861136 -2.027748 -0.047179 ... 0.119136 0.875824 0.005040 4 0.957386 -2.290720 0.030607 ... 0.051136 0.814110 0.134754 5 0.861136 -1.022414 0.009467 ... 0.257192 0.732059 0.010749 ... ... ... ... ... ... ... ... 2259 2.036009 -1.172087 -0.256681 ... 0.285872 0.714085 0.000043 2261 0.861136 -2.141291 -0.408666 ... 0.149688 0.849040 0.001272 2262 0.861136 -0.996681 0.068780 ... 0.205901 0.689083 0.105016 2263 0.957386 -1.157102 0.010095 ... 0.219850 0.744274 0.035876 2264 0.957386 -1.306555 -0.048721 ... 0.209149 0.765468 0.025383 [1906 rows x 7 columns] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.777 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-python :download:`Download Python source code: plot_b03simulation.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_b03simulation.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_