.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/indicators/plot_b08arc_elasticities.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_b08arc_elasticities.py: Arc elasticities ================ We use a previously estimated nested logit model and calculate arc elasticities. Details about this example are available in Section 3 of `Bierlaire (2018) Calculating indicators with PandasBiogeme `_ Michel Bierlaire, EPFL Sat Jun 28 2025, 20:59:31 .. GENERATED FROM PYTHON SOURCE LINES 15-26 .. code-block:: Python import sys from IPython.core.display_functions import display from biogeme.biogeme import BIOGEME from biogeme.data.optima import normalized_weight, read_data from biogeme.models import nested from biogeme.results_processing import EstimationResults from scenarios import scenario .. GENERATED FROM PYTHON SOURCE LINES 27-29 Obtain the specification for the default scenario The definition of the scenarios is available in :ref:`scenarios`. .. GENERATED FROM PYTHON SOURCE LINES 29-31 .. code-block:: Python v, nests, _, MarginalCostPT = scenario() .. GENERATED FROM PYTHON SOURCE LINES 32-33 Obtain the expression for the choice probability of the public transportation. .. GENERATED FROM PYTHON SOURCE LINES 33-35 .. code-block:: Python prob_pt = nested(v, None, nests, 0) .. GENERATED FROM PYTHON SOURCE LINES 36-38 We investigate a scenario where the price for public transportation increases by 20%. We extract the corresponding scenario. .. GENERATED FROM PYTHON SOURCE LINES 38-41 .. code-block:: Python v_after, _, _, marginal_cost_pt_after = scenario(factor=1.2) prob_pt_after = nested(v_after, None, nests, 0) .. GENERATED FROM PYTHON SOURCE LINES 42-43 Disaggregate elasticities .. GENERATED FROM PYTHON SOURCE LINES 43-49 .. code-block:: Python direct_elas_pt = ( (prob_pt_after - prob_pt) * MarginalCostPT / (prob_pt * (marginal_cost_pt_after - MarginalCostPT)) ) .. GENERATED FROM PYTHON SOURCE LINES 50-51 Formulas to simulate. .. GENERATED FROM PYTHON SOURCE LINES 51-57 .. code-block:: Python simulate = { 'weight': normalized_weight, 'Prob. PT': prob_pt, 'direct_elas_pt': direct_elas_pt, } .. GENERATED FROM PYTHON SOURCE LINES 58-59 Read the data .. GENERATED FROM PYTHON SOURCE LINES 59-61 .. code-block:: Python database = read_data() .. GENERATED FROM PYTHON SOURCE LINES 62-63 Create the Biogeme object. .. GENERATED FROM PYTHON SOURCE LINES 63-65 .. code-block:: Python the_biogeme = BIOGEME(database, simulate) .. GENERATED FROM PYTHON SOURCE LINES 66-67 Read the estimation results from the file .. GENERATED FROM PYTHON SOURCE LINES 67-77 .. code-block:: Python try: results = EstimationResults.from_yaml_file( filename='saved_results/b02estimation.yaml' ) except FileNotFoundError: sys.exit( 'Run first the script b02estimation.py in order to generate ' 'the file b02estimation.yaml.' ) .. GENERATED FROM PYTHON SOURCE LINES 78-80 `simulated_values` is a Panda dataframe with the same number of rows as the database, and as many columns as formulas to simulate. .. GENERATED FROM PYTHON SOURCE LINES 80-83 .. code-block:: Python simulated_values = the_biogeme.simulate(results.get_beta_values()) display(simulated_values) .. rst-class:: sphx-glr-script-out .. code-block:: none weight Prob. PT direct_elas_pt 0 0.893779 0.479431 NaN 1 0.868674 0.241046 -0.212075 2 0.868674 0.119893 -1.366282 3 0.965766 0.051229 -0.942913 4 0.868674 0.259609 -0.508500 ... ... ... ... 1894 2.053830 0.288859 -0.514652 1895 0.868674 0.149688 -0.822786 1896 0.868674 0.205928 -0.408671 1897 0.965766 0.222025 -0.667457 1898 0.965766 0.211584 -0.727161 [1899 rows x 3 columns] .. GENERATED FROM PYTHON SOURCE LINES 84-85 We calculate the aggregate elasticities. .. GENERATED FROM PYTHON SOURCE LINES 87-88 First, the weighted probabilities. .. GENERATED FROM PYTHON SOURCE LINES 88-92 .. code-block:: Python simulated_values['Weighted prob. PT'] = ( simulated_values['weight'] * simulated_values['Prob. PT'] ) .. GENERATED FROM PYTHON SOURCE LINES 93-94 Then the denominator of the aggregate elasticity expression. .. GENERATED FROM PYTHON SOURCE LINES 94-96 .. code-block:: Python denominator_pt = simulated_values['Weighted prob. PT'].sum() .. GENERATED FROM PYTHON SOURCE LINES 97-98 And finally the aggregate elasticities themselves. .. GENERATED FROM PYTHON SOURCE LINES 98-108 .. code-block:: Python direct_elas_pt = ( simulated_values['Weighted prob. PT'] * simulated_values['direct_elas_pt'] / denominator_pt ).sum() print( f'Aggregate direct arc elasticity of public transportation wrt cost: ' f'{direct_elas_pt:.3g}' ) .. rst-class:: sphx-glr-script-out .. code-block:: none Aggregate direct arc elasticity of public transportation wrt cost: -0.303 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.505 seconds) .. _sphx_glr_download_auto_examples_indicators_plot_b08arc_elasticities.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_b08arc_elasticities.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_b08arc_elasticities.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_b08arc_elasticities.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_