.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/swissmetro/plot_b21b_multiple_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_b21b_multiple_models_spec.py: .. _plot_b21b_multiple_models_spec: 21b. 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_b21a_multiple_models`, :ref:`plot_b21c_process_pareto`. Michel Bierlaire, EPFL Sat Jun 28 2025, 12:09:34 .. GENERATED FROM PYTHON SOURCE LINES 13-19 .. code-block:: Python from biogeme.biogeme import BIOGEME from biogeme.catalog import Catalog, segmentation_catalogs from biogeme.expressions import Beta, logzero from biogeme.models import boxcox, loglogit .. GENERATED FROM PYTHON SOURCE LINES 20-21 See the data processing script: :ref:`swissmetro_data`. .. GENERATED FROM PYTHON SOURCE LINES 21-38 .. code-block:: Python from swissmetro_data import ( CAR_AV_SP, CAR_CO_SCALED, CAR_TT_SCALED, CHOICE, GA, INCOME, MALE, SM_AV, SM_COST_SCALED, SM_TT_SCALED, TRAIN_AV_SP, TRAIN_COST_SCALED, TRAIN_TT_SCALED, database, ) .. GENERATED FROM PYTHON SOURCE LINES 39-40 Parameters to be estimated. .. GENERATED FROM PYTHON SOURCE LINES 40-45 .. 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 46-47 Segmentations. .. GENERATED FROM PYTHON SOURCE LINES 47-55 .. code-block:: Python gender_segmentation = database.generate_segmentation( variable=MALE, mapping={ 0: 'female', 1: 'male', }, ) .. GENERATED FROM PYTHON SOURCE LINES 56-69 .. code-block:: Python 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:: Python ga_segmentation = database.generate_segmentation( variable=GA, mapping={1: 'GA', 0: 'noGA'} ) .. GENERATED FROM PYTHON SOURCE LINES 75-80 .. code-block:: Python asc_segmentations = ( gender_segmentation, ga_segmentation, ) .. GENERATED FROM PYTHON SOURCE LINES 81-88 .. code-block:: Python 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:: Python 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:: Python (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:: Python ell_time = Beta('lambda_time', 1, -10, 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:: Python train_tt_catalog = Catalog.from_dict( catalog_name='train_tt', dict_of_expressions={ 'linear': TRAIN_TT_SCALED, 'log': logzero(TRAIN_TT_SCALED), 'boxcox': 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': 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': 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-148 .. code-block:: Python v_train = ( asc_train_catalog + b_time * train_tt_catalog + b_cost_catalog * TRAIN_COST_SCALED ) v_swissmetro = b_time * sm_tt_catalog + b_cost_catalog * SM_COST_SCALED v_car = asc_car_catalog + b_time * car_tt_catalog + b_cost_catalog * CAR_CO_SCALED .. GENERATED FROM PYTHON SOURCE LINES 149-150 Associate utility functions with the numbering of alternatives. .. GENERATED FROM PYTHON SOURCE LINES 150-152 .. code-block:: Python v = {1: v_train, 2: v_swissmetro, 3: v_car} .. GENERATED FROM PYTHON SOURCE LINES 153-154 Associate the availability conditions with the alternatives. .. GENERATED FROM PYTHON SOURCE LINES 154-156 .. code-block:: Python av = {1: TRAIN_AV_SP, 2: SM_AV, 3: CAR_AV_SP} .. GENERATED FROM PYTHON SOURCE LINES 157-159 Definition of the model. This is the contribution of each observation to the log likelihood function. .. GENERATED FROM PYTHON SOURCE LINES 159-162 .. code-block:: Python log_probability = loglogit(v, av, CHOICE) .. GENERATED FROM PYTHON SOURCE LINES 163-164 Create the biogeme object. .. GENERATED FROM PYTHON SOURCE LINES 164-167 .. code-block:: Python the_biogeme = BIOGEME(database, log_probability) the_biogeme.model_name = 'b21_multiple_models' .. GENERATED FROM PYTHON SOURCE LINES 168-169 Name of the Pareto file. .. GENERATED FROM PYTHON SOURCE LINES 169-170 .. code-block:: Python PARETO_FILE_NAME = 'b21_multiple_models.pareto' .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.010 seconds) .. _sphx_glr_download_auto_examples_swissmetro_plot_b21b_multiple_models_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_b21b_multiple_models_spec.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_b21b_multiple_models_spec.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_b21b_multiple_models_spec.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_