.. 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. :author: Michel Bierlaire :date: Wed Nov 1 17:39:47 2023 .. GENERATED FROM PYTHON SOURCE LINES 11-29 .. code-block:: Python import pandas as pd from biogeme.sampling_of_alternatives import ( SamplingContext, ChoiceSetsGeneration, GenerateModel, generate_segment_size, ) import biogeme.biogeme_logging as blog import biogeme.biogeme as bio from compare import compare from specification_sampling import V, combined_variables from alternatives import ( alternatives, ID_COLUMN, partitions, ) .. rst-class:: sphx-glr-script-out .. code-block:: none Number of asian restaurants: 33 .. GENERATED FROM PYTHON SOURCE LINES 30-32 .. code-block:: Python logger = blog.get_screen_logger(level=blog.INFO) .. GENERATED FROM PYTHON SOURCE LINES 33-35 The data file contains several columns associated with synthetic choices. Here we arbitrarily select `logit_4`. .. GENERATED FROM PYTHON SOURCE LINES 35-37 .. code-block:: Python CHOICE_COLUMN = 'logit_4' .. GENERATED FROM PYTHON SOURCE LINES 38-44 .. 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 45-49 .. code-block:: Python the_partition = partitions.get(PARTITION) if the_partition is None: raise ValueError(f'Unknown partition: {PARTITION}') .. GENERATED FROM PYTHON SOURCE LINES 50-52 .. code-block:: Python segment_sizes = generate_segment_size(SAMPLE_SIZE, the_partition.number_of_segments()) .. GENERATED FROM PYTHON SOURCE LINES 53-55 .. code-block:: Python observations = pd.read_csv(OBS_FILE) .. GENERATED FROM PYTHON SOURCE LINES 56-68 .. 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 69-71 .. 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 72-74 .. code-block:: Python the_data_generation = ChoiceSetsGeneration(context=context) .. GENERATED FROM PYTHON SOURCE LINES 75-77 .. code-block:: Python the_model_generation = GenerateModel(context=context) .. GENERATED FROM PYTHON SOURCE LINES 78-80 .. code-block:: Python biogeme_database = the_data_generation.sample_and_merge(recycle=False) .. rst-class:: sphx-glr-script-out .. code-block:: none Generating 10 alternatives for 10000 observations 0%| | 0/10000 [00:00
Value Rob. Std err Rob. t-test Rob. p-value
beta_chinese 0.607892 0.050236 12.100600 0.0
beta_ethiopian 0.436107 0.050743 8.594396 0.0
beta_french 0.637537 0.061926 10.295114 0.0
beta_indian 0.917637 0.043002 21.339266 0.0
beta_japanese 1.152123 0.046626 24.709975 0.0
beta_korean 0.713678 0.042509 16.788924 0.0
beta_lebanese 0.655038 0.062660 10.453805 0.0
beta_log_dist -0.586987 0.014885 -39.435942 0.0
beta_mexican 1.181418 0.036514 32.355573 0.0
beta_price -0.408117 0.012796 -31.894969 0.0
beta_rating 0.751644 0.015430 48.714276 0.0


.. GENERATED FROM PYTHON SOURCE LINES 103-105 .. code-block:: Python df, msg = compare(estimated_parameters) .. GENERATED FROM PYTHON SOURCE LINES 106-108 .. code-block:: Python print(df) .. rst-class:: sphx-glr-script-out .. code-block:: none Name True Value Estimated Value T-Test 0 beta_rating 0.75 0.751644 -0.106559 1 beta_price -0.40 -0.408117 0.634324 2 beta_chinese 0.75 0.607892 2.828786 3 beta_japanese 1.25 1.152123 2.099197 4 beta_korean 0.75 0.713678 0.854456 5 beta_indian 1.00 0.917637 1.915316 6 beta_french 0.75 0.637537 1.816087 7 beta_mexican 1.25 1.181418 1.878265 8 beta_lebanese 0.75 0.655038 1.515505 9 beta_ethiopian 0.50 0.436107 1.259136 10 beta_log_dist -0.60 -0.586987 -0.874276 .. GENERATED FROM PYTHON SOURCE LINES 109-110 .. code-block:: Python print(msg) .. rst-class:: sphx-glr-script-out .. code-block:: none Parameters not estimated: ['mu_asian', 'mu_downtown'] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 11.010 seconds) .. _sphx_glr_download_auto_examples_sampling_plot_b01logit.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_b01logit.ipynb ` .. 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 `_