.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/indicators/plot_b02estimation.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_indicators_plot_b02estimation.py: Estimation and simulation of a nested logit model ================================================= We estimate a nested logit model, and we perform simulation using the estimated model. Michel Bierlaire, EPFL Sat Jun 28 2025, 16:08:07 .. GENERATED FROM PYTHON SOURCE LINES 12-26 .. code-block:: Python from IPython.core.display_functions import display import biogeme.biogeme_logging as blog from biogeme.biogeme import BIOGEME from biogeme.calculator import get_value_c from biogeme.data.optima import read_data from biogeme.models import lognested from biogeme.results_processing import get_pandas_estimated_parameters from scenarios import scenario logger = blog.get_screen_logger(level=blog.INFO) logger.info('Example plot_b02estimation') .. rst-class:: sphx-glr-script-out .. code-block:: none Example plot_b02estimation .. GENERATED FROM PYTHON SOURCE LINES 27-29 Obtain the specification for the default scenario. The definition of the scenarios is available in :ref:`scenarios`. .. GENERATED FROM PYTHON SOURCE LINES 29-31 .. code-block:: Python V, nests, choice, _ = scenario() .. GENERATED FROM PYTHON SOURCE LINES 32-34 The choice model is a nested logit, with availability conditions For estimation, we need the log of the probability. .. GENERATED FROM PYTHON SOURCE LINES 34-36 .. code-block:: Python log_probability = lognested(util=V, availability=None, nests=nests, choice=choice) .. GENERATED FROM PYTHON SOURCE LINES 37-38 Get the database .. GENERATED FROM PYTHON SOURCE LINES 38-39 .. code-block:: Python database = read_data() .. GENERATED FROM PYTHON SOURCE LINES 40-41 Create the Biogeme object for estimation. .. GENERATED FROM PYTHON SOURCE LINES 41-44 .. code-block:: Python the_biogeme = BIOGEME(database, log_probability) the_biogeme.model_name = 'b02estimation' .. rst-class:: sphx-glr-script-out .. code-block:: none Biogeme parameters read from biogeme.toml. .. GENERATED FROM PYTHON SOURCE LINES 45-46 Estimate the parameters. Perform bootstrapping. .. GENERATED FROM PYTHON SOURCE LINES 46-48 .. code-block:: Python results = the_biogeme.estimate(run_bootstrap=True) .. rst-class:: sphx-glr-script-out .. code-block:: none *** Initial values of the parameters are obtained from the file __b02estimation.iter Parameter values restored from __b02estimation.iter Starting values for the algorithm: {'beta_time_fulltime': -1.5964670520847424, 'beta_time_other': -0.5526862214775381, 'beta_cost': -0.719072321557382, 'mu_no_car': 1.5237160520807354, 'asc_sm': 0.06300034044143561, 'beta_dist_male': -0.6876768914763282, 'beta_dist_female': -0.8321525469977931, 'beta_dist_unreported': -0.7047372090923671, 'asc_car': 0.2586072914689233} 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: 4.478787282617695e-06 Cause of termination: Relative gradient = 4.5e-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.466837 Calculate second derivatives and BHHH Re-estimate the model 100 times for bootstrapping Bootstraps: 0%| | 0/100 [00:00` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_b02estimation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_b02estimation.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_