.. 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. :author: Michel Bierlaire :date: Wed Nov 22 15:16:56 2023 .. GENERATED FROM PYTHON SOURCE LINES 16-25 .. code-block:: default import numpy as np import pandas as pd import biogeme.version as ver import biogeme.biogeme_logging as blog import biogeme.database as db import biogeme.loglikelihood as ll import biogeme.expressions as ex import biogeme.models as md .. GENERATED FROM PYTHON SOURCE LINES 26-27 Version of Biogeme. .. GENERATED FROM PYTHON SOURCE LINES 27-31 .. code-block:: default print(ver.getText()) logger = blog.get_screen_logger(level=blog.INFO) .. rst-class:: sphx-glr-script-out .. code-block:: none biogeme 3.2.13 [2023-12-23] 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 32-34 This module provides some basic expressions for the contribution of an observation to the (log) likelihood function. .. GENERATED FROM PYTHON SOURCE LINES 36-37 Let's consider first a simple choice model. .. GENERATED FROM PYTHON SOURCE LINES 39-48 .. code-block:: default V1 = 0 beta = ex.Beta('beta', 0, None, None, 0) sigma = ex.Beta('sigma', 1, 0, None, 0) V2 = beta + sigma * ex.bioDraws('V2', 'NORMAL') V = {1: V1, 2: V2} condprob = md.logit(V, None, 0) prob = ex.MonteCarlo(condprob) print(prob) .. rst-class:: sphx-glr-script-out .. code-block:: none MonteCarlo(exp(_bioLogLogitFullChoiceSet[choice=`0.0`]U=(1:`0.0`, 2:(Beta('beta', 0, -1.3407807929942596e+154, 1.3407807929942596e+154, 0) + (Beta('sigma', 1, 0, 1.3407807929942596e+154, 0) * bioDraws("V2", "NORMAL"))))av=(1:`1.0`, 2:`1.0`))) .. GENERATED FROM PYTHON SOURCE LINES 49-51 The first function simply takes the log of the probability for each observation. .. GENERATED FROM PYTHON SOURCE LINES 53-56 .. code-block:: default loglike = ll.loglikelihood(prob) print(loglike) .. rst-class:: sphx-glr-script-out .. code-block:: none log(MonteCarlo(exp(_bioLogLogitFullChoiceSet[choice=`0.0`]U=(1:`0.0`, 2:(Beta('beta', 0, -1.3407807929942596e+154, 1.3407807929942596e+154, 0) + (Beta('sigma', 1, 0, 1.3407807929942596e+154, 0) * bioDraws("V2", "NORMAL"))))av=(1:`1.0`, 2:`1.0`)))) .. GENERATED FROM PYTHON SOURCE LINES 57-59 The second function also involves the integral using Monte-Carlo simulation. .. GENERATED FROM PYTHON SOURCE LINES 61-64 .. code-block:: default loglike = ll.mixedloglikelihood(condprob) print(loglike) .. rst-class:: sphx-glr-script-out .. code-block:: none log(MonteCarlo(exp(_bioLogLogitFullChoiceSet[choice=`0.0`]U=(1:`0.0`, 2:(Beta('beta', 0, -1.3407807929942596e+154, 1.3407807929942596e+154, 0) + (Beta('sigma', 1, 0, 1.3407807929942596e+154, 0) * bioDraws("V2", "NORMAL"))))av=(1:`1.0`, 2:`1.0`)))) .. GENERATED FROM PYTHON SOURCE LINES 65-67 Regression models are often used in the context of hybrid choice models. Consider the following model. .. GENERATED FROM PYTHON SOURCE LINES 69-76 .. code-block:: default x = ex.Variable('x') y = ex.Variable('y') beta = ex.Beta('beta', 1, None, None, 0) sigma = ex.Beta('sigma', 1, None, None, 0) intercept = ex.Beta('intercept', 0, None, None, 0) model = intercept + beta * x .. GENERATED FROM PYTHON SOURCE LINES 77-82 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 82-85 .. code-block:: default like = ll.likelihoodregression(y, model, sigma) print(like) .. rst-class:: sphx-glr-script-out .. code-block:: none exp(((((-(((y - (Beta('intercept', 0, -1.3407807929942596e+154, 1.3407807929942596e+154, 0) + (Beta('beta', 1, -1.3407807929942596e+154, 1.3407807929942596e+154, 0) * x))) / Beta('sigma', 1, -1.3407807929942596e+154, 1.3407807929942596e+154, 0)) ** `2.0`)) / `2.0`) - (log((Beta('sigma', 1, -1.3407807929942596e+154, 1.3407807929942596e+154, 0) ** `2.0`)) / `2.0`)) - `0.9189385332`)) .. GENERATED FROM PYTHON SOURCE LINES 86-91 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 91-94 .. code-block:: default loglike = ll.loglikelihoodregression(y, model, sigma) print(loglike) .. rst-class:: sphx-glr-script-out .. code-block:: none ((((-(((y - (Beta('intercept', 0, -1.3407807929942596e+154, 1.3407807929942596e+154, 0) + (Beta('beta', 1, -1.3407807929942596e+154, 1.3407807929942596e+154, 0) * x))) / Beta('sigma', 1, -1.3407807929942596e+154, 1.3407807929942596e+154, 0)) ** `2.0`)) / `2.0`) - (log((Beta('sigma', 1, -1.3407807929942596e+154, 1.3407807929942596e+154, 0) ** `2.0`)) / `2.0`)) - `0.9189385332`) .. GENERATED FROM PYTHON SOURCE LINES 95-96 We compare the two on a small database. .. GENERATED FROM PYTHON SOURCE LINES 98-101 .. code-block:: default df = pd.DataFrame({'x': [-2, -1, 0, 1, 2], 'y': [1, 1, 1, 1, 1]}) my_data = db.Database('test', df) .. GENERATED FROM PYTHON SOURCE LINES 102-105 .. code-block:: default lr = like.getValue_c(my_data, prepareIds=True) lr .. rst-class:: sphx-glr-script-out .. code-block:: none array([0.00443185, 0.05399097, 0.24197072, 0.39894228, 0.24197072]) .. GENERATED FROM PYTHON SOURCE LINES 106-108 .. code-block:: default np.log(lr) .. rst-class:: sphx-glr-script-out .. code-block:: none array([-5.41893853, -2.91893853, -1.41893853, -0.91893853, -1.41893853]) .. GENERATED FROM PYTHON SOURCE LINES 109-110 .. code-block:: default loglike.getValue_c(my_data, prepareIds=True) .. rst-class:: sphx-glr-script-out .. code-block:: none array([-5.41893853, -2.91893853, -1.41893853, -0.91893853, -1.41893853]) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.005 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-python :download:`Download Python source code: plot_loglikelihood.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_loglikelihood.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_