.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/swissmetro/plot_b26triangular_panel_mixture.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_swissmetro_plot_b26triangular_panel_mixture.py: Triangular mixture with panel data ================================== Example of a mixture of logit models, using Monte-Carlo integration. The mixing distribution is user-defined (triangular, here). The datafile is organized as panel data. :author: Michel Bierlaire, EPFL :date: Tue Dec 6 18:30:44 2022 .. GENERATED FROM PYTHON SOURCE LINES 14-29 .. code-block:: Python import numpy as np import biogeme.biogeme as bio from biogeme import models import biogeme.biogeme_logging as blog from biogeme.expressions import ( Beta, bioDraws, MonteCarlo, PanelLikelihoodTrajectory, log, ) from biogeme.native_draws import RandomNumberGeneratorTuple from biogeme.parameters import Parameters .. GENERATED FROM PYTHON SOURCE LINES 30-31 See the data processing script: :ref:`swissmetro_panel`. .. GENERATED FROM PYTHON SOURCE LINES 31-49 .. code-block:: Python from swissmetro_panel import ( database, CHOICE, CAR_AV_SP, TRAIN_AV_SP, TRAIN_TT_SCALED, TRAIN_COST_SCALED, SM_TT_SCALED, SM_COST_SCALED, CAR_TT_SCALED, CAR_CO_SCALED, SM_AV, ) logger = blog.get_screen_logger(level=blog.INFO) logger.info('Example b26triangular_panel_mixture.py') .. rst-class:: sphx-glr-script-out .. code-block:: none Example b26triangular_panel_mixture.py .. GENERATED FROM PYTHON SOURCE LINES 50-51 Function generating the draws. .. GENERATED FROM PYTHON SOURCE LINES 51-59 .. code-block:: Python def the_triangular_generator(sample_size: int, number_of_draws: int) -> np.ndarray: """ Provide my own random number generator to the database. See the numpy.random documentation to obtain a list of other distributions. """ return np.random.triangular(-1, 0, 1, (sample_size, number_of_draws)) .. GENERATED FROM PYTHON SOURCE LINES 60-61 Associate the function with a name. .. GENERATED FROM PYTHON SOURCE LINES 61-68 .. code-block:: Python myRandomNumberGenerators = { 'TRIANGULAR': RandomNumberGeneratorTuple( the_triangular_generator, 'Draws from a triangular distribution', ) } .. GENERATED FROM PYTHON SOURCE LINES 69-70 Submit the generator to the database. .. GENERATED FROM PYTHON SOURCE LINES 70-72 .. code-block:: Python database.set_random_number_generators(myRandomNumberGenerators) .. GENERATED FROM PYTHON SOURCE LINES 73-74 Parameters to be estimated. .. GENERATED FROM PYTHON SOURCE LINES 74-76 .. code-block:: Python B_COST = Beta('B_COST', 0, None, None, 0) .. GENERATED FROM PYTHON SOURCE LINES 77-79 Define a random parameter, normally distributed across individuals, designed to be used for Monte-Carlo simulation. .. GENERATED FROM PYTHON SOURCE LINES 81-82 Mean of the distribution. .. GENERATED FROM PYTHON SOURCE LINES 82-84 .. code-block:: Python B_TIME = Beta('B_TIME', 0, None, None, 0) .. GENERATED FROM PYTHON SOURCE LINES 85-87 Scale of the distribution. It is advised not to use 0 as starting value for the following parameter. .. GENERATED FROM PYTHON SOURCE LINES 87-90 .. code-block:: Python B_TIME_S = Beta('B_TIME_S', 1, None, None, 0) B_TIME_RND = B_TIME + B_TIME_S * bioDraws('b_time_rnd', 'TRIANGULAR') .. GENERATED FROM PYTHON SOURCE LINES 91-92 We do the same for the constants, to address serial correlation. .. GENERATED FROM PYTHON SOURCE LINES 92-104 .. code-block:: Python ASC_CAR = Beta('ASC_CAR', 0, None, None, 0) ASC_CAR_S = Beta('ASC_CAR_S', 1, None, None, 0) ASC_CAR_RND = ASC_CAR + ASC_CAR_S * bioDraws('ASC_CAR_RND', 'TRIANGULAR') ASC_TRAIN = Beta('ASC_TRAIN', 0, None, None, 0) ASC_TRAIN_S = Beta('ASC_TRAIN_S', 1, None, None, 0) ASC_TRAIN_RND = ASC_TRAIN + ASC_TRAIN_S * bioDraws('ASC_TRAIN_RND', 'TRIANGULAR') ASC_SM = Beta('ASC_SM', 0, None, None, 1) ASC_SM_S = Beta('ASC_SM_S', 1, None, None, 0) ASC_SM_RND = ASC_SM + ASC_SM_S * bioDraws('ASC_SM_RND', 'TRIANGULAR') .. GENERATED FROM PYTHON SOURCE LINES 105-106 Definition of the utility functions. .. GENERATED FROM PYTHON SOURCE LINES 106-110 .. code-block:: Python V1 = ASC_TRAIN_RND + B_TIME_RND * TRAIN_TT_SCALED + B_COST * TRAIN_COST_SCALED V2 = ASC_SM_RND + B_TIME_RND * SM_TT_SCALED + B_COST * SM_COST_SCALED V3 = ASC_CAR_RND + B_TIME_RND * CAR_TT_SCALED + B_COST * CAR_CO_SCALED .. GENERATED FROM PYTHON SOURCE LINES 111-112 Associate utility functions with the numbering of alternatives. .. GENERATED FROM PYTHON SOURCE LINES 112-114 .. code-block:: Python V = {1: V1, 2: V2, 3: V3} .. GENERATED FROM PYTHON SOURCE LINES 115-116 Associate the availability conditions with the alternatives. .. GENERATED FROM PYTHON SOURCE LINES 116-118 .. code-block:: Python av = {1: TRAIN_AV_SP, 2: SM_AV, 3: CAR_AV_SP} .. GENERATED FROM PYTHON SOURCE LINES 119-121 Conditional to the random parameters, the likelihood of one observation is given by the logit model (called the kernel). .. GENERATED FROM PYTHON SOURCE LINES 121-123 .. code-block:: Python obsprob = models.logit(V, av, CHOICE) .. GENERATED FROM PYTHON SOURCE LINES 124-127 Conditional on the random parameters, the likelihood of all observations for one individual (the trajectory) is the product of the likelihood of each observation. .. GENERATED FROM PYTHON SOURCE LINES 127-129 .. code-block:: Python condprobIndiv = PanelLikelihoodTrajectory(obsprob) .. GENERATED FROM PYTHON SOURCE LINES 130-131 We integrate over the random parameters using Monte-Carlo .. GENERATED FROM PYTHON SOURCE LINES 131-133 .. code-block:: Python logprob = log(MonteCarlo(condprobIndiv)) .. GENERATED FROM PYTHON SOURCE LINES 134-137 As the objective is to illustrate the syntax, we calculate the Monte-Carlo approximation with a small number of draws. .. GENERATED FROM PYTHON SOURCE LINES 137-140 .. code-block:: Python the_biogeme = bio.BIOGEME(database, logprob, number_of_draws=100, seed=1223) the_biogeme.modelName = 'b26triangular_panel_mixture' .. rst-class:: sphx-glr-script-out .. code-block:: none Biogeme parameters read from biogeme.toml. .. GENERATED FROM PYTHON SOURCE LINES 141-142 Estimate the parameters. .. GENERATED FROM PYTHON SOURCE LINES 142-144 .. code-block:: Python results = the_biogeme.estimate() .. rst-class:: sphx-glr-script-out .. code-block:: none As the model is rather complex, we cancel the calculation of second derivatives. If you want to control the parameters, change the name of the algorithm in the TOML file from "automatic" to "simple_bounds" *** Initial values of the parameters are obtained from the file __b26triangular_panel_mixture.iter Cannot read file __b26triangular_panel_mixture.iter. Statement is ignored. The number of draws (100) is low. The results may not be meaningful. As the model is rather complex, we cancel the calculation of second derivatives. If you want to control the parameters, change the name of the algorithm in the TOML file from "automatic" to "simple_bounds" Optimization algorithm: hybrid Newton/BFGS with simple bounds [simple_bounds] ** Optimization: BFGS with trust region for simple bounds Iter. ASC_CAR ASC_CAR_S ASC_SM_S ASC_TRAIN ASC_TRAIN_S B_COST B_TIME B_TIME_S Function Relgrad Radius Rho 0 1 2 2 -1 2 -1 -1 2 4.5e+03 0.069 1 0.4 + 1 1.1e-16 3 3 0 3 0 -2 3 4.1e+03 0.044 1 0.26 + 2 0.26 3.3 3.1 -0.43 3.1 -1 -2.5 3 3.9e+03 0.04 10 1.2 ++ 3 0.26 3.3 3.1 -0.43 3.1 -1 -2.5 3 3.9e+03 0.04 5 -6.5 - 4 0.26 3.3 3.1 -0.43 3.1 -1 -2.5 3 3.9e+03 0.04 2.5 -4 - 5 0.26 3.3 3.1 -0.43 3.1 -1 -2.5 3 3.9e+03 0.04 1.2 -2 - 6 0.26 3.3 3.1 -0.43 3.1 -1 -2.5 3 3.9e+03 0.04 0.62 -0.5 - 7 0.42 3.9 3.4 -1.1 3.5 -1.6 -3.1 2.9 3.9e+03 0.052 0.62 0.53 + 8 0.23 4.1 3.6 -0.69 3.9 -2 -2.7 3.6 3.8e+03 0.027 0.62 0.64 + 9 0.36 4.4 3.7 -1 4.1 -1.9 -3.1 4.2 3.8e+03 0.014 0.62 0.88 + 10 -0.063 4.8 3.7 -0.65 4.4 -1.9 -3.7 4.5 3.7e+03 0.022 0.62 0.78 + 11 -0.063 4.8 3.7 -0.65 4.4 -1.9 -3.7 4.5 3.7e+03 0.022 0.31 -0.9 - 12 -0.063 4.8 3.7 -0.65 4.4 -1.9 -3.7 4.5 3.7e+03 0.022 0.16 -0.23 - 13 0.093 4.9 3.9 -0.71 4.6 -2.1 -3.9 4.3 3.7e+03 0.044 0.16 0.32 + 14 0.14 5 3.9 -0.69 4.6 -2.1 -3.7 4.5 3.7e+03 0.029 0.16 0.68 + 15 0.2 5.1 3.9 -0.66 4.7 -2.1 -3.9 4.5 3.7e+03 0.02 1.6 0.91 ++ 16 0.82 6.6 4.1 -0.19 5.6 -2.4 -4.8 5.8 3.7e+03 0.039 1.6 0.55 + 17 0.82 6.6 4.1 -0.19 5.6 -2.4 -4.8 5.8 3.7e+03 0.039 0.78 -1.1 - 18 0.036 6.9 4.1 -0.25 5.6 -2.3 -5 6 3.7e+03 0.014 0.78 0.15 + 19 0.036 6.9 4.1 -0.25 5.6 -2.3 -5 6 3.7e+03 0.014 0.39 -0.49 - 20 0.43 7 3.9 -0.48 5.6 -2.6 -5.1 6 3.7e+03 0.01 0.39 0.7 + 21 0.48 7.4 3.8 -0.26 5.7 -2.4 -5.1 6.1 3.7e+03 0.007 0.39 0.5 + 22 0.25 7.8 3.5 -0.19 5.8 -2.8 -5.3 6.3 3.7e+03 0.015 0.39 0.47 + 23 0.38 8.1 3.3 -0.58 5.7 -2.7 -5.3 6.4 3.7e+03 0.0069 0.39 0.18 + 24 0.41 8.5 3.1 -0.26 6 -2.5 -5.4 6.5 3.7e+03 0.0076 0.39 0.46 + 25 0.29 8.8 2.7 -0.24 6.1 -2.8 -5.4 6.5 3.7e+03 0.0057 0.39 0.57 + 26 0.29 8.8 2.7 -0.24 6.1 -2.8 -5.4 6.5 3.7e+03 0.0057 0.2 -0.89 - 27 0.12 8.8 2.7 -0.43 6.1 -2.8 -5.5 6.6 3.7e+03 0.0038 0.2 0.22 + 28 0.32 8.9 2.8 -0.35 6.1 -2.8 -5.5 6.6 3.7e+03 0.0029 0.2 0.74 + 29 0.32 8.9 2.8 -0.35 6.1 -2.8 -5.5 6.6 3.7e+03 0.0029 0.098 -0.85 - 30 0.32 8.9 2.8 -0.35 6.1 -2.8 -5.5 6.6 3.7e+03 0.0029 0.049 -0.44 - 31 0.27 8.9 2.8 -0.33 6.1 -2.8 -5.5 6.6 3.7e+03 0.0031 0.049 0.47 + 32 0.28 8.9 2.8 -0.37 6.2 -2.8 -5.6 6.7 3.7e+03 0.001 0.049 0.54 + 33 0.29 8.9 2.8 -0.33 6.2 -2.8 -5.6 6.7 3.7e+03 0.0022 0.049 0.76 + 34 0.27 9 2.8 -0.34 6.2 -2.8 -5.6 6.7 3.7e+03 0.00035 0.49 0.96 ++ 35 0.27 9 2.8 -0.34 6.2 -2.8 -5.6 6.7 3.7e+03 0.00035 0.066 -0.48 - 36 0.28 9.1 2.8 -0.34 6.2 -2.8 -5.6 6.7 3.7e+03 0.00018 0.066 0.79 + 37 0.28 9.1 2.8 -0.34 6.2 -2.8 -5.6 6.7 3.7e+03 0.00018 0.018 -2 - 38 0.28 9.1 2.8 -0.34 6.2 -2.8 -5.6 6.7 3.7e+03 0.00018 0.0091 0.0077 - 39 0.28 9.1 2.8 -0.34 6.2 -2.8 -5.6 6.7 3.7e+03 6e-05 0.0091 0.62 - Results saved in file b26triangular_panel_mixture.html Results saved in file b26triangular_panel_mixture.pickle .. GENERATED FROM PYTHON SOURCE LINES 145-147 .. code-block:: Python print(results.short_summary()) .. rst-class:: sphx-glr-script-out .. code-block:: none Results for model b26triangular_panel_mixture Nbr of parameters: 8 Sample size: 752 Observations: 6768 Excluded data: 3960 Final log likelihood: -3656.133 Akaike Information Criterion: 7328.266 Bayesian Information Criterion: 7365.248 .. GENERATED FROM PYTHON SOURCE LINES 148-150 .. code-block:: Python pandas_results = results.get_estimated_parameters() pandas_results .. raw:: html
Value Rob. Std err Rob. t-test Rob. p-value
ASC_CAR 0.272879 0.260035 1.049395 2.939962e-01
ASC_CAR_S 9.060495 0.918376 9.865782 0.000000e+00
ASC_SM_S 2.845355 0.545474 5.216297 1.825359e-07
ASC_TRAIN -0.337910 0.227348 -1.486311 1.371970e-01
ASC_TRAIN_S 6.197271 0.764653 8.104689 4.440892e-16
B_COST -2.840325 0.360247 -7.884375 3.108624e-15
B_TIME -5.625062 0.339386 -16.574249 0.000000e+00
B_TIME_S 6.751839 0.399455 16.902640 0.000000e+00


.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 21.121 seconds) .. _sphx_glr_download_auto_examples_swissmetro_plot_b26triangular_panel_mixture.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_b26triangular_panel_mixture.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_b26triangular_panel_mixture.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_b26triangular_panel_mixture.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_