.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/indicators/plot_b05revenues.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_b05revenues.py: Calculation of revenues ======================= We use an estimated model to calculate revenues. :author: Michel Bierlaire, EPFL :date: Wed Apr 12 21:02:19 2023 .. GENERATED FROM PYTHON SOURCE LINES 12-29 .. code-block:: default import sys import numpy as np try: import matplotlib.pyplot as plt can_plot = True except ModuleNotFoundError: can_plot = False from tqdm import tqdm from biogeme import models import biogeme.exceptions as excep import biogeme.biogeme as bio import biogeme.results as res from optima_data import database, normalized_weight from scenarios import scenario .. GENERATED FROM PYTHON SOURCE LINES 30-31 Read the estimation results from the file. .. GENERATED FROM PYTHON SOURCE LINES 31-41 .. code-block:: default try: results = res.bioResults(pickleFile='saved_results/b02estimation.pickle') except excep.BiogemeError: sys.exit( 'Run first the script b02simulation.py ' 'in order to generate the ' 'file b02estimation.pickle.' ) .. GENERATED FROM PYTHON SOURCE LINES 42-43 Function calculating the revenues .. GENERATED FROM PYTHON SOURCE LINES 43-85 .. code-block:: default def revenues(factor: float) -> tuple[float, float, float]: """Calculate the total revenues generated by public transportation, when the price is multiplied by a factor. :param factor: factor that multiplies the current cost of public transportation :return: total revenues, followed by the lower and upper bound of the confidence interval. """ # Obtain the specification for the default scenario V, nests, _, marginal_cost_scenario = scenario(factor=factor) # Obtain the expression for the choice probability of each alternative prob_pt = models.nested(V, None, nests, 0) # We now simulate the choice probabilities,the weight and the # price variable simulate = { 'weight': normalized_weight, 'Revenue public transportation': prob_pt * marginal_cost_scenario, } the_biogeme = bio.BIOGEME(database, simulate) simulated_values = the_biogeme.simulate(results.getBetaValues()) # We also calculate confidence intervals for the calculated quantities betas = the_biogeme.free_beta_names() beta_bootstrap = results.getBetasForSensitivityAnalysis(betas) left, right = the_biogeme.confidenceIntervals(beta_bootstrap, 0.9) revenues_pt = ( simulated_values['Revenue public transportation'] * simulated_values['weight'] ).sum() revenues_pt_left = (left['Revenue public transportation'] * left['weight']).sum() revenues_pt_right = (right['Revenue public transportation'] * right['weight']).sum() return revenues_pt, revenues_pt_left, revenues_pt_right .. GENERATED FROM PYTHON SOURCE LINES 86-87 Current revenues for public transportation .. GENERATED FROM PYTHON SOURCE LINES 87-94 .. code-block:: default r, r_left, r_right = revenues(factor=1.0) print( f'Total revenues for public transportation (for the sample): {r:.1f} CHF ' f'[{r_left:.1f} CHF, ' f'{r_right:.1f} CHF]' ) .. rst-class:: sphx-glr-script-out .. code-block:: none Total revenues for public transportation (for the sample): 3018.3 CHF [2418.8 CHF, 3683.3 CHF] .. GENERATED FROM PYTHON SOURCE LINES 95-96 We now investigate how the revenues vary with the multiplicative factor .. GENERATED FROM PYTHON SOURCE LINES 96-107 .. code-block:: default factors = np.arange(0.0, 5.0, 0.05) plot_revenues = [revenues(s) for s in tqdm(factors)] zipped = zip(*plot_revenues) rev = next(zipped) lower = next(zipped) upper = next(zipped) largest_revenue = max(rev) max_index = rev.index(largest_revenue) .. rst-class:: sphx-glr-script-out .. code-block:: none 0%| | 0/100 [00:00` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_b05revenues.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_