Skip to content

Commit f344397

Browse files
domenzaintwiecki
authored andcommitted
Implement log CDF for ExGaussian ditribution
1 parent 12fd699 commit f344397

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

pymc3/distributions/continuous.py

+24
Original file line numberDiff line numberDiff line change
@@ -2947,6 +2947,30 @@ def _repr_latex_(self, name=None, dist=None):
29472947
get_variable_name(sigma),
29482948
get_variable_name(nu))
29492949

2950+
def logcdf(self, value):
2951+
"""
2952+
Compute the log CDF for the ExGaussian distribution
2953+
2954+
References
2955+
----------
2956+
.. [Rigby2005] R.A. Rigby (2005).
2957+
"Generalized additive models for location, scale and shape"
2958+
http://dx.doi.org/10.1111/j.1467-9876.2005.00510.x
2959+
"""
2960+
mu = self.mu
2961+
sigma = self.sigma
2962+
sigma_2 = sigma**2
2963+
nu = self.nu
2964+
z = value - mu - sigma_2/nu
2965+
return tt.switch(
2966+
tt.gt(nu, 0.05 * sigma),
2967+
tt.log(std_cdf((value - mu)/sigma) -
2968+
std_cdf(z/sigma) * tt.exp(
2969+
((mu + (sigma_2/nu))**2 -
2970+
(mu**2) -
2971+
2 * value * ((sigma_2)/nu))/(2 * sigma_2))),
2972+
normal_lcdf(mu, sigma, value))
2973+
29502974

29512975
class VonMises(Continuous):
29522976
R"""

pymc3/tests/test_distributions.py

+19
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,25 @@ def test_ex_gaussian(self, value, mu, sigma, nu, logp):
11151115
pt = {'eg': value}
11161116
assert_almost_equal(model.fastlogp(pt), logp, decimal=select_by_precision(float64=6, float32=2), err_msg=str(pt))
11171117

1118+
@pytest.mark.parametrize('value,mu,sigma,nu,logcdf', [
1119+
(0.5, -50.000, 0.500, 0.500, 0.0000000),
1120+
(1.0, -1.000, 0.001, 0.001, 0.0000000),
1121+
(2.0, 0.001, 1.000, 1.000, -0.2365674),
1122+
(5.0, 0.500, 2.500, 2.500, -0.2886489),
1123+
(7.5, 2.000, 5.000, 5.000, -0.5655104),
1124+
(15.0, 5.000, 7.500, 7.500, -0.4545255),
1125+
(50.0, 50.000, 10.000, 10.000, -1.433714),
1126+
(1000.0, 500.000, 10.000, 20.000, -1.573708e-11),
1127+
])
1128+
def test_ex_gaussian_cdf(self, value, mu, sigma, nu, logcdf):
1129+
"""Log probabilities calculated using the pexGAUS function from the R package gamlss.
1130+
See e.g., doi: 10.1111/j.1467-9876.2005.00510.x, or http://www.gamlss.org/."""
1131+
assert_almost_equal(
1132+
ExGaussian.dist(mu=mu, sigma=sigma, nu=nu).logcdf(value).tag.test_value,
1133+
logcdf,
1134+
decimal=select_by_precision(float64=6, float32=2),
1135+
err_msg=str((value, mu, sigma, nu, logcdf)))
1136+
11181137
@pytest.mark.xfail(condition=(theano.config.floatX == "float32"), reason="Fails on float32")
11191138
def test_vonmises(self):
11201139
self.pymc3_matches_scipy(

0 commit comments

Comments
 (0)