Permalink
Browse files

Fix for Ticket #1842 : wrong value returned by scipy.stats.binom prob…

…ability mass function when true probability is 1

+ a test for it.
  • Loading branch information...
1 parent dbaaad3 commit c4b390b19df056ab9000f2b4169fa7b2d7222476 @pbrod pbrod committed with pv Mar 9, 2013
Showing with 7 additions and 1 deletion.
  1. +1 −1 scipy/stats/distributions.py
  2. +6 −0 scipy/stats/tests/test_distributions.py
View
2 scipy/stats/distributions.py
@@ -6597,7 +6597,7 @@ def _logpmf(self, x, n, p):
k = floor(x)
combiln = (gamln(n+1) - (gamln(k+1) +
gamln(n-k+1)))
- return combiln + k*np.log(p) + (n-k)*np.log(1-p)
+ return combiln + special.xlogy(k,p) + special.xlog1py(n-k, -p)
def _pmf(self, x, n, p):
return exp(self._logpmf(x, n, p))
def _cdf(self, x, n, p):
View
6 scipy/stats/tests/test_distributions.py
@@ -137,6 +137,12 @@ def test_rvs(self):
assert_(isinstance(val, numpy.ndarray))
assert_(val.dtype.char in typecodes['AllInteger'])
+ def test_pmf(self):
+ # regression test for Ticket #1842
+ vals1 = stats.binom.pmf(100, 100,1)
+ vals2 = stats.binom.pmf(0, 100,0)
+ assert_allclose(vals1, 1.0, rtol=1e-15, atol=0)
+ assert_allclose(vals2, 1.0, rtol=1e-15, atol=0)
class TestBernoulli(TestCase):
def test_rvs(self):

0 comments on commit c4b390b

Please sign in to comment.