Note
Go to the end to download the full example code.
Catalog of nonlinear specificationsΒΆ
Investigate of nonlinear specifications for the travel time variables:
linear specification,
Box-Cox transform,
power series,
for a total of 3 specifications. See Bierlaire and Ortelli (2023).
Michel Bierlaire, EPFL Sun Apr 27 2025, 15:47:18
from IPython.core.display_functions import display
import biogeme.biogeme_logging as blog
from biogeme.biogeme import BIOGEME
from biogeme.catalog import Catalog
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, Expression
from biogeme.models import boxcox, 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, 0, 0)
b_cost = Beta('b_cost', 0, None, 0, 0)
Non-linear specifications for the travel time.
Parameter of the Box-Cox transform.
lambda_travel_time = Beta('lambda_travel_time', 1, -10, 10, 0)
Coefficients of the power series.
square_tt_coef = Beta('square_tt_coef', 0, None, None, 0)
cube_tt_coef = Beta('cube_tt_coef', 0, None, None, 0)
Function calculation the power series.
def power_series(the_variable: Expression) -> Expression:
"""Generate the expression of a polynomial of degree 3
:param the_variable: variable of the polynomial
"""
return (
the_variable
+ square_tt_coef * the_variable**2
+ cube_tt_coef * the_variable * the_variable**3
)
Train travel time
Linear specification.
linear_train_tt = TRAIN_TT_SCALED
Box-Cox transform.
boxcox_train_tt = boxcox(TRAIN_TT_SCALED, lambda_travel_time)
Power series.
power_train_tt = power_series(TRAIN_TT_SCALED)
Definition of the catalog.
train_tt_catalog = Catalog.from_dict(
catalog_name='train_tt_catalog',
dict_of_expressions={
'linear': linear_train_tt,
'boxcox': boxcox_train_tt,
'power': power_train_tt,
},
)
Swissmetro travel time
Linear specification.
linear_sm_tt = SM_TT_SCALED
Box-Cox transform.
boxcox_sm_tt = boxcox(SM_TT_SCALED, lambda_travel_time)
Power series.
power_sm_tt = power_series(SM_TT_SCALED)
Definition of the catalog. Note that the controller is the same as for train.
sm_tt_catalog = Catalog.from_dict(
catalog_name='sm_tt_catalog',
dict_of_expressions={
'linear': linear_sm_tt,
'boxcox': boxcox_sm_tt,
'power': power_sm_tt,
},
controlled_by=train_tt_catalog.controlled_by,
)
Car travel time
Linear specification.
linear_car_tt = CAR_TT_SCALED
Box-Cox transform.
boxcox_car_tt = boxcox(CAR_TT_SCALED, lambda_travel_time)
Power series.
power_car_tt = power_series(CAR_TT_SCALED)
Definition of the catalog. Note that the controller is the same as for train.
car_tt_catalog = Catalog.from_dict(
catalog_name='car_tt_catalog',
dict_of_expressions={
'linear': linear_car_tt,
'boxcox': boxcox_car_tt,
'power': power_car_tt,
},
controlled_by=train_tt_catalog.controlled_by,
)
Definition of the utility functions.
v_train = asc_train + b_time * train_tt_catalog + b_cost * TRAIN_COST_SCALED
v_swissmetro = b_time * sm_tt_catalog + b_cost * SM_COST_SCALED
v_car = asc_car + b_time * car_tt_catalog + b_cost * 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 = 'b02nonlinear'
Biogeme parameters read from biogeme.toml.
Estimate the parameters.
dict_of_results = the_biogeme.estimate_catalog()
Estimating 3 models.
Biogeme parameters provided by the user.
*** Initial values of the parameters are obtained from the file __b02nonlinear_000000.iter
Parameter values restored from __b02nonlinear_000000.iter
Starting values for the algorithm: {'asc_train': -0.47411327059998587, 'b_time': -1.6235482439942566, 'lambda_travel_time': 0.547675083604391, 'b_cost': -0.7868015323226638, 'asc_car': 0.1372307552186725}
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
Optimization algorithm has converged.
Relative gradient: 4.533856943601959e-06
Cause of termination: Relative gradient = 4.5e-06 <= 6.1e-06
Number of function evaluations: 1
Number of gradient evaluations: 1
Number of hessian evaluations: 0
Algorithm: Newton with trust region for simple bound constraints
Number of iterations: 0
Optimization time: 0:00:00.381582
Calculate second derivatives and BHHH
Biogeme parameters provided by the user.
*** Initial values of the parameters are obtained from the file __b02nonlinear_000001.iter
Parameter values restored from __b02nonlinear_000001.iter
Starting values for the algorithm: {'asc_train': -0.45924298848053446, 'b_time': -2.093234635610363, 'square_tt_coef': -0.09712353444450088, 'cube_tt_coef': 0.00015926668110575546, 'b_cost': -0.8000283387508708, 'asc_car': 0.14751901883635934}
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.64 -1.1 -0.71 0.0065 8.7e+03 0.042 1 0.66 +
1 -0.66 -1.3 -0.69 -0.13 8.7e+03 0.029 1 0.68 +
2 -0.65 -1.3 -0.68 -0.26 8.7e+03 0.027 1 0.11 +
3 -0.65 -1.3 -0.68 -0.26 8.7e+03 0.027 0.065 0.0056 -
4 -0.62 -1.3 -0.64 -0.32 8.7e+03 0.021 0.065 0.37 +
5 -0.55 -1.4 -0.6 -0.39 8.7e+03 0.016 0.065 0.28 +
6 -0.55 -1.4 -0.6 -0.39 8.7e+03 0.016 0.033 0.0013 -
7 -0.55 -1.4 -0.6 -0.39 8.7e+03 0.016 0.016 0.0036 -
8 -0.55 -1.4 -0.6 -0.39 8.7e+03 0.016 0.0081 0.023 -
9 -0.55 -1.4 -0.6 -0.39 8.7e+03 0.016 0.0041 0.037 -
10 -0.55 -1.4 -0.6 -0.39 8.7e+03 0.016 0.002 0.045 -
11 -0.55 -1.4 -0.6 -0.39 8.7e+03 0.016 0.001 0.059 -
12 -0.55 -1.4 -0.6 -0.39 8.7e+03 0.016 0.00051 0.069 -
13 -0.55 -1.4 -0.6 -0.39 8.7e+03 0.016 0.00025 0.073 -
14 -0.55 -1.4 -0.6 -0.39 8.7e+03 0.016 0.00013 0.075 -
15 -0.55 -1.4 -0.6 -0.39 8.7e+03 0.016 6.4e-05 0.076 -
16 -0.55 -1.4 -0.6 -0.39 8.7e+03 0.016 3.2e-05 0.077 -
17 -0.55 -1.4 -0.6 -0.39 8.7e+03 0.016 1.6e-05 0.077 -
18 -0.55 -1.4 -0.6 -0.39 8.7e+03 0.016 8e-06 0.077 -
19 -0.55 -1.4 -0.6 -0.39 8.7e+03 0.016 4e-06 0.077 -
20 -0.55 -1.4 -0.6 -0.39 8.7e+03 0.016 2e-06 0.077 -
21 -0.55 -1.4 -0.6 -0.39 8.7e+03 0.016 9.9e-07 0.077 -
22 -0.55 -1.4 -0.6 -0.39 8.7e+03 0.016 5e-07 0.077 -
23 -0.55 -1.4 -0.6 -0.39 8.7e+03 0.016 2.5e-07 0.077 -
24 -0.55 -1.4 -0.6 -0.39 8.7e+03 0.016 1.2e-07 0.077 -
25 -0.55 -1.4 -0.6 -0.39 8.7e+03 0.016 6.2e-08 0.077 -
26 -0.55 -1.4 -0.6 -0.39 8.7e+03 0.016 3.1e-08 0.077 -
27 -0.55 -1.4 -0.6 -0.39 8.7e+03 0.016 1.6e-08 0.077 -
28 -0.55 -1.4 -0.6 -0.39 8.7e+03 0.016 7.8e-09 0.077 -
Optimization algorithm has *not* converged.
Algorithm: Newton with trust region for simple bound constraints
Cause of termination: Trust region is too small: 7.771988044889128e-09
Number of iterations: 29
Proportion of Hessian calculation: 6/6 = 100.0%
Optimization time: 0:00:00.319148
Calculate second derivatives and BHHH
It seems that the optimization algorithm did not converge. Therefore, the results may not correspond to the maximum likelihood estimator. Check the specification of the model, or the criteria for convergence of the algorithm.
Biogeme parameters provided by the user.
*** Initial values of the parameters are obtained from the file __b02nonlinear_000002.iter
Parameter values restored from __b02nonlinear_000002.iter
Starting values for the algorithm: {'asc_train': -0.652238664271019, 'b_time': -1.2789413398819158, 'b_cost': -0.7897904566401142, 'asc_car': 0.01622793815045202}
Optimization algorithm: hybrid Newton/BFGS with simple bounds [simple_bounds]
** Optimization: Newton with trust region for simple bounds
Iter. asc_train b_time square_tt_coef cube_tt_coef b_cost asc_car Function Relgrad Radius Rho
0 -0.65 -1.3 0 0 -0.79 0.016 1.7e+04 1.1 0.5 -1 -
1 -0.65 -1.3 0 0 -0.79 0.016 1.7e+04 1.1 0.25 -0.51 -
2 -0.65 -1.3 0 0 -0.79 0.016 1.7e+04 1.1 0.12 -0.025 -
3 -0.65 -1.3 0 0 -0.79 0.016 1.7e+04 1.1 0.062 0.072 -
4 -0.65 -1.3 0 0 -0.79 0.016 1.7e+04 1.1 0.031 0.077 -
5 -0.65 -1.3 0 0 -0.79 0.016 1.7e+04 1.1 0.016 0.076 -
6 -0.65 -1.3 0 0 -0.79 0.016 1.7e+04 1.1 0.0078 0.069 -
7 -0.65 -1.3 0.0078 0.0059 -0.79 0.015 1.7e+04 1.1 0.0078 0.15 +
8 -0.66 -1.3 0.016 0.013 -0.79 0.014 1.7e+04 1.2 0.0078 0.13 +
9 -0.66 -1.3 0.016 0.013 -0.79 0.014 1.7e+04 1.2 0.0039 -2.3 -
10 -0.66 -1.3 0.016 0.013 -0.79 0.014 1.7e+04 1.2 0.002 -0.95 -
11 -0.66 -1.3 0.016 0.013 -0.79 0.014 1.7e+04 1.2 0.00098 -0.72 -
12 -0.66 -1.3 0.016 0.013 -0.79 0.014 1.7e+04 1.2 0.00049 -0.67 -
13 -0.66 -1.3 0.016 0.013 -0.79 0.014 1.7e+04 1.2 0.00024 -0.64 -
14 -0.66 -1.3 0.016 0.013 -0.79 0.014 1.7e+04 1.2 0.00012 -0.63 -
15 -0.66 -1.3 0.016 0.013 -0.79 0.014 1.7e+04 1.2 6.1e-05 -0.62 -
16 -0.66 -1.3 0.016 0.013 -0.79 0.014 1.7e+04 1.2 3.1e-05 -0.62 -
17 -0.66 -1.3 0.016 0.013 -0.79 0.014 1.7e+04 1.2 1.5e-05 -0.62 -
18 -0.66 -1.3 0.016 0.013 -0.79 0.014 1.7e+04 1.2 7.6e-06 -0.62 -
19 -0.66 -1.3 0.016 0.013 -0.79 0.014 1.7e+04 1.2 3.8e-06 -0.62 -
20 -0.66 -1.3 0.016 0.013 -0.79 0.014 1.7e+04 1.2 1.9e-06 -0.62 -
21 -0.66 -1.3 0.016 0.013 -0.79 0.014 1.7e+04 1.2 9.5e-07 -0.62 -
22 -0.66 -1.3 0.016 0.013 -0.79 0.014 1.7e+04 1.2 4.8e-07 -0.62 -
23 -0.66 -1.3 0.016 0.013 -0.79 0.014 1.7e+04 1.2 2.4e-07 -0.62 -
24 -0.66 -1.3 0.016 0.013 -0.79 0.014 1.7e+04 1.2 1.2e-07 -0.62 -
25 -0.66 -1.3 0.016 0.013 -0.79 0.014 1.7e+04 1.2 6e-08 -0.62 -
26 -0.66 -1.3 0.016 0.013 -0.79 0.014 1.7e+04 1.2 3e-08 -0.62 -
27 -0.66 -1.3 0.016 0.013 -0.79 0.014 1.7e+04 1.2 1.5e-08 -0.62 -
Optimization algorithm has *not* converged.
Algorithm: Newton with trust region for simple bound constraints
Cause of termination: Trust region is too small: 1.4901161193847653e-08
Number of iterations: 28
Proportion of Hessian calculation: 3/3 = 100.0%
Optimization time: 0:00:00.598830
Calculate second derivatives and BHHH
It seems that the optimization algorithm did not converge. Therefore, the results may not correspond to the maximum likelihood estimator. Check the specification of the model, or the criteria for convergence of the algorithm.
Number of estimated models.
print(f'A total of {len(dict_of_results)} models have been estimated')
A total of 3 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_000002
Number of estimated parameters 5 ... 6
Sample size 10719 ... 10719
Final log likelihood -8626.22 ... -10816.29
Akaike Information Criterion 17262.44 ... 21644.57
Bayesian Information Criterion 17298.84 ... 21688.25
asc_train (t-test) -0.474 (-10.1) ... -0.658 (-2.63)
b_time (t-test) -1.62 (-28.9) ... -1.26 (-1.96)
lambda_travel_time (t-test) 0.548 (8.77) ...
b_cost (t-test) -0.787 (-15.7) ... -0.79 (-9.04)
asc_car (t-test) 0.137 (4.04) ... 0.0145 (0.094)
square_tt_coef (t-test) ... 0.0156 (0.0347)
cube_tt_coef (t-test) ... 0.0129 (0.833)
[12 rows x 3 columns]
Glossary
for short_name, spec in specs.items():
print(f'{short_name}\t{spec}')
Model_000000 train_tt_catalog:boxcox
Model_000001 train_tt_catalog:linear
Model_000002 train_tt_catalog:power
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_000001
Number of estimated parameters 4 5
Sample size 10719 10719
Final log likelihood -8890.14 -8626.22
Akaike Information Criterion 17788.28 17262.44
Bayesian Information Criterion 17817.4 17298.84
asc_train (t-test) -0.554 (-9.66) -0.474 (-10.1)
b_time (t-test) -1.38 (-18.4) -1.62 (-28.9)
b_cost (t-test) -0.603 (-13.6) -0.787 (-15.7)
asc_car (t-test) -0.387 (-9.87) 0.137 (4.04)
lambda_travel_time (t-test) 0.548 (8.77)
Glossary.
for short_name, spec in pareto_specs.items():
print(f'{short_name}\t{spec}')
Model_000000 train_tt_catalog:linear
Model_000001 train_tt_catalog:boxcox
Total running time of the script: (0 minutes 4.634 seconds)