.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/swissmetro/plot_b11cnl_old.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_b11cnl_old.py: Cross-nested logit (old syntax) =============================== Example of a cross-nested logit model, using the original syntax for nests. Since biogeme 3.13, a new syntax, more explicit, has been adopted. There are two nests: - one with existing alternatives (car and train), - one with public transportation alternatives (train and Swissmetro) :author: Michel Bierlaire, EPFL :date: Sun Apr 9 18:06:44 2023 .. GENERATED FROM PYTHON SOURCE LINES 17-23 .. code-block:: default import biogeme.biogeme_logging as blog import biogeme.biogeme as bio from biogeme import models from biogeme.expressions import Beta .. GENERATED FROM PYTHON SOURCE LINES 24-25 See the data processing script: :ref:`swissmetro_data`. .. GENERATED FROM PYTHON SOURCE LINES 25-42 .. code-block:: default from swissmetro_data import ( database, CHOICE, SM_AV, CAR_AV_SP, TRAIN_AV_SP, TRAIN_TT_SCALED, TRAIN_COST_SCALED, SM_TT_SCALED, SM_COST_SCALED, CAR_TT_SCALED, CAR_CO_SCALED, ) logger = blog.get_screen_logger(level=blog.INFO) logger.info('Example b11cnl.py') .. rst-class:: sphx-glr-script-out .. code-block:: none Example b11cnl.py .. GENERATED FROM PYTHON SOURCE LINES 43-44 Parameters to be estimated. .. GENERATED FROM PYTHON SOURCE LINES 44-50 .. code-block:: default 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 51-54 .. code-block:: default MU_EXISTING = Beta('MU_EXISTING', 1, 1, 10, 0) MU_PUBLIC = Beta('MU_PUBLIC', 1, 1, 10, 0) .. GENERATED FROM PYTHON SOURCE LINES 55-56 Definition of the utility functions. .. GENERATED FROM PYTHON SOURCE LINES 56-60 .. code-block:: default V1 = ASC_TRAIN + B_TIME * TRAIN_TT_SCALED + B_COST * TRAIN_COST_SCALED V2 = ASC_SM + B_TIME * SM_TT_SCALED + B_COST * SM_COST_SCALED V3 = ASC_CAR + B_TIME * CAR_TT_SCALED + B_COST * CAR_CO_SCALED .. GENERATED FROM PYTHON SOURCE LINES 61-62 Associate utility functions with the numbering of alternatives. .. GENERATED FROM PYTHON SOURCE LINES 62-64 .. code-block:: default V = {1: V1, 2: V2, 3: V3} .. GENERATED FROM PYTHON SOURCE LINES 65-66 Associate the availability conditions with the alternatives. .. GENERATED FROM PYTHON SOURCE LINES 66-68 .. code-block:: default av = {1: TRAIN_AV_SP, 2: SM_AV, 3: CAR_AV_SP} .. GENERATED FROM PYTHON SOURCE LINES 69-70 Definition of nests. .. GENERATED FROM PYTHON SOURCE LINES 70-81 .. code-block:: default ALPHA_EXISTING = Beta('ALPHA_EXISTING', 0.5, 0, 1, 0) alpha_existing = {1: ALPHA_EXISTING, 2: 0.0, 3: 1.0} ALPHA_PUBLIC = 1 - ALPHA_EXISTING alpha_public = {1: ALPHA_PUBLIC, 2: 1.0, 3: 0.0} nest_existing = MU_EXISTING, alpha_existing nest_public = MU_PUBLIC, alpha_public nests = nest_existing, nest_public .. GENERATED FROM PYTHON SOURCE LINES 82-83 The choice model is a cross-nested logit, with availability conditions. .. GENERATED FROM PYTHON SOURCE LINES 83-85 .. code-block:: default logprob = models.logcnl_avail(V, av, nests, CHOICE) .. rst-class:: sphx-glr-script-out .. code-block:: none /Users/bierlair/OnlineFiles/FilesOnGoogleDrive/github/biogeme/docs/examples/swissmetro/plot_b11cnl_old.py:83: DeprecationWarning: The function logcnl_avail is deprecated. It has been replaced by the function logcnl logprob = models.logcnl_avail(V, av, nests, CHOICE) It is recommended to define the nests of the cross-nested logit model using the objects OneNestForNestedLogit and NestsForCrossNestedLogit defined in biogeme.nests. .. GENERATED FROM PYTHON SOURCE LINES 86-87 Create the Biogeme object. .. GENERATED FROM PYTHON SOURCE LINES 87-90 .. code-block:: default the_biogeme = bio.BIOGEME(database, logprob) the_biogeme.modelName = 'b11cnl' .. rst-class:: sphx-glr-script-out .. code-block:: none File biogeme.toml has been parsed. .. GENERATED FROM PYTHON SOURCE LINES 91-92 Estimate the parameters .. GENERATED FROM PYTHON SOURCE LINES 92-94 .. code-block:: default results = the_biogeme.estimate() .. rst-class:: sphx-glr-script-out .. code-block:: none *** Initial values of the parameters are obtained from the file __b11cnl.iter Parameter values restored from __b11cnl.iter Optimization algorithm: hybrid Newton/BFGS with simple bounds [simple_bounds] ** Optimization: Newton with trust region for simple bounds Results saved in file b11cnl~00.html Results saved in file b11cnl~00.pickle .. GENERATED FROM PYTHON SOURCE LINES 95-97 .. code-block:: default print(results.short_summary()) .. rst-class:: sphx-glr-script-out .. code-block:: none Results for model b11cnl Nbr of parameters: 7 Sample size: 6768 Excluded data: 3960 Final log likelihood: -5214.049 Akaike Information Criterion: 10442.1 Bayesian Information Criterion: 10489.84 .. GENERATED FROM PYTHON SOURCE LINES 98-100 .. code-block:: default pandas_results = results.getEstimatedParameters() pandas_results .. raw:: html
Value Rob. Std err Rob. t-test Rob. p-value
ALPHA_EXISTING 0.495072 0.034752 14.246000 0.000000e+00
ASC_CAR -0.240459 0.053450 -4.498764 6.834958e-06
ASC_TRAIN 0.098277 0.069978 1.404403 1.601989e-01
B_COST -0.818885 0.058971 -13.886130 0.000000e+00
B_TIME -0.776846 0.102380 -7.587854 3.241851e-14
MU_EXISTING 2.514876 0.248325 10.127363 0.000000e+00
MU_PUBLIC 4.113595 0.496722 8.281479 2.220446e-16


.. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.367 seconds) .. _sphx_glr_download_auto_examples_swissmetro_plot_b11cnl_old.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_b11cnl_old.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_b11cnl_old.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_