{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Module biogeme.loglikelihood " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Examples of use of each function" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This webpage is 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. For examples of models, visit [biogeme.epfl.ch](http://biogeme.epfl.ch)." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2021-08-04 16:38:57.020900\n" ] } ], "source": [ "import datetime\n", "print(datetime.datetime.now())" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "biogeme 3.2.8 [2021-08-04]\n", "Version entirely written in Python\n", "Home page: http://biogeme.epfl.ch\n", "Submit questions to https://groups.google.com/d/forum/biogeme\n", "Michel Bierlaire, Transport and Mobility Laboratory, Ecole Polytechnique Fédérale de Lausanne (EPFL)\n", "\n" ] } ], "source": [ "import biogeme.version as ver\n", "print(ver.getText())" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import pandas as pd\n", "import biogeme.database as db\n", "import biogeme.loglikelihood as ll\n", "import biogeme.expressions as ex\n", "import biogeme.models as md" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This module provides some basic expressions for the contribution of an observation to the (log) likelihood function." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's consider first a simple choice model." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "MonteCarlo(exp(_bioLogLogitFullChoiceSet(1:`0`, 2:(beta(0) + (sigma(1) * bioDraws(\"V2\", \"NORMAL\"))))))\n" ] } ], "source": [ "V1 = 0\n", "beta = ex.Beta('beta', 0, None, None, 0)\n", "sigma = ex.Beta('sigma', 1, 0, None, 0)\n", "V2 = beta + sigma * ex.bioDraws('V2', 'NORMAL')\n", "V = {1: V1, 2: V2}\n", "condprob = md.logit(V, None, 0)\n", "prob = ex.MonteCarlo(condprob)\n", "print(prob)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The first function simply takes the log of the probability for each observation." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "log(MonteCarlo(exp(_bioLogLogitFullChoiceSet(1:`0`, 2:(beta(0) + (sigma(1) * bioDraws(\"V2\", \"NORMAL\")))))))\n" ] } ], "source": [ "loglike = ll.loglikelihood(prob)\n", "print(loglike)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The second function also involves the integral using Monte-Carlo simulation." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "log(MonteCarlo(exp(_bioLogLogitFullChoiceSet(1:`0`, 2:(beta(0) + (sigma(1) * bioDraws(\"V2\", \"NORMAL\")))))))\n" ] } ], "source": [ "loglike = ll.mixedloglikelihood(condprob)\n", "print(loglike)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Regression models are often used in the context of hybrid choice models. Consider the following model." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [], "source": [ "x = ex.Variable('x')\n", "y = ex.Variable('y')\n", "beta = ex.Beta('beta', 1, None, None, 0)\n", "sigma = ex.Beta('sigma', 1, None, None, 0)\n", "intercept = ex.Beta('intercept', 0, None, None, 0)\n", "model = intercept + beta * x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following function calculates the contribution to the likelihood. It is \n", "\n", " \\\\[\\frac{1}{\\sigma} \\phi\\left( \\frac{y-m}{\\sigma} \\right),\\\\]\n", " \n", " where $\\phi(\\cdot)$ is the pdf of the normal distribution.\n", "\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "exp(((((-(((y - (intercept(0) + (beta(1) * x))) / sigma(1)) ** `2`)) / `2`) - log(sigma(1))) - `0.9189385332`))\n" ] } ], "source": [ "like = ll.likelihoodregression(y, model, sigma)\n", "print(like)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following function calculates the log of the contribution to the likelihood. It is\n", "\n", "\\\\[-\\left( \\frac{(y-m)^2}{2\\sigma^2} \\right) -\n", " \\log(\\sigma) - \\frac{1}{2}\\log(2\\pi).\\\\]" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "((((-(((y - (intercept(0) + (beta(1) * x))) / sigma(1)) ** `2`)) / `2`) - log(sigma(1))) - `0.9189385332`)\n" ] } ], "source": [ "loglike = ll.loglikelihoodregression(y, model, sigma)\n", "print(loglike)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We compare the two on a small database" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "df = pd.DataFrame({'x': [-2, -1, 0, 1, 2],\n", " 'y': [1, 1, 1, 1, 1]})\n", "myData = db.Database('test', df)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0.00443185, 0.05399097, 0.24197073, 0.39894229, 0.24197073])" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "lr = like.getValue_c(myData)\n", "lr" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([-5.41893852, -2.91893852, -1.41893852, -0.91893852, -1.41893852])" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "np.log(lr)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([-5.41893852, -2.91893852, -1.41893852, -0.91893852, -1.41893852])" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "loglike.getValue_c(myData)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.5" } }, "nbformat": 4, "nbformat_minor": 4 }