.. 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. Michel Bierlaire, EPFL Sat Jun 28 2025, 21:08:23 .. GENERATED FROM PYTHON SOURCE LINES 12-22 .. code-block:: Python import numpy as np import pandas as pd from IPython.core.display_functions import display from biogeme.biogeme import BIOGEME from biogeme.database import Database from biogeme.draws import RandomNumberGeneratorTuple, get_halton_draws from biogeme.expressions import Draws, MonteCarlo, exp .. 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:: Python df = pd.DataFrame() df['FakeColumn'] = [1.0] database = Database('fake_database', df) .. GENERATED FROM PYTHON SOURCE LINES 31-49 .. code-block:: Python 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 = get_halton_draws( 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 50-57 .. code-block:: Python my_draws = { 'HALTON13_ANTI': RandomNumberGeneratorTuple( halton13_anti, 'Antithetic Halton draws, base 13, skipping 10', ) } .. GENERATED FROM PYTHON SOURCE LINES 58-59 Integrate with antithetic pseudo-random number generator. .. GENERATED FROM PYTHON SOURCE LINES 59-62 .. code-block:: Python integrand = exp(Draws('U', 'UNIFORM_ANTI')) simulated_integral = MonteCarlo(integrand) .. GENERATED FROM PYTHON SOURCE LINES 63-64 Integrate with antithetic Halton draws, base 13. .. GENERATED FROM PYTHON SOURCE LINES 64-67 .. code-block:: Python integrand_halton13 = exp(Draws('U_halton13', 'HALTON13_ANTI')) simulated_integral_halton13 = MonteCarlo(integrand_halton13) .. GENERATED FROM PYTHON SOURCE LINES 68-69 Integrate with antithetic MLHS. .. GENERATED FROM PYTHON SOURCE LINES 69-72 .. code-block:: Python integrand_mlhs = exp(Draws('U_mlhs', 'UNIFORM_MLHS_ANTI')) simulated_integral_mlhs = MonteCarlo(integrand_mlhs) .. GENERATED FROM PYTHON SOURCE LINES 73-74 True value .. GENERATED FROM PYTHON SOURCE LINES 74-76 .. code-block:: Python true_integral = exp(1.0) - 1.0 .. GENERATED FROM PYTHON SOURCE LINES 77-78 Number of draws. .. GENERATED FROM PYTHON SOURCE LINES 78-80 .. code-block:: Python R = 2_000_000 .. GENERATED FROM PYTHON SOURCE LINES 81-83 .. code-block:: Python error = simulated_integral - true_integral .. GENERATED FROM PYTHON SOURCE LINES 84-86 .. code-block:: Python error_halton13 = simulated_integral_halton13 - true_integral .. GENERATED FROM PYTHON SOURCE LINES 87-89 .. code-block:: Python error_mlhs = simulated_integral_mlhs - true_integral .. GENERATED FROM PYTHON SOURCE LINES 90-101 .. code-block:: Python simulate = { 'Analytical Integral': true_integral, 'Simulated Integral': simulated_integral, 'Error ': error, 'Simulated Integral (Halton13)': simulated_integral_halton13, 'Error (Halton13) ': error_halton13, 'Simulated Integral (MLHS)': simulated_integral_mlhs, 'Error (MLHS) ': error_mlhs, } .. GENERATED FROM PYTHON SOURCE LINES 102-109 .. code-block:: Python biosim = BIOGEME( database, simulate, random_number_generators=my_draws, number_of_draws=R ) biosim.modelName = 'b03antithetic' results = biosim.simulate(the_beta_values={}) display(results) .. rst-class:: sphx-glr-script-out .. code-block:: none /Users/bierlair/MyFiles/github/biogeme/docs/source/examples/montecarlo/plot_b03antithetic.py:105: DeprecationWarning: 'modelName' is deprecated. Please use 'model_name' instead. biosim.modelName = 'b03antithetic' Analytical Integral ... Error (MLHS) 0 1.718282 ... -5.448109e-11 [1 rows x 7 columns] .. GENERATED FROM PYTHON SOURCE LINES 110-111 Reorganize the results. .. GENERATED FROM PYTHON SOURCE LINES 111-125 .. code-block:: Python 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.71824 1.71828 1.71828 Error -4.56184e-05 2.88992e-08 -5.44811e-11 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.248 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-jupyter :download:`Download Jupyter notebook: plot_b03antithetic.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_b03antithetic.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_b03antithetic.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_