Skip to content

Commit 1f37e32

Browse files
author
Chris Fonnesbeck
committed
Merge pull request pymc-devs#353 from pymc-devs/lognormal
Added lognormal distribution
2 parents 99526a1 + 74cf639 commit 1f37e32

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

pymc/distributions/continuous.py

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from numpy.random import uniform as runiform, normal as rnormal
1212

1313
__all__ = ['Uniform', 'Flat', 'Normal', 'Beta', 'Exponential', 'Laplace',
14-
'T', 'Cauchy', 'Gamma', 'Bound', 'Tpos']
14+
'T', 'Cauchy', 'Gamma', 'Bound', 'Tpos', 'Lognormal']
1515

1616
def get_tau(tau=None, sd=None):
1717
if tau is None:
@@ -230,6 +230,54 @@ def logp(value):
230230
return locals()
231231

232232

233+
@tensordist(continuous)
234+
def Lognormal(mu=0, tau=1):
235+
"""
236+
Log-normal log-likelihood.
237+
238+
Distribution of any random variable whose logarithm is normally
239+
distributed. A variable might be modeled as log-normal if it can
240+
be thought of as the multiplicative product of many small
241+
independent factors.
242+
243+
.. math::
244+
f(x \mid \mu, \tau) = \sqrt{\frac{\tau}{2\pi}}\frac{
245+
\exp\left\{ -\frac{\tau}{2} (\ln(x)-\mu)^2 \right\}}{x}
246+
247+
:Parameters:
248+
- `x` : x > 0
249+
- `mu` : Location parameter.
250+
- `tau` : Scale parameter (tau > 0).
251+
252+
.. note::
253+
254+
:math:`E(X)=e^{\mu+\frac{1}{2\tau}}`
255+
:math:`Var(X)=(e^{1/\tau}-1)e^{2\mu+\frac{1}{\tau}}`
256+
257+
"""
258+
def logp(value):
259+
return bound(
260+
-0.5*tau*(log(value) - mu)**2 + 0.5*log(tau/(2.*pi)) - log(value),
261+
tau > 0)
262+
263+
mean = exp(mu + 1./(2*tau))
264+
median = exp(mu)
265+
mode = exp(mu - 1./tau)
266+
267+
variance = (exp(1./tau) - 1) * exp(2*mu + 1./tau)
268+
269+
logp.__doc__ = """
270+
Log-normal log-likelihood with paramters mu={0} and tau={1}.
271+
272+
Parameters
273+
----------
274+
value : float
275+
Input data
276+
"""
277+
278+
return locals()
279+
280+
233281
@tensordist(continuous)
234282
def T(nu, mu=0, lam=1):
235283
"""

pymc/tests/test_distributions.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ def test_negative_binomial():
6464
def test_laplace():
6565
checkd(Laplace, R, {'mu': R, 'b': Rplus})
6666

67+
def test_lognormal():
68+
checkd(Lognormal, Rplus, {'mu': R, 'tau': Rplus}, False)
6769

6870
def test_t():
6971
checkd(T, R, {'nu': Rplus, 'mu': R, 'lam': Rplus})

0 commit comments

Comments
 (0)