.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/swissmetro/plot_b21multiple_models_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_swissmetro_plot_b21multiple_models_spec.py: .. _plot_b21multiple_models_spec: Specification of a catalog of models ==================================== Specification of the catalogs used by the assisted specification algorithm. Note that this script does not perform any estimation. It is imported by other scripts: :ref:`plot_b21multiple_models`, :ref:`plot_b21process_pareto`. :author: Michel Bierlaire, EPFL :date: Fri Jul 21 17:46:09 2023 .. GENERATED FROM PYTHON SOURCE LINES 14-19 .. code-block:: default import biogeme.biogeme as bio from biogeme import models from biogeme.expressions import Beta, logzero 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-38 .. 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, INCOME, GA, ) .. GENERATED FROM PYTHON SOURCE LINES 39-40 Parameters to be estimated. .. GENERATED FROM PYTHON SOURCE LINES 40-45 .. 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 46-47 Segmentations. .. GENERATED FROM PYTHON SOURCE LINES 47-55 .. code-block:: default gender_segmentation = database.generate_segmentation( variable=MALE, mapping={ 0: 'female', 1: 'male', }, ) .. GENERATED FROM PYTHON SOURCE LINES 56-69 .. code-block:: default income_segmentation = database.generate_segmentation( variable=INCOME, mapping={ 0: 'inc-zero', 1: 'inc-under50', 2: 'inc-50-100', 3: 'inc-100+', 4: 'inc-unknown', }, ) print(f'{income_segmentation=}') .. rst-class:: sphx-glr-script-out .. code-block:: none income_segmentation=INCOME: [{0: 'inc-zero', 1: 'inc-under50', 2: 'inc-50-100', 3: 'inc-100+', 4: 'inc-unknown'}] ref: inc-zero .. GENERATED FROM PYTHON SOURCE LINES 70-74 .. code-block:: default ga_segmentation = database.generate_segmentation( variable=GA, mapping={1: 'GA', 0: 'noGA'} ) .. GENERATED FROM PYTHON SOURCE LINES 75-80 .. code-block:: default asc_segmentations = ( gender_segmentation, ga_segmentation, ) .. GENERATED FROM PYTHON SOURCE LINES 81-88 .. code-block:: default ASC_CAR_catalog, ASC_TRAIN_catalog = segmentation_catalogs( generic_name='ASC', beta_parameters=[ASC_CAR, ASC_TRAIN], potential_segmentations=asc_segmentations, maximum_number=2, ) .. GENERATED FROM PYTHON SOURCE LINES 89-94 .. code-block:: default cost_segmentations = ( ga_segmentation, income_segmentation, ) .. GENERATED FROM PYTHON SOURCE LINES 95-98 Note that the function returns a list. In this case, it contains only one element. This is the reason of the presence of the comma after B_COST_catalog .. GENERATED FROM PYTHON SOURCE LINES 98-105 .. code-block:: default (B_COST_catalog,) = segmentation_catalogs( generic_name='B_COST', beta_parameters=[B_COST], potential_segmentations=cost_segmentations, maximum_number=1, ) .. GENERATED FROM PYTHON SOURCE LINES 106-107 Parameter for Box-Cox transforms .. GENERATED FROM PYTHON SOURCE LINES 107-109 .. code-block:: default ell_time = Beta('lambda_time', 1, None, 10, 0) .. GENERATED FROM PYTHON SOURCE LINES 110-111 Potential non linear specifications of travel time. .. GENERATED FROM PYTHON SOURCE LINES 111-140 .. code-block:: default TRAIN_TT_catalog = Catalog.from_dict( catalog_name='TRAIN_TT', dict_of_expressions={ 'linear': TRAIN_TT_SCALED, 'log': logzero(TRAIN_TT_SCALED), 'boxcox': models.boxcox(TRAIN_TT_SCALED, ell_time), }, ) SM_TT_catalog = Catalog.from_dict( catalog_name='SM_TT', dict_of_expressions={ 'linear': SM_TT_SCALED, 'log': logzero(SM_TT_SCALED), 'boxcox': models.boxcox(SM_TT_SCALED, ell_time), }, controlled_by=TRAIN_TT_catalog.controlled_by, ) CAR_TT_catalog = Catalog.from_dict( catalog_name='CAR_TT', dict_of_expressions={ 'linear': CAR_TT_SCALED, 'log': logzero(CAR_TT_SCALED), 'boxcox': models.boxcox(CAR_TT_SCALED, ell_time), }, controlled_by=TRAIN_TT_catalog.controlled_by, ) .. GENERATED FROM PYTHON SOURCE LINES 141-142 Definition of the utility functions with linear cost. .. GENERATED FROM PYTHON SOURCE LINES 142-146 .. code-block:: default V1 = ASC_TRAIN_catalog + B_TIME * TRAIN_TT_catalog + B_COST_catalog * TRAIN_COST_SCALED V2 = B_TIME * SM_TT_catalog + B_COST_catalog * SM_COST_SCALED V3 = ASC_CAR_catalog + B_TIME * CAR_TT_catalog + B_COST_catalog * CAR_CO_SCALED .. GENERATED FROM PYTHON SOURCE LINES 147-148 Associate utility functions with the numbering of alternatives. .. GENERATED FROM PYTHON SOURCE LINES 148-150 .. code-block:: default V = {1: V1, 2: V2, 3: V3} .. GENERATED FROM PYTHON SOURCE LINES 151-152 Associate the availability conditions with the alternatives. .. GENERATED FROM PYTHON SOURCE LINES 152-154 .. code-block:: default av = {1: TRAIN_AV_SP, 2: SM_AV, 3: CAR_AV_SP} .. GENERATED FROM PYTHON SOURCE LINES 155-157 Definition of the model. This is the contribution of each observation to the log likelihood function. .. GENERATED FROM PYTHON SOURCE LINES 157-159 .. code-block:: default logprob = models.loglogit(V, av, CHOICE) .. GENERATED FROM PYTHON SOURCE LINES 160-165 .. code-block:: default print( f'Total number of possible specifications: ' f'{logprob.number_of_multiple_expressions()}' ) .. rst-class:: sphx-glr-script-out .. code-block:: none Total number of possible specifications: 36 .. GENERATED FROM PYTHON SOURCE LINES 166-167 Create the biogeme object. .. GENERATED FROM PYTHON SOURCE LINES 167-170 .. code-block:: default the_biogeme = bio.BIOGEME(database, logprob) the_biogeme.modelName = 'b21multiple_models' .. GENERATED FROM PYTHON SOURCE LINES 171-172 Name of the Pareto file. .. GENERATED FROM PYTHON SOURCE LINES 172-173 .. code-block:: default PARETO_FILE_NAME = 'b21multiple_models.pareto' .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.132 seconds) .. _sphx_glr_download_auto_examples_swissmetro_plot_b21multiple_models_spec.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_b21multiple_models_spec.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_b21multiple_models_spec.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_