Skip to content

Implement CDF, CCDF, LCDF, LCCDF for Normal distribution. #1861

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 1 commit into from
Closed
Show file tree
Hide file tree
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
40 changes: 39 additions & 1 deletion pymc3/distributions/continuous.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from pymc3.theanof import floatX
from . import transforms

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

__all__ = ['Uniform', 'Flat', 'Normal', 'Beta', 'Exponential', 'Laplace',
Expand Down Expand Up @@ -231,6 +231,44 @@ def logp(self, value):
return bound((-tau * (value - mu)**2 + tt.log(tau / np.pi / 2.)) / 2.,
sd > 0)

def cdf(self, value):
mu = self.mu
sd = self.sd
z = zvalue(value, mu=mu, sd=sd)

return tt.erfc(-z / tt.sqrt(2.))/2.

def ccdf(self, value):
mu = self.mu
sd = self.sd
z = zvalue(value, mu=mu, sd=sd)

return tt.erfc(z / tt.sqrt(2.))/2.

def lcdf(self, value):
mu = self.mu
sd = self.sd
z = zvalue(value, mu=mu, sd=sd)

return tt.switch(
tt.lt(z, -1.0),
tt.log(tt.erfcx(-z / tt.sqrt(2.)) / 2.) -
tt.sqr(tt.abs_(z)) / 2,
tt.log1p(-tt.erfc(z / tt.sqrt(2.)) / 2.)
)

def lccdf(self, value):
mu = self.mu
sd = self.sd
z = zvalue(value, mu=mu, sd=sd)

return tt.switch(
tt.gt(z, 1.0),
tt.log(tt.erfcx(z / tt.sqrt(2.)) / 2) -
tt.sqr(tt.abs_(z)) / 2.,
tt.log1p(-tt.erfc(-z / tt.sqrt(2.)) / 2.)
)


class HalfNormal(PositiveContinuous):
R"""
Expand Down
6 changes: 6 additions & 0 deletions pymc3/distributions/dist_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,9 @@ def i1(x):
x**9 / 1474560 + x**11 / 176947200 + x**13 / 29727129600,
np.e**x / (2 * np.pi * x)**0.5 * (1 - 3 / (8 * x) + 15 / (128 * x**2) + 315 / (3072 * x**3)
+ 14175 / (98304 * x**4)))

def zvalue(value, sd=1, mu=0):
"""
Calculate the z-value for a normal distribution. By default standard normal.
"""
return (value - mu) / tt.sqrt(2. * sd ** 2.)