Skip to content

Commit 299814c

Browse files
FIX: random.binomial fallback (#354)
* FIX: random.binomial fallback
1 parent 489b970 commit 299814c

File tree

2 files changed

+31
-68
lines changed

2 files changed

+31
-68
lines changed

dpnp/random/dpnp_iface_random.py

Lines changed: 18 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -163,60 +163,13 @@ def binomial(n, p, size=None):
163163
164164
Draw samples from a binomial distribution.
165165
166-
Samples are drawn from a binomial distribution with specified
167-
parameters, n trials and p probability of success where
168-
n an integer >= 0 and p is in the interval [0,1]. (n may be
169-
input as a float, but it is truncated to an integer in use)
166+
For full documentation refer to :obj:`numpy.random.binomial`.
170167
171-
Parameters
172-
----------
173-
n : int
174-
Parameter of the distribution, >= 0. Floats are also accepted,
175-
but they will be truncated to integers.
176-
p : float
177-
Parameter of the distribution, >= 0 and <=1.
178-
size : int or tuple of ints, optional
179-
Output shape. If the given shape is, e.g., ``(m, n, k)``, then
180-
``m * n * k`` samples are drawn. If size is ``None`` (default),
181-
a single value is returned if ``n`` and ``p`` are both scalars.
182-
Otherwise, ``np.broadcast(n, p).size`` samples are drawn.
183-
184-
Returns
185-
-------
186-
out : dparray, int32
187-
Drawn samples from the parameterized binomial distribution, where
188-
each sample is equal to the number of successes over the n trials.
189-
190-
Notes
191-
-----
192-
The probability density for the binomial distribution is
193-
194-
.. math:: P(N) = \\binom{n}{N}p^N(1-p)^{n-N},
195-
196-
where :math:`n` is the number of trials, :math:`p` is the probability
197-
of success, and :math:`N` is the number of successes.
198-
199-
When estimating the standard error of a proportion in a population by
200-
using a random sample, the normal distribution works well unless the
201-
product p*n <=5, where p = population proportion estimate, and n =
202-
number of samples, in which case the binomial distribution is used
203-
instead. For example, a sample of 15 people shows 4 who are left
204-
handed, and 11 who are right handed. Then p = 4/15 = 27%. 0.27*15 = 4,
205-
so the binomial distribution should be used in this case.
206-
207-
References
208-
----------
209-
.. [1] Dalgaard, Peter, "Introductory Statistics with R",
210-
Springer-Verlag, 2002.
211-
.. [2] Glantz, Stanton A. "Primer of Biostatistics.", McGraw-Hill,
212-
Fifth Edition, 2002.
213-
.. [3] Lentner, Marvin, "Elementary Applied Statistics", Bogden
214-
and Quigley, 1972.
215-
.. [4] Weisstein, Eric W. "Binomial Distribution." From MathWorld--A
216-
Wolfram Web Resource.
217-
http://mathworld.wolfram.com/BinomialDistribution.html
218-
.. [5] Wikipedia, "Binomial distribution",
219-
https://en.wikipedia.org/wiki/Binomial_distribution
168+
Limitations
169+
-----------
170+
Output array data type is :obj:`dpnp.int32`.
171+
Parameters ``n`` and ``p`` are supported as scalar.
172+
Otherwise, :obj:`numpy.random.binomial(n, p, size)` samples are drawn.
220173
221174
Examples
222175
--------
@@ -235,23 +188,20 @@ def binomial(n, p, size=None):
235188
"""
236189

237190
if not use_origin_backend(n) and dpnp_queue_is_cpu():
238-
if size is None:
239-
size = 1
240-
elif isinstance(size, tuple):
241-
for dim in size:
242-
if not isinstance(dim, int):
243-
checker_throw_value_error("binomial", "type(dim)", type(dim), int)
244-
elif not isinstance(size, int):
245-
checker_throw_value_error("binomial", "type(size)", type(size), int)
246-
247191
# TODO:
248192
# array_like of floats for `p` param
249-
if p > 1 or p < 0:
250-
checker_throw_value_error("binomial", "p", p, "in [0, 1]")
251-
if n < 0:
252-
checker_throw_value_error("binomial", "n", n, "non-negative")
253-
254-
return dpnp_binomial(int(n), p, size)
193+
if not dpnp.isscalar(n):
194+
pass
195+
elif not dpnp.isscalar(p):
196+
pass
197+
elif p > 1 or p < 0:
198+
pass
199+
elif n < 0:
200+
pass
201+
else:
202+
if size is None:
203+
size = 1
204+
return dpnp_binomial(int(n), p, size)
255205

256206
return call_origin(numpy.random.binomial, n, p, size)
257207

tests/test_random.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,19 @@ def test_binomial_check_extreme_value():
176176
assert numpy.unique(res)[0] == 5
177177

178178

179+
def test_binomial_invalid_args():
180+
size = 10
181+
n = -5 # non-negative `n` is expected
182+
p = 0.4 # OK
183+
with pytest.raises(ValueError):
184+
dpnp.random.binomial(n=n, p=p, size=size)
185+
186+
n = 5 # OK
187+
p = -0.5 # `p` is expected from [0, 1]
188+
with pytest.raises(ValueError):
189+
dpnp.random.binomial(n=n, p=p, size=size)
190+
191+
179192
def test_chisquare_seed():
180193
seed = 28041990
181194
size = 100

0 commit comments

Comments
 (0)