.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/indicators/plot_b07cross_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_b07cross_elasticities.py: Cross point elasticities ======================== We use a previously estimated nested logit model and calculate disaggregate and aggregate cross point 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:58 2023 .. GENERATED FROM PYTHON SOURCE LINES 17-38 .. code-block:: Python import sys from IPython.core.display_functions import display import biogeme.biogeme as bio from biogeme import models from biogeme.exceptions import BiogemeError import biogeme.results as res from biogeme.expressions import Derive from biogeme.data.optima import read_data, normalized_weight from scenarios import ( scenario, TimePT, TimeCar, MarginalCostPT, CostCarCHF, ) .. GENERATED FROM PYTHON SOURCE LINES 39-41 Obtain the specification for the default scenario The definition of the scenarios is available in :ref:`scenarios`. .. GENERATED FROM PYTHON SOURCE LINES 41-43 .. code-block:: Python V, nests, _, _ = scenario() .. GENERATED FROM PYTHON SOURCE LINES 44-45 Obtain the expression for the choice probability of each alternative. .. GENERATED FROM PYTHON SOURCE LINES 45-49 .. 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) .. GENERATED FROM PYTHON SOURCE LINES 50-52 Calculation of the cross elasticities. We use the 'Derive' operator to calculate the derivatives. .. GENERATED FROM PYTHON SOURCE LINES 52-57 .. code-block:: Python cross_elas_pt_time = Derive(prob_PT, 'TimeCar') * TimeCar / prob_PT cross_elas_pt_cost = Derive(prob_PT, 'CostCarCHF') * CostCarCHF / prob_PT cross_elas_car_time = Derive(prob_CAR, 'TimePT') * TimePT / prob_CAR cross_elas_car_cost = Derive(prob_CAR, 'MarginalCostPT') * MarginalCostPT / prob_CAR .. GENERATED FROM PYTHON SOURCE LINES 58-59 Formulas to simulate. .. GENERATED FROM PYTHON SOURCE LINES 59-70 .. code-block:: Python simulate = { 'weight': normalized_weight, 'Prob. car': prob_CAR, 'Prob. public transportation': prob_PT, 'Prob. slow modes': prob_SM, 'cross_elas_pt_time': cross_elas_pt_time, 'cross_elas_pt_cost': cross_elas_pt_cost, 'cross_elas_car_time': cross_elas_car_time, 'cross_elas_car_cost': cross_elas_car_cost, } .. GENERATED FROM PYTHON SOURCE LINES 71-72 Read the data .. GENERATED FROM PYTHON SOURCE LINES 72-74 .. code-block:: Python database = read_data() .. GENERATED FROM PYTHON SOURCE LINES 75-76 Create the Biogeme object. .. GENERATED FROM PYTHON SOURCE LINES 76-78 .. code-block:: Python the_biogeme = bio.BIOGEME(database, simulate) .. GENERATED FROM PYTHON SOURCE LINES 79-80 Read the estimation results from the file .. GENERATED FROM PYTHON SOURCE LINES 80-88 .. code-block:: Python try: results = res.bioResults(pickle_file='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 89-91 `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 91-94 .. 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. car ... cross_elas_car_time cross_elas_car_cost 0 0.886023 0.508061 ... 0.088764 0.000000 2 0.861136 0.574899 ... 0.039943 0.055117 3 0.861136 0.888093 ... 0.023328 0.197357 4 0.957386 0.790872 ... 0.051348 0.033850 5 0.861136 0.733898 ... 0.062363 0.194662 ... ... ... ... ... ... 2259 2.036009 0.710676 ... 0.091112 0.241483 2261 0.861136 0.849664 ... 0.142624 0.171358 2262 0.861136 0.688689 ... 0.092332 0.090492 2263 0.957386 0.747552 ... 0.051636 0.188157 2264 0.957386 0.767021 ... 0.059141 0.200758 [1906 rows x 8 columns] .. GENERATED FROM PYTHON SOURCE LINES 95-96 We calculate the aggregate elasticities. .. GENERATED FROM PYTHON SOURCE LINES 98-99 First, the weighted probabilities. .. GENERATED FROM PYTHON SOURCE LINES 99-106 .. code-block:: Python simulated_values['Weighted prob. car'] = ( simulated_values['weight'] * simulated_values['Prob. car'] ) simulated_values['Weighted prob. PT'] = ( simulated_values['weight'] * simulated_values['Prob. public transportation'] ) .. GENERATED FROM PYTHON SOURCE LINES 107-108 Then the denominators of the aggregate elasticity expressions. .. GENERATED FROM PYTHON SOURCE LINES 108-111 .. code-block:: Python denominator_car = simulated_values['Weighted prob. car'].sum() denominator_pt = simulated_values['Weighted prob. PT'].sum() .. GENERATED FROM PYTHON SOURCE LINES 112-113 And finally the aggregate elasticities themselves. .. GENERATED FROM PYTHON SOURCE LINES 115-116 Elasticity of car with respect to public transportation travel time. .. GENERATED FROM PYTHON SOURCE LINES 116-125 .. code-block:: Python cross_elas_term_car_time = ( simulated_values['Weighted prob. car'] * simulated_values['cross_elas_car_time'] / denominator_car ).sum() print( f'Aggregate cross elasticity of car wrt PT time: ' f'{cross_elas_term_car_time:.3g}' ) .. rst-class:: sphx-glr-script-out .. code-block:: none Aggregate cross elasticity of car wrt PT time: 0.0842 .. GENERATED FROM PYTHON SOURCE LINES 126-127 Elasticity of car with respect to public transportation travel cost. .. GENERATED FROM PYTHON SOURCE LINES 127-136 .. code-block:: Python cross_elas_term_car_cost = ( simulated_values['Weighted prob. car'] * simulated_values['cross_elas_car_cost'] / denominator_car ).sum() print( f'Aggregate cross elasticity of car wrt PT cost: ' f'{cross_elas_term_car_cost:.3g}' ) .. rst-class:: sphx-glr-script-out .. code-block:: none Aggregate cross elasticity of car wrt PT cost: 0.132 .. GENERATED FROM PYTHON SOURCE LINES 137-138 Elasticity of public transportatiom with respect to car travel time. .. GENERATED FROM PYTHON SOURCE LINES 138-147 .. code-block:: Python cross_elas_term_pt_time = ( simulated_values['Weighted prob. PT'] * simulated_values['cross_elas_pt_time'] / denominator_pt ).sum() print( f'Aggregate cross elasticity of PT wrt car time: ' f'{cross_elas_term_pt_time:.3g}' ) .. rst-class:: sphx-glr-script-out .. code-block:: none Aggregate cross elasticity of PT wrt car time: 0.0767 .. GENERATED FROM PYTHON SOURCE LINES 148-149 Elasticity of public transportatiom with respect to car travel cost. .. GENERATED FROM PYTHON SOURCE LINES 149-158 .. code-block:: Python cross_elas_term_pt_cost = ( simulated_values['Weighted prob. PT'] * simulated_values['cross_elas_pt_cost'] / denominator_pt ).sum() print( f'Aggregate cross direct elasticity of PT wrt car cost: ' f'{cross_elas_term_pt_cost:.3g}' ) .. rst-class:: sphx-glr-script-out .. code-block:: none Aggregate cross direct elasticity of PT wrt car cost: 0.221 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.587 seconds) .. _sphx_glr_download_auto_examples_indicators_plot_b07cross_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_b07cross_elasticities.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_b07cross_elasticities.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_b07cross_elasticities.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_