.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/swissmetro/plot_b10nested_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_b10nested_bottom.py: 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-21 .. code-block:: Python import biogeme.biogeme_logging as blog from IPython.core.display_functions import display 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 get_pandas_estimated_parameters .. GENERATED FROM PYTHON SOURCE LINES 22-23 See the data processing script: :ref:`swissmetro_data`. .. GENERATED FROM PYTHON SOURCE LINES 23-40 .. 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 b10nested_bottom.py') .. rst-class:: sphx-glr-script-out .. code-block:: none Example b10nested_bottom.py .. GENERATED FROM PYTHON SOURCE LINES 41-42 Parameters to be estimated. .. GENERATED FROM PYTHON SOURCE LINES 42-48 .. 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 49-55 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 55-57 .. code-block:: Python scale_parameter = Beta('scale_parameter', 0.5, 0.000001, 1.0, 0) .. GENERATED FROM PYTHON SOURCE LINES 58-59 Definition of the utility functions .. GENERATED FROM PYTHON SOURCE LINES 59-63 .. 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 64-65 Associate utility functions with the numbering of alternatives. .. GENERATED FROM PYTHON SOURCE LINES 65-67 .. code-block:: Python v = {1: v_train, 2: v_swissmetro, 3: v_car} .. GENERATED FROM PYTHON SOURCE LINES 68-69 Associate the availability conditions with the alternatives. .. GENERATED FROM PYTHON SOURCE LINES 69-71 .. code-block:: Python av = {1: TRAIN_AV_SP, 2: SM_AV, 3: CAR_AV_SP} .. GENERATED FROM PYTHON SOURCE LINES 72-74 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 74-81 .. 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 82-86 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 86-88 .. code-block:: Python log_probability = lognested_mev_mu(v, av, nests, CHOICE, scale_parameter) .. GENERATED FROM PYTHON SOURCE LINES 89-90 Create the Biogeme object. .. GENERATED FROM PYTHON SOURCE LINES 90-93 .. code-block:: Python the_biogeme = BIOGEME(database, log_probability) the_biogeme.model_name = 'b10nested_bottom' .. rst-class:: sphx-glr-script-out .. code-block:: none Biogeme parameters read from biogeme.toml. .. GENERATED FROM PYTHON SOURCE LINES 94-95 Estimate the parameters. .. GENERATED FROM PYTHON SOURCE LINES 95-97 .. code-block:: Python results = the_biogeme.estimate() .. rst-class:: sphx-glr-script-out .. code-block:: none *** Initial values of the parameters are obtained from the file __b10nested_bottom.iter Parameter values restored from __b10nested_bottom.iter Starting values for the algorithm: {'asc_train': -1.0514574230590428, 'b_time': -1.8459208032731405, 'b_cost': -1.7595679766064984, 'scale_parameter': 0.48686560976000715, 'asc_car': -0.3432114208534819} 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 Optimization algorithm has converged. Relative gradient: 5.0743624061136086e-06 Cause of termination: Relative gradient = 5.1e-06 <= 6.1e-06 Number of function evaluations: 1 Number of gradient evaluations: 1 Number of hessian evaluations: 0 Algorithm: BFGS with trust region for simple bound constraints Number of iterations: 0 Optimization time: 0:00:00.837689 Calculate second derivatives and BHHH File b10nested_bottom~00.html has been generated. File b10nested_bottom~00.yaml has been generated. .. GENERATED FROM PYTHON SOURCE LINES 98-100 .. code-block:: Python print(results.short_summary()) .. rst-class:: sphx-glr-script-out .. code-block:: none Results for model b10nested_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 101-103 .. 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 3.541 seconds) .. _sphx_glr_download_auto_examples_swissmetro_plot_b10nested_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_b10nested_bottom.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_b10nested_bottom.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_b10nested_bottom.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_