.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/montecarlo/plot_b02simple_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_b02simple_integral.py: Various integration methods =========================== Calculation of a simple integral using numerical integration and Monte-Carlo integration with various types of draws, including Halton draws base 13. It illustrates how to use draws that are not directly available in Biogeme. :author: Michel Bierlaire, EPFL :date: Thu Apr 13 20:46:01 2023 .. GENERATED FROM PYTHON SOURCE LINES 14-22 .. code-block:: default import numpy as np import pandas as pd import biogeme.database as db import biogeme.biogeme as bio from biogeme import draws from biogeme.expressions import exp, bioDraws, MonteCarlo .. GENERATED FROM PYTHON SOURCE LINES 23-25 We create a fake database with one entry, as it is required to store the draws. .. GENERATED FROM PYTHON SOURCE LINES 25-30 .. code-block:: default df = pd.DataFrame() df['FakeColumn'] = [1.0] database = db.Database('fakeDatabase', df) .. GENERATED FROM PYTHON SOURCE LINES 31-44 .. code-block:: default def halton13(sample_size: int, number_of_draws: int) -> np.array: """ The user can define new draws. For example, Halton draws with base 13, skipping the first 10 draws. :param sample_size: number of observations for which draws must be generated. :param number_of_draws: number of draws to generate. """ return draws.getHaltonDraws(sample_size, number_of_draws, base=13, skip=10) .. GENERATED FROM PYTHON SOURCE LINES 45-48 .. code-block:: default mydraws = {'HALTON13': (halton13, 'Halton draws, base 13, skipping 10')} database.setRandomNumberGenerators(mydraws) .. GENERATED FROM PYTHON SOURCE LINES 49-50 Integrate with pseudo-random number generator. .. GENERATED FROM PYTHON SOURCE LINES 50-53 .. code-block:: default integrand = exp(bioDraws('U', 'UNIFORM')) simulatedI = MonteCarlo(integrand) .. GENERATED FROM PYTHON SOURCE LINES 54-55 Integrate with Halton draws, base 2. .. GENERATED FROM PYTHON SOURCE LINES 55-58 .. code-block:: default integrand_halton = exp(bioDraws('U_halton', 'UNIFORM_HALTON2')) simulatedI_halton = MonteCarlo(integrand_halton) .. GENERATED FROM PYTHON SOURCE LINES 59-60 Integrate with Halton draws, base 13. .. GENERATED FROM PYTHON SOURCE LINES 60-63 .. code-block:: default integrand_halton13 = exp(bioDraws('U_halton13', 'HALTON13')) simulatedI_halton13 = MonteCarlo(integrand_halton13) .. GENERATED FROM PYTHON SOURCE LINES 64-65 Integrate with MLHS. .. GENERATED FROM PYTHON SOURCE LINES 65-68 .. code-block:: default integrand_mlhs = exp(bioDraws('U_mlhs', 'UNIFORM_MLHS')) simulatedI_mlhs = MonteCarlo(integrand_mlhs) .. GENERATED FROM PYTHON SOURCE LINES 69-70 True value .. GENERATED FROM PYTHON SOURCE LINES 70-72 .. code-block:: default trueI = exp(1.0) - 1.0 .. GENERATED FROM PYTHON SOURCE LINES 73-74 Number of draws. .. GENERATED FROM PYTHON SOURCE LINES 74-76 .. code-block:: default R = 20000 .. GENERATED FROM PYTHON SOURCE LINES 77-81 .. code-block:: default sampleVariance = MonteCarlo(integrand * integrand) - simulatedI * simulatedI stderr = (sampleVariance / R) ** 0.5 error = simulatedI - trueI .. GENERATED FROM PYTHON SOURCE LINES 82-89 .. code-block:: default sampleVariance_halton = ( MonteCarlo(integrand_halton * integrand_halton) - simulatedI_halton * simulatedI_halton ) stderr_halton = (sampleVariance_halton / R) ** 0.5 error_halton = simulatedI_halton - trueI .. GENERATED FROM PYTHON SOURCE LINES 90-97 .. code-block:: default sampleVariance_halton13 = ( MonteCarlo(integrand_halton13 * integrand_halton13) - simulatedI_halton13 * simulatedI_halton13 ) stderr_halton13 = (sampleVariance_halton13 / R) ** 0.5 error_halton13 = simulatedI_halton13 - trueI .. GENERATED FROM PYTHON SOURCE LINES 98-104 .. code-block:: default sampleVariance_mlhs = ( MonteCarlo(integrand_mlhs * integrand_mlhs) - simulatedI_mlhs * simulatedI_mlhs ) stderr_mlhs = (sampleVariance_mlhs / R) ** 0.5 error_mlhs = simulatedI_mlhs - trueI .. GENERATED FROM PYTHON SOURCE LINES 105-125 .. code-block:: default simulate = { 'Analytical Integral': trueI, 'Simulated Integral': simulatedI, 'Sample variance ': sampleVariance, 'Std Error ': stderr, 'Error ': error, 'Simulated Integral (Halton)': simulatedI_halton, 'Sample variance (Halton) ': sampleVariance_halton, 'Std Error (Halton) ': stderr_halton, 'Error (Halton) ': error_halton, 'Simulated Integral (Halton13)': simulatedI_halton13, 'Sample variance (Halton13) ': sampleVariance_halton13, 'Std Error (Halton13) ': stderr_halton13, 'Error (Halton13) ': error_halton13, 'Simulated Integral (MLHS)': simulatedI_mlhs, 'Sample variance (MLHS) ': sampleVariance_mlhs, 'Std Error (MLHS) ': stderr_mlhs, 'Error (MLHS) ': error_mlhs, } .. GENERATED FROM PYTHON SOURCE LINES 126-131 .. code-block:: default biosim = bio.BIOGEME(database, simulate) biosim.modelName = 'b02simple_integral' results = biosim.simulate(theBetaValues={}) results .. raw:: html
Analytical Integral Simulated Integral Sample variance Std Error Error Simulated Integral (Halton) Sample variance (Halton) Std Error (Halton) Error (Halton) Simulated Integral (Halton13) Sample variance (Halton13) Std Error (Halton13) Error (Halton13) Simulated Integral (MLHS) Sample variance (MLHS) Std Error (MLHS) Error (MLHS)
0 1.718282 1.718309 0.242098 0.003479 0.000027 1.718282 0.242036 0.003479 -2.442439e-07 1.718281 0.242035 0.003479 -6.103944e-07 1.718282 0.242036 0.003479 -4.164225e-12


.. GENERATED FROM PYTHON SOURCE LINES 132-133 Reorganize the results. .. GENERATED FROM PYTHON SOURCE LINES 133-159 .. code-block:: default print(f'Analytical integral: {results.iloc[0]["Analytical Integral"]:.6g}') print(f" \t{'Uniform':>15}\t{'Halton':>15}\t{'Halton13':>15}\t{'MLHS':>15}") print( f'Simulated\t{results.iloc[0]["Simulated Integral"]:>15.6g}\t' f'{results.iloc[0]["Simulated Integral (Halton)"]:>15.6g}\t' f'{results.iloc[0]["Simulated Integral (Halton13)"]:>15.6g}\t' f'{results.iloc[0]["Simulated Integral (MLHS)"]:>15.6g}' ) print( f'Sample var.\t{results.iloc[0]["Sample variance "]:>15.6g}\t' f'{results.iloc[0]["Sample variance (Halton) "]:>15.6g}\t' f'{results.iloc[0]["Sample variance (Halton13) "]:>15.6g}\t' f'{results.iloc[0]["Sample variance (MLHS) "]:>15.6g}' ) print( f'Std error\t{results.iloc[0]["Std Error "]:>15.6g}\t' f'{results.iloc[0]["Std Error (Halton) "]:>15.6g}\t' f'{results.iloc[0]["Std Error (Halton13) "]:>15.6g}\t' f'{results.iloc[0]["Std Error (MLHS) "]:>15.6g}' ) print( f'Error\t\t{results.iloc[0]["Error "]:>15.6g}\t' f'{results.iloc[0]["Error (Halton) "]:>15.6g}\t' f'{results.iloc[0]["Error (Halton13) "]:>15.6g}\t' f'{results.iloc[0]["Error (MLHS) "]:>15.6g}' ) .. rst-class:: sphx-glr-script-out .. code-block:: none Analytical integral: 1.71828 Uniform Halton Halton13 MLHS Simulated 1.71831 1.71828 1.71828 1.71828 Sample var. 0.242098 0.242036 0.242035 0.242036 Std error 0.00347921 0.00347876 0.00347876 0.00347876 Error 2.71241e-05 -2.44244e-07 -6.10394e-07 -4.16422e-12 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 48.422 seconds) .. _sphx_glr_download_auto_examples_montecarlo_plot_b02simple_integral.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_b02simple_integral.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_b02simple_integral.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_