Skip to content

Commit 15e9ac0

Browse files
author
Ralf Gommers
committed
Merge pull request scipy#371 from rgommers/ticket-1779-nbinom-pmf
BUG: fix precision issue with nbinom.pmf. Closes ticket 1779.
2 parents facd638 + 9fc353a commit 15e9ac0

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

scipy/stats/distributions.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6529,8 +6529,7 @@ def _rvs(self, n, p):
65296529
def _argcheck(self, n, p):
65306530
return (n >= 0) & (p >= 0) & (p <= 1)
65316531
def _pmf(self, x, n, p):
6532-
coeff = exp(gamln(n+x) - gamln(x+1) - gamln(n))
6533-
return coeff * power(p,n) * power(1-p,x)
6532+
return exp(self._logpmf(x, n, p))
65346533
def _logpmf(self, x, n, p):
65356534
coeff = gamln(n+x) - gamln(x+1) - gamln(n)
65366535
return coeff + n*log(p) + x*log(1-p)

scipy/stats/tests/test_distributions.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@ def test_rvs(self):
160160
assert_(isinstance(val, numpy.ndarray))
161161
assert_(val.dtype.char in typecodes['AllInteger'])
162162

163+
def test_pmf(self):
164+
# regression test for ticket 1779
165+
assert_allclose(np.exp(stats.nbinom.logpmf(700, 721, 0.52)),
166+
stats.nbinom.pmf(700, 721, 0.52))
167+
168+
163169
class TestGeom(TestCase):
164170
def test_rvs(self):
165171
vals = stats.geom.rvs(0.75, size=(2, 50))

0 commit comments

Comments
 (0)