Skip to content

Commit

Permalink
Add safe values/ parameters for BetaBinomial, Poisson, and `Hyper…
Browse files Browse the repository at this point in the history
…Geometric` to avoid errors when evaluating negative logcdfs.
  • Loading branch information
ricardoV94 committed Dec 29, 2020
1 parent 450a6f7 commit 2c3c2a0
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions pymc3/distributions/discrete.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,12 @@ def logcdf(self, value):
alpha = self.alpha
beta = self.beta
n = self.n
safe_lower = tt.switch(tt.lt(value, 0), value, 0)

return bound(
tt.switch(
tt.lt(value, n),
logsumexp(self.logp(tt.arange(0, value + 1)), keepdims=False),
logsumexp(self.logp(tt.arange(safe_lower, value + 1)), keepdims=False),
0,
),
0 <= value,
Expand Down Expand Up @@ -706,9 +707,12 @@ def logcdf(self, value):
"""
mu = self.mu
value = tt.floor(value)
# To avoid issue with #4340
safe_mu = tt.switch(tt.lt(mu, 0), 0, mu)
safe_value = tt.switch(tt.lt(value, 0), 0, value)

return bound(
tt.log(tt.gammaincc(value + 1, mu)),
tt.log(tt.gammaincc(safe_value + 1, safe_mu)),
0 <= value,
0 <= mu,
)
Expand Down Expand Up @@ -1133,11 +1137,12 @@ def logcdf(self, value):
N = self.N
n = self.n
k = self.k
safe_lower = tt.switch(tt.lt(value, 0), value, 0)

return bound(
tt.switch(
tt.lt(value, n),
logsumexp(self.logp(tt.arange(0, value + 1)), keepdims=False),
logsumexp(self.logp(tt.arange(safe_lower, value + 1)), keepdims=False),
0,
),
0 <= value,
Expand Down

0 comments on commit 2c3c2a0

Please sign in to comment.