.. 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. Michel Bierlaire, EPFL Sat Jun 28 2025, 18:57:49 .. GENERATED FROM PYTHON SOURCE LINES 11-28 .. code-block:: Python import sys import numpy as np from biogeme.biogeme import BIOGEME from biogeme.models import nested from biogeme.results_processing import EstimationResults try: import matplotlib.pyplot as plt can_plot = True except ModuleNotFoundError: can_plot = False from biogeme.data.optima import read_data, normalized_weight from scenarios import scenario .. GENERATED FROM PYTHON SOURCE LINES 29-30 Read the estimation results from the file. .. GENERATED FROM PYTHON SOURCE LINES 30-41 .. code-block:: Python try: results = EstimationResults.from_yaml_file( filename='saved_results/b02estimation.yaml' ) except FileNotFoundError: sys.exit( 'Run first the script b02simulation.py ' 'in order to generate the ' 'file b02estimation.yaml.' ) .. GENERATED FROM PYTHON SOURCE LINES 42-43 Read the data .. GENERATED FROM PYTHON SOURCE LINES 43-46 .. code-block:: Python database = read_data() .. GENERATED FROM PYTHON SOURCE LINES 47-48 Function calculating the revenues .. GENERATED FROM PYTHON SOURCE LINES 48-103 .. code-block:: Python 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. """ filename = f'revenue_{factor:.2f}.txt' SEPARATOR = '%' try: with open(filename, 'r') as f: line = f.read() revenue, left, right = line.split(SEPARATOR) return float(revenue), float(left), float(right) except FileNotFoundError: ... # Obtain the specification for the default scenario utilities, nests, _, marginal_cost_scenario = scenario(factor=factor) # Obtain the expression for the choice probability of each alternative prob_pt = nested(utilities, 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 = BIOGEME(database, simulate) simulated_values = the_biogeme.simulate(results.get_beta_values()) # We also calculate confidence intervals for the calculated quantities beta_bootstrap = results.get_betas_for_sensitivity_analysis() left, right = the_biogeme.confidence_intervals(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() with open(filename, 'w') as f: print( f'{revenues_pt} {SEPARATOR} {revenues_pt_left} {SEPARATOR} {revenues_pt_right}', file=f, ) return revenues_pt, revenues_pt_left, revenues_pt_right .. GENERATED FROM PYTHON SOURCE LINES 104-105 Current revenues for public transportation .. GENERATED FROM PYTHON SOURCE LINES 105-112 .. code-block:: Python 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): 3043.3 CHF [2529.8 CHF, 3661.2 CHF] .. GENERATED FROM PYTHON SOURCE LINES 113-114 We now investigate how the revenues vary with the multiplicative factor .. GENERATED FROM PYTHON SOURCE LINES 114-125 .. code-block:: Python factors = np.arange(0.0, 5.0, 0.1) plot_revenues = [revenues(s) for s in factors] zipped = zip(*plot_revenues) rev = next(zipped) lower = next(zipped) upper = next(zipped) largest_revenue = max(rev) max_index = rev.index(largest_revenue) .. GENERATED FROM PYTHON SOURCE LINES 126-131 .. code-block:: Python print( f'Largest revenue: {largest_revenue:.1f} obtained with ' f'factor {factors[max_index]:.1f}' ) .. rst-class:: sphx-glr-script-out .. code-block:: none Largest revenue: 3062.4 obtained with factor 1.2 .. GENERATED FROM PYTHON SOURCE LINES 132-141 .. code-block:: Python if can_plot: # We plot the results ax = plt.gca() ax.plot(factors, rev, label="Revenues") ax.plot(factors, lower, label="Lower bound of the CI") ax.plot(factors, upper, label="Upper bound of the CI") ax.legend() plt.show() .. image-sg:: /auto_examples/indicators/images/sphx_glr_plot_b05revenues_001.png :alt: plot b05revenues :srcset: /auto_examples/indicators/images/sphx_glr_plot_b05revenues_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.761 seconds) .. _sphx_glr_download_auto_examples_indicators_plot_b05revenues.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_b05revenues.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_b05revenues.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_b05revenues.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_