Mixture of logit models

Example of a uniform mixture of logit models, using Monte-Carlo integration.

author:

Michel Bierlaire, EPFL

date:

Sun Apr 9 17:48:20 2023

import biogeme.biogeme_logging as blog
import biogeme.biogeme as bio
from biogeme import models
from biogeme.expressions import (
    Beta,
    bioDraws,
    exp,
    log,
    MonteCarlo,
)
from biogeme.parameters import Parameters

See the data processing script: Data preparation for Swissmetro.

from swissmetro_data import (
    database,
    CHOICE,
    SM_AV,
    CAR_AV_SP,
    TRAIN_AV_SP,
    TRAIN_TT_SCALED,
    TRAIN_COST_SCALED,
    SM_TT_SCALED,
    SM_COST_SCALED,
    CAR_TT_SCALED,
    CAR_CO_SCALED,
)

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

Parameters to be estimated.

ASC_CAR = Beta('ASC_CAR', 0, None, None, 0)
ASC_TRAIN = Beta('ASC_TRAIN', 0, None, None, 0)
ASC_SM = Beta('ASC_SM', 0, None, None, 1)
B_COST = Beta('B_COST', 0, None, None, 0)

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

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

Definition of the utility functions.

V1 = ASC_TRAIN + B_TIME_RND * TRAIN_TT_SCALED + B_COST * TRAIN_COST_SCALED
V2 = ASC_SM + B_TIME_RND * SM_TT_SCALED + B_COST * SM_COST_SCALED
V3 = ASC_CAR + 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 b_time_rnd, we have a logit model (called the kernel).

prob = exp(models.loglogit(V, av, CHOICE))
# We integrate over b_time_rnd using Monte-Carlo
logprob = log(MonteCarlo(prob))

Create the Biogeme object.

the_biogeme = bio.BIOGEME(database, logprob, number_of_draws=100, seed=1223)
the_biogeme.modelName = 'b06unif_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 __b06unif_mixture.iter
Cannot read file __b06unif_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_TRAIN          B_COST          B_TIME        B_TIME_S     Function    Relgrad   Radius      Rho
    0              -1              -1              -1              -1               2      5.6e+03       0.09        1     0.39    +
    1              -1              -1              -1              -1               2      5.6e+03       0.09      0.5    -0.11    -
    2            -0.5            -1.5            -1.5            -1.3             1.5      5.4e+03      0.074      0.5     0.32    +
    3            -0.5            -1.5            -1.5            -1.3             1.5      5.4e+03      0.074     0.25    -0.17    -
    4           -0.25            -1.2            -1.2              -1             1.8      5.3e+03      0.029     0.25     0.47    +
    5            -0.5              -1              -1            -1.3             1.5      5.3e+03      0.041     0.25     0.16    +
    6            -0.5              -1              -1            -1.3             1.5      5.3e+03      0.041     0.12    -0.11    -
    7           -0.38           -0.88            -1.1            -1.2             1.5      5.3e+03      0.023     0.12     0.63    +
    8           -0.25              -1            -1.2            -1.3             1.4      5.3e+03      0.017     0.12     0.31    +
    9           -0.25           -0.88            -1.1            -1.4             1.5      5.3e+03      0.009     0.12     0.88    +
   10           -0.12           -0.75            -1.3            -1.5             1.7      5.2e+03      0.012     0.12     0.73    +
   11          -0.085           -0.69            -1.1            -1.6             1.7      5.2e+03     0.0075     0.12     0.66    +
   12          -0.027           -0.57            -1.2            -1.7             1.9      5.2e+03     0.0089     0.12     0.88    +
   13          -0.022           -0.59            -1.2            -1.8               2      5.2e+03     0.0095     0.12     0.67    +
   14          0.0095           -0.54            -1.2            -1.8             2.1      5.2e+03     0.0069     0.12     0.84    +
   15           0.019           -0.52            -1.2            -1.9             2.2      5.2e+03     0.0053      1.2      0.9   ++
   16           0.019           -0.52            -1.2            -1.9             2.2      5.2e+03     0.0053     0.62     -2.6    -
   17           0.019           -0.52            -1.2            -1.9             2.2      5.2e+03     0.0053     0.31     -0.4    -
   18           0.019           -0.52            -1.2            -1.9             2.2      5.2e+03     0.0053     0.16     -0.4    -
   19            0.11           -0.49            -1.3              -2             2.4      5.2e+03     0.0068     0.16     0.42    +
   20            0.11           -0.49            -1.3              -2             2.4      5.2e+03     0.0068    0.078    -0.21    -
   21           0.051           -0.43            -1.3              -2             2.4      5.2e+03       0.01    0.078     0.23    +
   22           0.071           -0.46            -1.2            -2.1             2.5      5.2e+03     0.0032    0.078     0.65    +
   23           0.095           -0.46            -1.3            -2.1             2.5      5.2e+03     0.0032    0.078     0.42    +
   24            0.12           -0.42            -1.2            -2.1             2.6      5.2e+03     0.0049    0.078     0.54    +
   25            0.12           -0.42            -1.2            -2.1             2.6      5.2e+03     0.0049    0.039    -0.61    -
   26            0.12           -0.42            -1.2            -2.1             2.6      5.2e+03     0.0049     0.02    -0.26    -
   27           0.098           -0.44            -1.2            -2.2             2.6      5.2e+03     0.0013     0.02     0.73    +
   28            0.11           -0.43            -1.3            -2.2             2.6      5.2e+03     0.0017      0.2     0.95   ++
   29            0.11           -0.43            -1.3            -2.2             2.6      5.2e+03     0.0017    0.098    -0.37    -
   30            0.13            -0.4            -1.2            -2.2             2.7      5.2e+03     0.0018    0.098     0.38    +
   31            0.12           -0.42            -1.3            -2.3             2.8      5.2e+03     0.0014    0.098     0.34    +
   32            0.12           -0.42            -1.3            -2.3             2.8      5.2e+03     0.0014    0.049     -4.9    -
   33            0.12           -0.42            -1.3            -2.3             2.8      5.2e+03     0.0014    0.024     -0.1    -
   34            0.14            -0.4            -1.3            -2.3             2.8      5.2e+03    0.00037    0.024     0.86    +
   35            0.14            -0.4            -1.3            -2.3             2.8      5.2e+03    0.00037    0.011     -4.4    -
   36            0.14            -0.4            -1.3            -2.3             2.8      5.2e+03    0.00037   0.0057    0.018    -
   37            0.13            -0.4            -1.3            -2.3             2.8      5.2e+03    0.00021   0.0057     0.81    +
   38            0.13            -0.4            -1.3            -2.3             2.8      5.2e+03    0.00015   0.0057     0.16    +
   39            0.13            -0.4            -1.3            -2.3             2.8      5.2e+03    0.00015   0.0028     -1.4    -
   40            0.13            -0.4            -1.3            -2.3             2.8      5.2e+03    0.00014   0.0028     0.67    +
   41            0.13            -0.4            -1.3            -2.3             2.8      5.2e+03    0.00014   0.0014    -0.43    -
   42            0.13            -0.4            -1.3            -2.3             2.8      5.2e+03    9.4e-05   0.0014     0.22    -
Results saved in file b06unif_mixture.html
Results saved in file b06unif_mixture.pickle
print(results.short_summary())
Results for model b06unif_mixture
Nbr of parameters:              5
Sample size:                    6768
Excluded data:                  3960
Final log likelihood:           -5217.519
Akaike Information Criterion:   10445.04
Bayesian Information Criterion: 10479.14
pandas_results = results.get_estimated_parameters()
pandas_results
Value Rob. Std err Rob. t-test Rob. p-value
ASC_CAR 0.132965 0.053298 2.494749 1.260463e-02
ASC_TRAIN -0.396974 0.066166 -5.999656 1.977364e-09
B_COST -1.274054 0.086105 -14.796527 0.000000e+00
B_TIME -2.282991 0.125459 -18.197115 0.000000e+00
B_TIME_S 2.812181 0.196404 14.318351 0.000000e+00


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

Gallery generated by Sphinx-Gallery