.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/assisted/plot_b05alt_spec_segmentation.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_b05alt_spec_segmentation.py: Segmentations and alternative specific specification ==================================================== We consider 4 specifications for the constants: - Not segmented - Segmented by GA (yearly subscription to public transport) - Segmented by luggage - Segmented both by GA and luggage We consider 6 specifications for the time coefficients: - Generic and not segmented - Generic and segmented with first class - Generic and segmented with trip purpose - Alternative specific and not segmented - Alternative specific and segmented with first class - Alternative specific and segmented with trip purpose We consider 2 specifications for the cost coefficients: - Generic - Alternative specific We obtain a total of 48 specifications. See `Bierlaire and Ortelli (2023) `_. Michel Bierlaire, EPFL Sun Apr 27 2025, 15:54:33 .. GENERATED FROM PYTHON SOURCE LINES 34-57 .. code-block:: Python import numpy as np from IPython.core.display_functions import display from biogeme.biogeme import BIOGEME from biogeme.catalog import generic_alt_specific_catalogs, segmentation_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 .. GENERATED FROM PYTHON SOURCE LINES 58-59 Read the data .. GENERATED FROM PYTHON SOURCE LINES 59-61 .. code-block:: Python database = read_data() .. GENERATED FROM PYTHON SOURCE LINES 62-63 Definition of the segmentations. .. GENERATED FROM PYTHON SOURCE LINES 63-76 .. code-block:: Python segmentation_ga = database.generate_segmentation( variable='GA', mapping={0: 'noGA', 1: 'GA'} ) segmentation_luggage = database.generate_segmentation( variable='LUGGAGE', mapping={0: 'no_lugg', 1: 'one_lugg', 3: 'several_lugg'} ) segmentation_first = database.generate_segmentation( variable='FIRST', mapping={0: '2nd_class', 1: '1st_class'} ) .. GENERATED FROM PYTHON SOURCE LINES 77-79 We consider two trip purposes: 'commuters' and anything else. We need to define a binary variable first. .. GENERATED FROM PYTHON SOURCE LINES 79-85 .. code-block:: Python database.dataframe['COMMUTERS'] = np.where(database.dataframe['PURPOSE'] == 1, 1, 0) segmentation_purpose = database.generate_segmentation( variable='COMMUTERS', mapping={0: 'non_commuters', 1: 'commuters'} ) .. GENERATED FROM PYTHON SOURCE LINES 86-87 Parameters to be estimated. .. GENERATED FROM PYTHON SOURCE LINES 87-92 .. 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 93-94 Catalogs for the alternative specific constants. .. GENERATED FROM PYTHON SOURCE LINES 94-105 .. code-block:: Python asc_train_catalog, asc_car_catalog = segmentation_catalogs( generic_name='asc', beta_parameters=[asc_train, asc_car], potential_segmentations=( segmentation_ga, segmentation_luggage, ), maximum_number=2, ) .. GENERATED FROM PYTHON SOURCE LINES 106-110 Catalog for the travel time coefficient. Note that the function returns a list of catalogs. Here, the list contains only one of them. This is why there is a comma after "B_TIME_catalog". .. GENERATED FROM PYTHON SOURCE LINES 110-121 .. code-block:: Python (b_time_catalog_dict,) = generic_alt_specific_catalogs( generic_name='b_time', beta_parameters=[b_time], alternatives=('train', 'swissmetro', 'car'), potential_segmentations=( segmentation_first, segmentation_purpose, ), maximum_number=1, ) .. GENERATED FROM PYTHON SOURCE LINES 122-123 Catalog for the travel cost coefficient. .. GENERATED FROM PYTHON SOURCE LINES 123-129 .. 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 130-131 Definition of the utility functions. .. GENERATED FROM PYTHON SOURCE LINES 131-146 .. code-block:: Python v_train = ( asc_train_catalog + 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_catalog + b_time_catalog_dict['car'] * CAR_TT_SCALED + b_cost_catalog_dict['car'] * 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:: Python v = {1: v_train, 2: v_swissmetro, 3: v_car} .. GENERATED FROM PYTHON SOURCE LINES 151-152 Associate the availability conditions with the alternatives. .. GENERATED FROM PYTHON SOURCE LINES 152-154 .. code-block:: Python 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:: Python log_probability = loglogit(v, av, CHOICE) .. GENERATED FROM PYTHON SOURCE LINES 160-161 Create the Biogeme object. .. GENERATED FROM PYTHON SOURCE LINES 161-166 .. code-block:: Python the_biogeme = BIOGEME( database, log_probability, generate_html=False, generate_yaml=False ) the_biogeme.model_name = 'b05alt_spec_segmentation' .. GENERATED FROM PYTHON SOURCE LINES 167-168 Estimate the parameters. .. GENERATED FROM PYTHON SOURCE LINES 168-170 .. code-block:: Python dict_of_results = the_biogeme.estimate_catalog() .. GENERATED FROM PYTHON SOURCE LINES 171-172 Number of estimated models. .. GENERATED FROM PYTHON SOURCE LINES 172-174 .. 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 48 models have been estimated .. GENERATED FROM PYTHON SOURCE LINES 175-176 All estimation results .. GENERATED FROM PYTHON SOURCE LINES 176-180 .. code-block:: Python compiled_results, specs = compile_estimation_results( dict_of_results, use_short_names=True ) .. GENERATED FROM PYTHON SOURCE LINES 181-184 .. 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_000047 Number of estimated parameters 9 ... 10 Sample size 10719 ... 10719 Final log likelihood -8562.469 ... -8382.888 Akaike Information Criterion 17142.94 ... 16785.78 Bayesian Information Criterion 17208.46 ... 16858.57 asc_train_ref (t-test) -1.37 (-15.8) ... -0.694 (-7.17) asc_train_diff_one_lugg (t-test) 0.956 (12.7) ... 0.772 (9.72) asc_train_diff_several_lugg (t-test) 0.946 (5.39) ... 0.679 (3.8) b_time_ref (t-test) -1.25 (-22.6) ... b_time_diff_commuters (t-test) -0.0704 (-0.337) ... b_cost (t-test) -0.782 (-15.5) ... asc_car_ref (t-test) 0.0456 (0.891) ... -0.371 (-5.78) asc_car_diff_one_lugg (t-test) -0.0792 (-1.56) ... -0.0975 (-1.95) asc_car_diff_several_lugg (t-test) -0.538 (-2.57) ... -0.54 (-2.52) asc_train (t-test) ... b_time_train_ref (t-test) ... b_time_train_diff_commuters (t-test) ... b_cost_train (t-test) ... -1.76 (-16.1) b_time_swissmetro_ref (t-test) ... b_time_swissmetro_diff_commuters (t-test) ... b_cost_swissmetro (t-test) ... -0.819 (-15.5) asc_car (t-test) ... b_time_car_ref (t-test) ... b_time_car_diff_commuters (t-test) ... b_cost_car (t-test) ... -0.366 (-4.77) b_time_train_diff_1st_class (t-test) ... b_time_swissmetro_diff_1st_class (t-test) ... b_time_car_diff_1st_class (t-test) ... asc_train_diff_GA (t-test) ... asc_car_diff_GA (t-test) ... b_time (t-test) ... -1.28 (-16.9) b_time_diff_1st_class (t-test) ... b_time_train (t-test) ... b_time_swissmetro (t-test) ... b_time_car (t-test) ... [35 rows x 48 columns] .. GENERATED FROM PYTHON SOURCE LINES 185-186 Glossary .. GENERATED FROM PYTHON SOURCE LINES 186-189 .. 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 asc:LUGGAGE;b_cost_gen_altspec:generic;b_time:COMMUTERS;b_time_gen_altspec:generic Model_000001 asc:no_seg;b_cost_gen_altspec:altspec;b_time:COMMUTERS;b_time_gen_altspec:altspec Model_000002 asc:LUGGAGE;b_cost_gen_altspec:generic;b_time:FIRST;b_time_gen_altspec:altspec Model_000003 asc:GA-LUGGAGE;b_cost_gen_altspec:generic;b_time:FIRST;b_time_gen_altspec:altspec Model_000004 asc:LUGGAGE;b_cost_gen_altspec:generic;b_time:no_seg;b_time_gen_altspec:generic Model_000005 asc:GA;b_cost_gen_altspec:altspec;b_time:FIRST;b_time_gen_altspec:generic Model_000006 asc:GA-LUGGAGE;b_cost_gen_altspec:generic;b_time:no_seg;b_time_gen_altspec:generic Model_000007 asc:GA;b_cost_gen_altspec:altspec;b_time:FIRST;b_time_gen_altspec:altspec Model_000008 asc:GA-LUGGAGE;b_cost_gen_altspec:altspec;b_time:FIRST;b_time_gen_altspec:altspec Model_000009 asc:LUGGAGE;b_cost_gen_altspec:altspec;b_time:FIRST;b_time_gen_altspec:generic Model_000010 asc:GA;b_cost_gen_altspec:generic;b_time:no_seg;b_time_gen_altspec:altspec Model_000011 asc:GA;b_cost_gen_altspec:generic;b_time:no_seg;b_time_gen_altspec:generic Model_000012 asc:GA-LUGGAGE;b_cost_gen_altspec:generic;b_time:FIRST;b_time_gen_altspec:generic Model_000013 asc:GA-LUGGAGE;b_cost_gen_altspec:altspec;b_time:FIRST;b_time_gen_altspec:generic Model_000014 asc:no_seg;b_cost_gen_altspec:generic;b_time:FIRST;b_time_gen_altspec:generic Model_000015 asc:GA;b_cost_gen_altspec:altspec;b_time:no_seg;b_time_gen_altspec:altspec Model_000016 asc:GA-LUGGAGE;b_cost_gen_altspec:altspec;b_time:COMMUTERS;b_time_gen_altspec:altspec Model_000017 asc:GA-LUGGAGE;b_cost_gen_altspec:altspec;b_time:COMMUTERS;b_time_gen_altspec:generic Model_000018 asc:no_seg;b_cost_gen_altspec:altspec;b_time:COMMUTERS;b_time_gen_altspec:generic Model_000019 asc:LUGGAGE;b_cost_gen_altspec:altspec;b_time:FIRST;b_time_gen_altspec:altspec Model_000020 asc:no_seg;b_cost_gen_altspec:altspec;b_time:FIRST;b_time_gen_altspec:generic Model_000021 asc:GA;b_cost_gen_altspec:altspec;b_time:COMMUTERS;b_time_gen_altspec:altspec Model_000022 asc:no_seg;b_cost_gen_altspec:generic;b_time:FIRST;b_time_gen_altspec:altspec Model_000023 asc:GA-LUGGAGE;b_cost_gen_altspec:generic;b_time:no_seg;b_time_gen_altspec:altspec Model_000024 asc:no_seg;b_cost_gen_altspec:altspec;b_time:no_seg;b_time_gen_altspec:generic Model_000025 asc:LUGGAGE;b_cost_gen_altspec:altspec;b_time:COMMUTERS;b_time_gen_altspec:generic Model_000026 asc:no_seg;b_cost_gen_altspec:generic;b_time:no_seg;b_time_gen_altspec:altspec Model_000027 asc:GA;b_cost_gen_altspec:altspec;b_time:no_seg;b_time_gen_altspec:generic Model_000028 asc:GA-LUGGAGE;b_cost_gen_altspec:generic;b_time:COMMUTERS;b_time_gen_altspec:altspec Model_000029 asc:GA;b_cost_gen_altspec:generic;b_time:FIRST;b_time_gen_altspec:altspec Model_000030 asc:GA;b_cost_gen_altspec:altspec;b_time:COMMUTERS;b_time_gen_altspec:generic Model_000031 asc:GA-LUGGAGE;b_cost_gen_altspec:altspec;b_time:no_seg;b_time_gen_altspec:generic Model_000032 asc:GA;b_cost_gen_altspec:generic;b_time:COMMUTERS;b_time_gen_altspec:generic Model_000033 asc:GA-LUGGAGE;b_cost_gen_altspec:altspec;b_time:no_seg;b_time_gen_altspec:altspec Model_000034 asc:GA;b_cost_gen_altspec:generic;b_time:COMMUTERS;b_time_gen_altspec:altspec Model_000035 asc:no_seg;b_cost_gen_altspec:generic;b_time:COMMUTERS;b_time_gen_altspec:altspec Model_000036 asc:LUGGAGE;b_cost_gen_altspec:generic;b_time:COMMUTERS;b_time_gen_altspec:altspec Model_000037 asc:GA;b_cost_gen_altspec:generic;b_time:FIRST;b_time_gen_altspec:generic Model_000038 asc:no_seg;b_cost_gen_altspec:altspec;b_time:no_seg;b_time_gen_altspec:altspec Model_000039 asc:LUGGAGE;b_cost_gen_altspec:generic;b_time:no_seg;b_time_gen_altspec:altspec Model_000040 asc:no_seg;b_cost_gen_altspec:generic;b_time:no_seg;b_time_gen_altspec:generic Model_000041 asc:GA-LUGGAGE;b_cost_gen_altspec:generic;b_time:COMMUTERS;b_time_gen_altspec:generic Model_000042 asc:LUGGAGE;b_cost_gen_altspec:altspec;b_time:no_seg;b_time_gen_altspec:altspec Model_000043 asc:no_seg;b_cost_gen_altspec:altspec;b_time:FIRST;b_time_gen_altspec:altspec Model_000044 asc:LUGGAGE;b_cost_gen_altspec:altspec;b_time:COMMUTERS;b_time_gen_altspec:altspec Model_000045 asc:no_seg;b_cost_gen_altspec:generic;b_time:COMMUTERS;b_time_gen_altspec:generic Model_000046 asc:LUGGAGE;b_cost_gen_altspec:generic;b_time:FIRST;b_time_gen_altspec:generic Model_000047 asc:LUGGAGE;b_cost_gen_altspec:altspec;b_time:no_seg;b_time_gen_altspec:generic .. GENERATED FROM PYTHON SOURCE LINES 190-191 Estimation results of the Pareto optimal models. .. GENERATED FROM PYTHON SOURCE LINES 191-196 .. code-block:: Python pareto_results = pareto_optimal(dict_of_results) compiled_pareto_results, pareto_specs = compile_estimation_results( pareto_results, use_short_names=True ) .. GENERATED FROM PYTHON SOURCE LINES 197-200 .. 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_000008 Number of estimated parameters 5 ... 11 Sample size 10719 ... 10719 Final log likelihood -8598.531 ... -8151.782 Akaike Information Criterion 17207.06 ... 16325.56 Bayesian Information Criterion 17243.46 ... 16405.64 asc_train (t-test) -0.707 (-12.5) ... b_time_ref (t-test) -0.916 (-10.9) ... b_time_diff_1st_class (t-test) -0.689 (-9.57) ... b_cost (t-test) -0.87 (-15.6) ... -0.753 (-14.7) asc_car (t-test) 0.0091 (0.245) ... asc_train_ref (t-test) ... -0.969 (-9.45) asc_train_diff_GA (t-test) ... 1.55 (20.9) b_time (t-test) ... asc_car_ref (t-test) ... -0.483 (-5.64) asc_car_diff_GA (t-test) ... -1 (-6.48) b_time_train_ref (t-test) ... -1.56 (-22.5) b_time_train_diff_commuters (t-test) ... 0.325 (2.84) b_cost_train (t-test) ... b_time_swissmetro_ref (t-test) ... -1.77 (-24.5) b_time_swissmetro_diff_commuters (t-test) ... 1.62 (10.3) b_cost_swissmetro (t-test) ... b_time_car_ref (t-test) ... -1.18 (-21.5) b_time_car_diff_commuters (t-test) ... 0.642 (3.92) b_cost_car (t-test) ... asc_train_diff_one_lugg (t-test) ... asc_train_diff_several_lugg (t-test) ... asc_car_diff_one_lugg (t-test) ... asc_car_diff_several_lugg (t-test) ... [28 rows x 9 columns] .. GENERATED FROM PYTHON SOURCE LINES 201-202 Glossary. .. GENERATED FROM PYTHON SOURCE LINES 202-204 .. 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 asc:no_seg;b_cost_gen_altspec:generic;b_time:FIRST;b_time_gen_altspec:generic Model_000001 asc:GA;b_cost_gen_altspec:generic;b_time:no_seg;b_time_gen_altspec:generic Model_000002 asc:GA;b_cost_gen_altspec:altspec;b_time:COMMUTERS;b_time_gen_altspec:altspec Model_000003 asc:GA-LUGGAGE;b_cost_gen_altspec:altspec;b_time:COMMUTERS;b_time_gen_altspec:altspec Model_000004 asc:no_seg;b_cost_gen_altspec:generic;b_time:no_seg;b_time_gen_altspec:generic Model_000005 asc:GA-LUGGAGE;b_cost_gen_altspec:generic;b_time:COMMUTERS;b_time_gen_altspec:altspec Model_000006 asc:GA;b_cost_gen_altspec:altspec;b_time:FIRST;b_time_gen_altspec:generic Model_000007 asc:GA;b_cost_gen_altspec:generic;b_time:FIRST;b_time_gen_altspec:generic Model_000008 asc:GA;b_cost_gen_altspec:generic;b_time:COMMUTERS;b_time_gen_altspec:altspec .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 56.122 seconds) .. _sphx_glr_download_auto_examples_assisted_plot_b05alt_spec_segmentation.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_b05alt_spec_segmentation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_b05alt_spec_segmentation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_b05alt_spec_segmentation.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_