.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/montecarlo/plot_b03antithetic.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.py: Antithetic draws ================ Calculation of a simple integral using Monte-Carlo integration. It illustrates how to use antithetic draws. :author: Michel Bierlaire, EPFL :date: Thu Apr 13 20:48:02 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 We create a fake database with one entry, as it is required to store the draws. .. GENERATED FROM PYTHON SOURCE LINES 23-28 .. code-block:: default df = pd.DataFrame() df['FakeColumn'] = [1.0] database = db.Database('fake_database', df) .. GENERATED FROM PYTHON SOURCE LINES 29-47 .. code-block:: default def halton13_anti(sample_size: int, number_of_draws: int) -> np.array: """The user can define new draws. For example, antithetic 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. """ # We first generate half of the number of requested draws. the_draws = draws.getHaltonDraws( sample_size, int(number_of_draws / 2.0), base=13, skip=10 ) # We complement them with their antithetic version. return np.concatenate((the_draws, 1 - the_draws), axis=1) .. GENERATED FROM PYTHON SOURCE LINES 48-56 .. code-block:: default mydraws = { 'HALTON13_ANTI': ( halton13_anti, 'Antithetic Halton draws, base 13, skipping 10', ) } database.setRandomNumberGenerators(mydraws) .. GENERATED FROM PYTHON SOURCE LINES 57-58 Integrate with antithetic pseudo-random number generator. .. GENERATED FROM PYTHON SOURCE LINES 58-61 .. code-block:: default integrand = exp(bioDraws('U', 'UNIFORM_ANTI')) simulatedI = MonteCarlo(integrand) .. GENERATED FROM PYTHON SOURCE LINES 62-63 Integrate with antithetic Halton draws, base 13. .. GENERATED FROM PYTHON SOURCE LINES 63-66 .. code-block:: default integrand_halton13 = exp(bioDraws('U_halton13', 'HALTON13_ANTI')) simulatedI_halton13 = MonteCarlo(integrand_halton13) .. GENERATED FROM PYTHON SOURCE LINES 67-68 Integrate with antithetic MLHS. .. GENERATED FROM PYTHON SOURCE LINES 68-71 .. code-block:: default integrand_mlhs = exp(bioDraws('U_mlhs', 'UNIFORM_MLHS_ANTI')) simulatedI_mlhs = MonteCarlo(integrand_mlhs) .. GENERATED FROM PYTHON SOURCE LINES 72-73 True value .. GENERATED FROM PYTHON SOURCE LINES 73-75 .. code-block:: default trueI = exp(1.0) - 1.0 .. GENERATED FROM PYTHON SOURCE LINES 76-77 Number of draws. .. GENERATED FROM PYTHON SOURCE LINES 77-79 .. code-block:: default R = 20000 .. GENERATED FROM PYTHON SOURCE LINES 80-82 .. code-block:: default error = simulatedI - trueI .. GENERATED FROM PYTHON SOURCE LINES 83-85 .. code-block:: default error_halton13 = simulatedI_halton13 - trueI .. GENERATED FROM PYTHON SOURCE LINES 86-88 .. code-block:: default error_mlhs = simulatedI_mlhs - trueI .. GENERATED FROM PYTHON SOURCE LINES 89-100 .. 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 101-106 .. code-block:: default biosim = bio.BIOGEME(database, simulate) biosim.modelName = 'b03antithetic' 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.718272 -0.00001 1.718282 -4.449617e-09 1.718282 -1.322897e-11


.. GENERATED FROM PYTHON SOURCE LINES 107-108 Reorganize the results. .. GENERATED FROM PYTHON SOURCE LINES 108-122 .. 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.71827 1.71828 1.71828 Error -9.65275e-06 -4.44962e-09 -1.3229e-11 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 21.911 seconds) .. _sphx_glr_download_auto_examples_montecarlo_plot_b03antithetic.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.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_b03antithetic.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_