.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/assisted/plot_b02nonlinear.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_assisted_plot_b02nonlinear.py: 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) `_. :author: Michel Bierlaire, EPFL :date: Thu Jul 13 21:31:54 2023 .. GENERATED FROM PYTHON SOURCE LINES 21-29 .. code-block:: default import biogeme.biogeme as bio import biogeme.biogeme_logging as blog from biogeme import models from biogeme.expressions import Expression, Beta from biogeme.models import boxcox from biogeme.catalog import Catalog from biogeme.results import compile_estimation_results, pareto_optimal .. GENERATED FROM PYTHON SOURCE LINES 30-31 See :ref:`swissmetro_data`. .. GENERATED FROM PYTHON SOURCE LINES 31-47 .. code-block:: default 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) .. GENERATED FROM PYTHON SOURCE LINES 48-49 Parameters to be estimated. .. GENERATED FROM PYTHON SOURCE LINES 49-54 .. code-block:: default 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) .. GENERATED FROM PYTHON SOURCE LINES 55-56 Non linear specifications for the travel time. .. GENERATED FROM PYTHON SOURCE LINES 58-59 Parameter of the Box-Cox transform. .. GENERATED FROM PYTHON SOURCE LINES 59-61 .. code-block:: default ell_travel_time = Beta('lambda_travel_time', 1, -10, 10, 0) .. GENERATED FROM PYTHON SOURCE LINES 62-63 Coefficients of the power series. .. GENERATED FROM PYTHON SOURCE LINES 63-67 .. code-block:: default square_tt_coef = Beta('square_tt_coef', 0, None, None, 0) cube_tt_coef = Beta('cube_tt_coef', 0, None, None, 0) .. GENERATED FROM PYTHON SOURCE LINES 68-69 Function calculation the power series. .. GENERATED FROM PYTHON SOURCE LINES 69-81 .. code-block:: default 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 ) .. GENERATED FROM PYTHON SOURCE LINES 82-83 Train travel time .. GENERATED FROM PYTHON SOURCE LINES 85-86 Linear specification. .. GENERATED FROM PYTHON SOURCE LINES 86-88 .. code-block:: default linear_train_tt = TRAIN_TT_SCALED .. GENERATED FROM PYTHON SOURCE LINES 89-90 Box-Cox transform. .. GENERATED FROM PYTHON SOURCE LINES 90-92 .. code-block:: default boxcox_train_tt = boxcox(TRAIN_TT_SCALED, ell_travel_time) .. GENERATED FROM PYTHON SOURCE LINES 93-94 Power series. .. GENERATED FROM PYTHON SOURCE LINES 94-96 .. code-block:: default power_train_tt = power_series(TRAIN_TT_SCALED) .. GENERATED FROM PYTHON SOURCE LINES 97-98 Definition of the catalog. .. GENERATED FROM PYTHON SOURCE LINES 98-107 .. code-block:: default 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, }, ) .. GENERATED FROM PYTHON SOURCE LINES 108-109 Swissmetro travel time .. GENERATED FROM PYTHON SOURCE LINES 111-112 Linear specification. .. GENERATED FROM PYTHON SOURCE LINES 112-114 .. code-block:: default linear_sm_tt = SM_TT_SCALED .. GENERATED FROM PYTHON SOURCE LINES 115-116 Box-Cox transform. .. GENERATED FROM PYTHON SOURCE LINES 116-118 .. code-block:: default boxcox_sm_tt = boxcox(SM_TT_SCALED, ell_travel_time) .. GENERATED FROM PYTHON SOURCE LINES 119-120 Power series. .. GENERATED FROM PYTHON SOURCE LINES 120-122 .. code-block:: default power_sm_tt = power_series(SM_TT_SCALED) .. GENERATED FROM PYTHON SOURCE LINES 123-124 Definition of the catalog. Note that the controller is the same as for train. .. GENERATED FROM PYTHON SOURCE LINES 124-134 .. code-block:: default 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, ) .. GENERATED FROM PYTHON SOURCE LINES 135-136 Car travel time .. GENERATED FROM PYTHON SOURCE LINES 138-139 Linear specification. .. GENERATED FROM PYTHON SOURCE LINES 139-141 .. code-block:: default linear_car_tt = CAR_TT_SCALED .. GENERATED FROM PYTHON SOURCE LINES 142-143 Box-Cox transform. .. GENERATED FROM PYTHON SOURCE LINES 143-145 .. code-block:: default boxcox_car_tt = boxcox(CAR_TT_SCALED, ell_travel_time) .. GENERATED FROM PYTHON SOURCE LINES 146-147 Power series. .. GENERATED FROM PYTHON SOURCE LINES 147-149 .. code-block:: default power_car_tt = power_series(CAR_TT_SCALED) .. GENERATED FROM PYTHON SOURCE LINES 150-151 Definition of the catalog. Note that the controller is the same as for train. .. GENERATED FROM PYTHON SOURCE LINES 151-161 .. code-block:: default 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, ) .. GENERATED FROM PYTHON SOURCE LINES 162-163 Definition of the utility functions. .. GENERATED FROM PYTHON SOURCE LINES 163-167 .. code-block:: default V1 = ASC_TRAIN + B_TIME * train_tt_catalog + B_COST * TRAIN_COST_SCALED V2 = B_TIME * sm_tt_catalog + B_COST * SM_COST_SCALED V3 = ASC_CAR + B_TIME * car_tt_catalog + B_COST * CAR_CO_SCALED .. GENERATED FROM PYTHON SOURCE LINES 168-169 Associate utility functions with the numbering of alternatives. .. GENERATED FROM PYTHON SOURCE LINES 169-171 .. code-block:: default V = {1: V1, 2: V2, 3: V3} .. GENERATED FROM PYTHON SOURCE LINES 172-173 Associate the availability conditions with the alternatives. .. GENERATED FROM PYTHON SOURCE LINES 173-175 .. code-block:: default av = {1: TRAIN_AV_SP, 2: SM_AV, 3: CAR_AV_SP} .. GENERATED FROM PYTHON SOURCE LINES 176-178 Definition of the model. This is the contribution of each observation to the log likelihood function. .. GENERATED FROM PYTHON SOURCE LINES 178-180 .. code-block:: default logprob = models.loglogit(V, av, CHOICE) .. GENERATED FROM PYTHON SOURCE LINES 181-182 Create the Biogeme object. .. GENERATED FROM PYTHON SOURCE LINES 182-187 .. code-block:: default the_biogeme = bio.BIOGEME(database, logprob) the_biogeme.modelName = 'b02nonlinear' the_biogeme.generate_html = False the_biogeme.generate_pickle = False .. rst-class:: sphx-glr-script-out .. code-block:: none File biogeme.toml has been parsed. .. GENERATED FROM PYTHON SOURCE LINES 188-189 Estimate the parameters. .. GENERATED FROM PYTHON SOURCE LINES 189-191 .. code-block:: default dict_of_results = the_biogeme.estimate_catalog() .. rst-class:: sphx-glr-script-out .. code-block:: none Estimating 3 models. File biogeme.toml has been parsed. *** Initial values of the parameters are obtained from the file __b02nonlinear_000000.iter Cannot read file __b02nonlinear_000000.iter. Statement is ignored. 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.49 -0.92 -0.88 -0.67 5.4e+03 0.041 10 1.1 ++ 1 -0.18 -0.73 -1 -1.2 5.3e+03 0.0072 1e+02 1.1 ++ 2 -0.16 -0.7 -1.1 -1.3 5.3e+03 0.00018 1e+03 1 ++ 3 -0.16 -0.7 -1.1 -1.3 5.3e+03 1.1e-07 1e+03 1 ++ Results saved in file b02nonlinear_000000.html Results saved in file b02nonlinear_000000.pickle File biogeme.toml has been parsed. *** Initial values of the parameters are obtained from the file __b02nonlinear_000001.iter Cannot read file __b02nonlinear_000001.iter. Statement is ignored. 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 lambda_travel_t Function Relgrad Radius Rho 0 -0.5 -0.71 -0.82 -1 1.7 5.6e+03 0.053 1 0.77 + 1 -0.35 -0.77 -1.3 -1.7 0.69 5.4e+03 0.032 10 1 ++ 2 0.017 -0.47 -1 -1.7 0.53 5.3e+03 0.002 1e+02 0.96 ++ 3 -0.0043 -0.48 -1.1 -1.7 0.51 5.3e+03 1.3e-05 1e+03 1 ++ 4 -0.0043 -0.48 -1.1 -1.7 0.51 5.3e+03 1.8e-09 1e+03 1 ++ Results saved in file b02nonlinear_000001.html Results saved in file b02nonlinear_000001.pickle File biogeme.toml has been parsed. *** Initial values of the parameters are obtained from the file __b02nonlinear_000002.iter Cannot read file __b02nonlinear_000002.iter. Statement is ignored. 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 cube_tt_coef square_tt_coef Function Relgrad Radius Rho 0 0 0 0 0 0 0 7e+03 0.27 0.5 -0.86 - 1 -0.079 -0.5 -0.5 -0.5 0 0 5.7e+03 7.4 5 1 ++ 2 -0.079 -0.5 -0.5 -0.5 0 0 5.7e+03 7.4 2.5 -10 - 3 -0.079 -0.5 -0.5 -0.5 0 0 5.7e+03 7.4 1.2 -6.4 - 4 -0.079 -0.5 -0.5 -0.5 0 0 5.7e+03 7.4 0.62 -3.8 - 5 -0.079 -0.5 -0.5 -0.5 0 0 5.7e+03 7.4 0.31 -2.3 - 6 -0.079 -0.5 -0.5 -0.5 0 0 5.7e+03 7.4 0.16 -1.8 - 7 -0.079 -0.5 -0.5 -0.5 0 0 5.7e+03 7.4 0.078 -1.8 - 8 -0.079 -0.5 -0.5 -0.5 0 0 5.7e+03 7.4 0.039 -2.2 - 9 -0.079 -0.5 -0.5 -0.5 0 0 5.7e+03 7.4 0.02 -2.7 - 10 -0.079 -0.5 -0.5 -0.5 0 0 5.7e+03 7.4 0.0098 -3.2 - 11 -0.079 -0.5 -0.5 -0.5 0 0 5.7e+03 7.4 0.0049 -3.5 - 12 -0.079 -0.5 -0.5 -0.5 0 0 5.7e+03 7.4 0.0024 -2.4 - 13 -0.079 -0.5 -0.5 -0.5 0 0 5.7e+03 7.4 0.0012 -1.6 - 14 -0.079 -0.5 -0.5 -0.5 0 0 5.7e+03 7.4 0.00061 -0.92 - 15 -0.079 -0.5 -0.5 -0.5 0 0 5.7e+03 7.4 0.00031 -0.11 - 16 -0.08 -0.5 -0.5 -0.5 -0.00031 0.00031 5.7e+03 3.5 0.00031 0.69 + 17 -0.08 -0.5 -0.5 -0.5 -0.00025 0.00061 5.7e+03 1.2 0.00031 0.87 + 18 -0.08 -0.5 -0.5 -0.5 -0.00027 0.00092 5.7e+03 0.15 0.0031 1 ++ 19 -0.08 -0.5 -0.5 -0.5 -0.00027 0.004 5.7e+03 0.95 0.031 1 ++ 20 -0.086 -0.52 -0.5 -0.53 -0.00041 0.034 5.6e+03 0.27 0.31 1 ++ 21 -0.12 -0.66 -0.6 -0.83 -0.0014 0.26 5.5e+03 3 0.31 0.6 + 22 -0.11 -0.78 -0.91 -0.98 -0.00018 -0.017 5.3e+03 5 0.31 0.64 + 23 -0.25 -0.8 -1 -1.3 -7.2e-05 -0.047 5.3e+03 4 3.1 1.1 ++ 24 -0.25 -0.8 -1 -1.3 -7.2e-05 -0.047 5.3e+03 4 0.83 -12 - 25 -0.25 -0.8 -1 -1.3 -7.2e-05 -0.047 5.3e+03 4 0.41 -1.5 - 26 -0.11 -0.8 -1.1 -1.7 0.00021 -0.11 5.3e+03 7.3 0.41 0.6 + 27 0.042 -0.4 -1.1 -2.1 0.00012 -0.088 5.2e+03 2.6 0.41 0.86 + 28 0.024 -0.41 -1.1 -2.4 0.00023 -0.11 5.2e+03 13 0.41 0.38 + 29 0.055 -0.39 -1.1 -2.3 0.00019 -0.11 5.2e+03 2 4.1 0.95 ++ 30 0.043 -0.41 -1.1 -2.3 0.00019 -0.1 5.2e+03 0.013 41 1 ++ 31 0.043 -0.41 -1.1 -2.3 0.00019 -0.1 5.2e+03 1.4e-05 4.1e+02 1 ++ 32 0.043 -0.41 -1.1 -2.3 0.00019 -0.1 5.2e+03 5.6e-07 4.1e+02 1 ++ Results saved in file b02nonlinear_000002.html Results saved in file b02nonlinear_000002.pickle .. GENERATED FROM PYTHON SOURCE LINES 192-193 Number of estimated models. .. GENERATED FROM PYTHON SOURCE LINES 193-195 .. code-block:: default print(f'A total of {len(dict_of_results)} models have been estimated') .. rst-class:: sphx-glr-script-out .. code-block:: none A total of 3 models have been estimated .. GENERATED FROM PYTHON SOURCE LINES 196-197 All estimation results .. GENERATED FROM PYTHON SOURCE LINES 197-201 .. code-block:: default compiled_results, specs = compile_estimation_results( dict_of_results, use_short_names=True ) .. GENERATED FROM PYTHON SOURCE LINES 202-204 .. code-block:: default compiled_results .. raw:: html
Model_000000 Model_000001 Model_000002
Number of estimated parameters 4 5 6
Sample size 6768 6768 6768
Final log likelihood -5331.252007 -5292.095411 -5236.262942
Akaike Information Criterion 10670.504014 10594.190822 10484.525883
Bayesian Information Criterion 10697.783857 10628.290626 10525.445649
ASC_CAR (t-test) -0.155 (-2.66) -0.00462 (-0.0963) 0.0434 (0.965)
ASC_TRAIN (t-test) -0.701 (-8.49) -0.485 (-7.53) -0.409 (-6.8)
B_COST (t-test) -1.08 (-15.9) -1.08 (-15.9) -1.11 (-16)
B_TIME (t-test) -1.28 (-12.3) -1.67 (-21.9) -2.32 (-22.6)
lambda_travel_time (t-test) 0.51 (6.6)
cube_tt_coef (t-test) 0.000193 (7.38)
square_tt_coef (t-test) -0.105 (-21.2)


.. GENERATED FROM PYTHON SOURCE LINES 205-206 Glossary .. GENERATED FROM PYTHON SOURCE LINES 206-209 .. code-block:: default for short_name, spec in specs.items(): print(f'{short_name}\t{spec}') .. rst-class:: sphx-glr-script-out .. code-block:: none Model_000000 train_tt_catalog:linear Model_000001 train_tt_catalog:boxcox Model_000002 train_tt_catalog:power .. GENERATED FROM PYTHON SOURCE LINES 210-211 Estimation results of the Pareto optimal models. .. GENERATED FROM PYTHON SOURCE LINES 211-216 .. code-block:: default pareto_results = pareto_optimal(dict_of_results) compiled_pareto_results, pareto_specs = compile_estimation_results( pareto_results, use_short_names=True ) .. rst-class:: sphx-glr-script-out .. code-block:: none No Pareto file has been provided .. GENERATED FROM PYTHON SOURCE LINES 217-219 .. code-block:: default compiled_pareto_results .. raw:: html
Model_000000 Model_000001 Model_000002
Number of estimated parameters 6 5 4
Sample size 6768 6768 6768
Final log likelihood -5236.262942 -5292.095411 -5331.252007
Akaike Information Criterion 10484.525883 10594.190822 10670.504014
Bayesian Information Criterion 10525.445649 10628.290626 10697.783857
ASC_CAR (t-test) 0.0434 (0.965) -0.00462 (-0.0963) -0.155 (-2.66)
ASC_TRAIN (t-test) -0.409 (-6.8) -0.485 (-7.53) -0.701 (-8.49)
B_COST (t-test) -1.11 (-16) -1.08 (-15.9) -1.08 (-15.9)
B_TIME (t-test) -2.32 (-22.6) -1.67 (-21.9) -1.28 (-12.3)
cube_tt_coef (t-test) 0.000193 (7.38)
square_tt_coef (t-test) -0.105 (-21.2)
lambda_travel_time (t-test) 0.51 (6.6)


.. GENERATED FROM PYTHON SOURCE LINES 220-221 Glossary. .. GENERATED FROM PYTHON SOURCE LINES 221-223 .. code-block:: default for short_name, spec in pareto_specs.items(): print(f'{short_name}\t{spec}') .. rst-class:: sphx-glr-script-out .. code-block:: none Model_000000 train_tt_catalog:power Model_000001 train_tt_catalog:boxcox Model_000002 train_tt_catalog:linear .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.893 seconds) .. _sphx_glr_download_auto_examples_assisted_plot_b02nonlinear.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_b02nonlinear.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_b02nonlinear.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_