.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/programmers/plot_biogeme.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_programmers_plot_biogeme.py: biogeme.biogeme =============== Examples of use of several functions. This is designed for programmers who need examples of use of the functions of the module. The examples are designed to illustrate the syntax. They do not correspond to any meaningful model. Michel Bierlaire Sun Jun 29 2025, 01:14:48 .. GENERATED FROM PYTHON SOURCE LINES 15-32 .. code-block:: Python import pandas as pd from IPython.core.display_functions import display import biogeme.biogeme_logging as blog from biogeme.biogeme import BIOGEME from biogeme.calculator import evaluate_formula from biogeme.database import Database from biogeme.expressions import Beta, Variable, exp from biogeme.function_output import FunctionOutput from biogeme.results_processing import get_pandas_estimated_parameters from biogeme.second_derivatives import SecondDerivativesMode from biogeme.tools import CheckDerivativesResults from biogeme.tools.files import files_of_type from biogeme.validation import ValidationResult from biogeme.version import get_text .. GENERATED FROM PYTHON SOURCE LINES 33-34 Version of Biogeme. .. GENERATED FROM PYTHON SOURCE LINES 34-37 .. code-block:: Python print(get_text()) .. rst-class:: sphx-glr-script-out .. code-block:: none biogeme 3.3.1 [2025-09-03] Home page: http://biogeme.epfl.ch Submit questions to https://groups.google.com/d/forum/biogeme Michel Bierlaire, Transport and Mobility Laboratory, Ecole Polytechnique Fédérale de Lausanne (EPFL) .. GENERATED FROM PYTHON SOURCE LINES 38-39 Logger. .. GENERATED FROM PYTHON SOURCE LINES 39-43 .. code-block:: Python logger = blog.get_screen_logger(level=blog.INFO) logger.info('Logger initialized') .. rst-class:: sphx-glr-script-out .. code-block:: none Logger initialized .. GENERATED FROM PYTHON SOURCE LINES 44-45 Definition of a database .. GENERATED FROM PYTHON SOURCE LINES 45-59 .. code-block:: Python df = pd.DataFrame( { 'Person': [1, 1, 1, 2, 2], 'Exclude': [0, 0, 1, 0, 1], 'Variable1': [1, 2, 3, 4, 5], 'Variable2': [10, 20, 30, 40, 50], 'Choice': [1, 2, 3, 1, 2], 'Av1': [0, 1, 1, 1, 1], 'Av2': [1, 1, 1, 1, 1], 'Av3': [0, 1, 1, 1, 1], } ) my_data = Database('test', df) .. GENERATED FROM PYTHON SOURCE LINES 60-61 Data .. GENERATED FROM PYTHON SOURCE LINES 61-64 .. code-block:: Python display(my_data.dataframe) .. rst-class:: sphx-glr-script-out .. code-block:: none Person Exclude Variable1 Variable2 Choice Av1 Av2 Av3 0 1.0 0.0 1.0 10.0 1.0 0.0 1.0 0.0 1 1.0 0.0 2.0 20.0 2.0 1.0 1.0 1.0 2 1.0 1.0 3.0 30.0 3.0 1.0 1.0 1.0 3 2.0 0.0 4.0 40.0 1.0 1.0 1.0 1.0 4 2.0 1.0 5.0 50.0 2.0 1.0 1.0 1.0 .. GENERATED FROM PYTHON SOURCE LINES 65-66 Definition of various expressions. .. GENERATED FROM PYTHON SOURCE LINES 66-74 .. code-block:: Python Variable1 = Variable('Variable1') Variable2 = Variable('Variable2') beta1 = Beta('beta1', -1.0, -3, 3, 0) beta2 = Beta('beta2', 2.0, -3, 10, 0) likelihood = -(beta1**2) * Variable1 - exp(beta2 * beta1) * Variable2 - beta2**4 simul = beta1 / Variable1 + beta2 / Variable2 dict_of_expressions = {'log_like': likelihood, 'beta1': beta1, 'simul': simul} .. GENERATED FROM PYTHON SOURCE LINES 75-76 Creation of the BIOGEME object. .. GENERATED FROM PYTHON SOURCE LINES 76-80 .. code-block:: Python my_biogeme = BIOGEME(my_data, dict_of_expressions) my_biogeme.model_name = 'simple_example' print(my_biogeme) .. rst-class:: sphx-glr-script-out .. code-block:: none Biogeme parameters read from biogeme.toml. simple_example: database [test]{'log_like': (((UnaryMinus(PowerConstant(, 2.0)) * ) - (exp(( * )) * )) - PowerConstant(, 4.0)), 'beta1': , 'simul': (( / ) + ( / ))} .. GENERATED FROM PYTHON SOURCE LINES 81-82 The data is stored in the Biogeme object. .. GENERATED FROM PYTHON SOURCE LINES 82-84 .. code-block:: Python display(my_biogeme.database.dataframe) .. rst-class:: sphx-glr-script-out .. code-block:: none Person Exclude Variable1 Variable2 Choice Av1 Av2 Av3 0 1.0 0.0 1.0 10.0 1.0 0.0 1.0 0.0 1 1.0 0.0 2.0 20.0 2.0 1.0 1.0 1.0 2 1.0 1.0 3.0 30.0 3.0 1.0 1.0 1.0 3 2.0 0.0 4.0 40.0 1.0 1.0 1.0 1.0 4 2.0 1.0 5.0 50.0 2.0 1.0 1.0 1.0 .. GENERATED FROM PYTHON SOURCE LINES 85-86 Log likelihood with the initial values of the parameters. .. GENERATED FROM PYTHON SOURCE LINES 86-88 .. code-block:: Python my_biogeme.calculate_init_likelihood() .. rst-class:: sphx-glr-script-out .. code-block:: none -115.30029248549191 .. GENERATED FROM PYTHON SOURCE LINES 89-91 Calculate the log-likelihood with a different value of the parameters. We retrieve the current value and add 1 to each of them. .. GENERATED FROM PYTHON SOURCE LINES 91-95 .. code-block:: Python x = my_biogeme.expressions_registry.free_betas_init_values x_plus = {key: value + 1.0 for key, value in x.items()} print(x_plus) .. rst-class:: sphx-glr-script-out .. code-block:: none {'beta1': 0.0, 'beta2': 3.0} .. GENERATED FROM PYTHON SOURCE LINES 96-104 .. code-block:: Python log_likelihood_x_plus = evaluate_formula( model_elements=my_biogeme.model_elements, the_betas=x_plus, second_derivatives_mode=SecondDerivativesMode.NEVER, numerically_safe=False, ) print(log_likelihood_x_plus) .. rst-class:: sphx-glr-script-out .. code-block:: none -555.0 .. GENERATED FROM PYTHON SOURCE LINES 105-106 Calculate the log-likelihood function and its derivatives. .. GENERATED FROM PYTHON SOURCE LINES 106-113 .. code-block:: Python the_function_output: FunctionOutput = my_biogeme.function_evaluator.evaluate( the_betas=x_plus, gradient=True, hessian=True, bhhh=True, ) .. GENERATED FROM PYTHON SOURCE LINES 114-116 .. code-block:: Python print(f'f = {the_function_output.function}') .. rst-class:: sphx-glr-script-out .. code-block:: none f = -555.0 .. GENERATED FROM PYTHON SOURCE LINES 117-119 .. code-block:: Python print(f'g = {the_function_output.gradient}') .. rst-class:: sphx-glr-script-out .. code-block:: none g = [-450. -540.] .. GENERATED FROM PYTHON SOURCE LINES 120-122 .. code-block:: Python pd.DataFrame(the_function_output.hessian) .. raw:: html
0 1
0 -1380.0 -150.0
1 -150.0 -540.0


.. GENERATED FROM PYTHON SOURCE LINES 123-126 .. code-block:: Python pd.DataFrame(the_function_output.bhhh) .. raw:: html
0 1
0 49500.0 48600.0
1 48600.0 58320.0


.. GENERATED FROM PYTHON SOURCE LINES 127-130 Check numerically the derivatives' implementation. The analytical derivatives are compared to the numerical derivatives obtains by finite differences. .. GENERATED FROM PYTHON SOURCE LINES 130-132 .. code-block:: Python check_results: CheckDerivativesResults = my_biogeme.check_derivatives(verbose=True) .. rst-class:: sphx-glr-script-out .. code-block:: none Comparing first derivatives x Gradient FinDiff Difference beta1 -10.6006 -10.6006 -9.40832e-07 beta2 -139.7 -139.7 3.80828e-06 Comparing second derivatives Row Col Hessian FinDiff Difference beta1 beta1 -111.201 -111.201 -9.27991e-07 beta2 beta1 20.3003 20.3003 1.42409e-06 beta1 beta2 20.3003 20.3003 -2.4484e-07 beta2 beta2 -260.3 -260.3 3.34428e-06 .. GENERATED FROM PYTHON SOURCE LINES 133-134 .. code-block:: Python print(f'f = {check_results.function}') .. rst-class:: sphx-glr-script-out .. code-block:: none f = -115.30029248549191 .. GENERATED FROM PYTHON SOURCE LINES 135-136 .. code-block:: Python print(f'g = {check_results.analytical_gradient}') .. rst-class:: sphx-glr-script-out .. code-block:: none g = [ -10.60058497 -139.69970751] .. GENERATED FROM PYTHON SOURCE LINES 137-138 .. code-block:: Python display(pd.DataFrame(check_results.analytical_hessian)) .. rst-class:: sphx-glr-script-out .. code-block:: none 0 1 0 -111.201170 20.300292 1 20.300292 -260.300292 .. GENERATED FROM PYTHON SOURCE LINES 139-140 .. code-block:: Python display(pd.DataFrame(check_results.finite_differences_gradient)) .. rst-class:: sphx-glr-script-out .. code-block:: none 0 0 -10.600584 1 -139.699711 .. GENERATED FROM PYTHON SOURCE LINES 141-143 .. code-block:: Python display(pd.DataFrame(check_results.finite_differences_hessian)) .. rst-class:: sphx-glr-script-out .. code-block:: none 0 1 0 -111.201169 20.300293 1 20.300291 -260.300296 .. GENERATED FROM PYTHON SOURCE LINES 144-146 Estimation ---------- .. GENERATED FROM PYTHON SOURCE LINES 148-149 Estimation of the parameters, with bootstrapping .. GENERATED FROM PYTHON SOURCE LINES 149-151 .. code-block:: Python results = my_biogeme.estimate(run_bootstrap=True) .. rst-class:: sphx-glr-script-out .. code-block:: none *** Initial values of the parameters are obtained from the file __simple_example.iter Parameter values restored from __simple_example.iter Starting values for the algorithm: {'beta1': -1.273263987213694, 'beta2': 1.2487688099301162} As the model is not too complex, we activate the calculation of second derivatives. To change this behavior, modify the algorithm to "simple_bounds" in the TOML file. Optimization algorithm: hybrid Newton/BFGS with simple bounds [simple_bounds] ** Optimization: Newton with trust region for simple bounds Optimization algorithm has converged. Relative gradient: 2.1439291582969576e-09 Cause of termination: Relative gradient = 2.1e-09 <= 6.1e-06 Number of function evaluations: 1 Number of gradient evaluations: 1 Number of hessian evaluations: 0 Algorithm: Newton with trust region for simple bound constraints Number of iterations: 0 Optimization time: 0:00:00.001324 Calculate second derivatives and BHHH Re-estimate the model 100 times for bootstrapping Bootstraps: 0%| | 0/100 [00:00` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_biogeme.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_biogeme.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_