.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/swissmetro/plot_b15b_panel_discrete.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_b15b_panel_discrete.py: 15b. Discrete mixture with panel data ===================================== Example of a discrete mixture of logit models, also called latent class model. The datafile is organized as panel data. Compared to :ref:`plot_b15a_panel_discrete`, we integrate before the discrete mixture to show that it is equivalent. Michel Bierlaire, EPFL Sat Jun 21 2025, 17:22:38 .. GENERATED FROM PYTHON SOURCE LINES 15-18 .. code-block:: Python from IPython.core.display_functions import display .. GENERATED FROM PYTHON SOURCE LINES 19-20 See the data processing script ``swissmetro_panel.py``. .. GENERATED FROM PYTHON SOURCE LINES 20-54 .. code-block:: Python from swissmetro_panel import ( CAR_AV_SP, CAR_CO_SCALED, CAR_TT_SCALED, CHOICE, SM_AV, SM_COST_SCALED, SM_TT_SCALED, TRAIN_AV_SP, TRAIN_COST_SCALED, TRAIN_TT_SCALED, database, ) import biogeme.biogeme_logging as blog from biogeme.biogeme import BIOGEME from biogeme.expressions import ( Beta, Draws, ExpressionOrNumeric, MonteCarlo, PanelLikelihoodTrajectory, log, ) from biogeme.models import logit from biogeme.results_processing import ( EstimationResults, get_pandas_estimated_parameters, ) logger = blog.get_screen_logger(level=blog.INFO) logger.info('Example b15panel_discrete_bis.py') .. rst-class:: sphx-glr-script-out .. code-block:: none Example b15panel_discrete_bis.py .. GENERATED FROM PYTHON SOURCE LINES 55-56 Parameters to be estimated. One version for each latent class. .. GENERATED FROM PYTHON SOURCE LINES 56-59 .. code-block:: Python NUMBER_OF_CLASSES = 2 b_cost = [Beta(f'b_cost_class{i}', 0, None, None, 0) for i in range(NUMBER_OF_CLASSES)] .. GENERATED FROM PYTHON SOURCE LINES 60-62 Define a random parameter, normally distributed across individuals, designed to be used for Monte-Carlo simulation. .. GENERATED FROM PYTHON SOURCE LINES 62-64 .. code-block:: Python b_time = [Beta(f'b_time_class{i}', 0, None, None, 0) for i in range(NUMBER_OF_CLASSES)] .. GENERATED FROM PYTHON SOURCE LINES 65-66 It is advised not to use 0 as starting value for the following parameter. .. GENERATED FROM PYTHON SOURCE LINES 66-74 .. code-block:: Python b_time_s = [ Beta(f'b_time_s_class{i}', 1, None, None, 0) for i in range(NUMBER_OF_CLASSES) ] b_time_rnd: list[ExpressionOrNumeric] = [ b_time[i] + b_time_s[i] * Draws(f'b_time_rnd_class{i}', 'NORMAL_ANTI') for i in range(NUMBER_OF_CLASSES) ] .. GENERATED FROM PYTHON SOURCE LINES 75-76 We do the same for the constants, to address serial correlation. .. GENERATED FROM PYTHON SOURCE LINES 76-108 .. code-block:: Python asc_car = [ Beta(f'asc_car_class{i}', 0, None, None, 0) for i in range(NUMBER_OF_CLASSES) ] asc_car_s = [ Beta(f'asc_car_s_class{i}', 1, None, None, 0) for i in range(NUMBER_OF_CLASSES) ] asc_car_rnd = [ asc_car[i] + asc_car_s[i] * Draws(f'asc_car_rnd_class{i}', 'NORMAL_ANTI') for i in range(NUMBER_OF_CLASSES) ] asc_train = [ Beta(f'asc_train_class{i}', 0, None, None, 0) for i in range(NUMBER_OF_CLASSES) ] asc_train_s = [ Beta(f'asc_train_s_class{i}', 1, None, None, 0) for i in range(NUMBER_OF_CLASSES) ] ASC_TRAIN_RND = [ asc_train[i] + asc_train_s[i] * Draws(f'asc_train_rnd_class{i}', 'NORMAL_ANTI') for i in range(NUMBER_OF_CLASSES) ] asc_sm = [Beta(f'asc_sm_class{i}', 0, None, None, 1) for i in range(NUMBER_OF_CLASSES)] asc_sm_s = [ Beta(f'asc_sm_s_class{i}', 1, None, None, 0) for i in range(NUMBER_OF_CLASSES) ] asc_sm_rnd = [ asc_sm[i] + asc_sm_s[i] * Draws(f'asc_sm_rnd_class{i}', 'NORMAL_ANTI') for i in range(NUMBER_OF_CLASSES) ] .. GENERATED FROM PYTHON SOURCE LINES 109-116 Parameter groups used in the generated reports. Each group contains the estimated parameters associated with one latent class. Fixed parameters and parameters that are replaced by identification constraints are not listed here, as they do not appear in the estimation results. The class membership parameter is not included in either group, and will therefore be reported in the automatically generated ``Other parameters`` section. .. GENERATED FROM PYTHON SOURCE LINES 116-137 .. code-block:: Python PARAMETER_GROUPS = { 'Class 0': [ 'b_cost_class0', 'asc_car_class0', 'asc_car_s_class0', 'asc_train_class0', 'asc_train_s_class0', 'asc_sm_s_class0', ], 'Class 1': [ 'b_cost_class1', 'b_time_class1', 'b_time_s_class1', 'asc_car_class1', 'asc_car_s_class1', 'asc_train_class1', 'asc_train_s_class1', 'asc_sm_s_class1', ], } .. GENERATED FROM PYTHON SOURCE LINES 138-139 Class membership probability. .. GENERATED FROM PYTHON SOURCE LINES 139-143 .. code-block:: Python score_class_0 = Beta('score_class_0', 0, None, None, 0) probability_class_0 = logit({0: score_class_0, 1: 0}, None, 0) probability_class_1 = logit({0: score_class_0, 1: 0}, None, 1) .. GENERATED FROM PYTHON SOURCE LINES 144-145 In class 0, it is assumed that the time coefficient is zero. .. GENERATED FROM PYTHON SOURCE LINES 145-147 .. code-block:: Python b_time_rnd[0] = 0 .. GENERATED FROM PYTHON SOURCE LINES 148-149 Utility functions. .. GENERATED FROM PYTHON SOURCE LINES 149-166 .. code-block:: Python v_train_per_class = [ ASC_TRAIN_RND[i] + b_time_rnd[i] * TRAIN_TT_SCALED + b_cost[i] * TRAIN_COST_SCALED for i in range(NUMBER_OF_CLASSES) ] v_swissmetro_per_class = [ asc_sm_rnd[i] + b_time_rnd[i] * SM_TT_SCALED + b_cost[i] * SM_COST_SCALED for i in range(NUMBER_OF_CLASSES) ] v_car_per_class = [ asc_car_rnd[i] + b_time_rnd[i] * CAR_TT_SCALED + b_cost[i] * CAR_CO_SCALED for i in range(NUMBER_OF_CLASSES) ] v_per_class = [ {1: v_train_per_class[i], 2: v_swissmetro_per_class[i], 3: v_car_per_class[i]} for i in range(NUMBER_OF_CLASSES) ] .. GENERATED FROM PYTHON SOURCE LINES 167-168 Associate the availability conditions with the alternatives .. GENERATED FROM PYTHON SOURCE LINES 168-170 .. code-block:: Python av = {1: TRAIN_AV_SP, 2: SM_AV, 3: CAR_AV_SP} .. GENERATED FROM PYTHON SOURCE LINES 171-173 The choice model is a discrete mixture of logit, with availability conditions We calculate the conditional probability for each class. .. GENERATED FROM PYTHON SOURCE LINES 173-178 .. code-block:: Python choice_probability_per_class = [ MonteCarlo(PanelLikelihoodTrajectory(logit(v_per_class[i], av, CHOICE))) for i in range(NUMBER_OF_CLASSES) ] .. GENERATED FROM PYTHON SOURCE LINES 179-180 Conditional to the random variables, likelihood for the individual. .. GENERATED FROM PYTHON SOURCE LINES 180-185 .. code-block:: Python choice_probability = ( probability_class_0 * choice_probability_per_class[0] + probability_class_1 * choice_probability_per_class[1] ) .. GENERATED FROM PYTHON SOURCE LINES 186-187 We integrate over the random variables using Monte-Carlo. .. GENERATED FROM PYTHON SOURCE LINES 187-189 .. code-block:: Python log_probability = log(choice_probability) .. GENERATED FROM PYTHON SOURCE LINES 190-193 The model is complex, and there are numerical issues when calculating the second derivatives. Therefore, we instruct Biogeme not to evaluate the second derivatives. As a consequence, the statistics reported after estimation are based on the BHHH matrix instead of the Rao-Cramer bound. .. GENERATED FROM PYTHON SOURCE LINES 193-203 .. code-block:: Python the_biogeme = BIOGEME( database, log_probability, number_of_draws=5_000, seed=1223, calculating_second_derivatives='never', group_of_parameters=PARAMETER_GROUPS, ) the_biogeme.model_name = 'b15b_panel_discrete' .. rst-class:: sphx-glr-script-out .. code-block:: none Biogeme parameters read from biogeme.toml. .. GENERATED FROM PYTHON SOURCE LINES 204-205 Estimate the parameters. .. GENERATED FROM PYTHON SOURCE LINES 205-212 .. code-block:: Python try: results = EstimationResults.from_yaml_file( filename=f'saved_results/{the_biogeme.model_name}.yaml' ) except FileNotFoundError: results = the_biogeme.estimate() .. GENERATED FROM PYTHON SOURCE LINES 213-215 .. code-block:: Python print(results.short_summary()) .. rst-class:: sphx-glr-script-out .. code-block:: none Results for model b15b_panel_discrete Nbr of parameters: 15 Sample size: 752 Observations: 6768 Excluded data: 0 Final log likelihood: -3524.886 Akaike Information Criterion: 7079.772 Bayesian Information Criterion: 7149.113 .. GENERATED FROM PYTHON SOURCE LINES 216-223 .. code-block:: Python pandas_results = get_pandas_estimated_parameters( estimation_results=results, group_of_parameters=PARAMETER_GROUPS, ) for group_name, pandas_table in pandas_results.items(): display(group_name if group_name else 'Estimated parameters') display(pandas_table) .. rst-class:: sphx-glr-script-out .. code-block:: none Class 0 Name Value BHHH std err. BHHH t-stat. BHHH p-value 1 asc_train_class0 -1.048732 0.683273 -1.534866 0.124817 2 asc_train_s_class0 3.112293 0.646691 4.812644 0.000001 3 b_cost_class0 -1.172174 0.536181 -2.186154 0.028804 4 asc_sm_s_class0 0.038444 6.378392 0.006027 0.995191 5 asc_car_class0 -4.863414 1.627290 -2.988658 0.002802 6 asc_car_s_class0 6.071837 1.855820 3.271780 0.001069 Class 1 Name Value BHHH std err. BHHH t-stat. BHHH p-value 7 asc_train_class1 -0.319711 0.284556 -1.123542 2.612073e-01 8 asc_train_s_class1 1.756490 0.457984 3.835264 1.254297e-04 9 b_time_class1 -6.956781 0.367292 -18.940748 0.000000e+00 10 b_time_s_class1 3.281235 0.353770 9.275053 0.000000e+00 11 b_cost_class1 -4.756458 0.253123 -18.791084 0.000000e+00 12 asc_sm_s_class1 1.698737 0.333604 5.092070 3.541753e-07 13 asc_car_class1 1.020537 0.215775 4.729635 2.249236e-06 14 asc_car_s_class1 2.875177 0.260602 11.032840 0.000000e+00 Other parameters Name Value BHHH std err. BHHH t-stat. BHHH p-value 0 score_class_0 -1.759754 0.226227 -7.778722 7.327472e-15 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.034 seconds) .. _sphx_glr_download_auto_examples_swissmetro_plot_b15b_panel_discrete.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_b15b_panel_discrete.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_b15b_panel_discrete.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_b15b_panel_discrete.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_