.. 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-19 .. code-block:: default import biogeme.biogeme_logging as blog import biogeme.biogeme as bio from biogeme import models import biogeme.segmentation as seg from biogeme.expressions import Beta, bioLinearUtility .. GENERATED FROM PYTHON SOURCE LINES 20-21 See the data processing script: :ref:`swissmetro_data`. .. GENERATED FROM PYTHON SOURCE LINES 21-40 .. code-block:: default 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 .. GENERATED FROM PYTHON SOURCE LINES 41-42 Parameters to be estimated. .. GENERATED FROM PYTHON SOURCE LINES 42-45 .. code-block:: default ASC_CAR = Beta('ASC_CAR', 0, None, None, 0) ASC_TRAIN = Beta('ASC_TRAIN', 0, None, None, 0) .. GENERATED FROM PYTHON SOURCE LINES 46-48 Starting value. We use starting values estimated from a previous run .. GENERATED FROM PYTHON SOURCE LINES 48-51 .. code-block:: default 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 52-53 Define segmentations. .. GENERATED FROM PYTHON SOURCE LINES 53-66 .. code-block:: default 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 67-68 Segmentation of the constants. .. GENERATED FROM PYTHON SOURCE LINES 68-73 .. code-block:: default 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 74-75 Definition of the utility functions. .. GENERATED FROM PYTHON SOURCE LINES 75-84 .. code-block:: default terms1 = [(B_TIME, TRAIN_TT_SCALED), (B_COST, TRAIN_COST_SCALED)] V1 = segmented_ASC_TRAIN + bioLinearUtility(terms1) terms2 = [(B_TIME, SM_TT_SCALED), (B_COST, SM_COST_SCALED)] V2 = bioLinearUtility(terms2) terms3 = [(B_TIME, CAR_TT_SCALED), (B_COST, CAR_CO_SCALED)] V3 = segmented_ASC_CAR + bioLinearUtility(terms3) .. GENERATED FROM PYTHON SOURCE LINES 85-86 Associate utility functions with the numbering of alternatives. .. GENERATED FROM PYTHON SOURCE LINES 86-88 .. code-block:: default V = {1: V1, 2: V2, 3: V3} .. GENERATED FROM PYTHON SOURCE LINES 89-90 Associate the availability conditions with the alternatives. .. GENERATED FROM PYTHON SOURCE LINES 90-92 .. code-block:: default av = {1: TRAIN_AV_SP, 2: SM_AV, 3: CAR_AV_SP} .. GENERATED FROM PYTHON SOURCE LINES 93-97 Definition of the model. This is the contribution of each observation to the log likelihood function. .. GENERATED FROM PYTHON SOURCE LINES 97-99 .. code-block:: default logprob = models.loglogit(V, av, CHOICE) .. GENERATED FROM PYTHON SOURCE LINES 100-103 User notes. These notes will be included as such in the report file. .. GENERATED FROM PYTHON SOURCE LINES 103-110 .. code-block:: default 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 111-112 Create the Biogeme object. .. GENERATED FROM PYTHON SOURCE LINES 112-116 .. code-block:: default the_biogeme = bio.BIOGEME( database, logprob, userNotes=USER_NOTES, parameter_file='b01logit_bis.toml' ) .. rst-class:: sphx-glr-script-out .. code-block:: none File b01logit_bis.toml has been parsed. .. GENERATED FROM PYTHON SOURCE LINES 117-121 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 121-124 .. code-block:: default the_biogeme.calculateNullLoglikelihood(av) the_biogeme.modelName = 'b01logit_bis' .. GENERATED FROM PYTHON SOURCE LINES 125-127 Turn off saving iterations. .. GENERATED FROM PYTHON SOURCE LINES 127-129 .. code-block:: default the_biogeme.saveIterations = False .. GENERATED FROM PYTHON SOURCE LINES 130-132 Estimate the parameters. .. GENERATED FROM PYTHON SOURCE LINES 132-135 .. code-block:: default the_biogeme.bootstrap_samples = 100 results = the_biogeme.estimate(run_bootstrap=True) .. rst-class:: sphx-glr-script-out .. code-block:: none Optimization algorithm: hybrid Newton/BFGS with simple bounds [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 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 ++ 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 ++ 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 144-146 Get general statistics. .. GENERATED FROM PYTHON SOURCE LINES 146-152 .. code-block:: default print('General statistics') print('------------------') stats = results.getGeneralStatistics() 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:06.475044 Nbr of threads: 12 .. GENERATED FROM PYTHON SOURCE LINES 153-155 Messages from the optimization algorithm. .. GENERATED FROM PYTHON SOURCE LINES 155-160 .. code-block:: default 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.556090729381411e-07 Cause of termination: Relative gradient = 6.6e-07 <= 6.1e-06 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.080396 .. GENERATED FROM PYTHON SOURCE LINES 161-163 Generate the file in Alogit format. .. GENERATED FROM PYTHON SOURCE LINES 163-166 .. code-block:: default results.writeF12(robustStdErr=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.F12 Estimation results in ALogit format generated: b01logit_bis.F12 .. GENERATED FROM PYTHON SOURCE LINES 167-169 Generate LaTeX code with the results. .. GENERATED FROM PYTHON SOURCE LINES 169-171 .. code-block:: default results.writeLaTeX() 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 Estimation results in LaTeX format generated: b01logit_bis.tex .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 6.800 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-python :download:`Download Python source code: plot_b01logit_bis.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_b01logit_bis.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_