.. 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. :author: Michel Bierlaire, EPFL :date: Thu Apr 13 20:42:24 2023 .. GENERATED FROM PYTHON SOURCE LINES 12-19 .. code-block:: Python import pandas as pd import biogeme.biogeme as bio import biogeme.database as db from biogeme.expressions import exp, bioDraws, MonteCarlo .. GENERATED FROM PYTHON SOURCE LINES 20-22 We create a fake database with one entry, as it is required to store the draws .. GENERATED FROM PYTHON SOURCE LINES 22-26 .. code-block:: Python df = pd.DataFrame() df['FakeColumn'] = [1.0] database = db.Database('fake_database', df) .. GENERATED FROM PYTHON SOURCE LINES 27-30 .. code-block:: Python integrand = exp(bioDraws('U', 'UNIFORM')) simulated_integral = MonteCarlo(integrand) .. GENERATED FROM PYTHON SOURCE LINES 31-33 .. code-block:: Python true_integral = exp(1.0) - 1.0 .. GENERATED FROM PYTHON SOURCE LINES 34-60 .. code-block:: Python R = 200 MULTIPLIER = 100000 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 = bio.BIOGEME(database, simulate, number_of_draws=R) R = biosim.number_of_draws biosim.modelName = 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: 200 Analytical Integral: 1.718281828459045 Simulated Integral: 1.6914968008726095 Sample variance : 0.22207804709564938 Std Error : 0.033322518444413037 Error : -0.02678502758643564 .. GENERATED FROM PYTHON SOURCE LINES 61-62 Change the number of draws .. GENERATED FROM PYTHON SOURCE LINES 62-68 .. code-block:: Python biogeme2 = bio.BIOGEME(database, simulate, number_of_draws=R * MULTIPLIER) biogeme2.modelName = '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: 20000000 Analytical Integral: 1.718281828459045 Simulated Integral: 1.7182794027362247 Sample variance : 0.24205309531650032 Std Error : 0.0347888700101412 Error : -2.4257228203872216e-06 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 14.457 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 `_