.. 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 `_ Michel Bierlaire, EPFL Sat Jun 28 2025, 20:58:34 .. GENERATED FROM PYTHON SOURCE LINES 16-28 .. 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.expressions import Derive from biogeme.models import nested from biogeme.results_processing import EstimationResults from scenarios import CostCarCHF, MarginalCostPT, TimeCar, TimePT, scenario .. GENERATED FROM PYTHON SOURCE LINES 29-31 Obtain the specification for the default scenario The definition of the scenarios is available in :ref:`scenarios`. .. GENERATED FROM PYTHON SOURCE LINES 31-33 .. code-block:: Python v, nests, _, _ = scenario() .. GENERATED FROM PYTHON SOURCE LINES 34-35 Obtain the expression for the choice probability of each alternative. .. GENERATED FROM PYTHON SOURCE LINES 35-39 .. code-block:: Python prob_pt = nested(v, None, nests, 0) prob_car = nested(v, None, nests, 1) prob_sm = nested(v, None, nests, 2) .. GENERATED FROM PYTHON SOURCE LINES 40-42 Calculation of the cross elasticities. We use the 'Derive' operator to calculate the derivatives. .. GENERATED FROM PYTHON SOURCE LINES 42-47 .. 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 48-49 Formulas to simulate. .. GENERATED FROM PYTHON SOURCE LINES 49-60 .. 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 61-62 Read the data .. GENERATED FROM PYTHON SOURCE LINES 62-64 .. code-block:: Python database = read_data() .. GENERATED FROM PYTHON SOURCE LINES 65-66 Create the Biogeme object. .. GENERATED FROM PYTHON SOURCE LINES 66-68 .. code-block:: Python the_biogeme = BIOGEME(database, simulate) .. GENERATED FROM PYTHON SOURCE LINES 69-70 Read the estimation results from the file .. GENERATED FROM PYTHON SOURCE LINES 70-80 .. 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 81-83 `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 83-86 .. 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.893779 0.519163 ... 0.112615 0.000000 1 0.868674 0.560879 ... 0.054624 0.052002 2 0.868674 0.875042 ... 0.035450 0.206904 3 0.965766 0.813492 ... 0.077716 0.039795 4 0.868674 0.729611 ... 0.083221 0.179214 ... ... ... ... ... ... 1894 2.053830 0.711088 ... 0.118143 0.216026 1895 0.868674 0.849006 ... 0.166098 0.155008 1896 0.868674 0.688196 ... 0.116714 0.088850 1897 0.965766 0.742196 ... 0.072399 0.182004 1898 0.965766 0.763148 ... 0.081859 0.191706 [1899 rows x 8 columns] .. GENERATED FROM PYTHON SOURCE LINES 87-88 We calculate the aggregate elasticities. .. GENERATED FROM PYTHON SOURCE LINES 90-91 First, the weighted probabilities. .. GENERATED FROM PYTHON SOURCE LINES 91-98 .. 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 99-100 Then the denominators of the aggregate elasticity expressions. .. GENERATED FROM PYTHON SOURCE LINES 100-103 .. code-block:: Python denominator_car = simulated_values['Weighted prob. car'].sum() denominator_pt = simulated_values['Weighted prob. PT'].sum() .. GENERATED FROM PYTHON SOURCE LINES 104-105 And finally the aggregate elasticities themselves. .. GENERATED FROM PYTHON SOURCE LINES 107-108 Elasticity of car with respect to public transportation travel time. .. GENERATED FROM PYTHON SOURCE LINES 108-117 .. 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.105 .. GENERATED FROM PYTHON SOURCE LINES 118-119 Elasticity of car with respect to public transportation travel cost. .. GENERATED FROM PYTHON SOURCE LINES 119-128 .. 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.125 .. GENERATED FROM PYTHON SOURCE LINES 129-130 Elasticity of public transportatiom with respect to car travel time. .. GENERATED FROM PYTHON SOURCE LINES 130-139 .. 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.094 .. GENERATED FROM PYTHON SOURCE LINES 140-141 Elasticity of public transportatiom with respect to car travel cost. .. GENERATED FROM PYTHON SOURCE LINES 141-150 .. 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.201 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 3.487 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 `_