GeezerVison is showing github.com — BUG: random: Fix generation of nan by dirichlet. · WarrenWeckesser/numpy@0b1d70e
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: numpy/random/_generator.pyx +11-1Lines changed: 11 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -4394,7 +4394,7 @@ cdef class Generator: | 4394 | 4394 | # val = val.T | 4395 | 4395 | # return val | 4396 | 4396 |
| 4397 | | - cdef np.npy_intp k, totsize, i, j | | 4397 | + cdef np.npy_intp k, totsize, i, j, m | 4398 | 4398 | cdef np.ndarray alpha_arr, val_arr, alpha_csum_arr | 4399 | 4399 | cdef double csum | 4400 | 4400 | cdef double *alpha_data | @@ -4408,6 +4408,10 @@ cdef class Generator: | 4408 | 4408 | np.NPY_ARRAY_ALIGNED | np.NPY_ARRAY_C_CONTIGUOUS) | 4409 | 4409 | if np.any(np.less(alpha_arr, 0)): | 4410 | 4410 | 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') | 4411 | 4415 | alpha_data = <double*>np.PyArray_DATA(alpha_arr) | 4412 | 4416 |
| 4413 | 4417 | if size is None: | @@ -4475,6 +4479,12 @@ cdef class Generator: | 4475 | 4479 | alpha_csum_data[j + 1]) | 4476 | 4480 | val_data[i + j] = acc * v | 4477 | 4481 | 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 | 4478 | 4488 | val_data[i + k - 1] = acc | 4479 | 4489 | i = i + k | 4480 | 4490 |
| |
Collapse file Copy file name to clipboardExpand all lines: numpy/random/tests/test_generator_mt19937.py +23-2Lines changed: 23 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 35 | 35 | }, | 36 | 36 | ] | 37 | 37 |
| | 38 | + | 38 | 39 | @pytest.fixture(scope='module', params=[True, False]) | 39 | 40 | def endpoint(request): | 40 | 41 | return request.param | @@ -145,6 +146,7 @@ def test_multinomial_pvals_float32(self): | 145 | 146 | with pytest.raises(ValueError, match=match): | 146 | 147 | random.multinomial(1, pvals) | 147 | 148 |
| | 149 | + | 148 | 150 | class TestMultivariateHypergeometric: | 149 | 151 |
| 150 | 152 | def setup_method(self): | @@ -1203,6 +1205,9 @@ def test_dirichlet_bad_alpha(self): | 1203 | 1205 | assert_raises(ValueError, random.dirichlet, [[[5], [1]], [[1], [5]]]) | 1204 | 1206 | assert_raises(ValueError, random.dirichlet, np.array([[5, 1], [1, 5]])) | 1205 | 1207 |
| | 1208 | + with pytest.raises(ValueError, match='must not be all zeros'): | | 1209 | + random.dirichlet([0, 0, 0]) | | 1210 | + | 1206 | 1211 | def test_dirichlet_alpha_non_contiguous(self): | 1207 | 1212 | a = np.array([51.72840233779265162, -1.0, 39.74494232180943953]) | 1208 | 1213 | alpha = a[::2] | @@ -1238,6 +1243,23 @@ def test_dirichlet_moderately_small_alpha(self): | 1238 | 1243 | sample_mean = sample.mean(axis=0) | 1239 | 1244 | assert_allclose(sample_mean, exact_mean, rtol=1e-3) | 1240 | 1245 |
| | 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 | + | 1241 | 1263 | def test_exponential(self): | 1242 | 1264 | random = Generator(MT19937(self.seed)) | 1243 | 1265 | actual = random.exponential(1.1234, size=(3, 2)) | @@ -1467,7 +1489,7 @@ def test_multivariate_normal(self, method): | 1467 | 1489 | mu, np.empty((3, 2))) | 1468 | 1490 | assert_raises(ValueError, random.multivariate_normal, | 1469 | 1491 | mu, np.eye(3)) | 1470 | | - | | 1492 | + | 1471 | 1493 | @pytest.mark.parametrize('mean, cov', [([0], [[1+1j]]), ([0j], [[1]])]) | 1472 | 1494 | def test_multivariate_normal_disallow_complex(self, mean, cov): | 1473 | 1495 | random = Generator(MT19937(self.seed)) | @@ -1847,7 +1869,6 @@ class TestBroadcast: | 1847 | 1869 | def setup_method(self): | 1848 | 1870 | self.seed = 123456789 | 1849 | 1871 |
| 1850 | | - | 1851 | 1872 | def test_uniform(self): | 1852 | 1873 | random = Generator(MT19937(self.seed)) | 1853 | 1874 | low = [0] | |
Collapse file Copy file name to clipboardExpand all lines: numpy/random/tests/test_randomstate.py +1-4Lines changed: 1 addition & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -812,10 +812,6 @@ def test_dirichlet_bad_alpha(self): | 812 | 812 | alpha = np.array([5.4e-01, -1.0e-16]) | 813 | 813 | assert_raises(ValueError, random.dirichlet, alpha) | 814 | 814 |
| 815 | | - def test_dirichlet_zero_alpha(self): | 816 | | - y = random.default_rng().dirichlet([5, 9, 0, 8]) | 817 | | - assert_equal(y[2], 0) | 818 | | - | 819 | 815 | def test_dirichlet_alpha_non_contiguous(self): | 820 | 816 | a = np.array([51.72840233779265162, -1.0, 39.74494232180943953]) | 821 | 817 | alpha = a[::2] | @@ -2061,6 +2057,7 @@ def test_randomstate_ctor_old_style_pickle(): | 2061 | 2057 | assert_equal(state_a['has_gauss'], state_b['has_gauss']) | 2062 | 2058 | assert_equal(state_a['gauss'], state_b['gauss']) | 2063 | 2059 |
| | 2060 | + | 2064 | 2061 | def test_hot_swap(restore_singleton_bitgen): | 2065 | 2062 | # GH 21808 | 2066 | 2063 | def_bg = np.random.default_rng(0) | |
|