GeezerVison is showing github.com — Merge pull request #24338 from charris/backport-24220 · numpy/numpy@baf6250
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 +18-11Lines changed: 18 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -4408,6 +4408,7 @@ 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 | + | 4411 | 4412 | alpha_data = <double*>np.PyArray_DATA(alpha_arr) | 4412 | 4413 |
| 4413 | 4414 | if size is None: | @@ -4467,17 +4468,23 @@ cdef class Generator: | 4467 | 4468 | csum += alpha_data[j] | 4468 | 4469 | alpha_csum_data[j] = csum | 4469 | 4470 |
| 4470 | | - with self.lock, nogil: | 4471 | | - while i < totsize: | 4472 | | - acc = 1. | 4473 | | - for j in range(k - 1): | 4474 | | - v = random_beta(&self._bitgen, alpha_data[j], | 4475 | | - alpha_csum_data[j + 1]) | 4476 | | - val_data[i + j] = acc * v | 4477 | | - acc *= (1. - v) | 4478 | | - val_data[i + k - 1] = acc | 4479 | | - i = i + k | 4480 | | - | | 4471 | + # If csum == 0, then all the values in alpha are 0, and there is | | 4472 | + # nothing to do, because diric was created with np.zeros(). | | 4473 | + if csum > 0: | | 4474 | + with self.lock, nogil: | | 4475 | + while i < totsize: | | 4476 | + acc = 1. | | 4477 | + for j in range(k - 1): | | 4478 | + v = random_beta(&self._bitgen, alpha_data[j], | | 4479 | + alpha_csum_data[j + 1]) | | 4480 | + val_data[i + j] = acc * v | | 4481 | + acc *= (1. - v) | | 4482 | + if alpha_csum_data[j + 1] == 0: | | 4483 | + # v must be 1, so acc is now 0. All | | 4484 | + # remaining elements will be left at 0. | | 4485 | + break | | 4486 | + val_data[i + k - 1] = acc | | 4487 | + i = i + k | 4481 | 4488 | else: | 4482 | 4489 | # Standard case: Unit normalisation of a vector of gamma random | 4483 | 4490 | # variates | |
Collapse file Copy file name to clipboardExpand all lines: numpy/random/tests/test_generator_mt19937.py +22-2Lines changed: 22 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): | @@ -1238,6 +1240,25 @@ def test_dirichlet_moderately_small_alpha(self): | 1238 | 1240 | sample_mean = sample.mean(axis=0) | 1239 | 1241 | assert_allclose(sample_mean, exact_mean, rtol=1e-3) | 1240 | 1242 |
| | 1243 | + # This set of parameters includes inputs with alpha.max() >= 0.1 and | | 1244 | + # alpha.max() < 0.1 to exercise both generation methods within the | | 1245 | + # dirichlet code. | | 1246 | + @pytest.mark.parametrize( | | 1247 | + 'alpha', | | 1248 | + [[5, 9, 0, 8], | | 1249 | + [0.5, 0, 0, 0], | | 1250 | + [1, 5, 0, 0, 1.5, 0, 0, 0], | | 1251 | + [0.01, 0.03, 0, 0.005], | | 1252 | + [1e-5, 0, 0, 0], | | 1253 | + [0.002, 0.015, 0, 0, 0.04, 0, 0, 0], | | 1254 | + [0.0], | | 1255 | + [0, 0, 0]], | | 1256 | + ) | | 1257 | + def test_dirichlet_multiple_zeros_in_alpha(self, alpha): | | 1258 | + alpha = np.array(alpha) | | 1259 | + y = random.dirichlet(alpha) | | 1260 | + assert_equal(y[alpha == 0], 0.0) | | 1261 | + | 1241 | 1262 | def test_exponential(self): | 1242 | 1263 | random = Generator(MT19937(self.seed)) | 1243 | 1264 | actual = random.exponential(1.1234, size=(3, 2)) | @@ -1467,7 +1488,7 @@ def test_multivariate_normal(self, method): | 1467 | 1488 | mu, np.empty((3, 2))) | 1468 | 1489 | assert_raises(ValueError, random.multivariate_normal, | 1469 | 1490 | mu, np.eye(3)) | 1470 | | - | | 1491 | + | 1471 | 1492 | @pytest.mark.parametrize('mean, cov', [([0], [[1+1j]]), ([0j], [[1]])]) | 1472 | 1493 | def test_multivariate_normal_disallow_complex(self, mean, cov): | 1473 | 1494 | random = Generator(MT19937(self.seed)) | @@ -1847,7 +1868,6 @@ class TestBroadcast: | 1847 | 1868 | def setup_method(self): | 1848 | 1869 | self.seed = 123456789 | 1849 | 1870 |
| 1850 | | - | 1851 | 1871 | def test_uniform(self): | 1852 | 1872 | random = Generator(MT19937(self.seed)) | 1853 | 1873 | 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) | |
|