.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/sampling/plot_b01logit.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_b01logit.py: Logit ===== Estimation of a logit model using sampling of alternatives. Michel Bierlaire Fri Jul 25 2025, 17:36:23 .. GENERATED FROM PYTHON SOURCE LINES 11-28 .. 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, alternatives, partitions from biogeme.biogeme import BIOGEME 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 .. rst-class:: sphx-glr-script-out .. code-block:: none ID rating price ... rest_lon distance downtown 0 0 1 4 ... 42.220972 71.735518 1.0 1 1 2 2 ... 50.549434 106.267205 0.0 2 2 3 3 ... 97.830520 136.298409 0.0 3 3 4 1 ... 69.152206 85.941147 0.0 4 4 4 3 ... 89.145620 96.773021 0.0 .. .. ... ... ... ... ... ... 95 95 4 3 ... 9.511387 84.166441 0.0 96 96 1 1 ... 92.144641 95.601366 0.0 97 97 4 2 ... 27.657518 30.440555 1.0 98 98 4 4 ... 32.303213 45.027143 1.0 99 99 4 1 ... 13.672495 25.703295 1.0 [100 rows x 16 columns] Number of asian restaurants: 33 .. GENERATED FROM PYTHON SOURCE LINES 29-31 .. code-block:: Python logger = blog.get_screen_logger(level=blog.INFO) .. GENERATED FROM PYTHON SOURCE LINES 32-34 The data file contains several columns associated with synthetic choices. Here we arbitrarily select `logit_4`. .. GENERATED FROM PYTHON SOURCE LINES 34-36 .. code-block:: Python CHOICE_COLUMN = 'logit_4' .. GENERATED FROM PYTHON SOURCE LINES 37-43 .. code-block:: Python SAMPLE_SIZE = 10 PARTITION = 'asian' MODEL_NAME = f'logit_{PARTITION}_{SAMPLE_SIZE}_alt' FILE_NAME = f'{MODEL_NAME}.dat' OBS_FILE = 'obs_choice.dat' .. GENERATED FROM PYTHON SOURCE LINES 44-48 .. code-block:: Python the_partition = partitions.get(PARTITION) if the_partition is None: raise ValueError(f'Unknown partition: {PARTITION}') .. GENERATED FROM PYTHON SOURCE LINES 49-51 .. code-block:: Python segment_sizes = generate_segment_size(SAMPLE_SIZE, the_partition.number_of_segments()) .. GENERATED FROM PYTHON SOURCE LINES 52-54 .. code-block:: Python observations = pd.read_csv(OBS_FILE) .. GENERATED FROM PYTHON SOURCE LINES 55-67 .. 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, ) .. GENERATED FROM PYTHON SOURCE LINES 68-70 .. 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 33, 67 Main sample: 10: 5/33, 5/67 .. GENERATED FROM PYTHON SOURCE LINES 71-73 .. code-block:: Python the_data_generation = ChoiceSetsGeneration(context=context) .. GENERATED FROM PYTHON SOURCE LINES 74-76 .. code-block:: Python the_model_generation = GenerateModel(context=context) .. GENERATED FROM PYTHON SOURCE LINES 77-79 .. code-block:: Python biogeme_database = the_data_generation.sample_and_merge(recycle=False) .. rst-class:: sphx-glr-script-out .. code-block:: none Generating 10 + 0 alternatives for 10000 observations 0%| | 0/10000 [00:00` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_b01logit.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_b01logit.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_