.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/montecarlo/plot_b05normal_mixture_monte_carlo.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_b05normal_mixture_monte_carlo.py: Monte-Carlo integration ======================= Calculation of a mixtures of logit models where the integral is calculated using numerical integration and Monte-Carlo integration with various types of draws. :author: Michel Bierlaire, EPFL :date: Thu Apr 13 20:58:50 2023 .. GENERATED FROM PYTHON SOURCE LINES 13-40 .. code-block:: default import biogeme.biogeme as bio from biogeme import models import biogeme.distributions as dist from biogeme.expressions import ( Expression, Integrate, RandomVariable, MonteCarlo, bioDraws, ) from swissmetro_one import ( database, TRAIN_TT_SCALED, TRAIN_COST_SCALED, SM_TT_SCALED, SM_COST_SCALED, CAR_TT_SCALED, CAR_CO_SCALED, TRAIN_AV_SP, SM_AV, CAR_AV_SP, CHOICE, ) .. GENERATED FROM PYTHON SOURCE LINES 41-43 .. code-block:: default R = 10000 .. GENERATED FROM PYTHON SOURCE LINES 44-45 Parameters .. GENERATED FROM PYTHON SOURCE LINES 45-52 .. code-block:: default ASC_CAR = 0.137 ASC_TRAIN = -0.402 ASC_SM = 0 B_TIME = -2.26 B_TIME_S = 1.66 B_COST = -1.29 .. GENERATED FROM PYTHON SOURCE LINES 53-54 Generate several versions of the error component. .. GENERATED FROM PYTHON SOURCE LINES 54-64 .. code-block:: default omega = RandomVariable('omega') density = dist.normalpdf(omega) B_TIME_RND = B_TIME + B_TIME_S * omega B_TIME_RND_normal = B_TIME + B_TIME_S * bioDraws('B_NORMAL', 'NORMAL') B_TIME_RND_anti = B_TIME + B_TIME_S * bioDraws('B_ANTI', 'NORMAL_ANTI') B_TIME_RND_halton = B_TIME + B_TIME_S * bioDraws('B_HALTON', 'NORMAL_HALTON2') B_TIME_RND_mlhs = B_TIME + B_TIME_S * bioDraws('B_MLHS', 'NORMAL_MLHS') B_TIME_RND_antimlhs = B_TIME + B_TIME_S * bioDraws('B_ANTIMLHS', 'NORMAL_MLHS_ANTI') .. GENERATED FROM PYTHON SOURCE LINES 65-88 .. code-block:: default def logit(the_b_time_rnd: Expression) -> Expression: """ Calculate the conditional logit model for a given random parameter. :param the_b_time_rnd: expression for the random parameter. :return: logit model to be integrated. """ v_1 = ASC_TRAIN + the_b_time_rnd * TRAIN_TT_SCALED + B_COST * TRAIN_COST_SCALED v_2 = ASC_SM + the_b_time_rnd * SM_TT_SCALED + B_COST * SM_COST_SCALED v_3 = ASC_CAR + the_b_time_rnd * CAR_TT_SCALED + B_COST * CAR_CO_SCALED # Associate utility functions with the numbering of alternatives v = {1: v_1, 2: v_2, 3: v_3} # Associate the availability conditions with the alternatives av = {1: TRAIN_AV_SP, 2: SM_AV, 3: CAR_AV_SP} # The choice model is a logit, with availability conditions integrand = models.logit(v, av, CHOICE) return integrand .. GENERATED FROM PYTHON SOURCE LINES 89-90 Generate each integral. .. GENERATED FROM PYTHON SOURCE LINES 90-97 .. code-block:: default numericalI = Integrate(logit(B_TIME_RND) * density, 'omega') normal = MonteCarlo(logit(B_TIME_RND_normal)) anti = MonteCarlo(logit(B_TIME_RND_anti)) halton = MonteCarlo(logit(B_TIME_RND_halton)) mlhs = MonteCarlo(logit(B_TIME_RND_mlhs)) antimlhs = MonteCarlo(logit(B_TIME_RND_antimlhs)) .. GENERATED FROM PYTHON SOURCE LINES 98-107 .. code-block:: default simulate = { 'Numerical': numericalI, 'MonteCarlo': normal, 'Antithetic': anti, 'Halton': halton, 'MLHS': mlhs, 'Antithetic MLHS': antimlhs, } .. GENERATED FROM PYTHON SOURCE LINES 108-111 .. code-block:: default biosim = bio.BIOGEME(database, simulate) biosim.number_of_draws = R .. GENERATED FROM PYTHON SOURCE LINES 112-115 .. code-block:: default results = biosim.simulate(theBetaValues={}) results .. raw:: html
Numerical MonteCarlo Antithetic Halton MLHS Antithetic MLHS
0 0.63785 0.637861 0.63787 0.637839 0.637839 0.637861


.. GENERATED FROM PYTHON SOURCE LINES 116-119 .. code-block:: default print(f'Number of draws: {R}') for c in results.columns: print(f'{c}:\t{results.loc[0,c]}') .. rst-class:: sphx-glr-script-out .. code-block:: none Number of draws: 10000 Numerical: 0.6378498355784457 MonteCarlo: 0.6378606134588761 Antithetic: 0.6378697548073272 Halton: 0.6378394335915274 MLHS: 0.6378393019627554 Antithetic MLHS: 0.6378608572485519 .. rst-class:: sphx-glr-timing **Total running time of the script:** (2 minutes 33.898 seconds) .. _sphx_glr_download_auto_examples_montecarlo_plot_b05normal_mixture_monte_carlo.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_b05normal_mixture_monte_carlo.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_b05normal_mixture_monte_carlo.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_