.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/swissmetro/plot_b20multiple_models.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_swissmetro_plot_b20multiple_models.py: Estimation of several models ============================ Example of the estimation of several specifications of the model. :author: Michel Bierlaire, EPFL :date: Mon Apr 10 12:19:46 2023 .. GENERATED FROM PYTHON SOURCE LINES 12-19 .. code-block:: default import biogeme.biogeme as bio from biogeme import models from biogeme.expressions import Beta, log from biogeme.results import compile_estimation_results, pareto_optimal from biogeme.catalog import Catalog, segmentation_catalogs .. GENERATED FROM PYTHON SOURCE LINES 20-21 See the data processing script: :ref:`swissmetro_data`. .. GENERATED FROM PYTHON SOURCE LINES 21-36 .. 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, MALE, ) .. GENERATED FROM PYTHON SOURCE LINES 37-38 Parameters to be estimated .. GENERATED FROM PYTHON SOURCE LINES 38-47 .. code-block:: default ASC_CAR = Beta('ASC_CAR', 0, None, None, 0) ASC_TRAIN = Beta('ASC_TRAIN', 0, None, None, 0) ASC_CAR_MALE = Beta('ASC_CAR_MALE', 0, None, None, 0) ASC_CAR_FEMALE = Beta('ASC_CAR_FEMALE', 0, None, None, 0) ASC_TRAIN_MALE = Beta('ASC_TRAIN_MALE', 0, None, None, 0) ASC_TRAIN_FEMALE = Beta('ASC_TRAIN_FEMALE', 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 48-52 .. code-block:: default segmentation_gender = database.generate_segmentation( variable=MALE, mapping={0: 'female', 1: 'male'} ) .. GENERATED FROM PYTHON SOURCE LINES 53-55 We define catalogs with two different specifications for the ASC_CAR: non segmented, and segmented. .. GENERATED FROM PYTHON SOURCE LINES 55-62 .. code-block:: default ASC_TRAIN_catalog, ASC_CAR_catalog = segmentation_catalogs( generic_name='ASC', beta_parameters=[ASC_TRAIN, ASC_CAR], potential_segmentations=(segmentation_gender,), maximum_number=1, ) .. GENERATED FROM PYTHON SOURCE LINES 63-64 We now define a catalog with the log travel time as well as the travel time. .. GENERATED FROM PYTHON SOURCE LINES 66-67 First for train .. GENERATED FROM PYTHON SOURCE LINES 67-75 .. code-block:: default train_tt_catalog = Catalog.from_dict( catalog_name='train_tt_catalog', dict_of_expressions={ 'linear': TRAIN_TT_SCALED, 'log': log(TRAIN_TT_SCALED), }, ) .. GENERATED FROM PYTHON SOURCE LINES 76-78 Then for SM. But we require that the specification is the same as train by defining the same controller. .. GENERATED FROM PYTHON SOURCE LINES 78-87 .. code-block:: default sm_tt_catalog = Catalog.from_dict( catalog_name='sm_tt_catalog', dict_of_expressions={ 'linear': SM_TT_SCALED, 'log': log(SM_TT_SCALED), }, controlled_by=train_tt_catalog.controlled_by, ) .. GENERATED FROM PYTHON SOURCE LINES 88-89 Definition of the utility functions with linear cost. .. GENERATED FROM PYTHON SOURCE LINES 89-93 .. code-block:: default V1 = ASC_TRAIN_catalog + B_TIME * train_tt_catalog + B_COST * TRAIN_COST_SCALED V2 = B_TIME * sm_tt_catalog + B_COST * SM_COST_SCALED V3 = ASC_CAR_catalog + B_TIME * CAR_TT_SCALED + B_COST * CAR_CO_SCALED .. GENERATED FROM PYTHON SOURCE LINES 94-95 Associate utility functions with the numbering of alternatives. .. GENERATED FROM PYTHON SOURCE LINES 95-97 .. code-block:: default V = {1: V1, 2: V2, 3: V3} .. GENERATED FROM PYTHON SOURCE LINES 98-99 Associate the availability conditions with the alternatives. .. GENERATED FROM PYTHON SOURCE LINES 99-101 .. code-block:: default av = {1: TRAIN_AV_SP, 2: SM_AV, 3: CAR_AV_SP} .. GENERATED FROM PYTHON SOURCE LINES 102-104 Definition of the model. This is the contribution of each observation to the log likelihood function. .. GENERATED FROM PYTHON SOURCE LINES 104-106 .. code-block:: default logprob = models.loglogit(V, av, CHOICE) .. GENERATED FROM PYTHON SOURCE LINES 107-110 .. code-block:: default the_biogeme = bio.BIOGEME(database, logprob) the_biogeme.modelName = 'b20multiple_models' .. GENERATED FROM PYTHON SOURCE LINES 111-113 .. code-block:: default dict_of_results = the_biogeme.estimate_catalog() .. GENERATED FROM PYTHON SOURCE LINES 114-118 .. code-block:: default print(f'A total of {len(dict_of_results)} models have been estimated:') for config, res in dict_of_results.items(): print(f'{config}: LL={res.data.logLike:.2f} K={res.data.nparam}') .. rst-class:: sphx-glr-script-out .. code-block:: none A total of 4 models have been estimated: ASC:no_seg;train_tt_catalog:linear: LL=-5331.25 K=4 ASC:MALE;train_tt_catalog:linear: LL=-5187.98 K=6 ASC:no_seg;train_tt_catalog:log: LL=-5350.59 K=4 ASC:MALE;train_tt_catalog:log: LL=-5184.07 K=6 .. GENERATED FROM PYTHON SOURCE LINES 119-122 .. code-block:: default summary, description = compile_estimation_results(dict_of_results, use_short_names=True) print(summary) .. rst-class:: sphx-glr-script-out .. code-block:: none Model_000000 Model_000001 Model_000002 Model_000003 Number of estimated parameters 4 6 4 6 Sample size 6768 6768 6768 6768 Final log likelihood -5331.252007 -5187.98341 -5350.594701 -5184.072742 Akaike Information Criterion 10670.504014 10387.96682 10709.189402 10380.145485 Bayesian Information Criterion 10697.783857 10428.886585 10736.469246 10421.06525 ASC_CAR (t-test) -0.155 (-2.66) -0.461 (-4.74) 1.43 (9.34) 1.19 (7.3) ASC_TRAIN (t-test) -0.701 (-8.49) 0.0906 (0.992) -0.727 (-9.81) 0.183 (2.05) B_COST (t-test) -1.08 (-15.9) -1.08 (-16.2) -1.06 (-15.8) -1.07 (-16.1) B_TIME (t-test) -1.28 (-12.3) -1.25 (-11.8) -1.37 (-14.1) -1.38 (-14.1) ASC_CAR_male (t-test) 0.309 (3.04) 0.261 (2.56) ASC_TRAIN_male (t-test) -1.23 (-15.5) -1.35 (-17.1) .. GENERATED FROM PYTHON SOURCE LINES 123-124 Explanation of the names of the models. .. GENERATED FROM PYTHON SOURCE LINES 124-128 .. code-block:: default for k, v in description.items(): if k != v: print(f'{k}: {v}') .. rst-class:: sphx-glr-script-out .. code-block:: none Model_000000: ASC:no_seg;train_tt_catalog:linear Model_000001: ASC:MALE;train_tt_catalog:linear Model_000002: ASC:no_seg;train_tt_catalog:log Model_000003: ASC:MALE;train_tt_catalog:log .. GENERATED FROM PYTHON SOURCE LINES 129-134 .. code-block:: default non_dominated_models = pareto_optimal(dict_of_results) print(f'Out of them, {len(non_dominated_models)} are non dominated.') for config, res in non_dominated_models.items(): print(f'{config}') .. rst-class:: sphx-glr-script-out .. code-block:: none Out of them, 2 are non dominated. ASC:no_seg;train_tt_catalog:linear ASC:MALE;train_tt_catalog:log .. GENERATED FROM PYTHON SOURCE LINES 135-139 .. code-block:: default summary, description = compile_estimation_results( non_dominated_models, use_short_names=False ) print(summary) .. rst-class:: sphx-glr-script-out .. code-block:: none ASC:no_seg;train_tt_catalog:linear ASC:MALE;train_tt_catalog:log Number of estimated parameters 4 6 Sample size 6768 6768 Final log likelihood -5331.252007 -5184.072742 Akaike Information Criterion 10670.504014 10380.145485 Bayesian Information Criterion 10697.783857 10421.06525 ASC_CAR (t-test) -0.155 (-2.66) 1.19 (7.3) ASC_TRAIN (t-test) -0.701 (-8.49) 0.183 (2.05) B_COST (t-test) -1.08 (-15.9) -1.07 (-16.1) B_TIME (t-test) -1.28 (-12.3) -1.38 (-14.1) ASC_CAR_male (t-test) 0.261 (2.56) ASC_TRAIN_male (t-test) -1.35 (-17.1) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.927 seconds) .. _sphx_glr_download_auto_examples_swissmetro_plot_b20multiple_models.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_b20multiple_models.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_b20multiple_models.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_