.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/programmers/plot_loglikelihood.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_loglikelihood.py: biogeme.loglikelihood ===================== 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, 10:59:55 .. GENERATED FROM PYTHON SOURCE LINES 17-37 .. code-block:: Python import numpy as np import pandas as pd from IPython.core.display_functions import display import biogeme.biogeme_logging as blog from biogeme.database import Database from biogeme.expressions import Beta, Draws, MonteCarlo, Variable from biogeme.jax_calculator.simple_formula import ( evaluate_simple_expression_per_row, ) from biogeme.loglikelihood import ( likelihoodregression, loglikelihood, loglikelihoodregression, mixedloglikelihood, ) from biogeme.models import logit from biogeme.second_derivatives import SecondDerivativesMode from biogeme.version import get_text .. GENERATED FROM PYTHON SOURCE LINES 38-39 Version of Biogeme. .. GENERATED FROM PYTHON SOURCE LINES 39-43 .. code-block:: Python print(get_text()) logger = blog.get_screen_logger(level=blog.INFO) .. rst-class:: sphx-glr-script-out .. code-block:: none biogeme 3.3.3a0 [2025-12-25] 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 44-46 This module provides some basic expressions for the contribution of an observation to the (log) likelihood function. .. GENERATED FROM PYTHON SOURCE LINES 48-49 Let's consider first a simple choice model. .. GENERATED FROM PYTHON SOURCE LINES 51-60 .. code-block:: Python v_1 = 0 beta = Beta('beta', 0, None, None, 0) sigma = Beta('sigma', 1, 0, None, 0) v_2 = beta + sigma * Draws('v2', 'NORMAL') v = {1: v_1, 2: v_2} conditional_probability = logit(v, None, 0) probability = MonteCarlo(conditional_probability) display(probability) .. rst-class:: sphx-glr-script-out .. code-block:: none MonteCarlo(exp(LogLogit[choice=`0.0`]U=(1:`0.0`, 2:(Beta('beta', 0, None, None, 0) + (Beta('sigma', 1, 0, None, 0) * Draws("v2", "NORMAL"))))[always available])) .. GENERATED FROM PYTHON SOURCE LINES 61-63 The first function simply takes the log of the probability for each observation. .. GENERATED FROM PYTHON SOURCE LINES 65-68 .. code-block:: Python loglike = loglikelihood(probability) display(loglike) .. rst-class:: sphx-glr-script-out .. code-block:: none log(MonteCarlo(exp(LogLogit[choice=`0.0`]U=(1:`0.0`, 2:(Beta('beta', 0, None, None, 0) + (Beta('sigma', 1, 0, None, 0) * Draws("v2", "NORMAL"))))[always available]))) .. GENERATED FROM PYTHON SOURCE LINES 69-71 The second function also involves the integral using Monte-Carlo simulation. .. GENERATED FROM PYTHON SOURCE LINES 73-76 .. code-block:: Python loglike = mixedloglikelihood(conditional_probability) print(loglike) .. rst-class:: sphx-glr-script-out .. code-block:: none log(MonteCarlo(exp(LogLogit[choice=`0.0`]U=(1:`0.0`, 2:(Beta('beta', 0, None, None, 0) + (Beta('sigma', 1, 0, None, 0) * Draws("v2", "NORMAL"))))[always available]))) .. GENERATED FROM PYTHON SOURCE LINES 77-79 Regression models are often used in the context of hybrid choice models. Consider the following model. .. GENERATED FROM PYTHON SOURCE LINES 81-88 .. code-block:: Python x = Variable('x') y = Variable('y') beta = Beta('beta', 1, None, None, 0) sigma = Beta('sigma', 1, None, None, 0) intercept = Beta('intercept', 0, None, None, 0) model = intercept + beta * x .. GENERATED FROM PYTHON SOURCE LINES 89-94 The following function calculates the contribution to the likelihood. It is .. math:: \frac{1}{\sigma} \phi\left( \frac{y-m}{\sigma} \right), where :math:`\phi(\cdot)` is the pdf of the normal distribution. .. GENERATED FROM PYTHON SOURCE LINES 94-97 .. code-block:: Python like = likelihoodregression(y, model, sigma) display(like) .. rst-class:: sphx-glr-script-out .. code-block:: none exp(((((-((y - (Beta('intercept', 0, None, None, 0) + (Beta('beta', 1, None, None, 0) * x))) / Beta('sigma', 1, None, None, 0))**2.0) / `2.0`) - (log(Beta('sigma', 1, None, None, 0)**2.0) / `2.0`)) - `0.9189385332`)) .. GENERATED FROM PYTHON SOURCE LINES 98-103 The following function calculates the log of the contribution to the likelihood. It is .. math:: -\left( \frac{(y-m)^2}{2\sigma^2} \right) - \log(\sigma) - \frac{1}{2}\log(2\pi). .. GENERATED FROM PYTHON SOURCE LINES 103-106 .. code-block:: Python loglike = loglikelihoodregression(y, model, sigma) print(loglike) .. rst-class:: sphx-glr-script-out .. code-block:: none ((((-((y - (Beta('intercept', 0, None, None, 0) + (Beta('beta', 1, None, None, 0) * x))) / Beta('sigma', 1, None, None, 0))**2.0) / `2.0`) - (log(Beta('sigma', 1, None, None, 0)**2.0) / `2.0`)) - `0.9189385332`) .. GENERATED FROM PYTHON SOURCE LINES 107-108 We compare the two on a small database. .. GENERATED FROM PYTHON SOURCE LINES 110-113 .. code-block:: Python df = pd.DataFrame({'x': [-2, -1, 0, 1, 2], 'y': [1, 1, 1, 1, 1]}) my_data = Database('test', df) .. GENERATED FROM PYTHON SOURCE LINES 114-123 .. code-block:: Python lr = evaluate_simple_expression_per_row( expression=like, database=my_data, numerically_safe=False, second_derivatives_mode=SecondDerivativesMode.NEVER, use_jit=True, ) display(f'Likelihood evaluated by Biogeme: {lr}') .. rst-class:: sphx-glr-script-out .. code-block:: none Likelihood evaluated by Biogeme: [0.00443185 0.05399097 0.24197072 0.39894228 0.24197072] .. GENERATED FROM PYTHON SOURCE LINES 124-126 .. code-block:: Python display(f'Log of the above likelihood: {np.log(lr)}') .. rst-class:: sphx-glr-script-out .. code-block:: none Log of the above likelihood: [-5.41893853 -2.91893853 -1.41893853 -0.91893853 -1.41893853] .. GENERATED FROM PYTHON SOURCE LINES 127-135 .. code-block:: Python log_lr = evaluate_simple_expression_per_row( expression=loglike, database=my_data, numerically_safe=False, second_derivatives_mode=SecondDerivativesMode.NEVER, use_jit=True, ) display(f'Log likelihood evaluated by Biogeme: {log_lr}') .. rst-class:: sphx-glr-script-out .. code-block:: none Log likelihood evaluated by Biogeme: [-5.41893853 -2.91893853 -1.41893853 -0.91893853 -1.41893853] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.052 seconds) .. _sphx_glr_download_auto_examples_programmers_plot_loglikelihood.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_loglikelihood.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_loglikelihood.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_loglikelihood.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_