summaryrefslogtreecommitdiff
path: root/numpy/random
diff options
context:
space:
mode:
authorWarren Weckesser <warren.weckesser@gmail.com>2020-04-05 23:25:53 -0400
committerWarren Weckesser <warren.weckesser@gmail.com>2020-04-05 23:35:50 -0400
commitf90f26c40ac4b37e1df8c76ee6745433557d999b (patch)
tree57af5f657f9769fdab28eb1dd45dd621c14fa7fe /numpy/random
parent0f6a3aaa34fe72586f8c66ecc4744fc960d62ae2 (diff)
downloadnumpy-f90f26c40ac4b37e1df8c76ee6745433557d999b.tar.gz
BUG: random: Disallow p=0 in negative_binomial
Make `Generator.negative_binomial` raise a ValueError if p=0. `negative_binomial(n, p)` draws samples from the distribution of the number of failures until n successes are encountered. If p is 0, then a success is never encountered, so the probability distribution is 0 for any finite number of failures. In other words, it is not really a meaningful distribution, so we disallow p=0. Closes gh-15913.
Diffstat (limited to 'numpy/random')
-rw-r--r--numpy/random/_generator.pyx6
-rw-r--r--numpy/random/tests/test_generator_mt19937.py5
2 files changed, 8 insertions, 3 deletions
diff --git a/numpy/random/_generator.pyx b/numpy/random/_generator.pyx
index 6b8a2f70b..27e006cc3 100644
--- a/numpy/random/_generator.pyx
+++ b/numpy/random/_generator.pyx
@@ -2844,14 +2844,14 @@ cdef class Generator:
Samples are drawn from a negative binomial distribution with specified
parameters, `n` successes and `p` probability of success where `n`
- is > 0 and `p` is in the interval [0, 1].
+ is > 0 and `p` is in the interval (0, 1].
Parameters
----------
n : float or array_like of floats
Parameter of the distribution, > 0.
p : float or array_like of floats
- Parameter of the distribution, >= 0 and <=1.
+ Parameter of the distribution. Must satisfy 0 < p <= 1.
size : int or tuple of ints, optional
Output shape. If the given shape is, e.g., ``(m, n, k)``, then
``m * n * k`` samples are drawn. If size is ``None`` (default),
@@ -2909,7 +2909,7 @@ cdef class Generator:
"""
return disc(&random_negative_binomial, &self._bitgen, size, self.lock, 2, 0,
n, 'n', CONS_POSITIVE_NOT_NAN,
- p, 'p', CONS_BOUNDED_0_1,
+ p, 'p', CONS_BOUNDED_GT_0_1,
0.0, '', CONS_NONE)
def poisson(self, lam=1.0, size=None):
diff --git a/numpy/random/tests/test_generator_mt19937.py b/numpy/random/tests/test_generator_mt19937.py
index b10c1310e..c4c1fa316 100644
--- a/numpy/random/tests/test_generator_mt19937.py
+++ b/numpy/random/tests/test_generator_mt19937.py
@@ -1297,6 +1297,11 @@ class TestRandomDist:
assert_raises(ValueError, random.negative_binomial, 100,
[np.nan] * 10)
+ def test_negative_binomial_p0_exception(self):
+ # Verify that p=0 raises an exception.
+ with assert_raises(ValueError):
+ x = random.negative_binomial(1, 0)
+
def test_noncentral_chisquare(self):
random = Generator(MT19937(self.seed))
actual = random.noncentral_chisquare(df=5, nonc=5, size=(3, 2))