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

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

See the data processing script: Panel data preparation for Swissmetro.

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')
Example b26triangular_panel_mixture.py

Function generating the draws.

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

Associate the function with a name.

myRandomNumberGenerators = {
    'TRIANGULAR': RandomNumberGeneratorTuple(
        the_triangular_generator,
        'Draws from a triangular distribution',
    )
}

Submit the generator to the database.

database.set_random_number_generators(myRandomNumberGenerators)

Parameters to be estimated.

B_COST = Beta('B_COST', 0, None, None, 0)

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

Mean of the distribution.

B_TIME = Beta('B_TIME', 0, None, None, 0)

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

B_TIME_S = Beta('B_TIME_S', 1, None, None, 0)
B_TIME_RND = B_TIME + B_TIME_S * bioDraws('b_time_rnd', 'TRIANGULAR')

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

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

Definition of the utility functions.

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

Associate utility functions with the numbering of alternatives.

V = {1: V1, 2: V2, 3: V3}

Associate the availability conditions with the alternatives.

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

Conditional to the random parameters, the likelihood of one observation is given by the logit model (called the kernel).

obsprob = models.logit(V, av, CHOICE)

Conditional on the random parameters, the likelihood of all observations for one individual (the trajectory) is the product of the likelihood of each observation.

condprobIndiv = PanelLikelihoodTrajectory(obsprob)

We integrate over the random parameters using Monte-Carlo

logprob = log(MonteCarlo(condprobIndiv))

As the objective is to illustrate the syntax, we calculate the Monte-Carlo approximation with a small number of draws.

the_biogeme = bio.BIOGEME(database, logprob, number_of_draws=100, seed=1223)
the_biogeme.modelName = 'b26triangular_panel_mixture'
Biogeme parameters read from biogeme.toml.

Estimate the parameters.

results = the_biogeme.estimate()
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
print(results.short_summary())
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
pandas_results = results.get_estimated_parameters()
pandas_results
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


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

Gallery generated by Sphinx-Gallery