Catalog for alternative specific coefficientsΒΆ

Investigate alternative specific parameters:

  • two specifications for the travel time coefficient: generic, and alternative specific,

  • two specifications for the travel cost coefficient: generic, and alternative specific,

for a total of 4 specifications. See Bierlaire and Ortelli (2023).

Michel Bierlaire, EPFL Sun Apr 27 2025, 15:49:05

from IPython.core.display_functions import display

import biogeme.biogeme_logging as blog
from biogeme.biogeme import BIOGEME
from biogeme.catalog import generic_alt_specific_catalogs
from biogeme.data.swissmetro 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,
    read_data,
)
from biogeme.expressions import Beta
from biogeme.models import loglogit
from biogeme.results_processing import compile_estimation_results, pareto_optimal

logger = blog.get_screen_logger(level=blog.INFO)

Parameters to be estimated.

asc_car = Beta('asc_car', 0, None, None, 0)
asc_train = Beta('asc_train', 0, None, None, 0)
b_time = Beta('b_time', 0, None, None, 0)
b_cost = Beta('b_cost', 0, None, None, 0)

Catalog for travel time coefficient.

(b_time_catalog_dict,) = generic_alt_specific_catalogs(
    generic_name='b_time',
    beta_parameters=[b_time],
    alternatives=('train', 'swissmetro', 'car'),
)

Catalog for travel cost coefficient.

(b_cost_catalog_dict,) = generic_alt_specific_catalogs(
    generic_name='b_cost',
    beta_parameters=[b_cost],
    alternatives=('train', 'swissmetro', 'car'),
)

Definition of the utility functions.

v_train = (
    asc_train
    + b_time_catalog_dict['train'] * TRAIN_TT_SCALED
    + b_cost_catalog_dict['train'] * TRAIN_COST_SCALED
)
v_swissmetro = (
    b_time_catalog_dict['swissmetro'] * SM_TT_SCALED
    + b_cost_catalog_dict['swissmetro'] * SM_COST_SCALED
)
v_car = (
    asc_car
    + b_time_catalog_dict['car'] * CAR_TT_SCALED
    + b_cost_catalog_dict['car'] * CAR_CO_SCALED
)

Associate utility functions with the numbering of alternatives.

v = {1: v_train, 2: v_swissmetro, 3: v_car}

Associate the availability conditions with the alternatives.

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

Definition of the model. This is the contribution of each observation to the log likelihood function.

log_probability = loglogit(v, av, CHOICE)

Read the data

database = read_data()

Create the Biogeme object.

the_biogeme = BIOGEME(
    database, log_probability, generate_html=False, generate_yaml=False
)
the_biogeme.model_name = 'b01alt_spec'
Biogeme parameters read from biogeme.toml.

Estimate the parameters.

dict_of_results = the_biogeme.estimate_catalog()
Estimating 4 models.
Biogeme parameters provided by the user.
*** Initial values of the parameters are obtained from the file __b01alt_spec_000000.iter
Cannot read file __b01alt_spec_000000.iter. Statement is ignored.
Starting values for the algorithm: {}
As the model is not too complex, we activate the calculation of second derivatives. To change this behavior, modify the algorithm to "simple_bounds" in the TOML file.
Optimization algorithm: hybrid Newton/BFGS with simple bounds [simple_bounds]
** Optimization: Newton with trust region for simple bounds
Iter.       asc_train    b_time_train    b_cost_train b_time_swissmet b_cost_swissmet         asc_car      b_time_car      b_cost_car     Function    Relgrad   Radius      Rho
    0           -0.65           -0.79           -0.93              -1           -0.63           -0.47           -0.73           -0.68      8.7e+03       0.08       10      1.1   ++
    1           -0.26            -1.3            -1.5            -1.4           -0.76           -0.56              -1           -0.64      8.5e+03      0.026    1e+02      1.2   ++
    2          -0.048            -1.4            -1.8            -1.5           -0.78           -0.59            -1.1           -0.66      8.4e+03     0.0041    1e+03      1.1   ++
    3           -0.02            -1.4            -1.9            -1.5           -0.79           -0.59            -1.1           -0.66      8.4e+03    0.00011    1e+04        1   ++
    4           -0.02            -1.4            -1.9            -1.5           -0.79           -0.59            -1.1           -0.66      8.4e+03    7.9e-08    1e+04        1   ++
Optimization algorithm has converged.
Relative gradient: 7.886755767707678e-08
Cause of termination: Relative gradient = 7.9e-08 <= 6.1e-06
Number of function evaluations: 16
Number of gradient evaluations: 11
Number of hessian evaluations: 5
Algorithm: Newton with trust region for simple bound constraints
Number of iterations: 5
Proportion of Hessian calculation: 5/5 = 100.0%
Optimization time: 0:00:00.424903
Calculate second derivatives and BHHH
Biogeme parameters provided by the user.
*** Initial values of the parameters are obtained from the file __b01alt_spec_000001.iter
Cannot read file __b01alt_spec_000001.iter. Statement is ignored.
Starting values for the algorithm: {}
Optimization algorithm: hybrid Newton/BFGS with simple bounds [simple_bounds]
** Optimization: Newton with trust region for simple bounds
Iter.       asc_train    b_time_train          b_cost b_time_swissmet         asc_car      b_time_car     Function    Relgrad   Radius      Rho
    0           -0.77           -0.88           -0.68              -1           -0.42           -0.78      8.8e+03      0.076       10      1.1   ++
    1           -0.37            -1.5           -0.78            -1.4           -0.53           -0.98      8.6e+03      0.025    1e+02      1.1   ++
    2           -0.17            -1.7           -0.79            -1.5           -0.54              -1      8.6e+03     0.0028    1e+03      1.1   ++
    3           -0.14            -1.8           -0.79            -1.5           -0.54              -1      8.6e+03      3e-05    1e+04        1   ++
    4           -0.14            -1.8           -0.79            -1.5           -0.54              -1      8.6e+03    3.4e-09    1e+04        1   ++
Optimization algorithm has converged.
Relative gradient: 3.426541855284712e-09
Cause of termination: Relative gradient = 3.4e-09 <= 6.1e-06
Number of function evaluations: 16
Number of gradient evaluations: 11
Number of hessian evaluations: 5
Algorithm: Newton with trust region for simple bound constraints
Number of iterations: 5
Proportion of Hessian calculation: 5/5 = 100.0%
Optimization time: 0:00:00.322586
Calculate second derivatives and BHHH
Biogeme parameters provided by the user.
*** Initial values of the parameters are obtained from the file __b01alt_spec_000002.iter
Cannot read file __b01alt_spec_000002.iter. Statement is ignored.
Starting values for the algorithm: {}
Optimization algorithm: hybrid Newton/BFGS with simple bounds [simple_bounds]
** Optimization: Newton with trust region for simple bounds
Iter.       asc_train          b_time    b_cost_train b_cost_swissmet         asc_car      b_cost_car     Function    Relgrad   Radius      Rho
    0           -0.59           -0.75           -0.92           -0.69           -0.42           -0.57      8.7e+03      0.053       10      1.1   ++
    1           -0.19            -1.2            -1.5           -0.78           -0.39           -0.39      8.5e+03      0.021    1e+02      1.2   ++
    2          -0.064            -1.3            -1.9           -0.82           -0.41           -0.38      8.4e+03     0.0039    1e+03      1.1   ++
    3          -0.046            -1.3            -1.9           -0.82           -0.42           -0.38      8.4e+03    0.00012    1e+04        1   ++
    4          -0.046            -1.3            -1.9           -0.82           -0.42           -0.38      8.4e+03      1e-07    1e+04        1   ++
Optimization algorithm has converged.
Relative gradient: 1.0424282105589559e-07
Cause of termination: Relative gradient = 1e-07 <= 6.1e-06
Number of function evaluations: 16
Number of gradient evaluations: 11
Number of hessian evaluations: 5
Algorithm: Newton with trust region for simple bound constraints
Number of iterations: 5
Proportion of Hessian calculation: 5/5 = 100.0%
Optimization time: 0:00:00.323873
Calculate second derivatives and BHHH
Biogeme parameters provided by the user.
*** Initial values of the parameters are obtained from the file __b01alt_spec_000003.iter
Cannot read file __b01alt_spec_000003.iter. Statement is ignored.
Starting values for the algorithm: {}
Optimization algorithm: hybrid Newton/BFGS with simple bounds [simple_bounds]
** Optimization: Newton with trust region for simple bounds
Iter.       asc_train          b_time          b_cost         asc_car     Function    Relgrad   Radius      Rho
    0           -0.76           -0.77            -0.7           -0.29      8.8e+03       0.04       10      1.1   ++
    1           -0.66            -1.2           -0.77         -0.0015      8.7e+03     0.0064    1e+02      1.1   ++
    2           -0.65            -1.3           -0.79           0.016      8.7e+03    0.00012    1e+03        1   ++
    3           -0.65            -1.3           -0.79           0.016      8.7e+03      4e-08    1e+03        1   ++
Optimization algorithm has converged.
Relative gradient: 3.954408093567457e-08
Cause of termination: Relative gradient = 4e-08 <= 6.1e-06
Number of function evaluations: 13
Number of gradient evaluations: 9
Number of hessian evaluations: 4
Algorithm: Newton with trust region for simple bound constraints
Number of iterations: 4
Proportion of Hessian calculation: 4/4 = 100.0%
Optimization time: 0:00:00.314302
Calculate second derivatives and BHHH

Number of estimated models.

print(f'A total of {len(dict_of_results)} models have been estimated')
A total of 4 models have been estimated

All estimation results

compiled_results, specs = compile_estimation_results(
    dict_of_results, use_short_names=True
)
display('All estimated models')
display(compiled_results)
All estimated models
                                     Model_000000  ...     Model_000003
Number of estimated parameters                  8  ...                4
Sample size                                 10719  ...            10719
Final log likelihood                    -8430.711  ...        -8670.163
Akaike Information Criterion             16877.42  ...         17348.33
Bayesian Information Criterion           16935.66  ...         17377.45
asc_train (t-test)              -0.0197  (-0.234)  ...    -0.652  (-12)
b_time_train (t-test)               -1.4  (-15.8)  ...
b_cost_train (t-test)              -1.85  (-14.2)  ...
b_time_swissmetro (t-test)         -1.46  (-14.4)  ...
b_cost_swissmetro (t-test)        -0.787  (-14.8)  ...
asc_car (t-test)                  -0.595  (-8.17)  ...  0.0162  (0.438)
b_time_car (t-test)                -1.06  (-11.1)  ...
b_cost_car (t-test)               -0.659  (-6.68)  ...
b_cost (t-test)                                    ...   -0.79  (-15.5)
b_time (t-test)                                    ...   -1.28  (-19.5)

[15 rows x 4 columns]

Glossary

for short_name, spec in specs.items():
    print(f'{short_name}\t{spec}')
Model_000000    b_cost_gen_altspec:altspec;b_time_gen_altspec:altspec
Model_000001    b_cost_gen_altspec:generic;b_time_gen_altspec:altspec
Model_000002    b_cost_gen_altspec:altspec;b_time_gen_altspec:generic
Model_000003    b_cost_gen_altspec:generic;b_time_gen_altspec:generic

Estimation results of the Pareto optimal models.

pareto_results = pareto_optimal(dict_of_results)
compiled_pareto_results, pareto_specs = compile_estimation_results(
    pareto_results, use_short_names=True
)
No Pareto file has been provided
display('Non dominated models')
display(compiled_pareto_results)
Non dominated models
                                   Model_000000  ...       Model_000002
Number of estimated parameters                4  ...                  8
Sample size                               10719  ...              10719
Final log likelihood                  -8670.163  ...          -8430.711
Akaike Information Criterion           17348.33  ...           16877.42
Bayesian Information Criterion         17377.45  ...           16935.66
asc_train (t-test)                -0.652  (-12)  ...  -0.0197  (-0.234)
b_time (t-test)                  -1.28  (-19.5)  ...
b_cost (t-test)                  -0.79  (-15.5)  ...
asc_car (t-test)                0.0162  (0.438)  ...    -0.595  (-8.17)
b_cost_train (t-test)                            ...     -1.85  (-14.2)
b_cost_swissmetro (t-test)                       ...    -0.787  (-14.8)
b_cost_car (t-test)                              ...    -0.659  (-6.68)
b_time_train (t-test)                            ...      -1.4  (-15.8)
b_time_swissmetro (t-test)                       ...     -1.46  (-14.4)
b_time_car (t-test)                              ...     -1.06  (-11.1)

[15 rows x 3 columns]

Glossary.

for short_name, spec in pareto_specs.items():
    print(f'{short_name}\t{spec}')
Model_000000    b_cost_gen_altspec:generic;b_time_gen_altspec:generic
Model_000001    b_cost_gen_altspec:altspec;b_time_gen_altspec:generic
Model_000002    b_cost_gen_altspec:altspec;b_time_gen_altspec:altspec

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

Gallery generated by Sphinx-Gallery