.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/montecarlo/plot_b01simple_integral.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_montecarlo_plot_b01simple_integral.py: Simple integral =============== Calculation of a simple integral using Monte-Carlo integration. Michel Bierlaire, EPFL Sat Jun 28 2025, 21:03:30 .. GENERATED FROM PYTHON SOURCE LINES 11-18 .. code-block:: Python import pandas as pd from biogeme.biogeme import BIOGEME from biogeme.database import Database from biogeme.expressions import Draws, MonteCarlo, exp .. GENERATED FROM PYTHON SOURCE LINES 19-21 We create a fake database with one entry, as it is required to store the draws .. GENERATED FROM PYTHON SOURCE LINES 21-25 .. code-block:: Python df = pd.DataFrame() df['FakeColumn'] = [1.0] database = Database('fake_database', df) .. GENERATED FROM PYTHON SOURCE LINES 26-29 .. code-block:: Python integrand = exp(Draws('U', 'UNIFORM')) simulated_integral = MonteCarlo(integrand) .. GENERATED FROM PYTHON SOURCE LINES 30-32 .. code-block:: Python true_integral = exp(1.0) - 1.0 .. GENERATED FROM PYTHON SOURCE LINES 33-59 .. code-block:: Python R = 2_000 MULTIPLIER = 100_000 sample_variance = ( MonteCarlo(integrand * integrand) - simulated_integral * simulated_integral ) stderr = (sample_variance / R) ** 0.5 error = simulated_integral - true_integral simulate = { 'Analytical Integral': true_integral, 'Simulated Integral': simulated_integral, 'Sample variance ': sample_variance, 'Std Error ': stderr, 'Error ': error, } biosim = BIOGEME(database, simulate, number_of_draws=R) R = biosim.number_of_draws biosim.model_name = f'01simpleIntegral_{R}' results = biosim.simulate(the_beta_values={}) print(f'Number of draws: {R}') for c in results.columns: print(f'{c}: {results.loc[0, c]}') .. rst-class:: sphx-glr-script-out .. code-block:: none Number of draws: 2000 Analytical Integral: 1.718281828459045 Simulated Integral: 1.7114018513619786 Sample variance : 0.24475248418333923 Std Error : 0.01106237958540881 Error : -0.0068799770970664476 .. GENERATED FROM PYTHON SOURCE LINES 60-61 Change the number of draws .. GENERATED FROM PYTHON SOURCE LINES 61-67 .. code-block:: Python biogeme2 = BIOGEME(database, simulate, number_of_draws=R * MULTIPLIER) biogeme2.model_name = '01simpleIntegral_{multiplier*R}' results2 = biogeme2.simulate(the_beta_values={}) print(f'Number of draws: {MULTIPLIER * R:_}') for c in results2.columns: print(f'{c}: {results2.loc[0, c]}') .. rst-class:: sphx-glr-script-out .. code-block:: none Number of draws: 200_000_000 Analytical Integral: 1.718281828459045 Simulated Integral: 1.7182840012739995 Sample variance : 0.24201170665587718 Std Error : 0.011000266057143281 Error : 2.1728149544042452e-06 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.659 seconds) .. _sphx_glr_download_auto_examples_montecarlo_plot_b01simple_integral.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_b01simple_integral.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_b01simple_integral.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_b01simple_integral.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_