8000 BUG: check for probabilities containing nan-value in random.choice by nritsche · Pull Request #11264 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: check for probabilities containing nan-value in random.choice #11264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion numpy/random/mtrand/mtrand.pyx
< 8000 td class="blob-num blob-num-addition empty-cell">
Original file line number Diff line number Diff line change
Expand Up @@ -1142,7 +1142,9 @@ cdef class RandomState:
raise ValueError("a and p must have same size")
if np.logical_or.reduce(p < 0):
raise ValueError("probabilities are not non-negative")
if abs(kahan_sum(pix, d) - 1.) > atol:

# negated to handle NaNs in p
Copy link
Member
@eric-wieser eric-wieser Jun 8, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about it, this would make sense to do to the p < 0 above instead - that would make the warning dissappear too.

So if not np.all(p >= 0): raise ...

if not abs(kahan_sum(pix, d) - 1.) <= atol:
raise ValueError("probabilities do not sum to 1")

shape = size
Expand Down
2 changes: 2 additions & 0 deletions numpy/random/tests/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,8 @@ def test_choice_exceptions(self):
assert_raises(ValueError, sample, [1, 2, 3], 4, replace=False)
assert_raises(ValueError, sample, [1, 2, 3], 2,
replace=False, p=[1, 0, 0])
with np.errstate(invalid='ignore'):
assert_raises(ValueError, sample, [42, 2, 3], p=[None, None, None])

def test_choice_return_shape(self):
p = [0.1, 0.9]
Expand Down
0