.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/sampling/plot_b03cnl.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_sampling_plot_b03cnl.py: Cross-nested logit ================== Estimation of a cross-nested logit model using sampling of alternatives. Michel Bierlaire Sat Jul 26 2025, 14:57:17 .. GENERATED FROM PYTHON SOURCE LINES 11-38 .. code-block:: Python import pandas as pd from IPython.core.display_functions import display import biogeme.biogeme_logging as blog from alternatives import ( ID_COLUMN, all_alternatives, alternatives, asian_and_downtown, only_asian, only_downtown, partitions, ) from biogeme.biogeme import BIOGEME from biogeme.expressions import Beta from biogeme.nests import NestsForCrossNestedLogit, OneNestForCrossNestedLogit from biogeme.results_processing import get_pandas_estimated_parameters from biogeme.sampling_of_alternatives import ( ChoiceSetsGeneration, GenerateModel, SamplingContext, generate_segment_size, ) from compare import compare from specification_sampling import V, combined_variables .. GENERATED FROM PYTHON SOURCE LINES 39-41 .. code-block:: Python logger = blog.get_screen_logger(level=blog.INFO) .. GENERATED FROM PYTHON SOURCE LINES 42-50 .. code-block:: Python PARTITION = 'downtown' MEV_PARTITION = 'uniform_asian_or_downtown' SAMPLE_SIZE = 10 # out of 100 alternatives SAMPLE_SIZE_MEV = 63 # out of 63 alternatives CHOICE_COLUMN = 'cnl_3' MODEL_NAME = f'cnl_{SAMPLE_SIZE}_{SAMPLE_SIZE_MEV}' FILE_NAME = f'{MODEL_NAME}.dat' .. GENERATED FROM PYTHON SOURCE LINES 51-55 .. code-block:: Python the_partition = partitions.get(PARTITION) if the_partition is None: raise ValueError(f'Unknown partition: {PARTITION}') .. GENERATED FROM PYTHON SOURCE LINES 56-60 .. code-block:: Python segment_sizes = list( generate_segment_size(SAMPLE_SIZE, the_partition.number_of_segments()) ) .. GENERATED FROM PYTHON SOURCE LINES 61-62 We use all alternatives in the nest. .. GENERATED FROM PYTHON SOURCE LINES 62-69 .. code-block:: Python mev_partition = partitions.get(MEV_PARTITION) if mev_partition is None: raise ValueError(f'Unknown partition: {MEV_PARTITION}') mev_segment_sizes = [ SAMPLE_SIZE_MEV, ] .. GENERATED FROM PYTHON SOURCE LINES 70-71 Nests .. GENERATED FROM PYTHON SOURCE LINES 73-74 Downtown .. GENERATED FROM PYTHON SOURCE LINES 74-82 .. code-block:: Python mu_downtown = Beta('mu_downtown', 1, 1, None, 0) downtown_alpha_dict = {i: 0.5 for i in asian_and_downtown} | { i: 1 for i in only_downtown } downtown_nest = OneNestForCrossNestedLogit( nest_param=mu_downtown, dict_of_alpha=downtown_alpha_dict, name='downtown' ) .. GENERATED FROM PYTHON SOURCE LINES 83-84 Asian .. GENERATED FROM PYTHON SOURCE LINES 84-95 .. code-block:: Python mu_asian = Beta('mu_asian', 1, 1, None, 0) asian_alpha_dict = {i: 0.5 for i in asian_and_downtown} | {i: 1.0 for i in only_asian} asian_nest = OneNestForCrossNestedLogit( nest_param=mu_asian, dict_of_alpha=asian_alpha_dict, name='asian' ) cnl_nests = NestsForCrossNestedLogit( choice_set=all_alternatives, tuple_of_nests=(downtown_nest, asian_nest), ) .. rst-class:: sphx-glr-script-out .. code-block:: none The following elements do not appear in any nest and are assumed each to be alone in a separate nest: {2, 4, 5, 6, 8, 10, 12, 16, 19, 21, 23, 25, 26, 28, 29, 35, 39, 41, 42, 43, 44, 52, 53, 58, 59, 62, 64, 65, 66, 67, 69, 71, 73, 82, 85, 95, 96}. If it is not the intention, check the assignment of alternatives to nests. .. GENERATED FROM PYTHON SOURCE LINES 96-98 .. code-block:: Python observations = pd.read_csv('obs_choice.dat') .. GENERATED FROM PYTHON SOURCE LINES 99-114 .. code-block:: Python context = SamplingContext( the_partition=the_partition, sample_sizes=segment_sizes, individuals=observations, choice_column=CHOICE_COLUMN, alternatives=alternatives, id_column=ID_COLUMN, biogeme_file_name=FILE_NAME, utility_function=V, combined_variables=combined_variables, mev_partition=mev_partition, mev_sample_sizes=mev_segment_sizes, cnl_nests=cnl_nests, ) .. GENERATED FROM PYTHON SOURCE LINES 115-117 .. code-block:: Python logger.info(context.reporting()) .. rst-class:: sphx-glr-script-out .. code-block:: none Size of the choice set: 100 Main partition: 2 segment(s) of size 46, 54 Main sample: 10: 5/46, 5/54 Nbr of MEV alternatives: 63 MEV partition: 1 segment(s) of size 63 MEV sample: 63: 63/63 .. GENERATED FROM PYTHON SOURCE LINES 118-121 .. code-block:: Python the_data_generation = ChoiceSetsGeneration(context=context) the_model_generation = GenerateModel(context=context) .. GENERATED FROM PYTHON SOURCE LINES 122-124 .. code-block:: Python biogeme_database = the_data_generation.sample_and_merge(recycle=False) .. rst-class:: sphx-glr-script-out .. code-block:: none Generating 10 + 63 alternatives for 10000 observations 0%| | 0/10000 [00:00` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_b03cnl.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_b03cnl.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_