GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — BUG: random: Fix generation of nan by dirichlet. · WarrenWeckesser/numpy@0b1d70e

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • numpy/random
    • _generator.pyx
    • tests
      • test_generator_mt19937.py
      • test_randomstate.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎numpy/random/_generator.pyx‎

Copy file name to clipboardExpand all lines: numpy/random/_generator.pyx
+11-1Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4394,7 +4394,7 @@ cdef class Generator:
43944394
# val = val.T
43954395
# return val
43964396

4397-
cdef np.npy_intp k, totsize, i, j
4397+
cdef np.npy_intp k, totsize, i, j, m
43984398
cdef np.ndarray alpha_arr, val_arr, alpha_csum_arr
43994399
cdef double csum
44004400
cdef double *alpha_data
@@ -4408,6 +4408,10 @@ cdef class Generator:
44084408
np.NPY_ARRAY_ALIGNED | np.NPY_ARRAY_C_CONTIGUOUS)
44094409
if np.any(np.less(alpha_arr, 0)):
44104410
raise ValueError('alpha < 0')
4411+
if k > 0 and np.sum(alpha_arr) == 0:
4412+
# Requiring k > 0 for this check means we allow alpha to be [].
4413+
# Each "random variate" in that case is also [].
4414+
raise ValueError('alpha must not be all zeros')
44114415
alpha_data = <double*>np.PyArray_DATA(alpha_arr)
44124416

44134417
if size is None:
@@ -4475,6 +4479,12 @@ cdef class Generator:
44754479
alpha_csum_data[j + 1])
44764480
val_data[i + j] = acc * v
44774481
acc *= (1. - v)
4482+
if alpha_csum_data[j + 1] == 0:
4483+
# In this case, v must be 1, so acc is now 0.
4484+
# Set all remaining elements of this sample to 0.
4485+
for m in range(j + 1, k - 2):
4486+
val_data[i + m] = 0
4487+
break
44784488
val_data[i + k - 1] = acc
44794489
i = i + k
44804490

Collapse file

‎numpy/random/tests/test_generator_mt19937.py‎

Copy file name to clipboardExpand all lines: numpy/random/tests/test_generator_mt19937.py
+23-2Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
},
3636
]
3737

38+
3839
@pytest.fixture(scope='module', params=[True, False])
3940
def endpoint(request):
4041
return request.param
@@ -145,6 +146,7 @@ def test_multinomial_pvals_float32(self):
145146
with pytest.raises(ValueError, match=match):
146147
random.multinomial(1, pvals)
147148

149+
148150
class TestMultivariateHypergeometric:
149151

150152
def setup_method(self):
@@ -1203,6 +1205,9 @@ def test_dirichlet_bad_alpha(self):
12031205
assert_raises(ValueError, random.dirichlet, [[[5], [1]], [[1], [5]]])
12041206
assert_raises(ValueError, random.dirichlet, np.array([[5, 1], [1, 5]]))
12051207

1208+
with pytest.raises(ValueError, match='must not be all zeros'):
1209+
random.dirichlet([0, 0, 0])
1210+
12061211
def test_dirichlet_alpha_non_contiguous(self):
12071212
a = np.array([51.72840233779265162, -1.0, 39.74494232180943953])
12081213
alpha = a[::2]
@@ -1238,6 +1243,23 @@ def test_dirichlet_moderately_small_alpha(self):
12381243
sample_mean = sample.mean(axis=0)
12391244
assert_allclose(sample_mean, exact_mean, rtol=1e-3)
12401245

1246+
# This set of parameters includes inputs with alpha.max() >= 0.1 and
1247+
# alpha.max() < 0.1 to exercise both generation methods within the
1248+
# dirichlet code.
1249+
@pytest.mark.parametrize(
1250+
'alpha',
1251+
[[5, 9, 0, 8],
1252+
[0.5, 0, 0, 0],
1253+
[1, 5, 0, 0, 1.5, 0, 0, 0],
1254+
[0.01, 0.03, 0, 0.005],
1255+
[1e-5, 0, 0, 0],
1256+
[0.002, 0.015, 0, 0, 0.04, 0, 0, 0]],
1257+
)
1258+
def test_dirichlet_multiple_zeros_in_alpha(self, alpha):
1259+
alpha = np.array(alpha)
1260+
y = random.dirichlet(alpha)
1261+
assert_equal(y[alpha == 0], 0.0)
1262+
12411263
def test_exponential(self):
12421264
random = Generator(MT19937(self.seed))
12431265
actual = random.exponential(1.1234, size=(3, 2))
@@ -1467,7 +1489,7 @@ def test_multivariate_normal(self, method):
14671489
mu, np.empty((3, 2)))
14681490
assert_raises(ValueError, random.multivariate_normal,
14691491
mu, np.eye(3))
1470-
1492+
14711493
@pytest.mark.parametrize('mean, cov', [([0], [[1+1j]]), ([0j], [[1]])])
14721494
def test_multivariate_normal_disallow_complex(self, mean, cov):
14731495
random = Generator(MT19937(self.seed))
@@ -1847,7 +1869,6 @@ class TestBroadcast:
18471869
def setup_method(self):
18481870
self.seed = 123456789
18491871

1850-
18511872
def test_uniform(self):
18521873
random = Generator(MT19937(self.seed))
18531874
low = [0]
Collapse file

‎numpy/random/tests/test_randomstate.py‎

Copy file name to clipboardExpand all lines: numpy/random/tests/test_randomstate.py
+1-4Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -812,10 +812,6 @@ def test_dirichlet_bad_alpha(self):
812812
alpha = np.array([5.4e-01, -1.0e-16])
813813
assert_raises(ValueError, random.dirichlet, alpha)
814814

815-
def test_dirichlet_zero_alpha(self):
816-
y = random.default_rng().dirichlet([5, 9, 0, 8])
817-
assert_equal(y[2], 0)
818-
819815
def test_dirichlet_alpha_non_contiguous(self):
820816
a = np.array([51.72840233779265162, -1.0, 39.74494232180943953])
821817
alpha = a[::2]
@@ -2061,6 +2057,7 @@ def test_randomstate_ctor_old_style_pickle():
20612057
assert_equal(state_a['has_gauss'], state_b['has_gauss'])
20622058
assert_equal(state_a['gauss'], state_b['gauss'])
20632059

2060+
20642061
def test_hot_swap(restore_singleton_bitgen):
20652062
# GH 21808
20662063
def_bg = np.random.default_rng(0)

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.