.. 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 `_ :author: Michel Bierlaire, EPFL :date: Wed Apr 12 21:00:10 2023 .. GENERATED FROM PYTHON SOURCE LINES 16-25 .. code-block:: default import sys import biogeme.biogeme as bio import biogeme.exceptions as excep from biogeme import models import biogeme.results as res from optima_data import database, normalized_weight from scenarios import scenario .. GENERATED FROM PYTHON SOURCE LINES 26-28 Obtain the specification for the default scenario The definition of the scenarios is available in :ref:`scenarios`. .. GENERATED FROM PYTHON SOURCE LINES 28-30 .. code-block:: default V, nests, _, MarginalCostPT = scenario() .. GENERATED FROM PYTHON SOURCE LINES 31-32 Obtain the expression for the choice probability of the public transportation. .. GENERATED FROM PYTHON SOURCE LINES 32-34 .. code-block:: default prob_PT = models.nested(V, None, nests, 0) .. GENERATED FROM PYTHON SOURCE LINES 35-37 We investigate a scenario where the price for public transportation increases by 20%. We extract the corresponding scenario. .. GENERATED FROM PYTHON SOURCE LINES 37-40 .. code-block:: default V_after, _, _, MarginalCostPT_after = scenario(factor=1.2) prob_PT_after = models.nested(V_after, None, nests, 0) .. GENERATED FROM PYTHON SOURCE LINES 41-42 Disaggregate elasticities .. GENERATED FROM PYTHON SOURCE LINES 42-48 .. code-block:: default direct_elas_pt = ( (prob_PT_after - prob_PT) * MarginalCostPT / (prob_PT * (MarginalCostPT_after - MarginalCostPT)) ) .. GENERATED FROM PYTHON SOURCE LINES 49-50 Formulas to simulate. .. GENERATED FROM PYTHON SOURCE LINES 50-56 .. code-block:: default simulate = { 'weight': normalized_weight, 'Prob. PT': prob_PT, 'direct_elas_pt': direct_elas_pt, } .. GENERATED FROM PYTHON SOURCE LINES 57-58 Create the Biogeme object. .. GENERATED FROM PYTHON SOURCE LINES 58-60 .. code-block:: default the_biogeme = bio.BIOGEME(database, simulate) .. GENERATED FROM PYTHON SOURCE LINES 61-62 Read the estimation results from the file .. GENERATED FROM PYTHON SOURCE LINES 62-70 .. code-block:: default try: results = res.bioResults(pickleFile='saved_results/b02estimation.pickle') except excep.BiogemeError: sys.exit( 'Run first the script b02estimation.py in order to generate ' 'the file b02estimation.pickle.' ) .. GENERATED FROM PYTHON SOURCE LINES 71-73 `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 73-76 .. code-block:: default simulated_values = the_biogeme.simulate(results.getBetaValues()) simulated_values .. raw:: html
weight Prob. PT direct_elas_pt
0 0.886023 0.476807 0.000000
2 0.861136 0.238681 -0.212447
3 0.861136 0.119136 -1.362762
4 0.957386 0.051136 -0.941739
5 0.861136 0.257192 -0.508224
... ... ... ...
2259 2.036009 0.285872 -0.514608
2261 0.861136 0.149688 -0.819720
2262 0.861136 0.205901 -0.407408
2263 0.957386 0.219850 -0.667577
2264 0.957386 0.209149 -0.727399

1906 rows × 3 columns



.. GENERATED FROM PYTHON SOURCE LINES 77-78 We calculate the aggregate elasticities. .. GENERATED FROM PYTHON SOURCE LINES 80-81 First, the weighted probabilities. .. GENERATED FROM PYTHON SOURCE LINES 81-85 .. code-block:: default simulated_values['Weighted prob. PT'] = ( simulated_values['weight'] * simulated_values['Prob. PT'] ) .. GENERATED FROM PYTHON SOURCE LINES 86-87 Then the denominator of the aggregate elasticity expression. .. GENERATED FROM PYTHON SOURCE LINES 87-89 .. code-block:: default denominator_pt = simulated_values['Weighted prob. PT'].sum() .. GENERATED FROM PYTHON SOURCE LINES 90-91 And finally the aggregate elasticities themselves. .. GENERATED FROM PYTHON SOURCE LINES 91-101 .. code-block:: default 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.299 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.456 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-python :download:`Download Python source code: plot_b08arc_elasticities.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_b08arc_elasticities.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_