.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/montecarlo/plot_b03antithetic_explicit.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_b03antithetic_explicit.py: Antithetic draws explicitly generated ===================================== Calculation of a simple integral using Monte-Carlo integration. It illustrates how to use antothetic draws, explicitly generared. :author: Michel Bierlaire, EPFL :date: Thu Apr 13 20:49:50 2023 .. GENERATED FROM PYTHON SOURCE LINES 12-20 .. 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 21-23 .. code-block:: default R = 10000 .. GENERATED FROM PYTHON SOURCE LINES 24-26 We create a fake database with one entry, as it is required to store the draws .. GENERATED FROM PYTHON SOURCE LINES 26-31 .. code-block:: default df = pd.DataFrame() df['FakeColumn'] = [1.0] database = db.Database('fake_database', df) .. GENERATED FROM PYTHON SOURCE LINES 32-45 .. 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 46-49 .. code-block:: default my_draws = {'HALTON13': (halton13, 'Halton draws, base 13, skipping 10')} database.setRandomNumberGenerators(my_draws) .. GENERATED FROM PYTHON SOURCE LINES 50-54 .. code-block:: default U = bioDraws('U', 'UNIFORM') integrand = exp(U) + exp(1 - U) simulatedI = MonteCarlo(integrand) / 2.0 .. GENERATED FROM PYTHON SOURCE LINES 55-59 .. code-block:: default U_halton13 = bioDraws('U_halton13', 'HALTON13') integrand_halton13 = exp(U_halton13) + exp(1 - U_halton13) simulatedI_halton13 = MonteCarlo(integrand_halton13) / 2.0 .. GENERATED FROM PYTHON SOURCE LINES 60-64 .. code-block:: default U_mlhs = bioDraws('U_mlhs', 'UNIFORM_MLHS') integrand_mlhs = exp(U_mlhs) + exp(1 - U_mlhs) simulatedI_mlhs = MonteCarlo(integrand_mlhs) / 2.0 .. GENERATED FROM PYTHON SOURCE LINES 65-67 .. code-block:: default trueI = exp(1.0) - 1.0 .. GENERATED FROM PYTHON SOURCE LINES 68-70 .. code-block:: default error = simulatedI - trueI .. GENERATED FROM PYTHON SOURCE LINES 71-73 .. code-block:: default error_halton13 = simulatedI_halton13 - trueI .. GENERATED FROM PYTHON SOURCE LINES 74-76 .. code-block:: default error_mlhs = simulatedI_mlhs - trueI .. GENERATED FROM PYTHON SOURCE LINES 77-87 .. code-block:: default simulate = { 'Analytical Integral': trueI, 'Simulated Integral': simulatedI, 'Error ': error, 'Simulated Integral (Halton13)': simulatedI_halton13, 'Error (Halton13) ': error_halton13, 'Simulated Integral (MLHS)': simulatedI_mlhs, 'Error (MLHS) ': error_mlhs, } .. GENERATED FROM PYTHON SOURCE LINES 88-91 .. code-block:: default biosim = bio.BIOGEME(database, simulate) biosim.modelName = 'b03antithetic_explicit' .. GENERATED FROM PYTHON SOURCE LINES 92-95 .. code-block:: default results = biosim.simulate(theBetaValues={}) results .. raw:: html
Analytical Integral Simulated Integral Error Simulated Integral (Halton13) Error (Halton13) Simulated Integral (MLHS) Error (MLHS)
0 1.718282 1.718282 3.982962e-07 1.718282 1.315155e-08 1.718282 4.733991e-13


.. GENERATED FROM PYTHON SOURCE LINES 96-110 .. code-block:: default print(f"Analytical integral: {results.iloc[0]['Analytical Integral']:.6f}") print( f" \t{'Uniform (Anti)':>15}\t{'Halton13 (Anti)':>15}\t{'MLHS (Anti)':>15}" ) print( f"Simulated\t{results.iloc[0]['Simulated Integral']:>15.6g}\t" f"{results.iloc[0]['Simulated Integral (Halton13)']:>15.6g}\t" f"{results.iloc[0]['Simulated Integral (MLHS)']:>15.6g}" ) print( f"Error\t\t{results.iloc[0]['Error ']:>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.718282 Uniform (Anti) Halton13 (Anti) MLHS (Anti) Simulated 1.71828 1.71828 1.71828 Error 3.98296e-07 1.31515e-08 4.73399e-13 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 40.093 seconds) .. _sphx_glr_download_auto_examples_montecarlo_plot_b03antithetic_explicit.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_b03antithetic_explicit.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_b03antithetic_explicit.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_