Skip to content

Logit-normal distribution #2077

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 64 additions & 45 deletions pymc3/distributions/continuous.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

from .dist_math import bound, logpow, gammaln, betaln, std_cdf, i0, i1, alltrue_elemwise
from .distribution import Continuous, draw_values, generate_samples, Bound
from pymc3.math import logit

__all__ = ['Uniform', 'Flat', 'Normal', 'Beta', 'Exponential', 'Laplace',
'StudentT', 'Cauchy', 'HalfCauchy', 'Gamma', 'Weibull',
Expand Down Expand Up @@ -63,8 +64,7 @@ def assert_negative_support(var, label, distname, value=-1e-6):


def get_tau_sd(tau=None, sd=None):
"""
Find precision and standard deviation
"""Find precision and standard deviation

.. math::
\tau = \frac{1}{\sigma^2}
Expand Down Expand Up @@ -104,8 +104,7 @@ def get_tau_sd(tau=None, sd=None):


class Uniform(Continuous):
R"""
Continuous uniform log-likelihood.
R"""Continuous uniform log-likelihood.

.. math::

Expand Down Expand Up @@ -153,8 +152,7 @@ def logp(self, value):


class Flat(Continuous):
"""
Uninformative log-likelihood that returns 0 regardless of
"""Uninformative log-likelihood that returns 0 regardless of
the passed value.
"""

Expand All @@ -170,8 +168,7 @@ def logp(self, value):


class Normal(Continuous):
R"""
Univariate normal log-likelihood.
R"""Univariate normal log-likelihood.

.. math::

Expand Down Expand Up @@ -233,8 +230,7 @@ def logp(self, value):


class HalfNormal(PositiveContinuous):
R"""
Half-normal log-likelihood.
R"""Half-normal log-likelihood.

.. math::

Expand Down Expand Up @@ -284,8 +280,7 @@ def logp(self, value):


class Wald(PositiveContinuous):
R"""
Wald log-likelihood.
R"""Wald log-likelihood.

.. math::

Expand Down Expand Up @@ -405,8 +400,7 @@ def logp(self, value):


class Beta(UnitContinuous):
R"""
Beta log-likelihood.
R"""Beta log-likelihood.

.. math::

Expand Down Expand Up @@ -493,8 +487,7 @@ def logp(self, value):


class Exponential(PositiveContinuous):
R"""
Exponential log-likelihood.
R"""Exponential log-likelihood.

.. math::

Expand Down Expand Up @@ -535,8 +528,7 @@ def logp(self, value):


class Laplace(Continuous):
R"""
Laplace log-likelihood.
R"""Laplace log-likelihood.

.. math::

Expand Down Expand Up @@ -580,8 +572,7 @@ def logp(self, value):


class Lognormal(PositiveContinuous):
R"""
Log-normal log-likelihood.
R"""Log-normal log-likelihood.

Distribution of any random variable whose logarithm is normally
distributed. A variable might be modeled as log-normal if it can
Expand All @@ -597,7 +588,7 @@ class Lognormal(PositiveContinuous):
======== =========================================================================
Support :math:`x \in (0, 1)`
Mean :math:`\exp\{\mu + \frac{1}{2\tau}\}`
Variance :math:\(\exp\{\frac{1}{\tau}\} - 1\) \times \exp\{2\mu + \frac{1}{\tau}\}
Variance :math:`\(\exp\{\frac{1}{\tau}\} - 1\) \times \exp\{2\mu + \frac{1}{\tau}\}`
======== =========================================================================

Parameters
Expand Down Expand Up @@ -643,9 +634,48 @@ def logp(self, value):
tau > 0)


class Logitnormal(PositiveContinuous):
R"""Logit-normal log-likelihood.

.. math::

f(x \mid \mu, \tau) =
\frac{1}{x(x-1)} \sqrt{\frac{\tau}{2\pi}}
\exp\left\{ -\frac{\tau}{2} (\logit(x)-\mu)^2 \right\}

======== =========================================================================
Support :math:`x \in (0, 1)`
Mean undefined
Variance undefined
======== =========================================================================

Parameters
----------
mu : float
Location parameter.
tau : float
Scale parameter (tau > 0).
"""

def __init__(self, mu=0.5, tau=None, *args, **kwargs):
super(Logitnormal, self).__init__(*args, **kwargs)

self.mu = mu = tt.as_tensor_variable(mu)
self.tau = tau = tt.as_tensor_variable(tau)

self.median = logit(mu)
assert_negative_support(tau, 'tau', 'Logitnormal')

def logp(self, value):
mu = self.mu
tau = self.tau
return bound(-0.5 * tau * (logit(value) - mu) ** 2
+ 0.5 * tt.log(tau / (2. * np.pi))
- tt.log(value * (1 - value)), value > 0, value < 1, tau > 0)


class StudentT(Continuous):
R"""
Non-central Student's T log-likelihood.
R"""Non-central Student's T log-likelihood.

Describes a normal variable whose precision is gamma distributed.
If only nu parameter is passed, this specifies a standard (central)
Expand Down Expand Up @@ -708,8 +738,7 @@ def logp(self, value):


class Pareto(PositiveContinuous):
R"""
Pareto log-likelihood.
R"""Pareto log-likelihood.

Often used to characterize wealth distribution, or other examples of the
80/20 rule.
Expand Down Expand Up @@ -770,8 +799,7 @@ def logp(self, value):


class Cauchy(Continuous):
R"""
Cauchy log-likelihood.
R"""Cauchy log-likelihood.

Also known as the Lorentz or the Breit-Wigner distribution.

Expand Down Expand Up @@ -822,8 +850,7 @@ def logp(self, value):


class HalfCauchy(PositiveContinuous):
R"""
Half-Cauchy log-likelihood.
R"""Half-Cauchy log-likelihood.

.. math::

Expand Down Expand Up @@ -868,8 +895,7 @@ def logp(self, value):


class Gamma(PositiveContinuous):
R"""
Gamma log-likelihood.
R"""Gamma log-likelihood.

Represents the sum of alpha exponentially distributed random variables,
each of which has mean beta.
Expand Down Expand Up @@ -951,8 +977,7 @@ def logp(self, value):


class InverseGamma(PositiveContinuous):
R"""
Inverse gamma log-likelihood, the reciprocal of the gamma distribution.
R"""Inverse gamma log-likelihood, the reciprocal of the gamma distribution.

.. math::

Expand Down Expand Up @@ -1012,8 +1037,7 @@ def logp(self, value):


class ChiSquared(Gamma):
R"""
:math:`\chi^2` log-likelihood.
R""":math:`\chi^2` log-likelihood.

.. math::

Expand All @@ -1038,8 +1062,7 @@ def __init__(self, nu, *args, **kwargs):


class Weibull(PositiveContinuous):
R"""
Weibull log-likelihood.
R"""Weibull log-likelihood.

.. math::

Expand Down Expand Up @@ -1102,8 +1125,7 @@ def StudentTpos(*args, **kwargs):


class ExGaussian(Continuous):
R"""
Exponentially modified Gaussian log-likelihood.
R"""Exponentially modified Gaussian log-likelihood.

Results from the convolution of a normal distribution with an exponential
distribution.
Expand Down Expand Up @@ -1184,8 +1206,7 @@ def logp(self, value):


class VonMises(Continuous):
R"""
Univariate VonMises log-likelihood.
R"""Univariate VonMises log-likelihood.

.. math::
f(x \mid \mu, \kappa) =
Expand Down Expand Up @@ -1232,8 +1253,7 @@ def logp(self, value):


class SkewNormal(Continuous):
R"""
Univariate skew-normal log-likelihood.
R"""Univariate skew-normal log-likelihood.

.. math::
f(x \mid \mu, \tau, \alpha) =
Expand Down Expand Up @@ -1307,8 +1327,7 @@ def logp(self, value):


class Triangular(Continuous):
"""
Continuous Triangular log-likelihood
"""Continuous Triangular log-likelihood
Implemented by J. A. Fonseca 22/12/16

Parameters
Expand Down