Note
Go to the end to download the full example code.
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).
- author:
Michel Bierlaire, EPFL
- date:
Thu Jul 13 16:18:10 2023
import biogeme.biogeme as bio
import biogeme.biogeme_logging as blog
from biogeme import models
from biogeme.expressions import Beta
from biogeme.catalog import generic_alt_specific_catalogs
from biogeme.results import compile_estimation_results, pareto_optimal
from biogeme.data.swissmetro import (
read_data,
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)
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', 'SM', 'CAR')
)
Catalog for travel cost coefficient.
(B_COST_catalog_dict,) = generic_alt_specific_catalogs(
generic_name='B_COST', beta_parameters=[B_COST], alternatives=('TRAIN', 'SM', 'CAR')
)
Definition of the utility functions.
V1 = (
ASC_TRAIN
+ B_TIME_catalog_dict['TRAIN'] * TRAIN_TT_SCALED
+ B_COST_catalog_dict['TRAIN'] * TRAIN_COST_SCALED
)
V2 = (
B_TIME_catalog_dict['SM'] * SM_TT_SCALED
+ B_COST_catalog_dict['SM'] * SM_COST_SCALED
)
V3 = (
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: V1, 2: V2, 3: V3}
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.
logprob = models.loglogit(V, av, CHOICE)
Read the data
database = read_data()
Create the Biogeme object.
the_biogeme = bio.BIOGEME(database, logprob)
the_biogeme.modelName = 'b01alt_spec'
the_biogeme.generate_html = False
the_biogeme.generate_pickle = False
Biogeme parameters read from biogeme.toml.
Estimate the parameters.
dict_of_results = the_biogeme.estimate_catalog()
Estimating 4 models.
Biogeme parameters read from biogeme.toml.
As the model is not too complex, we activate the calculation of second derivatives. If you want to change it, 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 __b01alt_spec_000000.iter
Cannot read file __b01alt_spec_000000.iter. Statement is ignored.
As the model is not too complex, we activate the calculation of second derivatives. If you want to change it, 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: Newton with trust region for simple bounds
Iter. ASC_CAR ASC_TRAIN B_COST B_TIME_CAR B_TIME_SM B_TIME_TRAIN Function Relgrad Radius Rho
0 -0.42 -0.77 -0.68 -0.78 -1 -0.88 8.8e+03 0.076 10 1.1 ++
1 -0.53 -0.37 -0.78 -0.98 -1.4 -1.5 8.6e+03 0.025 1e+02 1.1 ++
2 -0.54 -0.17 -0.79 -1 -1.5 -1.7 8.6e+03 0.0028 1e+03 1.1 ++
3 -0.54 -0.17 -0.79 -1 -1.5 -1.7 8.6e+03 3e-05 1e+03 1 ++
Biogeme parameters read from biogeme.toml.
As the model is not too complex, we activate the calculation of second derivatives. If you want to change it, 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 __b01alt_spec_000001.iter
Cannot read file __b01alt_spec_000001.iter. Statement is ignored.
As the model is not too complex, we activate the calculation of second derivatives. If you want to change it, 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: Newton with trust region for simple bounds
Iter. ASC_CAR ASC_TRAIN B_COST_CAR B_COST_SM B_COST_TRAIN B_TIME_CAR B_TIME_SM B_TIME_TRAIN Function Relgrad Radius Rho
0 -0.6 -0.032 -0.33 -0.8 -1 -1.2 -1.4 -1.6 8.6e+03 0.055 10 0.96 ++
1 -0.59 -0.021 -0.64 -0.77 -1.6 -1 -1.5 -1.5 8.4e+03 0.016 1e+02 1.1 ++
2 -0.59 -0.019 -0.66 -0.79 -1.8 -1.1 -1.5 -1.4 8.4e+03 0.002 1e+03 1.1 ++
3 -0.59 -0.019 -0.66 -0.79 -1.8 -1.1 -1.5 -1.4 8.4e+03 3e-05 1e+03 1 ++
Biogeme parameters read from biogeme.toml.
As the model is not too complex, we activate the calculation of second derivatives. If you want to change it, 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 __b01alt_spec_000002.iter
Cannot read file __b01alt_spec_000002.iter. Statement is ignored.
As the model is not too complex, we activate the calculation of second derivatives. If you want to change it, 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: Newton with trust region for simple bounds
Iter. ASC_CAR ASC_TRAIN B_COST B_TIME Function Relgrad Radius Rho
0 -0.16 -0.53 -0.66 -0.87 8.8e+03 0.048 10 1.1 ++
1 -0.003 -0.63 -0.77 -1.2 8.7e+03 0.0073 1e+02 1.1 ++
2 0.016 -0.65 -0.79 -1.3 8.7e+03 0.00016 1e+03 1 ++
3 0.016 -0.65 -0.79 -1.3 8.7e+03 7.3e-08 1e+03 1 ++
Biogeme parameters read from biogeme.toml.
As the model is not too complex, we activate the calculation of second derivatives. If you want to change it, 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 __b01alt_spec_000003.iter
Cannot read file __b01alt_spec_000003.iter. Statement is ignored.
As the model is not too complex, we activate the calculation of second derivatives. If you want to change it, 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: Newton with trust region for simple bounds
Iter. ASC_CAR ASC_TRAIN B_COST_CAR B_COST_SM B_COST_TRAIN B_TIME Function Relgrad Radius Rho
0 -0.39 0.098 -0.39 -0.81 -1.8 -1.3 8.5e+03 0.026 1 0.8 +
1 -0.42 -0.044 -0.38 -0.82 -1.9 -1.3 8.4e+03 0.0019 10 1 ++
2 -0.42 -0.044 -0.38 -0.82 -1.9 -1.3 8.4e+03 1.5e-05 10 1 ++
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
)
compiled_results
Glossary
for short_name, spec in specs.items():
print(f'{short_name}\t{spec}')
Model_000000 B_COST_gen_altspec:generic;B_TIME_gen_altspec:altspec
Model_000001 B_COST_gen_altspec:altspec;B_TIME_gen_altspec:altspec
Model_000002 B_COST_gen_altspec:generic;B_TIME_gen_altspec:generic
Model_000003 B_COST_gen_altspec:altspec;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
compiled_pareto_results
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:altspec
Model_000002 B_COST_gen_altspec:altspec;B_TIME_gen_altspec:generic
Total running time of the script: (0 minutes 1.286 seconds)