Skip to content
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

Nimare fix #8397

Closed
wants to merge 9 commits into from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion numba/cpython/randomimpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1782,6 +1782,13 @@ def multinomial_inner(n, pvals, out):
fl = out.flat
sz = out.size
plen = len(pvals)

psum = 0.0
for i in range(plen):
psum += pvals[i]
if psum > 1 + 1e-10:
raise AssertionError("np.random.multinomial(): Sum of probabilities "
"cannot exceed 1.")

for i in range(0, sz, plen):
# Loop body: take a set of n experiments and fill up
Expand All @@ -1797,7 +1804,7 @@ def multinomial_inner(n, pvals, out):
# distribution over the remaining number of experiments.
for j in range(0, plen - 1):
p_j = pvals[j]
n_j = fl[i + j] = np.random.binomial(n_experiments, p_j / p_sum)
n_j = fl[i + j] = np.random.binomial(n_experiments, min(p_j / p_sum, 1))
n_experiments -= n_j
if n_experiments <= 0:
# Note the output was initialized to zero
Expand Down
13 changes: 13 additions & 0 deletions numba/tests/test_random.py
Original file line number Diff line number Diff line change
Expand Up @@ -1407,6 +1407,19 @@ def test_multinomial_3_tuple(self):
for sample in res.reshape((-1, res.shape[-1])):
self._check_sample(n, pvals, sample)

def test_multinomial_4_floating_point(self):
"""
Test multinomial floating point
"""
cfunc = jit(nopython=True)(numpy_multinomial2)
# The following values _just_ exceed one in sum
n, pvals = 2, np.array([0.4166666666666667, 0.5833333333333334, 0.0])
res = cfunc(n, pvals)
self.assertEqual(len(res), 3)
self.assertEqual(res[2], 0)
self.assertLessEqual(res[0], 2)
self.assertLessEqual(res[1], 2)


class TestRandomDirichlet(BaseTest):
alpha = np.array([1, 1, 1, 2], dtype=np.float64)
Expand Down