.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/swissmetro/plot_b01logit_bis.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_b01logit_bis.py: Illustration of additional features of Biogeme ============================================== Same model as b01logit, using bioLinearUtility, segmentations and features. :author: Michel Bierlaire, EPFL :date: Sun Apr 9 17:03:31 2023 .. GENERATED FROM PYTHON SOURCE LINES 13-24 .. code-block:: Python import biogeme.biogeme as bio import biogeme.biogeme_logging as blog import biogeme.segmentation as seg from biogeme import models from biogeme.expressions import Beta, bioLinearUtility, LinearTermTuple logger = blog.get_screen_logger(level=blog.INFO) logger.info('Example b01logit.py') .. rst-class:: sphx-glr-script-out .. code-block:: none Example b01logit.py .. GENERATED FROM PYTHON SOURCE LINES 25-26 See the data processing script: :ref:`swissmetro_data`. .. GENERATED FROM PYTHON SOURCE LINES 26-45 .. code-block:: Python from swissmetro_data import ( database, CHOICE, GA, CAR_AV_SP, TRAIN_AV_SP, TRAIN_TT_SCALED, TRAIN_COST_SCALED, SM_TT_SCALED, SM_COST_SCALED, CAR_TT_SCALED, CAR_CO_SCALED, MALE, SM_AV, ) logger = blog.get_screen_logger(level=blog.INFO) logger.info('Example b01logit_bis.py') .. rst-class:: sphx-glr-script-out .. code-block:: none Example b01logit_bis.py Example b01logit_bis.py .. GENERATED FROM PYTHON SOURCE LINES 46-47 Parameters to be estimated. .. GENERATED FROM PYTHON SOURCE LINES 47-50 .. code-block:: Python ASC_CAR = Beta('ASC_CAR', 0, None, None, 0) ASC_TRAIN = Beta('ASC_TRAIN', 0, None, None, 0) .. GENERATED FROM PYTHON SOURCE LINES 51-53 Starting value. We use starting values estimated from a previous run .. GENERATED FROM PYTHON SOURCE LINES 53-56 .. code-block:: Python B_TIME = Beta('B_TIME', -1.28, None, None, 0) B_COST = Beta('B_COST', -1.08, None, None, 0) .. GENERATED FROM PYTHON SOURCE LINES 57-58 Define segmentations. .. GENERATED FROM PYTHON SOURCE LINES 58-71 .. code-block:: Python gender_segmentation = database.generate_segmentation( variable=MALE, mapping={0: 'female', 1: 'male'} ) GA_segmentation = database.generate_segmentation( variable=GA, mapping={0: 'without_ga', 1: 'with_ga'} ) segmentations_for_asc = [ gender_segmentation, GA_segmentation, ] .. GENERATED FROM PYTHON SOURCE LINES 72-73 Segmentation of the constants. .. GENERATED FROM PYTHON SOURCE LINES 73-78 .. code-block:: Python ASC_TRAIN_segmentation = seg.Segmentation(ASC_TRAIN, segmentations_for_asc) segmented_ASC_TRAIN = ASC_TRAIN_segmentation.segmented_beta() ASC_CAR_segmentation = seg.Segmentation(ASC_CAR, segmentations_for_asc) segmented_ASC_CAR = ASC_CAR_segmentation.segmented_beta() .. GENERATED FROM PYTHON SOURCE LINES 79-80 Definition of the utility functions. .. GENERATED FROM PYTHON SOURCE LINES 80-98 .. code-block:: Python terms1 = [ LinearTermTuple(beta=B_TIME, x=TRAIN_TT_SCALED), LinearTermTuple(beta=B_COST, x=TRAIN_COST_SCALED), ] V1 = segmented_ASC_TRAIN + bioLinearUtility(terms1) terms2 = [ LinearTermTuple(beta=B_TIME, x=SM_TT_SCALED), LinearTermTuple(beta=B_COST, x=SM_COST_SCALED), ] V2 = bioLinearUtility(terms2) terms3 = [ LinearTermTuple(beta=B_TIME, x=CAR_TT_SCALED), LinearTermTuple(beta=B_COST, x=CAR_CO_SCALED), ] V3 = segmented_ASC_CAR + bioLinearUtility(terms3) .. GENERATED FROM PYTHON SOURCE LINES 99-100 Associate utility functions with the numbering of alternatives. .. GENERATED FROM PYTHON SOURCE LINES 100-102 .. code-block:: Python V = {1: V1, 2: V2, 3: V3} .. GENERATED FROM PYTHON SOURCE LINES 103-104 Associate the availability conditions with the alternatives. .. GENERATED FROM PYTHON SOURCE LINES 104-106 .. code-block:: Python av = {1: TRAIN_AV_SP, 2: SM_AV, 3: CAR_AV_SP} .. GENERATED FROM PYTHON SOURCE LINES 107-111 Definition of the model. This is the contribution of each observation to the log likelihood function. .. GENERATED FROM PYTHON SOURCE LINES 111-113 .. code-block:: Python logprob = models.loglogit(V, av, CHOICE) .. GENERATED FROM PYTHON SOURCE LINES 114-117 User notes. These notes will be included as such in the report file. .. GENERATED FROM PYTHON SOURCE LINES 117-125 .. code-block:: Python USER_NOTES = ( 'Example of a logit model with three alternatives: Train, Car and' ' Swissmetro. Same as 01logit and ' 'introducing some options and features. In particular, bioLinearUtility,' ' and automatic segmentation of parameters.' ) .. GENERATED FROM PYTHON SOURCE LINES 126-127 Create the Biogeme object. We include users notes, and we ask not to calculate the second derivatives. .. GENERATED FROM PYTHON SOURCE LINES 127-131 .. code-block:: Python the_biogeme = bio.BIOGEME( database, logprob, user_notes=USER_NOTES, second_derivatives=0 ) .. rst-class:: sphx-glr-script-out .. code-block:: none Biogeme parameters read from biogeme.toml. Biogeme parameters read from biogeme.toml. .. GENERATED FROM PYTHON SOURCE LINES 132-136 Calculate the null log likelihood for reporting. As we have used starting values different from 0, the initial model is not the equal probability model. .. GENERATED FROM PYTHON SOURCE LINES 136-139 .. code-block:: Python the_biogeme.calculate_null_loglikelihood(av) the_biogeme.modelName = 'b01logit_bis' .. GENERATED FROM PYTHON SOURCE LINES 140-142 Turn off saving iterations. .. GENERATED FROM PYTHON SOURCE LINES 142-144 .. code-block:: Python the_biogeme.save_iterations = False .. GENERATED FROM PYTHON SOURCE LINES 145-147 Estimate the parameters. .. GENERATED FROM PYTHON SOURCE LINES 147-150 .. code-block:: Python the_biogeme.bootstrap_samples = 100 results = the_biogeme.estimate(run_bootstrap=True) .. rst-class:: sphx-glr-script-out .. code-block:: none As the model is not too complex, we activate the calculation of second derivatives. If you want to change it, change the name of the algorithm in the TOML file from "automatic" to "simple_bounds" As the model is not too complex, we activate the calculation of second derivatives. If you want to change it, change the name of the algorithm in the TOML file from "automatic" to "simple_bounds" As the model is not too complex, we activate the calculation of second derivatives. If you want to change it, change the name of the algorithm in the TOML file from "automatic" to "simple_bounds" As the model is not too complex, we activate the calculation of second derivatives. If you want to change it, change the name of the algorithm in the TOML file from "automatic" to "simple_bounds" Optimization algorithm: hybrid Newton/BFGS with simple bounds [simple_bounds] Optimization algorithm: hybrid Newton/BFGS with simple bounds [simple_bounds] ** Optimization: Newton with trust region for simple bounds ** Optimization: Newton with trust region for simple bounds Iter. ASC_CAR ASC_CAR_male ASC_CAR_with_ga ASC_TRAIN ASC_TRAIN_male ASC_TRAIN_with_ B_COST B_TIME Function Relgrad Radius Rho Iter. ASC_CAR ASC_CAR_male ASC_CAR_with_ga ASC_TRAIN ASC_TRAIN_male ASC_TRAIN_with_ B_COST B_TIME Function Relgrad Radius Rho 0 -0.38 -0.046 -0.2 -0.21 -0.78 1 -1 -1.3 5.1e+03 0.053 10 1.1 ++ 0 -0.38 -0.046 -0.2 -0.21 -0.78 1 -1 -1.3 5.1e+03 0.053 10 1.1 ++ 1 -0.61 0.42 -0.42 -0.53 -1 1.8 -1.1 -1.1 4.9e+03 0.0091 1e+02 1 ++ 1 -0.61 0.42 -0.42 -0.53 -1 1.8 -1.1 -1.1 4.9e+03 0.0091 1e+02 1 ++ 2 -0.61 0.41 -0.41 -0.53 -1.1 1.9 -1.1 -1.2 4.9e+03 0.00031 1e+03 1 ++ 2 -0.61 0.41 -0.41 -0.53 -1.1 1.9 -1.1 -1.2 4.9e+03 0.00031 1e+03 1 ++ 3 -0.61 0.41 -0.41 -0.53 -1.1 1.9 -1.1 -1.2 4.9e+03 6.6e-07 1e+03 1 ++ 3 -0.61 0.41 -0.41 -0.53 -1.1 1.9 -1.1 -1.2 4.9e+03 6.6e-07 1e+03 1 ++ Re-estimate the model 100 times for bootstrapping Re-estimate the model 100 times for bootstrapping 0%| | 0/100 [00:00
Value Rob. Std err Rob. t-test Rob. p-value
ASC_CAR -0.612868 0.097955 -6.256612 3.934308e-10
ASC_CAR_male 0.408127 0.102408 3.985298 6.739540e-05
ASC_CAR_with_ga -0.414517 0.198807 -2.085019 3.706757e-02
ASC_TRAIN -0.534244 0.102923 -5.190713 2.094900e-07
ASC_TRAIN_male -1.103399 0.085401 -12.920189 0.000000e+00
ASC_TRAIN_with_ga 1.889379 0.091601 20.626287 0.000000e+00
B_COST -1.089785 0.072442 -15.043632 0.000000e+00
B_TIME -1.173068 0.106327 -11.032621 0.000000e+00


.. GENERATED FROM PYTHON SOURCE LINES 159-161 Get general statistics. .. GENERATED FROM PYTHON SOURCE LINES 161-167 .. code-block:: Python print('General statistics') print('------------------') stats = results.get_general_statistics() for description, (value, formatting) in stats.items(): print(f'{description}: {value:{formatting}}') .. rst-class:: sphx-glr-script-out .. code-block:: none General statistics ------------------ Number of estimated parameters: 8 Sample size: 6768 Excluded observations: 3960 Null log likelihood: -6964.663 Init log likelihood: -5533.155 Final log likelihood: -4943.895 Likelihood ratio test for the null model: 4041.535 Rho-square for the null model: 0.29 Rho-square-bar for the null model: 0.289 Likelihood ratio test for the init. model: 1178.519 Rho-square for the init. model: 0.106 Rho-square-bar for the init. model: 0.105 Akaike Information Criterion: 9903.791 Bayesian Information Criterion: 9958.351 Final gradient norm: 5.3376E-03 Bootstrapping time: 0:00:04.642499 Nbr of threads: 12 .. GENERATED FROM PYTHON SOURCE LINES 168-170 Messages from the optimization algorithm. .. GENERATED FROM PYTHON SOURCE LINES 170-175 .. code-block:: Python print('Optimization algorithm') print('----------------------') for description, message in results.data.optimizationMessages.items(): print(f'{description}:\t{message}') .. rst-class:: sphx-glr-script-out .. code-block:: none Optimization algorithm ---------------------- Relative gradient: 6.556090431875208e-07 Cause of termination: Relative gradient = 6.6e-07 <= 0.00012 Number of function evaluations: 5 Number of gradient evaluations: 5 Number of hessian evaluations: 4 Algorithm: Newton with trust region for simple bound constraints Number of iterations: 4 Proportion of Hessian calculation: 4/4 = 100.0% Optimization time: 0:00:00.066021 .. GENERATED FROM PYTHON SOURCE LINES 176-178 Generate the file in Alogit format. .. GENERATED FROM PYTHON SOURCE LINES 178-181 .. code-block:: Python results.write_f12(robust_std_err=True) print(f'Estimation results in ALogit format generated: {results.data.F12FileName}') .. rst-class:: sphx-glr-script-out .. code-block:: none Results saved in file b01logit_bis~19.F12 Results saved in file b01logit_bis~19.F12 Estimation results in ALogit format generated: b01logit_bis~19.F12 .. GENERATED FROM PYTHON SOURCE LINES 182-184 Generate LaTeX code with the results. .. GENERATED FROM PYTHON SOURCE LINES 184-186 .. code-block:: Python results.write_latex() print(f'Estimation results in LaTeX format generated: {results.data.latexFileName}') .. rst-class:: sphx-glr-script-out .. code-block:: none Results saved in file b01logit_bis.tex Results saved in file b01logit_bis.tex Estimation results in LaTeX format generated: b01logit_bis.tex .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 4.859 seconds) .. _sphx_glr_download_auto_examples_swissmetro_plot_b01logit_bis.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_b01logit_bis.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_b01logit_bis.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_b01logit_bis.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_