.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/indicators/plot_b09wtp.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_b09wtp.py: Calculation of willingness to pay ================================= We calculate and plot willingness to pay. Details about this example are available in Section 4 of `Bierlaire (2018) Calculating indicators with PandasBiogeme `_ Michel Bierlaire, EPFL Sat Jun 28 2025, 21:00:12 .. GENERATED FROM PYTHON SOURCE LINES 15-34 .. code-block:: Python import sys import numpy as np import pandas as pd from IPython.core.display_functions import display from biogeme.biogeme import BIOGEME from biogeme.results_processing import EstimationResults try: import matplotlib.pyplot as plt can_plot = True except ModuleNotFoundError: can_plot = False from biogeme.expressions import Derive from biogeme.data.optima import read_data, normalized_weight from scenarios import scenario .. GENERATED FROM PYTHON SOURCE LINES 35-37 Obtain the specification for the default scenario The definition of the scenarios is available in :ref:`scenarios`. .. GENERATED FROM PYTHON SOURCE LINES 37-42 .. code-block:: Python v, _, _, _ = scenario() v_pt = v[0] v_car = v[1] .. GENERATED FROM PYTHON SOURCE LINES 43-44 Calculation of the willingness to pay using derivatives. .. GENERATED FROM PYTHON SOURCE LINES 44-47 .. code-block:: Python wtp_pt_time = Derive(v_pt, 'TimePT') / Derive(v_pt, 'MarginalCostPT') wtp_car_time = Derive(v_car, 'TimeCar') / Derive(v_car, 'CostCarCHF') .. GENERATED FROM PYTHON SOURCE LINES 48-49 Formulas to simulate. .. GENERATED FROM PYTHON SOURCE LINES 49-55 .. code-block:: Python simulate = { 'weight': normalized_weight, 'WTP PT time': wtp_pt_time, 'WTP CAR time': wtp_car_time, } .. GENERATED FROM PYTHON SOURCE LINES 56-57 Read the data .. GENERATED FROM PYTHON SOURCE LINES 57-59 .. code-block:: Python database = read_data() .. GENERATED FROM PYTHON SOURCE LINES 60-61 Create the Biogeme object. .. GENERATED FROM PYTHON SOURCE LINES 61-63 .. code-block:: Python the_biogeme = BIOGEME(database, simulate) .. GENERATED FROM PYTHON SOURCE LINES 64-65 Read the estimation results from the file. .. GENERATED FROM PYTHON SOURCE LINES 65-75 .. 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 76-78 `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 78-81 .. 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 WTP PT time WTP CAR time 0 0.893779 0.038447 0.038447 1 0.868674 0.038447 0.038447 2 0.868674 0.038447 0.038447 3 0.965766 0.111038 0.111038 4 0.868674 0.038447 0.038447 ... ... ... ... 1894 2.053830 0.038447 0.038447 1895 0.868674 0.111038 0.111038 1896 0.868674 0.111038 0.111038 1897 0.965766 0.038447 0.038447 1898 0.965766 0.038447 0.038447 [1899 rows x 3 columns] .. GENERATED FROM PYTHON SOURCE LINES 82-83 Note the multiplication by 60 to have the valus of time per hour and not per minute. .. GENERATED FROM PYTHON SOURCE LINES 83-85 .. code-block:: Python wtpcar = (60 * simulated_values['WTP CAR time'] * simulated_values['weight']).mean() .. GENERATED FROM PYTHON SOURCE LINES 86-87 Calculate confidence intervals .. GENERATED FROM PYTHON SOURCE LINES 87-89 .. code-block:: Python b = results.get_betas_for_sensitivity_analysis() .. GENERATED FROM PYTHON SOURCE LINES 90-92 Returns data frame containing, for each simulated value, the left and right bounds of the confidence interval calculated by simulation. .. GENERATED FROM PYTHON SOURCE LINES 92-94 .. code-block:: Python left, right = the_biogeme.confidence_intervals(b, 0.9) .. rst-class:: sphx-glr-script-out .. code-block:: none 0%| | 0/100 [00:00 tuple[float, float, float]: """ Check the value for groups of the population. Define a function that works for any filter to avoid repeating code. :param the_filter: pandas filter :return: willingness-to-pay for car and confidence interval """ size = the_filter.sum() sim = simulated_values[the_filter] total_weight = sim['weight'].sum() weight = sim['weight'] * size / total_weight _wtpcar = (60 * sim['WTP CAR time'] * weight).mean() _wtpcar_left = (60 * left[the_filter]['WTP CAR time'] * weight).mean() _wtpcar_right = (60 * right[the_filter]['WTP CAR time'] * weight).mean() return _wtpcar, _wtpcar_left, _wtpcar_right .. GENERATED FROM PYTHON SOURCE LINES 140-141 Full time workers. .. GENERATED FROM PYTHON SOURCE LINES 141-145 .. code-block:: Python a_filter = database.dataframe['OccupStat'] == 1 w, l, r = wtp_for_subgroup(a_filter) print(f'WTP car for workers: {w:.3g} CI:[{l:.3g}, {r:.3g}]') .. rst-class:: sphx-glr-script-out .. code-block:: none WTP car for workers: 6.66 CI:[4.04, 11.7] .. GENERATED FROM PYTHON SOURCE LINES 146-147 Females. .. GENERATED FROM PYTHON SOURCE LINES 147-151 .. code-block:: Python a_filter = database.dataframe['Gender'] == 2 w, l, r = wtp_for_subgroup(a_filter) print(f'WTP car for females: {w:.3g} CI:[{l:.3g}, {r:.3g}]') .. rst-class:: sphx-glr-script-out .. code-block:: none WTP car for females: 3.09 CI:[1.28, 6.21] .. GENERATED FROM PYTHON SOURCE LINES 152-153 Males. .. GENERATED FROM PYTHON SOURCE LINES 153-157 .. code-block:: Python a_filter = database.dataframe['Gender'] == 1 w, l, r = wtp_for_subgroup(a_filter) print(f'WTP car for males : {w:.3g} CI:[{l:.3g}, {r:.3g}]') .. rst-class:: sphx-glr-script-out .. code-block:: none WTP car for males : 4.89 CI:[2.67, 8.98] .. GENERATED FROM PYTHON SOURCE LINES 158-160 We plot the distribution of WTP in the population. In this case, there are only two values .. GENERATED FROM PYTHON SOURCE LINES 160-168 .. code-block:: Python if can_plot: plt.hist( 60 * simulated_values['WTP CAR time'], weights=simulated_values['weight'], ) plt.xlabel('WTP (CHF/hour)') plt.ylabel('Individuals') plt.show() .. image-sg:: /auto_examples/indicators/images/sphx_glr_plot_b09wtp_001.png :alt: plot b09wtp :srcset: /auto_examples/indicators/images/sphx_glr_plot_b09wtp_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 8.147 seconds) .. _sphx_glr_download_auto_examples_indicators_plot_b09wtp.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_b09wtp.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_b09wtp.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_b09wtp.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_