.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/assisted/plot_b03alt_spec.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_b03alt_spec.py: 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 .. GENERATED FROM PYTHON SOURCE LINES 21-46 .. code-block:: Python 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) .. GENERATED FROM PYTHON SOURCE LINES 47-48 Parameters to be estimated. .. GENERATED FROM PYTHON SOURCE LINES 48-53 .. code-block:: Python 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) .. GENERATED FROM PYTHON SOURCE LINES 54-55 Catalog for travel time coefficient. .. GENERATED FROM PYTHON SOURCE LINES 55-61 .. code-block:: Python (b_time_catalog_dict,) = generic_alt_specific_catalogs( generic_name='b_time', beta_parameters=[b_time], alternatives=('train', 'swissmetro', 'car'), ) .. GENERATED FROM PYTHON SOURCE LINES 62-63 Catalog for travel cost coefficient. .. GENERATED FROM PYTHON SOURCE LINES 63-69 .. code-block:: Python (b_cost_catalog_dict,) = generic_alt_specific_catalogs( generic_name='b_cost', beta_parameters=[b_cost], alternatives=('train', 'swissmetro', 'car'), ) .. GENERATED FROM PYTHON SOURCE LINES 70-71 Definition of the utility functions. .. GENERATED FROM PYTHON SOURCE LINES 71-86 .. code-block:: Python 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 ) .. GENERATED FROM PYTHON SOURCE LINES 87-88 Associate utility functions with the numbering of alternatives. .. GENERATED FROM PYTHON SOURCE LINES 88-90 .. code-block:: Python v = {1: v_train, 2: v_swissmetro, 3: v_car} .. GENERATED FROM PYTHON SOURCE LINES 91-92 Associate the availability conditions with the alternatives. .. GENERATED FROM PYTHON SOURCE LINES 92-94 .. code-block:: Python av = {1: TRAIN_AV_SP, 2: SM_AV, 3: CAR_AV_SP} .. GENERATED FROM PYTHON SOURCE LINES 95-97 Definition of the model. This is the contribution of each observation to the log likelihood function. .. GENERATED FROM PYTHON SOURCE LINES 97-99 .. code-block:: Python log_probability = loglogit(v, av, CHOICE) .. GENERATED FROM PYTHON SOURCE LINES 100-101 Read the data .. GENERATED FROM PYTHON SOURCE LINES 101-103 .. code-block:: Python database = read_data() .. GENERATED FROM PYTHON SOURCE LINES 104-105 Create the Biogeme object. .. GENERATED FROM PYTHON SOURCE LINES 105-110 .. code-block:: Python the_biogeme = BIOGEME( database, log_probability, generate_html=False, generate_yaml=False ) the_biogeme.model_name = 'b01alt_spec' .. rst-class:: sphx-glr-script-out .. code-block:: none Biogeme parameters read from biogeme.toml. .. GENERATED FROM PYTHON SOURCE LINES 111-112 Estimate the parameters. .. GENERATED FROM PYTHON SOURCE LINES 112-114 .. code-block:: Python dict_of_results = the_biogeme.estimate_catalog() .. rst-class:: sphx-glr-script-out .. code-block:: none 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 .. GENERATED FROM PYTHON SOURCE LINES 115-116 Number of estimated models. .. GENERATED FROM PYTHON SOURCE LINES 116-118 .. code-block:: Python 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 4 models have been estimated .. GENERATED FROM PYTHON SOURCE LINES 119-120 All estimation results .. GENERATED FROM PYTHON SOURCE LINES 120-124 .. code-block:: Python compiled_results, specs = compile_estimation_results( dict_of_results, use_short_names=True ) .. GENERATED FROM PYTHON SOURCE LINES 125-128 .. code-block:: Python display('All estimated models') display(compiled_results) .. rst-class:: sphx-glr-script-out .. code-block:: none 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] .. GENERATED FROM PYTHON SOURCE LINES 129-130 Glossary .. GENERATED FROM PYTHON SOURCE LINES 130-133 .. code-block:: Python for short_name, spec in specs.items(): print(f'{short_name}\t{spec}') .. rst-class:: sphx-glr-script-out .. code-block:: none 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 .. GENERATED FROM PYTHON SOURCE LINES 134-135 Estimation results of the Pareto optimal models. .. GENERATED FROM PYTHON SOURCE LINES 135-140 .. code-block:: Python 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 141-144 .. code-block:: Python display('Non dominated models') display(compiled_pareto_results) .. rst-class:: sphx-glr-script-out .. code-block:: none 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] .. GENERATED FROM PYTHON SOURCE LINES 145-146 Glossary. .. GENERATED FROM PYTHON SOURCE LINES 146-148 .. code-block:: Python 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 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 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 3.883 seconds) .. _sphx_glr_download_auto_examples_assisted_plot_b03alt_spec.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_b03alt_spec.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_b03alt_spec.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_b03alt_spec.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_