|
1 | 1 | # This file includes tests from numpy.random module: |
2 | 2 | # https://github.com/numpy/numpy/blob/main/numpy/random/tests/test_random.py |
3 | 3 |
|
| 4 | +import sys |
4 | 5 | import warnings |
5 | 6 |
|
6 | 7 | import numpy as np |
@@ -1128,3 +1129,178 @@ def test_three_arg_funcs(self): |
1128 | 1129 |
|
1129 | 1130 | out = func(argOne, argTwo[0], argThree) |
1130 | 1131 | assert_equal(out.shape, tgtShape) |
| 1132 | + |
| 1133 | + |
| 1134 | +class TestRegression: |
| 1135 | + |
| 1136 | + def test_VonMises_range(self): |
| 1137 | + # Make sure generated random variables are in [-pi, pi]. |
| 1138 | + # Regression test for ticket #986. |
| 1139 | + for mu in np.linspace(-7.0, 7.0, 5): |
| 1140 | + r = mkl_random.vonmises(mu, 1, 50) |
| 1141 | + assert_(np.all(r > -np.pi) and np.all(r <= np.pi)) |
| 1142 | + |
| 1143 | + def test_hypergeometric_range(self): |
| 1144 | + # Test for ticket #921 |
| 1145 | + assert_(np.all(mkl_random.hypergeometric(3, 18, 11, size=10) < 4)) |
| 1146 | + assert_(np.all(mkl_random.hypergeometric(18, 3, 11, size=10) > 0)) |
| 1147 | + |
| 1148 | + # Test for ticket #5623 |
| 1149 | + args = [ |
| 1150 | + (2**20 - 2, 2**20 - 2, 2**20 - 2), # Check for 32-bit systems |
| 1151 | + ] |
| 1152 | + for arg in args: |
| 1153 | + assert_(mkl_random.hypergeometric(*arg) > 0) |
| 1154 | + |
| 1155 | + def test_logseries_convergence(self): |
| 1156 | + # Test for ticket #923 |
| 1157 | + N = 1000 |
| 1158 | + mkl_random.seed(0) |
| 1159 | + rvsn = mkl_random.logseries(0.8, size=N) |
| 1160 | + # these two frequency counts should be close to theoretical |
| 1161 | + # numbers with this large sample |
| 1162 | + # theoretical large N result is 0.49706795 |
| 1163 | + freq = np.sum(rvsn == 1) / N |
| 1164 | + msg = f"Frequency was {freq:f}, should be > 0.45" |
| 1165 | + assert_(freq > 0.45, msg) |
| 1166 | + # theoretical large N result is 0.19882718 |
| 1167 | + freq = np.sum(rvsn == 2) / N |
| 1168 | + msg = f"Frequency was {freq:f}, should be < 0.23" |
| 1169 | + assert_(freq < 0.23, msg) |
| 1170 | + |
| 1171 | + def test_shuffle_mixed_dimension(self): |
| 1172 | + # Test for trac ticket #2074 |
| 1173 | + # only check that shuffle does not raise an error |
| 1174 | + for t in [ |
| 1175 | + [1, 2, 3, None], |
| 1176 | + [(1, 1), (2, 2), (3, 3), None], |
| 1177 | + [1, (2, 2), (3, 3), None], |
| 1178 | + [(1, 1), 2, 3, None], |
| 1179 | + ]: |
| 1180 | + shuffled = list(t) |
| 1181 | + mkl_random.shuffle(shuffled) |
| 1182 | + |
| 1183 | + def test_call_within_randomstate(self): |
| 1184 | + # Check that custom RandomState does not call into global state |
| 1185 | + m = mkl_random.RandomState() |
| 1186 | + m.seed(1234) |
| 1187 | + res = m.choice(10, size=10, p=np.ones(10) / 10.0) |
| 1188 | + for i in range(3): |
| 1189 | + mkl_random.seed(i) |
| 1190 | + m.seed(1234) |
| 1191 | + # If m.state is not honored, the result will change |
| 1192 | + assert_array_equal(m.choice(10, size=10, p=np.ones(10) / 10.0), res) |
| 1193 | + |
| 1194 | + def test_multivariate_normal_size_types(self): |
| 1195 | + # Test for multivariate_normal issue with 'size' argument. |
| 1196 | + # Check that the multivariate_normal size argument can be a |
| 1197 | + # numpy integer. |
| 1198 | + mkl_random.multivariate_normal([0], [[0]], size=1) |
| 1199 | + mkl_random.multivariate_normal([0], [[0]], size=np.int_(1)) |
| 1200 | + mkl_random.multivariate_normal([0], [[0]], size=np.int64(1)) |
| 1201 | + |
| 1202 | + def test_beta_small_parameters(self): |
| 1203 | + # Test that beta with small a and b parameters does not produce |
| 1204 | + # NaNs due to roundoff errors causing 0 / 0, gh-5851 |
| 1205 | + mkl_random.seed(1234567890) |
| 1206 | + x = mkl_random.beta(0.0001, 0.0001, size=100) |
| 1207 | + assert_(not np.any(np.isnan(x)), "Nans in mkl_random.beta") |
| 1208 | + |
| 1209 | + def test_choice_sum_of_probs_tolerance(self): |
| 1210 | + # The sum of probs should be 1.0 with some tolerance. |
| 1211 | + # For low precision dtypes the tolerance was too tight. |
| 1212 | + # See numpy github issue 6123. |
| 1213 | + mkl_random.seed(1234) |
| 1214 | + a = [1, 2, 3] |
| 1215 | + counts = [4, 4, 2] |
| 1216 | + for dt in np.float16, np.float32, np.float64: |
| 1217 | + probs = np.array(counts, dtype=dt) / sum(counts) |
| 1218 | + c = mkl_random.choice(a, p=probs) |
| 1219 | + assert_(c in a) |
| 1220 | + assert_raises(ValueError, mkl_random.choice, a, p=probs * 0.9) |
| 1221 | + |
| 1222 | + def test_shuffle_of_array_of_different_length_strings(self): |
| 1223 | + # Test that permuting an array of different length strings |
| 1224 | + # will not cause a segfault on garbage collection |
| 1225 | + # Tests gh-7710 |
| 1226 | + mkl_random.seed(1234) |
| 1227 | + |
| 1228 | + a = np.array(["a", "a" * 1000]) |
| 1229 | + |
| 1230 | + for _ in range(100): |
| 1231 | + mkl_random.shuffle(a) |
| 1232 | + |
| 1233 | + # Force Garbage Collection - should not segfault. |
| 1234 | + import gc |
| 1235 | + |
| 1236 | + gc.collect() |
| 1237 | + |
| 1238 | + def test_shuffle_of_array_of_objects(self): |
| 1239 | + # Test that permuting an array of objects will not cause |
| 1240 | + # a segfault on garbage collection. |
| 1241 | + # See gh-7719 |
| 1242 | + mkl_random.seed(1234) |
| 1243 | + a = np.array([np.arange(1), np.arange(4)], dtype=object) |
| 1244 | + |
| 1245 | + for _ in range(1000): |
| 1246 | + mkl_random.shuffle(a) |
| 1247 | + |
| 1248 | + # Force Garbage Collection - should not segfault. |
| 1249 | + import gc |
| 1250 | + |
| 1251 | + gc.collect() |
| 1252 | + |
| 1253 | + def test_permutation_subclass(self): |
| 1254 | + class N(np.ndarray): |
| 1255 | + pass |
| 1256 | + |
| 1257 | + rng = mkl_random.RandomState() |
| 1258 | + orig = np.arange(3).view(N) |
| 1259 | + rng.permutation(orig) |
| 1260 | + assert_array_equal(orig, np.arange(3).view(N)) |
| 1261 | + |
| 1262 | + class M: |
| 1263 | + a = np.arange(5) |
| 1264 | + |
| 1265 | + def __array__(self, dtype=None, copy=None): |
| 1266 | + return self.a |
| 1267 | + |
| 1268 | + m = M() |
| 1269 | + rng.permutation(m) |
| 1270 | + assert_array_equal(m.__array__(), np.arange(5)) |
| 1271 | + |
| 1272 | + def test_warns_byteorder(self): |
| 1273 | + # GH 13159 |
| 1274 | + other_byteord_dt = "<i4" if sys.byteorder == "big" else ">i4" |
| 1275 | + with pytest.deprecated_call(match="non-native byteorder is not"): |
| 1276 | + mkl_random.randint(0, 200, size=10, dtype=other_byteord_dt) |
| 1277 | + |
| 1278 | + def test_named_argument_initialization(self): |
| 1279 | + # GH 13669 |
| 1280 | + rs1 = mkl_random.RandomState(123456789) |
| 1281 | + rs2 = mkl_random.RandomState(seed=123456789) |
| 1282 | + assert rs1.randint(0, 100) == rs2.randint(0, 100) |
| 1283 | + |
| 1284 | + def test_choice_return_dtype(self): |
| 1285 | + # GH 9867, now long since the NumPy default changed. |
| 1286 | + c = mkl_random.choice(10, p=[0.1] * 10, size=2) |
| 1287 | + assert c.dtype == np.dtype(np.long) |
| 1288 | + c = mkl_random.choice(10, p=[0.1] * 10, replace=False, size=2) |
| 1289 | + assert c.dtype == np.dtype(np.long) |
| 1290 | + c = mkl_random.choice(10, size=2) |
| 1291 | + assert c.dtype == np.dtype(np.long) |
| 1292 | + c = mkl_random.choice(10, replace=False, size=2) |
| 1293 | + assert c.dtype == np.dtype(np.long) |
| 1294 | + |
| 1295 | + |
| 1296 | +def test_multinomial_empty(): |
| 1297 | + # gh-20483 |
| 1298 | + # Ensure that empty p-vals are correctly handled |
| 1299 | + assert mkl_random.multinomial(10, []).shape == (0,) |
| 1300 | + assert mkl_random.multinomial(3, [], size=(7, 5, 3)).shape == (7, 5, 3, 0) |
| 1301 | + |
| 1302 | + |
| 1303 | +def test_multinomial_1d_pval(): |
| 1304 | + # gh-20483 |
| 1305 | + with pytest.raises(TypeError, match="pvals must be a 1-d"): |
| 1306 | + mkl_random.multinomial(10, 0.3) |
0 commit comments