.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/swissmetro/plot_b10_nested_bottom.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_swissmetro_plot_b10_nested_bottom.py: 10. Nested logit model normalized from bottom ============================================= Example of a nested logit model where the normalization is done at the bottom level. Michel Bierlaire, EPFL Sat Jun 21 2025, 16:31:18 .. GENERATED FROM PYTHON SOURCE LINES 12-25 .. code-block:: Python from IPython.core.display_functions import display import biogeme.biogeme_logging as blog from biogeme.biogeme import BIOGEME from biogeme.expressions import Beta from biogeme.models import lognested_mev_mu from biogeme.nests import NestsForNestedLogit, OneNestForNestedLogit from biogeme.results_processing import ( EstimationResults, get_pandas_estimated_parameters, ) .. GENERATED FROM PYTHON SOURCE LINES 26-27 See the data processing script: :ref:`swissmetro_data`. .. GENERATED FROM PYTHON SOURCE LINES 27-44 .. code-block:: Python from swissmetro_data import ( CAR_AV_SP, CAR_CO_SCALED, CAR_TT_SCALED, CHOICE, SM_AV, SM_COST_SCALED, SM_TT_SCALED, TRAIN_AV_SP, TRAIN_COST_SCALED, TRAIN_TT_SCALED, database, ) logger = blog.get_screen_logger(level=blog.INFO) logger.info('Example b10_nested_bottom.py') .. rst-class:: sphx-glr-script-out .. code-block:: none Example b10_nested_bottom.py .. GENERATED FROM PYTHON SOURCE LINES 45-46 Parameters to be estimated. .. GENERATED FROM PYTHON SOURCE LINES 46-52 .. code-block:: Python asc_car = Beta('asc_car', 0, None, None, 0) asc_train = Beta('asc_train', 0, None, None, 0) asc_sm = Beta('asc_sm', 0, None, None, 1) b_time = Beta('b_time', 0, None, None, 0) b_cost = Beta('b_cost', 0, None, None, 0) .. GENERATED FROM PYTHON SOURCE LINES 53-59 This is the scale parameter of the choice model. It is usually normalized to one. In this example, we normalize the nest parameter instead, and estimate the scale parameter for the model. If the lower bound is set to zero, the model cannot be evaluated. Therefore, we set the lower bound to a small number, strictly larger than zero. .. GENERATED FROM PYTHON SOURCE LINES 59-61 .. code-block:: Python scale_parameter = Beta('scale_parameter', 0.5, 0.000001, 1.0, 0) .. GENERATED FROM PYTHON SOURCE LINES 62-63 Definition of the utility functions .. GENERATED FROM PYTHON SOURCE LINES 63-67 .. code-block:: Python v_train = asc_train + b_time * TRAIN_TT_SCALED + b_cost * TRAIN_COST_SCALED v_swissmetro = asc_sm + b_time * SM_TT_SCALED + b_cost * SM_COST_SCALED v_car = asc_car + b_time * CAR_TT_SCALED + b_cost * CAR_CO_SCALED .. GENERATED FROM PYTHON SOURCE LINES 68-69 Associate utility functions with the numbering of alternatives. .. GENERATED FROM PYTHON SOURCE LINES 69-71 .. code-block:: Python v = {1: v_train, 2: v_swissmetro, 3: v_car} .. GENERATED FROM PYTHON SOURCE LINES 72-73 Associate the availability conditions with the alternatives. .. GENERATED FROM PYTHON SOURCE LINES 73-75 .. code-block:: Python av = {1: TRAIN_AV_SP, 2: SM_AV, 3: CAR_AV_SP} .. GENERATED FROM PYTHON SOURCE LINES 76-78 Definition of nests. Only the non trivial nests must be defined. A trivial nest is a nest containing exactly one alternative. The nest parameter is normalized to 1. .. GENERATED FROM PYTHON SOURCE LINES 78-85 .. code-block:: Python nest_parameter = 1.0 existing = OneNestForNestedLogit( nest_param=nest_parameter, list_of_alternatives=[1, 3], name='existing' ) nests = NestsForNestedLogit(choice_set=list(v), tuple_of_nests=(existing,)) .. 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}. If it is not the intention, check the assignment of alternatives to nests. .. GENERATED FROM PYTHON SOURCE LINES 86-90 Definition of the model. This is the contribution of each observation to the log likelihood function. The choice model is a nested logit, with availability conditions, where the scale parameter mu is explicitly involved. .. GENERATED FROM PYTHON SOURCE LINES 90-92 .. code-block:: Python log_probability = lognested_mev_mu(v, av, nests, CHOICE, scale_parameter) .. GENERATED FROM PYTHON SOURCE LINES 93-94 Create the Biogeme object. .. GENERATED FROM PYTHON SOURCE LINES 94-97 .. code-block:: Python the_biogeme = BIOGEME(database, log_probability) the_biogeme.model_name = 'b10_nested_bottom' .. rst-class:: sphx-glr-script-out .. code-block:: none Biogeme parameters read from biogeme.toml. .. GENERATED FROM PYTHON SOURCE LINES 98-99 Estimate the parameters. .. GENERATED FROM PYTHON SOURCE LINES 99-106 .. code-block:: Python try: results = EstimationResults.from_yaml_file( filename=f'saved_results/{the_biogeme.model_name}.yaml' ) except FileNotFoundError: results = the_biogeme.estimate() .. rst-class:: sphx-glr-script-out .. code-block:: none *** Initial values of the parameters are obtained from the file __b10_nested_bottom.iter Cannot read file __b10_nested_bottom.iter. Statement is ignored. Starting values for the algorithm: {} As the model is rather complex, we cancel the calculation of second derivatives. If you want to control the parameters, change the algorithm from "automatic" to "simple_bounds" in the TOML file. Optimization algorithm: hybrid Newton/BFGS with simple bounds [simple_bounds] ** Optimization: BFGS with trust region for simple bounds Iter. asc_train b_time b_cost scale_parameter asc_car Function Relgrad Radius Rho 0 -1 -1 -1 1e-06 1 5.8e+03 0.039 1 0.23 + 1 -0.88 -1.2 -1.3 1 0.62 5.8e+03 0.15 1 0.15 + 2 -0.45 -1.5 -1.1 0.87 -0.38 5.4e+03 0.044 1 0.47 + 3 -0.45 -1.5 -1.1 0.87 -0.38 5.4e+03 0.044 0.44 -1 - 4 -0.89 -1.5 -1.6 0.44 0.06 5.3e+03 0.028 0.44 0.3 + 5 -0.89 -1.5 -1.6 0.44 0.06 5.3e+03 0.028 0.22 -0.69 - 6 -1.1 -1.8 -1.4 0.65 -0.16 5.3e+03 0.029 0.22 0.15 + 7 -0.9 -1.7 -1.4 0.45 -0.38 5.3e+03 0.015 0.22 0.29 + 8 -0.93 -1.7 -1.6 0.57 -0.28 5.2e+03 0.0044 0.22 0.66 + 9 -0.93 -1.7 -1.6 0.57 -0.28 5.2e+03 0.0044 0.11 -1 - 10 -0.93 -1.7 -1.6 0.57 -0.28 5.2e+03 0.0044 0.054 -0.11 - 11 -0.98 -1.7 -1.6 0.52 -0.23 5.2e+03 0.0049 0.054 0.33 + 12 -0.98 -1.8 -1.6 0.54 -0.28 5.2e+03 0.0029 0.054 0.51 + 13 -0.98 -1.8 -1.6 0.54 -0.28 5.2e+03 0.0029 0.027 -0.34 - 14 -0.98 -1.8 -1.6 0.54 -0.28 5.2e+03 0.0029 0.014 0.041 - 15 -0.97 -1.8 -1.6 0.53 -0.28 5.2e+03 0.0024 0.014 0.69 + 16 -0.98 -1.8 -1.6 0.52 -0.29 5.2e+03 0.0023 0.14 0.92 ++ 17 -0.98 -1.8 -1.6 0.52 -0.29 5.2e+03 0.0023 0.068 -1 - 18 -0.98 -1.8 -1.6 0.52 -0.29 5.2e+03 0.0023 0.034 -1.3 - 19 -0.98 -1.8 -1.6 0.52 -0.29 5.2e+03 0.0023 0.017 -0.034 - 20 -1 -1.8 -1.6 0.51 -0.28 5.2e+03 0.0024 0.017 0.46 + 21 -1 -1.8 -1.7 0.52 -0.29 5.2e+03 0.0017 0.017 0.71 + 22 -1 -1.8 -1.7 0.5 -0.3 5.2e+03 0.0018 0.017 0.6 + 23 -1 -1.8 -1.7 0.5 -0.31 5.2e+03 0.0011 0.17 0.95 ++ 24 -1 -1.8 -1.7 0.5 -0.31 5.2e+03 0.0011 0.085 -1.2 - 25 -1.1 -1.8 -1.8 0.48 -0.32 5.2e+03 0.0018 0.085 0.24 + 26 -1.1 -1.8 -1.8 0.48 -0.32 5.2e+03 0.0018 0.043 -1.2 - 27 -1 -1.9 -1.8 0.49 -0.36 5.2e+03 0.0017 0.043 0.12 + 28 -1 -1.9 -1.8 0.49 -0.36 5.2e+03 0.0017 0.021 -0.0035 - 29 -1.1 -1.9 -1.8 0.48 -0.35 5.2e+03 0.0009 0.021 0.59 + 30 -1.1 -1.9 -1.8 0.48 -0.35 5.2e+03 0.0009 0.011 -0.19 - 31 -1.1 -1.8 -1.8 0.49 -0.35 5.2e+03 0.00053 0.011 0.59 + 32 -1.1 -1.8 -1.8 0.49 -0.35 5.2e+03 0.00053 0.0053 -0.048 - 33 -1.1 -1.8 -1.8 0.49 -0.35 5.2e+03 0.00027 0.0053 0.49 + 34 -1 -1.8 -1.8 0.49 -0.34 5.2e+03 0.00016 0.0053 0.4 + 35 -1 -1.8 -1.8 0.49 -0.34 5.2e+03 0.00016 0.0027 -0.33 - 36 -1.1 -1.8 -1.8 0.49 -0.34 5.2e+03 5.5e-05 0.0027 0.64 + 37 -1.1 -1.8 -1.8 0.49 -0.34 5.2e+03 5.5e-05 0.0013 -2.7 - 38 -1.1 -1.8 -1.8 0.49 -0.34 5.2e+03 5.5e-05 0.00067 -0.37 - 39 -1.1 -1.8 -1.8 0.49 -0.34 5.2e+03 5.9e-05 0.00067 0.62 + 40 -1.1 -1.8 -1.8 0.49 -0.34 5.2e+03 5.9e-05 0.00033 -0.59 - 41 -1.1 -1.8 -1.8 0.49 -0.34 5.2e+03 7.6e-05 0.00033 0.19 + 42 -1.1 -1.8 -1.8 0.49 -0.34 5.2e+03 1.2e-05 0.0033 0.92 ++ 43 -1.1 -1.8 -1.8 0.49 -0.34 5.2e+03 1.2e-05 0.0017 -7.5 - 44 -1.1 -1.8 -1.8 0.49 -0.34 5.2e+03 1.2e-05 0.00083 -1.5 - 45 -1.1 -1.8 -1.8 0.49 -0.34 5.2e+03 1.2e-05 0.00042 -0.06 - 46 -1.1 -1.8 -1.8 0.49 -0.34 5.2e+03 4.1e-06 0.00042 0.83 - Optimization algorithm has converged. Relative gradient: 4.075657253042217e-06 Cause of termination: Relative gradient = 4.1e-06 <= 6.1e-06 Number of function evaluations: 100 Number of gradient evaluations: 53 Number of hessian evaluations: 0 Algorithm: BFGS with trust region for simple bound constraints Number of iterations: 47 Proportion of Hessian calculation: 0/26 = 0.0% Optimization time: 0:00:00.417002 Calculate second derivatives and BHHH File b10_nested_bottom.html has been generated. File b10_nested_bottom.yaml has been generated. .. GENERATED FROM PYTHON SOURCE LINES 107-109 .. code-block:: Python print(results.short_summary()) .. rst-class:: sphx-glr-script-out .. code-block:: none Results for model b10_nested_bottom Nbr of parameters: 5 Sample size: 6768 Excluded data: 3960 Final log likelihood: -5236.9 Akaike Information Criterion: 10483.8 Bayesian Information Criterion: 10517.9 .. GENERATED FROM PYTHON SOURCE LINES 110-112 .. code-block:: Python pandas_results = get_pandas_estimated_parameters(estimation_results=results) display(pandas_results) .. rst-class:: sphx-glr-script-out .. code-block:: none Name Value Robust std err. Robust t-stat. Robust p-value 0 asc_train -1.051457 0.164958 -6.374090 1.840521e-10 1 b_time -1.845921 0.225646 -8.180598 2.220446e-16 2 b_cost -1.759568 0.149300 -11.785451 0.000000e+00 3 scale_parameter 0.486866 0.038917 12.510471 0.000000e+00 4 asc_car -0.343211 0.118811 -2.888721 3.868117e-03 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.522 seconds) .. _sphx_glr_download_auto_examples_swissmetro_plot_b10_nested_bottom.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_b10_nested_bottom.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_b10_nested_bottom.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_b10_nested_bottom.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_