.. 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:: default 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 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:: default 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:: default CHOICE_COLUMN = 'logit_4' .. GENERATED FROM PYTHON SOURCE LINES 38-44 .. code-block:: default 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:: default 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:: default segment_sizes = generate_segment_size(SAMPLE_SIZE, the_partition.number_of_segments()) .. GENERATED FROM PYTHON SOURCE LINES 53-55 .. code-block:: default observations = pd.read_csv(OBS_FILE) .. GENERATED FROM PYTHON SOURCE LINES 56-68 .. code-block:: default 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:: default 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:: default the_data_generation = ChoiceSetsGeneration(context=context) .. GENERATED FROM PYTHON SOURCE LINES 75-77 .. code-block:: default the_model_generation = GenerateModel(context=context) .. GENERATED FROM PYTHON SOURCE LINES 78-80 .. code-block:: default 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.624533 0.050571 12.349548 0.0
beta_ethiopian 0.441458 0.050674 8.711710 0.0
beta_french 0.641939 0.062615 10.252160 0.0
beta_indian 0.927575 0.042896 21.623899 0.0
beta_japanese 1.191176 0.046636 25.541986 0.0
beta_korean 0.726871 0.042680 17.030570 0.0
beta_lebanese 0.708292 0.062616 11.311743 0.0
beta_log_dist -0.595134 0.015042 -39.564050 0.0
beta_mexican 1.216204 0.036573 33.254283 0.0
beta_price -0.405947 0.012733 -31.882617 0.0
beta_rating 0.759850 0.015470 49.116968 0.0


.. GENERATED FROM PYTHON SOURCE LINES 103-105 .. code-block:: default df, msg = compare(estimated_parameters) .. GENERATED FROM PYTHON SOURCE LINES 106-108 .. code-block:: default print(df) .. rst-class:: sphx-glr-script-out .. code-block:: none Name True Value Estimated Value T-Test 0 beta_rating 0.75 0.759850 -0.636700 1 beta_price -0.40 -0.405947 0.467069 2 beta_chinese 0.75 0.624533 2.480988 3 beta_japanese 1.25 1.191176 1.261347 4 beta_korean 0.75 0.726871 0.541905 5 beta_indian 1.00 0.927575 1.688384 6 beta_french 0.75 0.641939 1.725793 7 beta_mexican 1.25 1.216204 0.924075 8 beta_lebanese 0.75 0.708292 0.666092 9 beta_ethiopian 0.50 0.441458 1.155270 10 beta_log_dist -0.60 -0.595134 -0.323468 .. GENERATED FROM PYTHON SOURCE LINES 109-110 .. code-block:: default 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 16.782 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-python :download:`Download Python source code: plot_b01logit.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_b01logit.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_