.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/sampling/plot_b02nested.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_b02nested.py: Nested logit ============ Estimation of a nested logit model using sampling of alternatives. Michel Bierlaire Sat Jul 26 2025, 13:01:22 .. GENERATED FROM PYTHON SOURCE LINES 11-30 .. 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, partitions from biogeme.biogeme import BIOGEME from biogeme.expressions import Beta from biogeme.nests import NestsForNestedLogit, OneNestForNestedLogit 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 31-33 .. code-block:: Python logger = blog.get_screen_logger(level=blog.INFO) .. GENERATED FROM PYTHON SOURCE LINES 34-42 .. code-block:: Python SAMPLE_SIZE = 20 # out of 100 SAMPLE_SIZE_MEV = 33 # out of 33 CHOICE_COLUMN = 'nested_0' PARTITION = 'downtown' MEV_PARTITION = 'uniform_asian' MODEL_NAME = f'nested_{PARTITION}_{SAMPLE_SIZE}' FILE_NAME = f'{MODEL_NAME}.dat' .. GENERATED FROM PYTHON SOURCE LINES 43-47 .. code-block:: Python the_partition = partitions.get(PARTITION) if the_partition is None: raise ValueError(f'Unknown partition: {PARTITION}') .. GENERATED FROM PYTHON SOURCE LINES 48-50 .. code-block:: Python segment_sizes = generate_segment_size(SAMPLE_SIZE, the_partition.number_of_segments()) .. GENERATED FROM PYTHON SOURCE LINES 51-52 We use all alternatives in the nest. .. GENERATED FROM PYTHON SOURCE LINES 52-57 .. 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 58-60 .. code-block:: Python observations = pd.read_csv('obs_choice.dat') .. GENERATED FROM PYTHON SOURCE LINES 61-75 .. 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, ) .. GENERATED FROM PYTHON SOURCE LINES 76-78 .. 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: 20: 10/46, 10/54 Nbr of MEV alternatives: 33 MEV partition: 1 segment(s) of size 33 MEV sample: 33: 33/33 .. GENERATED FROM PYTHON SOURCE LINES 79-82 .. code-block:: Python the_data_generation = ChoiceSetsGeneration(context=context) the_model_generation = GenerateModel(context=context) .. GENERATED FROM PYTHON SOURCE LINES 83-85 .. code-block:: Python biogeme_database = the_data_generation.sample_and_merge(recycle=False) .. rst-class:: sphx-glr-script-out .. code-block:: none Generating 20 + 33 alternatives for 10000 observations 0%| | 0/10000 [00:00` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_b02nested.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_b02nested.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_