.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/swissmetro/plot_b23a_binary_logit.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_b23a_binary_logit.py: 23a. Binary logit model ======================= Example of a binary logit model. Two alternatives: Train and Car. All observations such that the Swissmetro was chosen haven been removed from the sample. Michel Bierlaire, EPFL Sat Jun 28 2025, 12:42:27 .. GENERATED FROM PYTHON SOURCE LINES 14-25 .. code-block:: Python from IPython.core.display_functions import display from biogeme.biogeme import BIOGEME from biogeme.expressions import Beta from biogeme.models import loglogit from biogeme.results_processing import ( EstimationResults, get_pandas_estimated_parameters, ) .. GENERATED FROM PYTHON SOURCE LINES 26-27 See the data processing script: :ref:`swissmetro_binary`. .. GENERATED FROM PYTHON SOURCE LINES 27-38 .. code-block:: Python from swissmetro_binary import ( CAR_AV_SP, CAR_CO_SCALED, CAR_TT_SCALED, CHOICE, TRAIN_AV_SP, TRAIN_COST_SCALED, TRAIN_TT_SCALED, database, ) .. GENERATED FROM PYTHON SOURCE LINES 39-40 Parameters to be estimated. .. GENERATED FROM PYTHON SOURCE LINES 40-46 .. code-block:: Python asc_car = Beta('asc_car', 0, None, None, 0) b_time_car = Beta('b_time_car', 0, None, None, 0) b_time_train = Beta('b_time_train', 0, None, None, 0) b_cost_car = Beta('b_cost_car', 0, None, None, 0) b_cost_train = Beta('b_cost_train', 0, None, None, 0) .. GENERATED FROM PYTHON SOURCE LINES 47-49 Definition of the utility functions. We estimate a binary logit model. There are only two alternatives. .. GENERATED FROM PYTHON SOURCE LINES 49-52 .. code-block:: Python v_train = b_time_train * TRAIN_TT_SCALED + b_cost_train * TRAIN_COST_SCALED v_car = asc_car + b_time_car * CAR_TT_SCALED + b_cost_car * CAR_CO_SCALED .. GENERATED FROM PYTHON SOURCE LINES 53-54 Associate utility functions with the numbering of alternatives. .. GENERATED FROM PYTHON SOURCE LINES 54-56 .. code-block:: Python v = {1: v_train, 3: v_car} .. GENERATED FROM PYTHON SOURCE LINES 57-58 Associate the availability conditions with the alternatives. .. GENERATED FROM PYTHON SOURCE LINES 58-60 .. code-block:: Python av = {1: TRAIN_AV_SP, 3: CAR_AV_SP} .. GENERATED FROM PYTHON SOURCE LINES 61-63 Definition of the model. This is the contribution of each observation to the log likelihood function. .. GENERATED FROM PYTHON SOURCE LINES 63-65 .. code-block:: Python log_probability = loglogit(v, av, CHOICE) .. GENERATED FROM PYTHON SOURCE LINES 66-67 Create the Biogeme object .. GENERATED FROM PYTHON SOURCE LINES 67-70 .. code-block:: Python the_biogeme = BIOGEME(database, log_probability) the_biogeme.model_name = 'b23a_logit' .. GENERATED FROM PYTHON SOURCE LINES 71-72 Estimate the parameters. .. GENERATED FROM PYTHON SOURCE LINES 72-79 .. code-block:: Python try: results = EstimationResults.from_yaml_file( filename=f'saved_results/{the_biogeme.model_name}.yaml' ) except FileNotFoundError: results = the_biogeme.estimate() .. GENERATED FROM PYTHON SOURCE LINES 80-82 .. code-block:: Python print(results.short_summary()) .. rst-class:: sphx-glr-script-out .. code-block:: none Results for model b23a_logit Nbr of parameters: 5 Sample size: 2232 Excluded data: 8496 Final log likelihood: -872.9052 Akaike Information Criterion: 1755.81 Bayesian Information Criterion: 1784.364 .. GENERATED FROM PYTHON SOURCE LINES 83-85 .. 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 std err. Robust t-stat. Robust p-value 0 b_time_train -1.134867 0.210637 -5.387777 7.133434e-08 1 b_cost_train -2.393364 0.272021 -8.798468 0.000000e+00 2 asc_car -0.896101 0.178268 -5.026696 4.990013e-07 3 b_time_car -0.383847 0.310672 -1.235537 2.166306e-01 4 b_cost_car -1.088054 0.295942 -3.676576 2.363851e-04 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.851 seconds) .. _sphx_glr_download_auto_examples_swissmetro_plot_b23a_binary_logit.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_b23a_binary_logit.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_b23a_binary_logit.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_b23a_binary_logit.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_