.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/sampling/plot_b03cnl.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_b03cnl.py: Cross-nested logit ================== Estimation of a cross-nested logit model using sampling of alternatives. :author: Michel Bierlaire :date: Wed Nov 1 18:00:33 2023 .. GENERATED FROM PYTHON SOURCE LINES 11-35 .. code-block:: Python import pandas as pd from biogeme.sampling_of_alternatives import ( SamplingContext, ChoiceSetsGeneration, GenerateModel, generate_segment_size, ) from biogeme.expressions import Beta import biogeme.biogeme_logging as blog import biogeme.biogeme as bio from biogeme.nests import OneNestForCrossNestedLogit, NestsForCrossNestedLogit from specification_sampling import V, combined_variables from compare import compare from alternatives import ( alternatives, ID_COLUMN, partitions, all_alternatives, asian_and_downtown, only_downtown, only_asian, ) .. GENERATED FROM PYTHON SOURCE LINES 36-38 .. code-block:: Python logger = blog.get_screen_logger(level=blog.INFO) .. GENERATED FROM PYTHON SOURCE LINES 39-47 .. code-block:: Python PARTITION = 'downtown' MEV_PARTITION = 'uniform_asian_or_downtown' SAMPLE_SIZE = 10 # out of 100 alternatives SAMPLE_SIZE_MEV = 63 # out of 63 alternatives CHOICE_COLUMN = 'cnl_3' MODEL_NAME = f'cnl_{SAMPLE_SIZE}_{SAMPLE_SIZE_MEV}' FILE_NAME = f'{MODEL_NAME}.dat' .. GENERATED FROM PYTHON SOURCE LINES 48-52 .. code-block:: Python the_partition = partitions.get(PARTITION) if the_partition is None: raise ValueError(f'Unknown partition: {PARTITION}') .. GENERATED FROM PYTHON SOURCE LINES 53-57 .. code-block:: Python segment_sizes = list( generate_segment_size(SAMPLE_SIZE, the_partition.number_of_segments()) ) .. GENERATED FROM PYTHON SOURCE LINES 58-59 We use all alternatives in the nest. .. GENERATED FROM PYTHON SOURCE LINES 59-66 .. 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 67-68 Nests .. GENERATED FROM PYTHON SOURCE LINES 70-71 Downtown .. GENERATED FROM PYTHON SOURCE LINES 71-79 .. code-block:: Python mu_downtown = Beta('mu_downtown', 1, 1, None, 0) downtown_alpha_dict = {i: 0.5 for i in asian_and_downtown} | { i: 1 for i in only_downtown } downtown_nest = OneNestForCrossNestedLogit( nest_param=mu_downtown, dict_of_alpha=downtown_alpha_dict, name='downtown' ) .. GENERATED FROM PYTHON SOURCE LINES 80-81 Asian .. GENERATED FROM PYTHON SOURCE LINES 81-92 .. code-block:: Python mu_asian = Beta('mu_asian', 1, 1, None, 0) asian_alpha_dict = {i: 0.5 for i in asian_and_downtown} | {i: 1.0 for i in only_asian} asian_nest = OneNestForCrossNestedLogit( nest_param=mu_asian, dict_of_alpha=asian_alpha_dict, name='asian' ) cnl_nests = NestsForCrossNestedLogit( choice_set=all_alternatives, tuple_of_nests=(downtown_nest, asian_nest), ) .. rst-class:: sphx-glr-script-out .. code-block:: none The following elements do not appear in any nest and are assumed each to be alone in a separate nest: {2, 4, 5, 6, 8, 10, 12, 16, 19, 21, 23, 25, 26, 28, 29, 35, 39, 41, 42, 43, 44, 52, 53, 58, 59, 62, 64, 65, 66, 67, 69, 71, 73, 82, 85, 95, 96}. If it is not the intention, check the assignment of alternatives to nests. .. GENERATED FROM PYTHON SOURCE LINES 93-95 .. code-block:: Python observations = pd.read_csv('obs_choice.dat') .. GENERATED FROM PYTHON SOURCE LINES 96-111 .. 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, cnl_nests=cnl_nests, ) .. GENERATED FROM PYTHON SOURCE LINES 112-114 .. 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: 10: 5/46, 5/54 Nbr of MEV alternatives: 63 MEV partition: 1 segment(s) of size 63 MEV sample: 63: 63/63 .. GENERATED FROM PYTHON SOURCE LINES 115-118 .. code-block:: Python the_data_generation = ChoiceSetsGeneration(context=context) the_model_generation = GenerateModel(context=context) .. GENERATED FROM PYTHON SOURCE LINES 119-121 .. code-block:: Python biogeme_database = the_data_generation.sample_and_merge(recycle=False) .. rst-class:: sphx-glr-script-out .. code-block:: none Generating 10 + 63 alternatives for 10000 observations 0%| | 0/10000 [00:00
Value Rob. Std err Rob. t-test Rob. p-value
beta_chinese -0.355854 0.065737 -5.413305 6.187183e-08
beta_ethiopian 0.470517 0.048806 9.640480 0.000000e+00
beta_french 1.313453 0.063627 20.643022 0.000000e+00
beta_indian -0.014493 0.054403 -0.266405 7.899274e-01
beta_japanese 0.494104 0.052761 9.364955 0.000000e+00
beta_korean -0.318190 0.053328 -5.966692 2.421114e-09
beta_lebanese 0.607425 0.065966 9.208228 0.000000e+00
beta_log_dist -0.621389 0.024518 -25.344483 0.000000e+00
beta_mexican 1.413177 0.055236 25.584268 0.000000e+00
beta_price -0.521240 0.020715 -25.161987 0.000000e+00
beta_rating 1.044178 0.037827 27.604277 0.000000e+00
mu_asian 1.016981 0.037306 27.260280 0.000000e+00
mu_downtown 1.013238 0.028033 36.144043 0.000000e+00


.. GENERATED FROM PYTHON SOURCE LINES 146-148 .. code-block:: Python df, msg = compare(estimated_parameters) .. GENERATED FROM PYTHON SOURCE LINES 149-151 .. 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 1.044178 -7.777002 1 beta_price -0.40 -0.521240 5.852654 2 beta_chinese 0.75 -0.355854 16.822418 3 beta_japanese 1.25 0.494104 14.326799 4 beta_korean 0.75 -0.318190 20.030660 5 beta_indian 1.00 -0.014493 18.647743 6 beta_french 0.75 1.313453 -8.855572 7 beta_mexican 1.25 1.413177 -2.954174 8 beta_lebanese 0.75 0.607425 2.161352 9 beta_ethiopian 0.50 0.470517 0.604089 10 beta_log_dist -0.60 -0.621389 0.872401 11 mu_asian 2.00 1.016981 26.349899 12 mu_downtown 2.00 1.013238 35.199584 .. GENERATED FROM PYTHON SOURCE LINES 152-153 .. code-block:: Python print(msg) .. rst-class:: sphx-glr-timing **Total running time of the script:** (4 minutes 31.760 seconds) .. _sphx_glr_download_auto_examples_sampling_plot_b03cnl.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_b03cnl.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_b03cnl.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_b03cnl.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_