/
loglikelihood.py
106 lines (72 loc) · 3.31 KB
/
loglikelihood.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
""" Functions to calculate the log likelihood
:author: Michel Bierlaire
:date: Fri Mar 29 17:11:44 2019
"""
# Too constraining
# pylint: disable=invalid-name,
from biogeme.expressions import exp, log, MonteCarlo
def loglikelihood(prob):
"""
Simply computes the log of the probability
:param prob: An expression providing the value of the probability.
:type prob: biogeme.expressions.Expression
:return: the logarithm of the probability.
:rtype: biogeme.expressions.Expression
"""
return log(prob)
def mixedloglikelihood(prob):
"""Compute a simulated loglikelihood function
:param prob: An expression providing the value of the
probability. Although it is not formally necessary,
the expression should contain one or more random
variables of a given distribution, and therefore
is defined as
.. math:: P(i|\\xi_1,\\ldots,\\xi_L)
:type prob: biogeme.expressions.Expression
:return: the simulated loglikelihood, given by
.. math:: \\ln\\left(\\sum_{r=1}^R
P(i|\\xi^r_1,\\ldots,\\xi^r_L) \\right)
where :math:`R` is the number of draws, and :math:`\\xi_j^r`
is the rth draw of the random variable :math:`\\xi_j`.
:rtype: biogeme.expressions.Expression
"""
ell = MonteCarlo(prob)
return log(ell)
def likelihoodregression(meas, model, sigma):
"""Computes likelihood function of a regression model.
:param meas: An expression providing the value :math:`y` of the measure
for the current observation.
:type meas: biogeme.expressions.Expression
:param model: An expression providing the output :math:`m` of the model
for the current observation.
:type model: biogeme.expressions.Expression
:param sigma: An expression (typically, a parameter) providing the
standard error :math:`\\sigma` of the error term.
:type sigma: biogeme.expressions.Expression
:return: The likelihood of the regression, assuming a normal distribution,
that is
.. math:: \\frac{1}{\\sigma} \\phi\\left( \\frac{y-m}{\\sigma} \\right)
where :math:`\\phi(\\cdot)` is the pdf of the normal distribution.
:rtype: biogeme.expressions.Expression
"""
return exp(loglikelihoodregression(meas, model, sigma))
def loglikelihoodregression(meas, model, sigma):
"""Computes log likelihood function of a regression model.
:param meas: An expression providing the value :math:`y` of the
measure for the current observation.
:type meas: biogeme.expressions.Expression
:param model: An expression providing the output :math:`m` of the
model for the current observation.
:type model: biogeme.expressions.Expression
:param sigma: An expression (typically, a parameter) providing
the standard error :math:`\\sigma` of the error term.
:type sigma: biogeme.expressions.Expression
:return: the likelihood of the regression, assuming a normal distribution,
that is
.. math:: -\\left( \\frac{(y-m)^2}{2\\sigma^2} \\right) -
\\log(\\sigma) - \\frac{1}{2}\\log(2\\pi)
:rtype: biogeme.expressions.Expression
"""
t = (meas - model) / sigma
f = -(t ** 2) / 2 - log(sigma) - 0.9189385332
return f