Note
Go to the end to download the full example code.
28. Explicit parameter overrides in a simple modelΒΆ
This example illustrates the preprocessing API used to control parameters in
an expression. The override is applied before the expression is passed to
BIOGEME; estimation itself is unchanged.
Two types of control are shown:
b_costis replaced by a fixedBetawith a user-supplied initial value and bounds;asc_trainis replaced byNumeric(0), so it is no longer an estimated parameter.
The same mechanism can later be used with parameters generated by catalogs, assisted specification, or latent-variable builders.
Michel Bierlaire, EPFL
from swissmetro_data 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,
database,
)
import biogeme.biogeme_logging as blog
from biogeme.biogeme import BIOGEME
from biogeme.expressions import (
Beta,
Numeric,
ParameterOverrides,
apply_parameter_overrides,
)
from biogeme.models import loglogit
logger = blog.get_screen_logger(level=blog.INFO)
logger.info('Example plot_b28_parameter_overrides.py')
Example plot_b28_parameter_overrides.py
Define the ordinary multinomial-logit specification.
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)
v_train = asc_train + b_time * TRAIN_TT_SCALED + b_cost * TRAIN_COST_SCALED
v_swissmetro = b_time * SM_TT_SCALED + b_cost * SM_COST_SCALED
v_car = asc_car + b_time * CAR_TT_SCALED + b_cost * CAR_CO_SCALED
utilities = {1: v_train, 2: v_swissmetro, 3: v_car}
availability = {1: TRAIN_AV_SP, 2: SM_AV, 3: CAR_AV_SP}
log_probability = loglogit(utilities, availability, CHOICE)
Explicitly control parameters before creating the BIOGEME object. The key is the original Beta name. A replacement can be a full Beta definition or any other valid Biogeme expression.
overrides = ParameterOverrides()
overrides.set('b_cost', Beta('b_cost', -1.0, -10.0, 0.0, 1))
overrides.set('asc_train', Numeric(0))
log_probability = apply_parameter_overrides(log_probability, overrides)
Estimate the resulting model.
biogeme = BIOGEME(database, log_probability)
biogeme.model_name = 'b28_parameter_overrides'
results = biogeme.estimate()
print(results.short_summary())
print(results.get_beta_values())
Biogeme parameters read from biogeme.toml.
*** Initial values of the parameters are obtained from the file __b28_parameter_overrides.iter
Cannot read file __b28_parameter_overrides.iter. Statement is ignored.
Starting values for the algorithm: {}
Analytical Hessian method: full
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. b_time asc_car Function Relgrad Radius Rho
0 -1 -0.22 5.7e+03 0.095 10 1.1 ++
1 -1.7 0.13 5.4e+03 0.026 1e+02 1.1 ++
2 -1.8 0.19 5.4e+03 0.0013 1e+03 1 ++
3 -1.8 0.19 5.4e+03 2.9e-06 1e+03 1 ++
Optimization algorithm has converged.
Relative gradient: 2.9461076069227843e-06
Cause of termination: Relative gradient = 2.9e-06 <= 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:01.913543
Optimization is complete. Save recoverable results in b28_parameter_overrides.yaml.
File b28_parameter_overrides.yaml has been generated.
Calculate final gradient and BHHH
File b28_parameter_overrides.yaml has been generated.
Calculate second derivatives
File b28_parameter_overrides.yaml has been generated.
File b28_parameter_overrides.html has been generated.
File b28_parameter_overrides.yaml has been generated.
Results for model b28_parameter_overrides
Nbr of parameters: 2
Sample size: 6768
Excluded data: 3960
Final log likelihood: -5415.079
Akaike Information Criterion: 10834.16
Bayesian Information Criterion: 10847.8
{'b_time': -1.8213472801478152, 'asc_car': 0.19347324604661698}
Total running time of the script: (0 minutes 3.327 seconds)