.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/indicators/plot_b06point_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_b06point_elasticities.py: Direct point elasticities ========================= We use a previously estimated nested logit model and calculate disaggregate and aggregate direct 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, 19:21:30 .. 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, distance_km, 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 direct elasticities. We use the 'Derive' operator to calculate the derivatives. .. GENERATED FROM PYTHON SOURCE LINES 42-52 .. code-block:: Python direct_elas_pt_time = Derive(prob_pt, 'TimePT') * TimePT / prob_pt direct_elas_pt_cost = Derive(prob_pt, 'MarginalCostPT') * MarginalCostPT / prob_pt direct_elas_car_time = Derive(prob_car, 'TimeCar') * TimeCar / prob_car direct_elas_car_cost = Derive(prob_car, 'CostCarCHF') * CostCarCHF / prob_car direct_elas_sm_dist = Derive(prob_sm, 'distance_km') * distance_km / prob_sm .. GENERATED FROM PYTHON SOURCE LINES 53-54 Formulas to simulate. .. GENERATED FROM PYTHON SOURCE LINES 54-66 .. code-block:: Python simulate = { 'weight': normalized_weight, 'Prob. car': prob_car, 'Prob. public transportation': prob_pt, 'Prob. slow modes': prob_sm, 'direct_elas_pt_time': direct_elas_pt_time, 'direct_elas_pt_cost': direct_elas_pt_cost, 'direct_elas_car_time': direct_elas_car_time, 'direct_elas_car_cost': direct_elas_car_cost, 'direct_elas_sm_dist': direct_elas_sm_dist, } .. GENERATED FROM PYTHON SOURCE LINES 67-68 Read the data .. GENERATED FROM PYTHON SOURCE LINES 68-70 .. code-block:: Python database = read_data() .. GENERATED FROM PYTHON SOURCE LINES 71-72 Create the Biogeme object. .. GENERATED FROM PYTHON SOURCE LINES 72-74 .. code-block:: Python the_biogeme = BIOGEME(database, simulate) .. GENERATED FROM PYTHON SOURCE LINES 75-76 Read the estimation results from the file .. GENERATED FROM PYTHON SOURCE LINES 76-86 .. 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 87-89 `simulated_values` is a Pandas dataframe with the same number of rows as the database, and as many columns as formulas to simulate. .. GENERATED FROM PYTHON SOURCE LINES 89-92 .. 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 ... direct_elas_car_cost direct_elas_sm_dist 0 0.893779 0.519165 ... -0.156968 -6.275368 1 0.868674 0.560868 ... -0.020208 -0.690945 2 0.868674 0.875040 ... -0.030370 -6.230584 3 0.965766 0.813507 ... -0.022260 -1.734141 4 0.868674 0.729616 ... -0.052688 -4.718355 ... ... ... ... ... ... 1894 2.053830 0.711097 ... -0.116747 -10.650385 1895 0.868674 0.849018 ... -0.049505 -8.084431 1896 0.868674 0.688197 ... -0.030267 -1.857448 1897 0.965766 0.742202 ... -0.047084 -3.406664 1898 0.965766 0.763156 ... -0.054497 -3.968513 [1899 rows x 9 columns] .. GENERATED FROM PYTHON SOURCE LINES 93-94 We calculate the aggregate elasticities. .. GENERATED FROM PYTHON SOURCE LINES 96-97 First, the weighted probabilities. .. GENERATED FROM PYTHON SOURCE LINES 97-107 .. 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'] ) simulated_values['Weighted prob. SM'] = ( simulated_values['weight'] * simulated_values['Prob. slow modes'] ) .. GENERATED FROM PYTHON SOURCE LINES 108-109 Then the denominators of the aggregate elasticity expressions. .. GENERATED FROM PYTHON SOURCE LINES 109-113 .. code-block:: Python denominator_car = simulated_values['Weighted prob. car'].sum() denominator_pt = simulated_values['Weighted prob. PT'].sum() denominator_sm = simulated_values['Weighted prob. SM'].sum() .. GENERATED FROM PYTHON SOURCE LINES 114-115 And finally the aggregate elasticities themselves. .. GENERATED FROM PYTHON SOURCE LINES 117-118 Elasticity of car with respect to time. .. GENERATED FROM PYTHON SOURCE LINES 118-129 .. code-block:: Python direct_elas_term_car_time = ( simulated_values['Weighted prob. car'] * simulated_values['direct_elas_car_time'] / denominator_car ).sum() print( f'Aggregate direct point elasticity of car wrt time: ' f'{direct_elas_term_car_time:.3g}' ) .. rst-class:: sphx-glr-script-out .. code-block:: none Aggregate direct point elasticity of car wrt time: -0.0437 .. GENERATED FROM PYTHON SOURCE LINES 130-131 Elasticity of car with respect to cost. .. GENERATED FROM PYTHON SOURCE LINES 131-141 .. code-block:: Python direct_elas_term_car_cost = ( simulated_values['Weighted prob. car'] * simulated_values['direct_elas_car_cost'] / denominator_car ).sum() print( f'Aggregate direct point elasticity of car wrt cost: ' f'{direct_elas_term_car_cost:.3g}' ) .. rst-class:: sphx-glr-script-out .. code-block:: none Aggregate direct point elasticity of car wrt cost: -0.0917 .. GENERATED FROM PYTHON SOURCE LINES 142-143 Elasticity of public transportation with respect to time. .. GENERATED FROM PYTHON SOURCE LINES 143-153 .. code-block:: Python direct_elas_term_pt_time = ( simulated_values['Weighted prob. PT'] * simulated_values['direct_elas_pt_time'] / denominator_pt ).sum() print( f'Aggregate direct point elasticity of PT wrt time: ' f'{direct_elas_term_pt_time:.3g}' ) .. rst-class:: sphx-glr-script-out .. code-block:: none Aggregate direct point elasticity of PT wrt time: -0.268 .. GENERATED FROM PYTHON SOURCE LINES 154-155 Elasticity of public transportation with respect to cost. .. GENERATED FROM PYTHON SOURCE LINES 155-165 .. code-block:: Python direct_elas_term_pt_cost = ( simulated_values['Weighted prob. PT'] * simulated_values['direct_elas_pt_cost'] / denominator_pt ).sum() print( f'Aggregate direct point elasticity of PT wrt cost: ' f'{direct_elas_term_pt_cost:.3g}' ) .. rst-class:: sphx-glr-script-out .. code-block:: none Aggregate direct point elasticity of PT wrt cost: -0.324 .. GENERATED FROM PYTHON SOURCE LINES 166-167 Elasticity of slow modes with respect to distance. .. GENERATED FROM PYTHON SOURCE LINES 167-176 .. code-block:: Python direct_elas_term_sm_dist = ( simulated_values['Weighted prob. SM'] * simulated_values['direct_elas_sm_dist'] / denominator_sm ).sum() print( f'Aggregate direct point elasticity of SM wrt distance: ' f'{direct_elas_term_sm_dist:.3g}' ) .. rst-class:: sphx-glr-script-out .. code-block:: none Aggregate direct point elasticity of SM wrt distance: -1.09 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 4.108 seconds) .. _sphx_glr_download_auto_examples_indicators_plot_b06point_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_b06point_elasticities.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_b06point_elasticities.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_b06point_elasticities.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_