Skip to content
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

Fix ExGaussian logp #4050

Merged
merged 2 commits into from
Aug 14, 2020
Merged
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
2 changes: 1 addition & 1 deletion RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

### Maintenance
- Mentioned the way to do any random walk with `theano.tensor.cumsum()` in `GaussianRandomWalk` docstrings (see [#4048](https://github.com/pymc-devs/pymc3/pull/4048)).

- Fixed numerical instability in ExGaussian's logp by preventing `logpow` from returning `-inf` (see [#4050](https://github.com/pymc-devs/pymc3/pull/4050)).

### Documentation

Expand Down
23 changes: 16 additions & 7 deletions pymc3/distributions/continuous.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
nodes in PyMC.
"""
import numpy as np
import theano
import theano.tensor as tt
from scipy import stats
from scipy.special import expit
Expand Down Expand Up @@ -3268,13 +3269,21 @@ def logp(self, value):
sigma = self.sigma
nu = self.nu

# This condition suggested by exGAUS.R from gamlss
lp = tt.switch(tt.gt(nu, 0.05 * sigma),
- tt.log(nu) + (mu - value) / nu + 0.5 * (sigma / nu)**2
+ logpow(std_cdf((value - mu) / sigma - sigma / nu), 1.),
- tt.log(sigma * tt.sqrt(2 * np.pi))
- 0.5 * ((value - mu) / sigma)**2)
return bound(lp, sigma > 0., nu > 0.)
standardized_val = (value - mu) / sigma
cdf_val = std_cdf(standardized_val - sigma / nu)
cdf_val_safe = tt.switch(tt.eq(cdf_val, 0), np.finfo(theano.config.floatX).eps, cdf_val)

# This condition is suggested by exGAUS.R from gamlss
lp = tt.switch(
tt.gt(nu, 0.05 * sigma),
-tt.log(nu)
+ (mu - value) / nu
+ 0.5 * (sigma / nu) ** 2
+ logpow(cdf_val_safe, 1.0),
-tt.log(sigma * tt.sqrt(2 * np.pi)) - 0.5 * standardized_val ** 2,
)

return bound(lp, sigma > 0.0, nu > 0.0)

def _repr_latex_(self, name=None, dist=None):
if dist is None:
Expand Down