.. 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. :author: Michel Bierlaire :date: Wed Nov 1 18:00:15 2023 .. GENERATED FROM PYTHON SOURCE LINES 11-33 .. code-block:: Python import pandas as pd from biogeme.sampling_of_alternatives import ( SamplingContext, ChoiceSetsGeneration, GenerateModel, generate_segment_size, ) from biogeme.expressions import Beta from biogeme.nests import OneNestForNestedLogit, NestsForNestedLogit import biogeme.biogeme_logging as blog import biogeme.biogeme as bio from specification_sampling import V, combined_variables from compare import compare from alternatives import ( alternatives, ID_COLUMN, partitions, asian, all_alternatives, ) .. GENERATED FROM PYTHON SOURCE LINES 34-36 .. code-block:: Python logger = blog.get_screen_logger(level=blog.INFO) .. GENERATED FROM PYTHON SOURCE LINES 37-45 .. 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 46-50 .. code-block:: Python the_partition = partitions.get(PARTITION) if the_partition is None: raise ValueError(f'Unknown partition: {PARTITION}') .. GENERATED FROM PYTHON SOURCE LINES 51-53 .. code-block:: Python segment_sizes = generate_segment_size(SAMPLE_SIZE, the_partition.number_of_segments()) .. GENERATED FROM PYTHON SOURCE LINES 54-55 We use all alternatives in the nest. .. GENERATED FROM PYTHON SOURCE LINES 55-60 .. 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 61-63 .. code-block:: Python observations = pd.read_csv('obs_choice.dat') .. GENERATED FROM PYTHON SOURCE LINES 64-78 .. 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 79-81 .. 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 82-85 .. code-block:: Python the_data_generation = ChoiceSetsGeneration(context=context) the_model_generation = GenerateModel(context=context) .. GENERATED FROM PYTHON SOURCE LINES 86-88 .. 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
Value Rob. Std err Rob. t-test Rob. p-value
beta_chinese 0.700229 0.071300 9.820906 0.0
beta_ethiopian 0.503377 0.040232 12.511976 0.0
beta_french 0.744952 0.048852 15.249200 0.0
beta_indian 0.976977 0.063607 15.359560 0.0
beta_japanese 1.236949 0.054294 22.782394 0.0
beta_korean 0.696263 0.061596 11.303638 0.0
beta_lebanese 0.713084 0.049902 14.289703 0.0
beta_log_dist -0.588648 0.012729 -46.243923 0.0
beta_mexican 1.204193 0.029153 41.306497 0.0
beta_price -0.404956 0.012275 -32.989608 0.0
beta_rating 0.763591 0.015256 50.052379 0.0
mu_asian 2.019067 0.059071 34.180512 0.0


.. GENERATED FROM PYTHON SOURCE LINES 124-126 .. code-block:: Python df, msg = compare(estimated_parameters) .. GENERATED FROM PYTHON SOURCE LINES 127-129 .. 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.763591 -0.890885 1 beta_price -0.40 -0.404956 0.403743 2 beta_chinese 0.75 0.700229 0.698055 3 beta_japanese 1.25 1.236949 0.240375 4 beta_korean 0.75 0.696263 0.872402 5 beta_indian 1.00 0.976977 0.361951 6 beta_french 0.75 0.744952 0.103343 7 beta_mexican 1.25 1.204193 1.571277 8 beta_lebanese 0.75 0.713084 0.739766 9 beta_ethiopian 0.50 0.503377 -0.083940 10 beta_log_dist -0.60 -0.588648 -0.891786 11 mu_asian 2.00 2.019067 -0.322789 .. GENERATED FROM PYTHON SOURCE LINES 130-131 .. code-block:: Python print(msg) .. rst-class:: sphx-glr-script-out .. code-block:: none Parameters not estimated: ['mu_downtown'] .. rst-class:: sphx-glr-timing **Total running time of the script:** (1 minutes 16.858 seconds) .. _sphx_glr_download_auto_examples_sampling_plot_b02nested.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_b02nested.ipynb ` .. 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 `_