.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/swissmetro/plot_b11c_cnl_sparse.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_b11c_cnl_sparse.py: 11c. Cross-nested logit with a sparse structure =============================================== Example of a cross-nested logit model with two nests: - one with existing alternatives (car and train), - one with public transportation alternatives (train and Swissmetro) This illustrates the possibility to ignore all membership parameters that are 0. Michel Bierlaire, EPFL Sat Jun 21 2025, 16:50:19 .. GENERATED FROM PYTHON SOURCE LINES 17-29 .. 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 logcnl from biogeme.nests import NestsForCrossNestedLogit, OneNestForCrossNestedLogit from biogeme.results_processing import ( EstimationResults, get_pandas_estimated_parameters, ) .. GENERATED FROM PYTHON SOURCE LINES 30-31 See the data processing script: :ref:`swissmetro_data`. .. GENERATED FROM PYTHON SOURCE LINES 31-48 .. 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 b11c_cnl_sparse.py') .. rst-class:: sphx-glr-script-out .. code-block:: none Example b11c_cnl_sparse.py .. GENERATED FROM PYTHON SOURCE LINES 49-50 Parameters to be estimated. .. GENERATED FROM PYTHON SOURCE LINES 50-56 .. 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 57-60 .. code-block:: Python existing_nest_parameter = Beta('existing_nest_parameter', 1, 1, 5, 0) public_nest_parameter = Beta('public_nest_parameter', 1, 1, 5, 0) .. GENERATED FROM PYTHON SOURCE LINES 61-62 Nest membership parameters. .. GENERATED FROM PYTHON SOURCE LINES 62-66 .. code-block:: Python alpha_existing = Beta('alpha_existing', 0.5, 0, 1, 0) alpha_public = 1 - alpha_existing .. GENERATED FROM PYTHON SOURCE LINES 67-68 Definition of the utility functions .. GENERATED FROM PYTHON SOURCE LINES 68-72 .. 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 73-74 Associate utility functions with the numbering of alternatives .. GENERATED FROM PYTHON SOURCE LINES 74-76 .. code-block:: Python v = {1: v_train, 2: v_swissmetro, 3: v_car} .. GENERATED FROM PYTHON SOURCE LINES 77-78 Associate the availability conditions with the alternatives .. GENERATED FROM PYTHON SOURCE LINES 78-80 .. code-block:: Python av = {1: TRAIN_AV_SP, 2: SM_AV, 3: CAR_AV_SP} .. GENERATED FROM PYTHON SOURCE LINES 81-82 Definition of nests. .. GENERATED FROM PYTHON SOURCE LINES 84-85 The parameter for alternative 2 is omitted, which is equivalent to sez it to zero. .. GENERATED FROM PYTHON SOURCE LINES 85-91 .. code-block:: Python nest_existing = OneNestForCrossNestedLogit( nest_param=existing_nest_parameter, dict_of_alpha={1: alpha_existing, 3: 1.0}, name='existing', ) .. GENERATED FROM PYTHON SOURCE LINES 92-93 The parameter for alternative 3 is omitted, which is equivalent to sez it to zero. .. GENERATED FROM PYTHON SOURCE LINES 93-103 .. code-block:: Python nest_public = OneNestForCrossNestedLogit( nest_param=public_nest_parameter, dict_of_alpha={1: alpha_public, 2: 1.0}, name='public', ) nests = NestsForCrossNestedLogit( choice_set=[1, 2, 3], tuple_of_nests=(nest_existing, nest_public) ) .. GENERATED FROM PYTHON SOURCE LINES 104-105 The choice model is a cross-nested logit, with availability conditions. .. GENERATED FROM PYTHON SOURCE LINES 105-107 .. code-block:: Python log_probability = logcnl(v, av, nests, CHOICE) .. GENERATED FROM PYTHON SOURCE LINES 108-109 Create the Biogeme object .. GENERATED FROM PYTHON SOURCE LINES 109-112 .. code-block:: Python the_biogeme = BIOGEME(database, log_probability) the_biogeme.model_name = 'b11c_cnl_sparse' .. rst-class:: sphx-glr-script-out .. code-block:: none Biogeme parameters read from biogeme.toml. .. GENERATED FROM PYTHON SOURCE LINES 113-114 Estimate the parameters. .. GENERATED FROM PYTHON SOURCE LINES 114-121 .. 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 __b11c_cnl_sparse.iter Cannot read file __b11c_cnl_sparse.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 alpha_existing existing_nest_p asc_car public_nest_par Function Relgrad Radius Rho 0 -1 -1 -1 0.5 2 -1 2 5.8e+03 0.11 1 0.24 + 1 -0.55 -0.54 -1.2 1 1.9 -0.64 1.9 5.5e+03 0.13 1 0.4 + 2 -0.55 -0.54 -1.2 1 1.9 -0.64 1.9 5.5e+03 0.13 0.5 -0.53 - 3 -0.55 -0.54 -1.2 1 1.9 -0.64 1.9 5.5e+03 0.13 0.25 0.078 - 4 -0.8 -0.79 -1.1 0.75 1.9 -0.39 1.9 5.3e+03 0.059 0.25 0.51 + 5 -0.55 -0.83 -0.94 0.87 1.8 -0.24 1.8 5.2e+03 0.0086 0.25 0.75 + 6 -0.55 -0.83 -0.94 0.87 1.8 -0.24 1.8 5.2e+03 0.0086 0.12 -3.6 - 7 -0.55 -0.83 -0.94 0.87 1.8 -0.24 1.8 5.2e+03 0.0086 0.062 -0.61 - 8 -0.55 -0.83 -0.94 0.87 1.8 -0.24 1.8 5.2e+03 0.0086 0.031 -0.16 - 9 -0.52 -0.86 -0.91 0.87 1.8 -0.27 1.8 5.2e+03 0.01 0.031 0.45 + 10 -0.52 -0.89 -0.94 0.84 1.8 -0.24 1.8 5.2e+03 0.014 0.031 0.3 + 11 -0.49 -0.87 -0.94 0.87 1.9 -0.25 1.8 5.2e+03 0.0073 0.031 0.71 + 12 -0.49 -0.89 -0.93 0.85 1.9 -0.22 1.8 5.2e+03 0.0079 0.031 0.84 + 13 -0.45 -0.89 -0.93 0.85 1.9 -0.22 1.8 5.2e+03 0.0049 0.031 0.81 + 14 -0.44 -0.91 -0.92 0.83 1.9 -0.2 1.8 5.2e+03 0.0066 0.031 0.86 + 15 -0.41 -0.9 -0.92 0.83 1.9 -0.21 1.8 5.2e+03 0.0036 0.031 0.78 + 16 -0.39 -0.91 -0.91 0.8 2 -0.19 1.8 5.2e+03 0.0048 0.031 0.89 + 17 -0.36 -0.91 -0.91 0.79 2 -0.2 1.8 5.2e+03 0.0032 0.031 0.8 + 18 -0.34 -0.91 -0.91 0.76 2 -0.19 1.8 5.2e+03 0.0038 0.031 0.86 + 19 -0.31 -0.91 -0.91 0.75 2 -0.2 1.8 5.2e+03 0.0022 0.031 0.76 + 20 -0.3 -0.91 -0.91 0.72 2.1 -0.2 1.8 5.2e+03 0.0028 0.031 0.82 + 21 -0.28 -0.9 -0.9 0.72 2.1 -0.2 1.8 5.2e+03 0.0017 0.031 0.88 + 22 -0.27 -0.89 -0.9 0.7 2.1 -0.2 1.8 5.2e+03 0.0024 0.031 0.83 + 23 -0.26 -0.89 -0.89 0.7 2.2 -0.21 1.9 5.2e+03 0.0019 0.031 0.82 + 24 -0.26 -0.89 -0.89 0.7 2.2 -0.21 1.9 5.2e+03 0.0019 0.016 -0.51 - 25 -0.25 -0.88 -0.9 0.7 2.2 -0.2 1.9 5.2e+03 0.0015 0.016 0.66 + 26 -0.26 -0.88 -0.89 0.69 2.2 -0.21 1.9 5.2e+03 0.0022 0.016 0.88 + 27 -0.26 -0.88 -0.89 0.69 2.2 -0.21 1.9 5.2e+03 0.0022 0.0078 0.09 - 28 -0.26 -0.88 -0.89 0.69 2.2 -0.21 1.9 5.2e+03 0.0022 0.0039 -0.14 - 29 -0.25 -0.88 -0.9 0.69 2.2 -0.21 1.9 5.2e+03 0.0012 0.0039 0.6 + 30 -0.25 -0.88 -0.9 0.69 2.2 -0.21 1.9 5.2e+03 0.0016 0.0039 0.89 + 31 -0.25 -0.88 -0.89 0.69 2.2 -0.21 1.9 5.2e+03 0.00097 0.039 0.97 ++ 32 -0.24 -0.87 -0.89 0.68 2.2 -0.21 1.9 5.2e+03 0.0013 0.39 0.95 ++ 33 -0.24 -0.87 -0.89 0.68 2.2 -0.21 1.9 5.2e+03 0.0013 0.2 0.029 - 34 -0.24 -0.87 -0.89 0.68 2.2 -0.21 1.9 5.2e+03 0.0013 0.098 -0.5 - 35 -0.24 -0.87 -0.89 0.68 2.2 -0.21 1.9 5.2e+03 0.0013 0.049 0.058 - 36 -0.23 -0.87 -0.88 0.67 2.3 -0.23 2 5.2e+03 0.0032 0.049 0.6 + 37 -0.21 -0.85 -0.89 0.66 2.3 -0.22 2 5.2e+03 0.0015 0.049 0.87 + 38 -0.19 -0.85 -0.87 0.64 2.3 -0.23 2.1 5.2e+03 0.0023 0.049 0.89 + 39 -0.18 -0.84 -0.88 0.64 2.3 -0.22 2.1 5.2e+03 0.0012 0.49 0.95 ++ 40 -0.18 -0.84 -0.88 0.64 2.3 -0.22 2.1 5.2e+03 0.0012 0.24 -0.34 - 41 -0.15 -0.87 -0.9 0.64 2.4 -0.24 2.4 5.2e+03 0.006 0.24 0.15 + 42 -0.15 -0.87 -0.9 0.64 2.4 -0.24 2.4 5.2e+03 0.006 0.12 -0.28 - 43 -0.15 -0.87 -0.9 0.64 2.4 -0.24 2.4 5.2e+03 0.006 0.061 -0.19 - 44 -0.097 -0.82 -0.85 0.59 2.4 -0.24 2.4 5.2e+03 0.0034 0.061 0.83 + 45 -0.097 -0.82 -0.85 0.59 2.4 -0.24 2.4 5.2e+03 0.0034 0.031 -2.7 - 46 -0.097 -0.82 -0.85 0.59 2.4 -0.24 2.4 5.2e+03 0.0034 0.015 -1.3 - 47 -0.097 -0.82 -0.85 0.59 2.4 -0.24 2.4 5.2e+03 0.0034 0.0076 -0.84 - 48 -0.097 -0.82 -0.85 0.59 2.4 -0.24 2.4 5.2e+03 0.0034 0.0038 0.074 - 49 -0.1 -0.82 -0.85 0.58 2.4 -0.24 2.4 5.2e+03 0.0014 0.0038 0.61 + 50 -0.099 -0.82 -0.86 0.59 2.4 -0.24 2.4 5.2e+03 0.0013 0.038 0.94 ++ 51 -0.11 -0.82 -0.88 0.59 2.4 -0.23 2.5 5.2e+03 0.002 0.038 0.49 + 52 -0.1 -0.82 -0.86 0.58 2.4 -0.24 2.5 5.2e+03 0.0013 0.038 0.81 + 53 -0.082 -0.82 -0.86 0.57 2.4 -0.23 2.6 5.2e+03 0.0032 0.038 0.66 + 54 -0.075 -0.83 -0.87 0.58 2.4 -0.23 2.6 5.2e+03 0.0011 0.038 0.89 + 55 -0.071 -0.81 -0.86 0.57 2.4 -0.24 2.6 5.2e+03 0.0018 0.038 0.8 + 56 -0.073 -0.82 -0.87 0.57 2.4 -0.24 2.7 5.2e+03 0.001 0.038 0.82 + 57 -0.052 -0.82 -0.85 0.56 2.4 -0.24 2.7 5.2e+03 0.0016 0.038 0.79 + 58 -0.056 -0.82 -0.87 0.56 2.4 -0.24 2.7 5.2e+03 0.00089 0.38 0.95 ++ 59 -0.0088 -0.78 -0.84 0.52 2.4 -0.27 3.1 5.2e+03 0.0026 0.38 0.65 + 60 -0.0088 -0.78 -0.84 0.52 2.4 -0.27 3.1 5.2e+03 0.0026 0.19 -4.8 - 61 -0.0088 -0.78 -0.84 0.52 2.4 -0.27 3.1 5.2e+03 0.0026 0.095 -4.7 - 62 -0.0088 -0.78 -0.84 0.52 2.4 -0.27 3.1 5.2e+03 0.0026 0.048 -1.3 - 63 0.0086 -0.81 -0.85 0.55 2.4 -0.22 3.1 5.2e+03 0.0024 0.048 0.39 + 64 0.0086 -0.81 -0.85 0.55 2.4 -0.22 3.1 5.2e+03 0.0024 0.024 -0.68 - 65 0.017 -0.81 -0.85 0.52 2.4 -0.24 3.2 5.2e+03 0.00084 0.024 0.65 + 66 0.017 -0.81 -0.85 0.52 2.4 -0.24 3.2 5.2e+03 0.00084 0.012 0.04 - 67 0.0082 -0.81 -0.85 0.53 2.4 -0.24 3.2 5.2e+03 0.00083 0.012 0.84 + 68 0.01 -0.81 -0.85 0.53 2.4 -0.24 3.2 5.2e+03 0.0013 0.012 0.61 + 69 0.011 -0.8 -0.85 0.53 2.4 -0.24 3.2 5.2e+03 0.00083 0.12 0.94 ++ 70 0.011 -0.8 -0.85 0.53 2.4 -0.24 3.2 5.2e+03 0.00083 0.06 -0.59 - 71 0.022 -0.8 -0.83 0.53 2.5 -0.24 3.3 5.2e+03 0.002 0.06 0.27 + 72 0.036 -0.81 -0.85 0.52 2.5 -0.25 3.3 5.2e+03 0.0016 0.06 0.71 + 73 0.036 -0.81 -0.85 0.52 2.5 -0.25 3.3 5.2e+03 0.0016 0.03 -4.2 - 74 0.036 -0.81 -0.85 0.52 2.5 -0.25 3.3 5.2e+03 0.0016 0.015 -1.5 - 75 0.036 -0.81 -0.85 0.52 2.5 -0.25 3.3 5.2e+03 0.0016 0.0075 0.017 - 76 0.03 -0.8 -0.85 0.52 2.5 -0.24 3.3 5.2e+03 0.0012 0.0075 0.56 + 77 0.025 -0.8 -0.84 0.53 2.5 -0.24 3.3 5.2e+03 0.0008 0.0075 0.72 + 78 0.028 -0.8 -0.84 0.52 2.5 -0.24 3.3 5.2e+03 0.0008 0.075 0.94 ++ 79 0.033 -0.79 -0.83 0.52 2.5 -0.24 3.4 5.2e+03 0.0018 0.075 0.59 + 80 0.033 -0.79 -0.83 0.52 2.5 -0.24 3.4 5.2e+03 0.0018 0.037 0.067 - 81 0.033 -0.79 -0.84 0.53 2.5 -0.23 3.4 5.2e+03 0.0009 0.037 0.36 + 82 0.059 -0.8 -0.84 0.51 2.5 -0.23 3.5 5.2e+03 0.0013 0.037 0.59 + 83 0.059 -0.8 -0.84 0.51 2.5 -0.23 3.5 5.2e+03 0.0013 0.019 -0.56 - 84 0.04 -0.79 -0.84 0.52 2.5 -0.24 3.5 5.2e+03 0.00077 0.019 0.61 + 85 0.052 -0.79 -0.84 0.51 2.5 -0.24 3.5 5.2e+03 0.00075 0.019 0.66 + 86 0.051 -0.8 -0.83 0.52 2.5 -0.23 3.5 5.2e+03 0.00075 0.019 0.5 + 87 0.044 -0.79 -0.84 0.51 2.5 -0.24 3.5 5.2e+03 0.00074 0.019 0.67 + 88 0.058 -0.79 -0.83 0.51 2.5 -0.24 3.6 5.2e+03 0.00074 0.019 0.82 + 89 0.049 -0.79 -0.83 0.51 2.5 -0.24 3.6 5.2e+03 0.00073 0.019 0.68 + 90 0.055 -0.79 -0.84 0.51 2.5 -0.24 3.6 5.2e+03 0.00072 0.19 0.93 ++ 91 0.089 -0.78 -0.82 0.49 2.5 -0.24 3.8 5.2e+03 0.0025 0.19 0.42 + 92 0.08 -0.77 -0.83 0.5 2.5 -0.25 4 5.2e+03 0.0019 0.19 0.54 + 93 0.08 -0.77 -0.83 0.5 2.5 -0.25 4 5.2e+03 0.0019 0.093 -0.87 - 94 0.089 -0.77 -0.82 0.5 2.6 -0.24 4 5.2e+03 0.00057 0.093 0.5 + 95 0.089 -0.77 -0.82 0.5 2.6 -0.24 4 5.2e+03 0.00057 0.041 0.022 - 96 0.085 -0.77 -0.82 0.5 2.5 -0.24 4 5.2e+03 0.00036 0.41 1 ++ 97 0.085 -0.77 -0.82 0.5 2.5 -0.24 4 5.2e+03 0.00036 0.04 -0.24 - 98 0.085 -0.77 -0.82 0.5 2.5 -0.24 4 5.2e+03 0.00036 0.02 -1.1 - 99 0.085 -0.77 -0.82 0.5 2.5 -0.24 4 5.2e+03 0.00036 0.01 -0.47 - 100 0.085 -0.77 -0.82 0.5 2.5 -0.24 4 5.2e+03 0.00036 0.005 -0.22 - 101 0.09 -0.78 -0.82 0.5 2.5 -0.24 4 5.2e+03 0.00027 0.005 0.65 + 102 0.091 -0.78 -0.82 0.5 2.5 -0.24 4 5.2e+03 0.00028 0.005 0.4 + 103 0.089 -0.78 -0.82 0.5 2.5 -0.24 4 5.2e+03 0.00017 0.05 0.99 ++ 104 0.094 -0.78 -0.82 0.5 2.5 -0.24 4.1 5.2e+03 0.00014 0.5 1.1 ++ 105 0.098 -0.78 -0.82 0.5 2.5 -0.24 4.1 5.2e+03 0.0002 5 0.92 ++ 106 0.098 -0.78 -0.82 0.49 2.5 -0.24 4.1 5.2e+03 7.3e-05 5 0.58 + 107 0.098 -0.78 -0.82 0.49 2.5 -0.24 4.1 5.2e+03 2.8e-06 5 0.99 + Optimization algorithm has converged. Relative gradient: 2.769519681924656e-06 Cause of termination: Relative gradient = 2.8e-06 <= 6.1e-06 Number of function evaluations: 255 Number of gradient evaluations: 147 Number of hessian evaluations: 0 Algorithm: BFGS with trust region for simple bound constraints Number of iterations: 108 Proportion of Hessian calculation: 0/73 = 0.0% Optimization time: 0:00:01.589921 Calculate second derivatives and BHHH File b11c_cnl_sparse.html has been generated. File b11c_cnl_sparse.yaml has been generated. .. GENERATED FROM PYTHON SOURCE LINES 122-124 .. code-block:: Python print(results.short_summary()) .. rst-class:: sphx-glr-script-out .. code-block:: none Results for model b11c_cnl_sparse 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 125-127 .. 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 t-stat. Robust p-value 0 asc_train 0.098268 ... 1.404207 1.602572e-01 1 b_time -0.776853 ... -7.587858 3.241851e-14 2 b_cost -0.818892 ... -13.886190 0.000000e+00 3 alpha_existing 0.495084 ... 14.245344 0.000000e+00 4 existing_nest_parameter 2.514860 ... 10.127306 0.000000e+00 5 asc_car -0.240441 ... -4.498402 6.846623e-06 6 public_nest_parameter 4.113503 ... 8.281134 2.220446e-16 [7 rows x 5 columns] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 5.545 seconds) .. _sphx_glr_download_auto_examples_swissmetro_plot_b11c_cnl_sparse.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_b11c_cnl_sparse.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_b11c_cnl_sparse.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_b11c_cnl_sparse.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_