16. Latent class model with panel data

Bayesian estimation of a discrete mixture of logit models, also called latent class model. The class membership model includes socio-economic variables. The datafile is organized as panel data.

Michel Bierlaire, EPFL Sat Nov 15 2025, 18:12:05

from pathlib import Path

from IPython.core.display_functions import display

See the data processing script swissmetro_panel.py.

from swissmetro_panel import (
    CAR_AV_SP,
    CAR_CO_SCALED,
    CAR_TT_SCALED,
    CHOICE,
    INCOME,
    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.bayesian_estimation import (
    BayesianResults,
    BayesianResultsSummary,
    get_pandas_estimated_parameters,
)
from biogeme.biogeme import BIOGEME
from biogeme.expressions import (
    Beta,
    DistributedParameter,
    Draws,
    exp,
    log,
)
from biogeme.models import logit

logger = blog.get_screen_logger(level=blog.INFO)
logger.info('Example b16_panel_discrete_socio_eco.py')
Example b16_panel_discrete_socio_eco.py

Parameters to be estimated. One version for each latent_old class.

NUMBER_OF_CLASSES = 2
b_cost = [Beta(f'b_cost_class{i}', 0, None, None, 0) for i in range(NUMBER_OF_CLASSES)]

Define a random parameter, normally distributed across individuals, designed to be used for Monte-Carlo simulation.

b_time = [Beta(f'b_time_class{i}', 0, None, None, 0) for i in range(NUMBER_OF_CLASSES)]

It is advised not to use 0 as starting value for the following parameter.

b_time_s = [
    Beta(f'b_time_s_class{i}', 1, None, None, 0) for i in range(NUMBER_OF_CLASSES)
]
b_time_rnd = [
    DistributedParameter(
        f'b_time_rnd_class{i}',
        b_time[i] + b_time_s[i] * Draws(f'b_time_eps_class{i}', 'NORMAL'),
    )
    for i in range(NUMBER_OF_CLASSES)
]

We do the same for the constants, to address serial correlation.

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 = [
    DistributedParameter(
        f'asc_car_rnd_class{i}',
        asc_car[i] + asc_car_s[i] * Draws(f'asc_car_eps_class{i}', 'NORMAL'),
    )
    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 = [
    DistributedParameter(
        f'asc_train_rnd_class{i}',
        asc_train[i] + asc_train_s[i] * Draws(f'asc_train_eps_class{i}', 'NORMAL'),
    )
    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 = [
    DistributedParameter(
        f'asc_sm_rnd_class{i}',
        asc_sm[i] + asc_sm_s[i] * Draws(f'asc_sm_eps_class{i}', 'NORMAL'),
    )
    for i in range(NUMBER_OF_CLASSES)
]

Parameters for the class membership model.

class_cte = Beta('class_cte', 0, None, None, 0)
class_inc = Beta('class_inc', 0, None, None, 0)

In class 0, it is assumed that the time coefficient is zero

b_time_rnd[0] = 0

Utility functions.

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)
]

Associate the availability conditions with the alternatives

av = {1: TRAIN_AV_SP, 2: SM_AV, 3: CAR_AV_SP}

The choice model is a discrete mixture of logit, with availability conditions We calculate the conditional probability for each class.

conditional_probability_per_class = [
    logit(v_per_class[i], av, CHOICE) for i in range(NUMBER_OF_CLASSES)
]

Class membership model.

score_class_0 = class_cte + class_inc * INCOME
probability_class_1 = 1 / (1 + exp(score_class_0))
probability_class_0 = 1 - probability_class_1

Conditional on the random variables, likelihood for the individual.

conditional_choice_probability = (
    probability_class_0 * conditional_probability_per_class[0]
    + probability_class_1 * conditional_probability_per_class[1]
)

We need the log probability per observation

conditional_log_probability = log(conditional_choice_probability)

%%

the_biogeme = BIOGEME(
    database,
    conditional_log_probability,
    warmup=40,
    bayesian_draws=40,
    chains=1,
)
the_biogeme.model_name = 'b16_panel_discrete_socio_eco'
Biogeme parameters read from biogeme.toml.

Estimate the posterior distribution of the parameters, or read the results if already available.

yaml_file = Path('saved_results') / f'{the_biogeme.model_name}.yaml'
try:
    summary_results = BayesianResultsSummary.from_yaml_file(filename=yaml_file)
except FileNotFoundError:
    results: BayesianResults = the_biogeme.bayesian_estimation()
    summary_results = results.to_summary()
print(summary_results.short_summary())
Sample size                                              6768
Sampler                                                  NUTS
Number of chains                                         1
Number of draws per chain                                40
Total number of draws                                    40
Acceptance rate target                                   0.9
Run time                                                 0:00:58.674240
Posterior predictive log-likelihood (sum of log mean p)  -2154.52
Expected log-likelihood E[log L(Y|θ)]                    -2346.52
Best-draw log-likelihood (posterior upper bound)         -2280.37
LOO (Leave-One-Out Cross-Validation)                     -2651.27
LOO Standard Error                                       74.13
Effective number of parameters (p_LOO)                   496.75

Present the parameter estimates in a pandas table.

pandas_results = get_pandas_estimated_parameters(
    estimation_results=summary_results,
)
display(pandas_results)
                  Name  Value (mean)  ...  ESS (bulk)  ESS (tail)
0            class_cte     -2.593753  ...    2.293440    7.440039
1            class_inc     -0.253694  ...   14.733293   12.448812
2     asc_train_class0     -6.980200  ...    4.942098    5.459770
3   asc_train_s_class0     -2.831652  ...    1.958333   13.439434
4        b_cost_class0     -0.764623  ...    7.196165   10.133333
5      asc_sm_s_class0     11.835171  ...   26.434782   26.358728
6       asc_car_class0     -1.977684  ...    1.997318    5.459770
7     asc_car_s_class0      0.461648  ...    1.754327    8.553742
8     asc_train_class1      0.303439  ...    5.747487   17.130537
9   asc_train_s_class1     -0.595310  ...    4.552773    6.322795
10       b_time_class1     -7.977100  ...    3.755834    5.422761
11     b_time_s_class1      4.987208  ...    3.316032   20.957976
12       b_cost_class1     -5.267833  ...    3.502352    5.422761
13     asc_sm_s_class1     -2.616948  ...   15.108897    5.733685
14      asc_car_class1      0.698826  ...   19.398671   33.611418
15    asc_car_s_class1     -4.842079  ...    2.386388   11.506435

[16 rows x 12 columns]

Report the variables stored in the Bayesian estimation results.

display(summary_results.report_stored_variables())
            group       variable           dims    shape
0   constant_data      CAR_AV_SP          [obs]   [6768]
1   constant_data  CAR_CO_SCALED          [obs]   [6768]
2   constant_data  CAR_TT_SCALED          [obs]   [6768]
3   constant_data         CHOICE          [obs]   [6768]
4   constant_data         INCOME          [obs]   [6768]
..            ...            ...            ...      ...
90   sample_stats         energy  [chain, draw]  [1, 40]
91   sample_stats             lp  [chain, draw]  [1, 40]
92   sample_stats        n_steps  [chain, draw]  [1, 40]
93   sample_stats      step_size  [chain, draw]  [1, 40]
94   sample_stats     tree_depth  [chain, draw]  [1, 40]

[95 rows x 4 columns]

Total running time of the script: (0 minutes 0.049 seconds)

Gallery generated by Sphinx-Gallery