.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/assisted/plot_b01model.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_b01model.py: Investigation of several choice models ====================================== Investigate several choice models: - logit - nested logit with two nests: public and private transportation - nested logit with two nests existing and future modes for a total of 3 specifications. See `Bierlaire and Ortelli (2023) `_. :author: Michel Bierlaire, EPFL :date: Fri Jul 14 09:47:21 2023 .. GENERATED FROM PYTHON SOURCE LINES 20-28 .. code-block:: default import biogeme.biogeme as bio import biogeme.biogeme_logging as blog from biogeme import models from biogeme.expressions import Beta from biogeme.catalog import Catalog from biogeme.nests import OneNestForNestedLogit, NestsForNestedLogit from biogeme.results import compile_estimation_results, pareto_optimal .. GENERATED FROM PYTHON SOURCE LINES 29-30 See :ref:`swissmetro_data`. .. GENERATED FROM PYTHON SOURCE LINES 30-46 .. 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 47-48 Parameters to be estimated .. GENERATED FROM PYTHON SOURCE LINES 48-53 .. 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, None, 0) B_COST = Beta('B_COST', 0, None, None, 0) .. GENERATED FROM PYTHON SOURCE LINES 54-55 Definition of the utility functions .. GENERATED FROM PYTHON SOURCE LINES 55-59 .. code-block:: default V1 = ASC_TRAIN + B_TIME * TRAIN_TT_SCALED + B_COST * TRAIN_COST_SCALED V2 = B_TIME * SM_TT_SCALED + B_COST * SM_COST_SCALED V3 = ASC_CAR + B_TIME * CAR_TT_SCALED + B_COST * CAR_CO_SCALED .. GENERATED FROM PYTHON SOURCE LINES 60-61 Associate utility functions with the numbering of alternatives .. GENERATED FROM PYTHON SOURCE LINES 61-63 .. code-block:: default V = {1: V1, 2: V2, 3: V3} .. GENERATED FROM PYTHON SOURCE LINES 64-65 Associate the availability conditions with the alternatives .. GENERATED FROM PYTHON SOURCE LINES 65-67 .. code-block:: default av = {1: TRAIN_AV_SP, 2: SM_AV, 3: CAR_AV_SP} .. GENERATED FROM PYTHON SOURCE LINES 68-70 Definition of the logit model. This is the contribution of each observation to the log likelihood function. .. GENERATED FROM PYTHON SOURCE LINES 70-72 .. code-block:: default logprob_logit = models.loglogit(V, av, CHOICE) .. GENERATED FROM PYTHON SOURCE LINES 73-74 Nested logit model: nest with existing alternatives. .. GENERATED FROM PYTHON SOURCE LINES 74-82 .. code-block:: default mu_existing = Beta('mu_existing', 1, 1, 10, 0) existing = OneNestForNestedLogit( nest_param=mu_existing, list_of_alternatives=[1, 3], name='Existing' ) nests_existing = NestsForNestedLogit(choice_set=list(V), tuple_of_nests=(existing,)) logprob_nested_existing = models.lognested(V, av, nests_existing, CHOICE) .. GENERATED FROM PYTHON SOURCE LINES 83-84 Nested logit model: nest with public transportation alternatives. .. GENERATED FROM PYTHON SOURCE LINES 84-92 .. code-block:: default mu_public = Beta('mu_public', 1, 1, 10, 0) public = OneNestForNestedLogit( nest_param=mu_public, list_of_alternatives=[1, 2], name='Public' ) nests_public = NestsForNestedLogit(choice_set=list(V), tuple_of_nests=(public,)) logprob_nested_public = models.lognested(V, av, nests_public, CHOICE) .. GENERATED FROM PYTHON SOURCE LINES 93-94 Catalog. .. GENERATED FROM PYTHON SOURCE LINES 94-103 .. code-block:: default model_catalog = Catalog.from_dict( catalog_name='model_catalog', dict_of_expressions={ 'logit': logprob_logit, 'nested existing': logprob_nested_existing, 'nested public': logprob_nested_public, }, ) .. GENERATED FROM PYTHON SOURCE LINES 104-105 Create the Biogeme object. .. GENERATED FROM PYTHON SOURCE LINES 105-110 .. code-block:: default the_biogeme = bio.BIOGEME(database, model_catalog) the_biogeme.modelName = 'b01model' 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 111-112 Estimate the parameters. .. GENERATED FROM PYTHON SOURCE LINES 112-114 .. 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 __b01model_000000.iter Cannot read file __b01model_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 b01model_000000.html Results saved in file b01model_000000.pickle File biogeme.toml has been parsed. *** Initial values of the parameters are obtained from the file __b01model_000001.iter Cannot read file __b01model_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 mu_public Function Relgrad Radius Rho 0 -0.49 -0.92 -0.88 -0.67 1 5.4e+03 0.041 10 1.1 ++ 1 -0.31 -0.86 -1.1 -1.1 1 5.3e+03 0.011 1e+02 1.1 ++ 2 -0.15 -0.7 -1.1 -1.3 1 5.3e+03 0.00021 1e+03 1 ++ 3 -0.15 -0.7 -1.1 -1.3 1 5.3e+03 2.1e-07 1e+03 1 ++ Results saved in file b01model_000001.html Results saved in file b01model_000001.pickle File biogeme.toml has been parsed. *** Initial values of the parameters are obtained from the file __b01model_000002.iter Cannot read file __b01model_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 mu_existing Function Relgrad Radius Rho 0 0.1 -0.75 -1 -0.8 1.5 5.4e+03 0.082 10 0.92 ++ 1 -0.22 -0.28 -0.82 -0.86 2.2 5.3e+03 0.076 10 0.44 + 2 -0.17 -0.52 -0.7 -0.7 2.7 5.3e+03 0.023 10 0.72 + 3 -0.17 -0.52 -0.7 -0.7 2.7 5.3e+03 0.023 0.84 -4.6 - 4 -0.14 -0.52 -0.87 -0.92 1.8 5.2e+03 0.006 0.84 0.63 + 5 -0.16 -0.51 -0.87 -0.91 2 5.2e+03 0.0014 8.4 1.1 ++ 6 -0.17 -0.51 -0.86 -0.9 2.1 5.2e+03 8.1e-05 84 1 ++ 7 -0.17 -0.51 -0.86 -0.9 2.1 5.2e+03 2.8e-07 84 1 ++ Results saved in file b01model_000002.html Results saved in file b01model_000002.pickle .. GENERATED FROM PYTHON SOURCE LINES 115-116 Number of estimated models. .. GENERATED FROM PYTHON SOURCE LINES 116-118 .. 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 119-120 All estimation results .. GENERATED FROM PYTHON SOURCE LINES 120-123 .. code-block:: default compiled_results, specs = compile_estimation_results( dict_of_results, use_short_names=True ) .. GENERATED FROM PYTHON SOURCE LINES 124-126 .. code-block:: default compiled_results .. raw:: html
Model_000000 Model_000001 Model_000002
Number of estimated parameters 4 5 5
Sample size 6768 6768 6768
Final log likelihood -5331.252007 -5331.252007 -5236.900014
Akaike Information Criterion 10670.504014 10672.504014 10483.800027
Bayesian Information Criterion 10697.783857 10706.603818 10517.899832
ASC_CAR (t-test) -0.155 (-2.66) -0.155 (-2.03) -0.167 (-3.07)
ASC_TRAIN (t-test) -0.701 (-8.49) -0.701 (-5.22) -0.512 (-6.47)
B_COST (t-test) -1.08 (-15.9) -1.08 (-14.4) -0.857 (-14.3)
B_TIME (t-test) -1.28 (-12.3) -1.28 (-10.5) -0.899 (-8.39)
mu_public (t-test) 1 (8.78)
mu_existing (t-test) 2.05 (12.5)


.. GENERATED FROM PYTHON SOURCE LINES 127-128 Glossary .. GENERATED FROM PYTHON SOURCE LINES 128-131 .. 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 model_catalog:logit Model_000001 model_catalog:nested public Model_000002 model_catalog:nested existing .. GENERATED FROM PYTHON SOURCE LINES 132-133 Estimation results of the Pareto optimal models. .. GENERATED FROM PYTHON SOURCE LINES 133-138 .. 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 139-141 .. code-block:: default compiled_pareto_results .. raw:: html
Model_000000 Model_000001
Number of estimated parameters 4 5
Sample size 6768 6768
Final log likelihood -5331.252007 -5236.900014
Akaike Information Criterion 10670.504014 10483.800027
Bayesian Information Criterion 10697.783857 10517.899832
ASC_CAR (t-test) -0.155 (-2.66) -0.167 (-3.07)
ASC_TRAIN (t-test) -0.701 (-8.49) -0.512 (-6.47)
B_COST (t-test) -1.08 (-15.9) -0.857 (-14.3)
B_TIME (t-test) -1.28 (-12.3) -0.899 (-8.39)
mu_existing (t-test) 2.05 (12.5)


.. GENERATED FROM PYTHON SOURCE LINES 142-143 Glossary. .. GENERATED FROM PYTHON SOURCE LINES 143-145 .. 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 model_catalog:logit Model_000001 model_catalog:nested existing .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.617 seconds) .. _sphx_glr_download_auto_examples_assisted_plot_b01model.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_b01model.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_b01model.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_