Mercurial > hg > vamp-build-and-test
comparison DEPENDENCIES/mingw32/Python27/Lib/site-packages/numpy/random/tests/test_random.py @ 87:2a2c65a20a8b
Add Python libs and headers
author | Chris Cannam |
---|---|
date | Wed, 25 Feb 2015 14:05:22 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
86:413a9d26189e | 87:2a2c65a20a8b |
---|---|
1 from __future__ import division, absolute_import, print_function | |
2 | |
3 import numpy as np | |
4 from numpy.testing import ( | |
5 TestCase, run_module_suite, assert_, assert_raises, assert_equal, | |
6 assert_warns) | |
7 from numpy import random | |
8 from numpy.compat import asbytes | |
9 import sys | |
10 | |
11 class TestSeed(TestCase): | |
12 def test_scalar(self): | |
13 s = np.random.RandomState(0) | |
14 assert_equal(s.randint(1000), 684) | |
15 s = np.random.RandomState(4294967295) | |
16 assert_equal(s.randint(1000), 419) | |
17 | |
18 def test_array(self): | |
19 s = np.random.RandomState(range(10)) | |
20 assert_equal(s.randint(1000), 468) | |
21 s = np.random.RandomState(np.arange(10)) | |
22 assert_equal(s.randint(1000), 468) | |
23 s = np.random.RandomState([0]) | |
24 assert_equal(s.randint(1000), 973) | |
25 s = np.random.RandomState([4294967295]) | |
26 assert_equal(s.randint(1000), 265) | |
27 | |
28 def test_invalid_scalar(self): | |
29 # seed must be a unsigned 32 bit integers | |
30 assert_raises(TypeError, np.random.RandomState, -0.5) | |
31 assert_raises(ValueError, np.random.RandomState, -1) | |
32 | |
33 def test_invalid_array(self): | |
34 # seed must be a unsigned 32 bit integers | |
35 assert_raises(TypeError, np.random.RandomState, [-0.5]) | |
36 assert_raises(ValueError, np.random.RandomState, [-1]) | |
37 assert_raises(ValueError, np.random.RandomState, [4294967296]) | |
38 assert_raises(ValueError, np.random.RandomState, [1, 2, 4294967296]) | |
39 assert_raises(ValueError, np.random.RandomState, [1, -2, 4294967296]) | |
40 | |
41 class TestBinomial(TestCase): | |
42 def test_n_zero(self): | |
43 # Tests the corner case of n == 0 for the binomial distribution. | |
44 # binomial(0, p) should be zero for any p in [0, 1]. | |
45 # This test addresses issue #3480. | |
46 zeros = np.zeros(2, dtype='int') | |
47 for p in [0, .5, 1]: | |
48 assert_(random.binomial(0, p) == 0) | |
49 np.testing.assert_array_equal(random.binomial(zeros, p), zeros) | |
50 | |
51 def test_p_is_nan(self): | |
52 # Issue #4571. | |
53 assert_raises(ValueError, random.binomial, 1, np.nan) | |
54 | |
55 | |
56 class TestMultinomial(TestCase): | |
57 def test_basic(self): | |
58 random.multinomial(100, [0.2, 0.8]) | |
59 | |
60 def test_zero_probability(self): | |
61 random.multinomial(100, [0.2, 0.8, 0.0, 0.0, 0.0]) | |
62 | |
63 def test_int_negative_interval(self): | |
64 assert_(-5 <= random.randint(-5, -1) < -1) | |
65 x = random.randint(-5, -1, 5) | |
66 assert_(np.all(-5 <= x)) | |
67 assert_(np.all(x < -1)) | |
68 | |
69 def test_size(self): | |
70 # gh-3173 | |
71 p = [0.5, 0.5] | |
72 assert_equal(np.random.multinomial(1, p, np.uint32(1)).shape, (1, 2)) | |
73 assert_equal(np.random.multinomial(1, p, np.uint32(1)).shape, (1, 2)) | |
74 assert_equal(np.random.multinomial(1, p, np.uint32(1)).shape, (1, 2)) | |
75 assert_equal(np.random.multinomial(1, p, [2, 2]).shape, (2, 2, 2)) | |
76 assert_equal(np.random.multinomial(1, p, (2, 2)).shape, (2, 2, 2)) | |
77 assert_equal(np.random.multinomial(1, p, np.array((2, 2))).shape, | |
78 (2, 2, 2)) | |
79 | |
80 assert_raises(TypeError, np.random.multinomial, 1, p, | |
81 np.float(1)) | |
82 | |
83 | |
84 class TestSetState(TestCase): | |
85 def setUp(self): | |
86 self.seed = 1234567890 | |
87 self.prng = random.RandomState(self.seed) | |
88 self.state = self.prng.get_state() | |
89 | |
90 def test_basic(self): | |
91 old = self.prng.tomaxint(16) | |
92 self.prng.set_state(self.state) | |
93 new = self.prng.tomaxint(16) | |
94 assert_(np.all(old == new)) | |
95 | |
96 def test_gaussian_reset(self): | |
97 # Make sure the cached every-other-Gaussian is reset. | |
98 old = self.prng.standard_normal(size=3) | |
99 self.prng.set_state(self.state) | |
100 new = self.prng.standard_normal(size=3) | |
101 assert_(np.all(old == new)) | |
102 | |
103 def test_gaussian_reset_in_media_res(self): | |
104 # When the state is saved with a cached Gaussian, make sure the | |
105 # cached Gaussian is restored. | |
106 | |
107 self.prng.standard_normal() | |
108 state = self.prng.get_state() | |
109 old = self.prng.standard_normal(size=3) | |
110 self.prng.set_state(state) | |
111 new = self.prng.standard_normal(size=3) | |
112 assert_(np.all(old == new)) | |
113 | |
114 def test_backwards_compatibility(self): | |
115 # Make sure we can accept old state tuples that do not have the | |
116 # cached Gaussian value. | |
117 old_state = self.state[:-2] | |
118 x1 = self.prng.standard_normal(size=16) | |
119 self.prng.set_state(old_state) | |
120 x2 = self.prng.standard_normal(size=16) | |
121 self.prng.set_state(self.state) | |
122 x3 = self.prng.standard_normal(size=16) | |
123 assert_(np.all(x1 == x2)) | |
124 assert_(np.all(x1 == x3)) | |
125 | |
126 def test_negative_binomial(self): | |
127 # Ensure that the negative binomial results take floating point | |
128 # arguments without truncation. | |
129 self.prng.negative_binomial(0.5, 0.5) | |
130 | |
131 class TestRandomDist(TestCase): | |
132 # Make sure the random distrobution return the correct value for a | |
133 # given seed | |
134 | |
135 def setUp(self): | |
136 self.seed = 1234567890 | |
137 | |
138 def test_rand(self): | |
139 np.random.seed(self.seed) | |
140 actual = np.random.rand(3, 2) | |
141 desired = np.array([[0.61879477158567997, 0.59162362775974664], | |
142 [0.88868358904449662, 0.89165480011560816], | |
143 [0.4575674820298663, 0.7781880808593471]]) | |
144 np.testing.assert_array_almost_equal(actual, desired, decimal=15) | |
145 | |
146 def test_randn(self): | |
147 np.random.seed(self.seed) | |
148 actual = np.random.randn(3, 2) | |
149 desired = np.array([[1.34016345771863121, 1.73759122771936081], | |
150 [1.498988344300628, -0.2286433324536169], | |
151 [2.031033998682787, 2.17032494605655257]]) | |
152 np.testing.assert_array_almost_equal(actual, desired, decimal=15) | |
153 | |
154 def test_randint(self): | |
155 np.random.seed(self.seed) | |
156 actual = np.random.randint(-99, 99, size=(3, 2)) | |
157 desired = np.array([[31, 3], | |
158 [-52, 41], | |
159 [-48, -66]]) | |
160 np.testing.assert_array_equal(actual, desired) | |
161 | |
162 def test_random_integers(self): | |
163 np.random.seed(self.seed) | |
164 actual = np.random.random_integers(-99, 99, size=(3, 2)) | |
165 desired = np.array([[31, 3], | |
166 [-52, 41], | |
167 [-48, -66]]) | |
168 np.testing.assert_array_equal(actual, desired) | |
169 | |
170 def test_random_sample(self): | |
171 np.random.seed(self.seed) | |
172 actual = np.random.random_sample((3, 2)) | |
173 desired = np.array([[0.61879477158567997, 0.59162362775974664], | |
174 [0.88868358904449662, 0.89165480011560816], | |
175 [0.4575674820298663, 0.7781880808593471]]) | |
176 np.testing.assert_array_almost_equal(actual, desired, decimal=15) | |
177 | |
178 def test_choice_uniform_replace(self): | |
179 np.random.seed(self.seed) | |
180 actual = np.random.choice(4, 4) | |
181 desired = np.array([2, 3, 2, 3]) | |
182 np.testing.assert_array_equal(actual, desired) | |
183 | |
184 def test_choice_nonuniform_replace(self): | |
185 np.random.seed(self.seed) | |
186 actual = np.random.choice(4, 4, p=[0.4, 0.4, 0.1, 0.1]) | |
187 desired = np.array([1, 1, 2, 2]) | |
188 np.testing.assert_array_equal(actual, desired) | |
189 | |
190 def test_choice_uniform_noreplace(self): | |
191 np.random.seed(self.seed) | |
192 actual = np.random.choice(4, 3, replace=False) | |
193 desired = np.array([0, 1, 3]) | |
194 np.testing.assert_array_equal(actual, desired) | |
195 | |
196 def test_choice_nonuniform_noreplace(self): | |
197 np.random.seed(self.seed) | |
198 actual = np.random.choice(4, 3, replace=False, | |
199 p=[0.1, 0.3, 0.5, 0.1]) | |
200 desired = np.array([2, 3, 1]) | |
201 np.testing.assert_array_equal(actual, desired) | |
202 | |
203 def test_choice_noninteger(self): | |
204 np.random.seed(self.seed) | |
205 actual = np.random.choice(['a', 'b', 'c', 'd'], 4) | |
206 desired = np.array(['c', 'd', 'c', 'd']) | |
207 np.testing.assert_array_equal(actual, desired) | |
208 | |
209 def test_choice_exceptions(self): | |
210 sample = np.random.choice | |
211 assert_raises(ValueError, sample, -1, 3) | |
212 assert_raises(ValueError, sample, 3., 3) | |
213 assert_raises(ValueError, sample, [[1, 2], [3, 4]], 3) | |
214 assert_raises(ValueError, sample, [], 3) | |
215 assert_raises(ValueError, sample, [1, 2, 3, 4], 3, | |
216 p=[[0.25, 0.25], [0.25, 0.25]]) | |
217 assert_raises(ValueError, sample, [1, 2], 3, p=[0.4, 0.4, 0.2]) | |
218 assert_raises(ValueError, sample, [1, 2], 3, p=[1.1, -0.1]) | |
219 assert_raises(ValueError, sample, [1, 2], 3, p=[0.4, 0.4]) | |
220 assert_raises(ValueError, sample, [1, 2, 3], 4, replace=False) | |
221 assert_raises(ValueError, sample, [1, 2, 3], 2, replace=False, | |
222 p=[1, 0, 0]) | |
223 | |
224 def test_choice_return_shape(self): | |
225 p = [0.1, 0.9] | |
226 # Check scalar | |
227 assert_(np.isscalar(np.random.choice(2, replace=True))) | |
228 assert_(np.isscalar(np.random.choice(2, replace=False))) | |
229 assert_(np.isscalar(np.random.choice(2, replace=True, p=p))) | |
230 assert_(np.isscalar(np.random.choice(2, replace=False, p=p))) | |
231 assert_(np.isscalar(np.random.choice([1, 2], replace=True))) | |
232 assert_(np.random.choice([None], replace=True) is None) | |
233 a = np.array([1, 2]) | |
234 arr = np.empty(1, dtype=object) | |
235 arr[0] = a | |
236 assert_(np.random.choice(arr, replace=True) is a) | |
237 | |
238 # Check 0-d array | |
239 s = tuple() | |
240 assert_(not np.isscalar(np.random.choice(2, s, replace=True))) | |
241 assert_(not np.isscalar(np.random.choice(2, s, replace=False))) | |
242 assert_(not np.isscalar(np.random.choice(2, s, replace=True, p=p))) | |
243 assert_(not np.isscalar(np.random.choice(2, s, replace=False, p=p))) | |
244 assert_(not np.isscalar(np.random.choice([1, 2], s, replace=True))) | |
245 assert_(np.random.choice([None], s, replace=True).ndim == 0) | |
246 a = np.array([1, 2]) | |
247 arr = np.empty(1, dtype=object) | |
248 arr[0] = a | |
249 assert_(np.random.choice(arr, s, replace=True).item() is a) | |
250 | |
251 # Check multi dimensional array | |
252 s = (2, 3) | |
253 p = [0.1, 0.1, 0.1, 0.1, 0.4, 0.2] | |
254 assert_(np.random.choice(6, s, replace=True).shape, s) | |
255 assert_(np.random.choice(6, s, replace=False).shape, s) | |
256 assert_(np.random.choice(6, s, replace=True, p=p).shape, s) | |
257 assert_(np.random.choice(6, s, replace=False, p=p).shape, s) | |
258 assert_(np.random.choice(np.arange(6), s, replace=True).shape, s) | |
259 | |
260 def test_bytes(self): | |
261 np.random.seed(self.seed) | |
262 actual = np.random.bytes(10) | |
263 desired = asbytes('\x82Ui\x9e\xff\x97+Wf\xa5') | |
264 np.testing.assert_equal(actual, desired) | |
265 | |
266 def test_shuffle(self): | |
267 # Test lists, arrays, and multidimensional versions of both: | |
268 for conv in [lambda x: x, | |
269 np.asarray, | |
270 lambda x: [(i, i) for i in x], | |
271 lambda x: np.asarray([(i, i) for i in x])]: | |
272 np.random.seed(self.seed) | |
273 alist = conv([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) | |
274 np.random.shuffle(alist) | |
275 actual = alist | |
276 desired = conv([0, 1, 9, 6, 2, 4, 5, 8, 7, 3]) | |
277 np.testing.assert_array_equal(actual, desired) | |
278 | |
279 def test_shuffle_flexible(self): | |
280 # gh-4270 | |
281 arr = [(0, 1), (2, 3)] | |
282 dt = np.dtype([('a', np.int32, 1), ('b', np.int32, 1)]) | |
283 nparr = np.array(arr, dtype=dt) | |
284 a, b = nparr[0].copy(), nparr[1].copy() | |
285 for i in range(50): | |
286 np.random.shuffle(nparr) | |
287 assert_(a in nparr) | |
288 assert_(b in nparr) | |
289 | |
290 def test_shuffle_masked(self): | |
291 # gh-3263 | |
292 a = np.ma.masked_values(np.reshape(range(20), (5,4)) % 3 - 1, -1) | |
293 b = np.ma.masked_values(np.arange(20) % 3 - 1, -1) | |
294 ma = np.ma.count_masked(a) | |
295 mb = np.ma.count_masked(b) | |
296 for i in range(50): | |
297 np.random.shuffle(a) | |
298 self.assertEqual(ma, np.ma.count_masked(a)) | |
299 np.random.shuffle(b) | |
300 self.assertEqual(mb, np.ma.count_masked(b)) | |
301 | |
302 def test_beta(self): | |
303 np.random.seed(self.seed) | |
304 actual = np.random.beta(.1, .9, size=(3, 2)) | |
305 desired = np.array( | |
306 [[1.45341850513746058e-02, 5.31297615662868145e-04], | |
307 [1.85366619058432324e-06, 4.19214516800110563e-03], | |
308 [1.58405155108498093e-04, 1.26252891949397652e-04]]) | |
309 np.testing.assert_array_almost_equal(actual, desired, decimal=15) | |
310 | |
311 def test_binomial(self): | |
312 np.random.seed(self.seed) | |
313 actual = np.random.binomial(100.123, .456, size=(3, 2)) | |
314 desired = np.array([[37, 43], | |
315 [42, 48], | |
316 [46, 45]]) | |
317 np.testing.assert_array_equal(actual, desired) | |
318 | |
319 def test_chisquare(self): | |
320 np.random.seed(self.seed) | |
321 actual = np.random.chisquare(50, size=(3, 2)) | |
322 desired = np.array([[63.87858175501090585, 68.68407748911370447], | |
323 [65.77116116901505904, 47.09686762438974483], | |
324 [72.3828403199695174, 74.18408615260374006]]) | |
325 np.testing.assert_array_almost_equal(actual, desired, decimal=13) | |
326 | |
327 def test_dirichlet(self): | |
328 np.random.seed(self.seed) | |
329 alpha = np.array([51.72840233779265162, 39.74494232180943953]) | |
330 actual = np.random.mtrand.dirichlet(alpha, size=(3, 2)) | |
331 desired = np.array([[[0.54539444573611562, 0.45460555426388438], | |
332 [0.62345816822039413, 0.37654183177960598]], | |
333 [[0.55206000085785778, 0.44793999914214233], | |
334 [0.58964023305154301, 0.41035976694845688]], | |
335 [[0.59266909280647828, 0.40733090719352177], | |
336 [0.56974431743975207, 0.43025568256024799]]]) | |
337 np.testing.assert_array_almost_equal(actual, desired, decimal=15) | |
338 | |
339 def test_dirichlet_size(self): | |
340 # gh-3173 | |
341 p = np.array([51.72840233779265162, 39.74494232180943953]) | |
342 assert_equal(np.random.dirichlet(p, np.uint32(1)).shape, (1, 2)) | |
343 assert_equal(np.random.dirichlet(p, np.uint32(1)).shape, (1, 2)) | |
344 assert_equal(np.random.dirichlet(p, np.uint32(1)).shape, (1, 2)) | |
345 assert_equal(np.random.dirichlet(p, [2, 2]).shape, (2, 2, 2)) | |
346 assert_equal(np.random.dirichlet(p, (2, 2)).shape, (2, 2, 2)) | |
347 assert_equal(np.random.dirichlet(p, np.array((2, 2))).shape, (2, 2, 2)) | |
348 | |
349 assert_raises(TypeError, np.random.dirichlet, p, np.float(1)) | |
350 | |
351 def test_exponential(self): | |
352 np.random.seed(self.seed) | |
353 actual = np.random.exponential(1.1234, size=(3, 2)) | |
354 desired = np.array([[1.08342649775011624, 1.00607889924557314], | |
355 [2.46628830085216721, 2.49668106809923884], | |
356 [0.68717433461363442, 1.69175666993575979]]) | |
357 np.testing.assert_array_almost_equal(actual, desired, decimal=15) | |
358 | |
359 def test_f(self): | |
360 np.random.seed(self.seed) | |
361 actual = np.random.f(12, 77, size=(3, 2)) | |
362 desired = np.array([[1.21975394418575878, 1.75135759791559775], | |
363 [1.44803115017146489, 1.22108959480396262], | |
364 [1.02176975757740629, 1.34431827623300415]]) | |
365 np.testing.assert_array_almost_equal(actual, desired, decimal=15) | |
366 | |
367 def test_gamma(self): | |
368 np.random.seed(self.seed) | |
369 actual = np.random.gamma(5, 3, size=(3, 2)) | |
370 desired = np.array([[24.60509188649287182, 28.54993563207210627], | |
371 [26.13476110204064184, 12.56988482927716078], | |
372 [31.71863275789960568, 33.30143302795922011]]) | |
373 np.testing.assert_array_almost_equal(actual, desired, decimal=14) | |
374 | |
375 def test_geometric(self): | |
376 np.random.seed(self.seed) | |
377 actual = np.random.geometric(.123456789, size=(3, 2)) | |
378 desired = np.array([[8, 7], | |
379 [17, 17], | |
380 [5, 12]]) | |
381 np.testing.assert_array_equal(actual, desired) | |
382 | |
383 def test_gumbel(self): | |
384 np.random.seed(self.seed) | |
385 actual = np.random.gumbel(loc=.123456789, scale=2.0, size=(3, 2)) | |
386 desired = np.array([[0.19591898743416816, 0.34405539668096674], | |
387 [-1.4492522252274278, -1.47374816298446865], | |
388 [1.10651090478803416, -0.69535848626236174]]) | |
389 np.testing.assert_array_almost_equal(actual, desired, decimal=15) | |
390 | |
391 def test_hypergeometric(self): | |
392 np.random.seed(self.seed) | |
393 actual = np.random.hypergeometric(10.1, 5.5, 14, size=(3, 2)) | |
394 desired = np.array([[10, 10], | |
395 [10, 10], | |
396 [9, 9]]) | |
397 np.testing.assert_array_equal(actual, desired) | |
398 | |
399 # Test nbad = 0 | |
400 actual = np.random.hypergeometric(5, 0, 3, size=4) | |
401 desired = np.array([3, 3, 3, 3]) | |
402 np.testing.assert_array_equal(actual, desired) | |
403 | |
404 actual = np.random.hypergeometric(15, 0, 12, size=4) | |
405 desired = np.array([12, 12, 12, 12]) | |
406 np.testing.assert_array_equal(actual, desired) | |
407 | |
408 # Test ngood = 0 | |
409 actual = np.random.hypergeometric(0, 5, 3, size=4) | |
410 desired = np.array([0, 0, 0, 0]) | |
411 np.testing.assert_array_equal(actual, desired) | |
412 | |
413 actual = np.random.hypergeometric(0, 15, 12, size=4) | |
414 desired = np.array([0, 0, 0, 0]) | |
415 np.testing.assert_array_equal(actual, desired) | |
416 | |
417 def test_laplace(self): | |
418 np.random.seed(self.seed) | |
419 actual = np.random.laplace(loc=.123456789, scale=2.0, size=(3, 2)) | |
420 desired = np.array([[0.66599721112760157, 0.52829452552221945], | |
421 [3.12791959514407125, 3.18202813572992005], | |
422 [-0.05391065675859356, 1.74901336242837324]]) | |
423 np.testing.assert_array_almost_equal(actual, desired, decimal=15) | |
424 | |
425 def test_logistic(self): | |
426 np.random.seed(self.seed) | |
427 actual = np.random.logistic(loc=.123456789, scale=2.0, size=(3, 2)) | |
428 desired = np.array([[1.09232835305011444, 0.8648196662399954], | |
429 [4.27818590694950185, 4.33897006346929714], | |
430 [-0.21682183359214885, 2.63373365386060332]]) | |
431 np.testing.assert_array_almost_equal(actual, desired, decimal=15) | |
432 | |
433 def test_lognormal(self): | |
434 np.random.seed(self.seed) | |
435 actual = np.random.lognormal(mean=.123456789, sigma=2.0, size=(3, 2)) | |
436 desired = np.array([[16.50698631688883822, 36.54846706092654784], | |
437 [22.67886599981281748, 0.71617561058995771], | |
438 [65.72798501792723869, 86.84341601437161273]]) | |
439 np.testing.assert_array_almost_equal(actual, desired, decimal=13) | |
440 | |
441 def test_logseries(self): | |
442 np.random.seed(self.seed) | |
443 actual = np.random.logseries(p=.923456789, size=(3, 2)) | |
444 desired = np.array([[2, 2], | |
445 [6, 17], | |
446 [3, 6]]) | |
447 np.testing.assert_array_equal(actual, desired) | |
448 | |
449 def test_multinomial(self): | |
450 np.random.seed(self.seed) | |
451 actual = np.random.multinomial(20, [1/6.]*6, size=(3, 2)) | |
452 desired = np.array([[[4, 3, 5, 4, 2, 2], | |
453 [5, 2, 8, 2, 2, 1]], | |
454 [[3, 4, 3, 6, 0, 4], | |
455 [2, 1, 4, 3, 6, 4]], | |
456 [[4, 4, 2, 5, 2, 3], | |
457 [4, 3, 4, 2, 3, 4]]]) | |
458 np.testing.assert_array_equal(actual, desired) | |
459 | |
460 def test_multivariate_normal(self): | |
461 np.random.seed(self.seed) | |
462 mean = (.123456789, 10) | |
463 # Hmm... not even symmetric. | |
464 cov = [[1, 0], [1, 0]] | |
465 size = (3, 2) | |
466 actual = np.random.multivariate_normal(mean, cov, size) | |
467 desired = np.array([[[-1.47027513018564449, 10.], | |
468 [-1.65915081534845532, 10.]], | |
469 [[-2.29186329304599745, 10.], | |
470 [-1.77505606019580053, 10.]], | |
471 [[-0.54970369430044119, 10.], | |
472 [0.29768848031692957, 10.]]]) | |
473 np.testing.assert_array_almost_equal(actual, desired, decimal=15) | |
474 | |
475 # Check for default size, was raising deprecation warning | |
476 actual = np.random.multivariate_normal(mean, cov) | |
477 desired = np.array([-0.79441224511977482, 10.]) | |
478 np.testing.assert_array_almost_equal(actual, desired, decimal=15) | |
479 | |
480 # Check that non positive-semidefinite covariance raises warning | |
481 mean = [0, 0] | |
482 cov = [[1, 1 + 1e-10], [1 + 1e-10, 1]] | |
483 assert_warns(RuntimeWarning, np.random.multivariate_normal, mean, cov) | |
484 | |
485 def test_negative_binomial(self): | |
486 np.random.seed(self.seed) | |
487 actual = np.random.negative_binomial(n=100, p=.12345, size=(3, 2)) | |
488 desired = np.array([[848, 841], | |
489 [892, 611], | |
490 [779, 647]]) | |
491 np.testing.assert_array_equal(actual, desired) | |
492 | |
493 def test_noncentral_chisquare(self): | |
494 np.random.seed(self.seed) | |
495 actual = np.random.noncentral_chisquare(df=5, nonc=5, size=(3, 2)) | |
496 desired = np.array([[23.91905354498517511, 13.35324692733826346], | |
497 [31.22452661329736401, 16.60047399466177254], | |
498 [5.03461598262724586, 17.94973089023519464]]) | |
499 np.testing.assert_array_almost_equal(actual, desired, decimal=14) | |
500 | |
501 def test_noncentral_f(self): | |
502 np.random.seed(self.seed) | |
503 actual = np.random.noncentral_f(dfnum=5, dfden=2, nonc=1, | |
504 size=(3, 2)) | |
505 desired = np.array([[1.40598099674926669, 0.34207973179285761], | |
506 [3.57715069265772545, 7.92632662577829805], | |
507 [0.43741599463544162, 1.1774208752428319]]) | |
508 np.testing.assert_array_almost_equal(actual, desired, decimal=14) | |
509 | |
510 def test_normal(self): | |
511 np.random.seed(self.seed) | |
512 actual = np.random.normal(loc=.123456789, scale=2.0, size=(3, 2)) | |
513 desired = np.array([[2.80378370443726244, 3.59863924443872163], | |
514 [3.121433477601256, -0.33382987590723379], | |
515 [4.18552478636557357, 4.46410668111310471]]) | |
516 np.testing.assert_array_almost_equal(actual, desired, decimal=15) | |
517 | |
518 def test_pareto(self): | |
519 np.random.seed(self.seed) | |
520 actual = np.random.pareto(a=.123456789, size=(3, 2)) | |
521 desired = np.array( | |
522 [[2.46852460439034849e+03, 1.41286880810518346e+03], | |
523 [5.28287797029485181e+07, 6.57720981047328785e+07], | |
524 [1.40840323350391515e+02, 1.98390255135251704e+05]]) | |
525 # For some reason on 32-bit x86 Ubuntu 12.10 the [1, 0] entry in this | |
526 # matrix differs by 24 nulps. Discussion: | |
527 # http://mail.scipy.org/pipermail/numpy-discussion/2012-September/063801.html | |
528 # Consensus is that this is probably some gcc quirk that affects | |
529 # rounding but not in any important way, so we just use a looser | |
530 # tolerance on this test: | |
531 np.testing.assert_array_almost_equal_nulp(actual, desired, nulp=30) | |
532 | |
533 def test_poisson(self): | |
534 np.random.seed(self.seed) | |
535 actual = np.random.poisson(lam=.123456789, size=(3, 2)) | |
536 desired = np.array([[0, 0], | |
537 [1, 0], | |
538 [0, 0]]) | |
539 np.testing.assert_array_equal(actual, desired) | |
540 | |
541 def test_poisson_exceptions(self): | |
542 lambig = np.iinfo('l').max | |
543 lamneg = -1 | |
544 assert_raises(ValueError, np.random.poisson, lamneg) | |
545 assert_raises(ValueError, np.random.poisson, [lamneg]*10) | |
546 assert_raises(ValueError, np.random.poisson, lambig) | |
547 assert_raises(ValueError, np.random.poisson, [lambig]*10) | |
548 | |
549 def test_power(self): | |
550 np.random.seed(self.seed) | |
551 actual = np.random.power(a=.123456789, size=(3, 2)) | |
552 desired = np.array([[0.02048932883240791, 0.01424192241128213], | |
553 [0.38446073748535298, 0.39499689943484395], | |
554 [0.00177699707563439, 0.13115505880863756]]) | |
555 np.testing.assert_array_almost_equal(actual, desired, decimal=15) | |
556 | |
557 def test_rayleigh(self): | |
558 np.random.seed(self.seed) | |
559 actual = np.random.rayleigh(scale=10, size=(3, 2)) | |
560 desired = np.array([[13.8882496494248393, 13.383318339044731], | |
561 [20.95413364294492098, 21.08285015800712614], | |
562 [11.06066537006854311, 17.35468505778271009]]) | |
563 np.testing.assert_array_almost_equal(actual, desired, decimal=14) | |
564 | |
565 def test_standard_cauchy(self): | |
566 np.random.seed(self.seed) | |
567 actual = np.random.standard_cauchy(size=(3, 2)) | |
568 desired = np.array([[0.77127660196445336, -6.55601161955910605], | |
569 [0.93582023391158309, -2.07479293013759447], | |
570 [-4.74601644297011926, 0.18338989290760804]]) | |
571 np.testing.assert_array_almost_equal(actual, desired, decimal=15) | |
572 | |
573 def test_standard_exponential(self): | |
574 np.random.seed(self.seed) | |
575 actual = np.random.standard_exponential(size=(3, 2)) | |
576 desired = np.array([[0.96441739162374596, 0.89556604882105506], | |
577 [2.1953785836319808, 2.22243285392490542], | |
578 [0.6116915921431676, 1.50592546727413201]]) | |
579 np.testing.assert_array_almost_equal(actual, desired, decimal=15) | |
580 | |
581 def test_standard_gamma(self): | |
582 np.random.seed(self.seed) | |
583 actual = np.random.standard_gamma(shape=3, size=(3, 2)) | |
584 desired = np.array([[5.50841531318455058, 6.62953470301903103], | |
585 [5.93988484943779227, 2.31044849402133989], | |
586 [7.54838614231317084, 8.012756093271868]]) | |
587 np.testing.assert_array_almost_equal(actual, desired, decimal=14) | |
588 | |
589 def test_standard_normal(self): | |
590 np.random.seed(self.seed) | |
591 actual = np.random.standard_normal(size=(3, 2)) | |
592 desired = np.array([[1.34016345771863121, 1.73759122771936081], | |
593 [1.498988344300628, -0.2286433324536169], | |
594 [2.031033998682787, 2.17032494605655257]]) | |
595 np.testing.assert_array_almost_equal(actual, desired, decimal=15) | |
596 | |
597 def test_standard_t(self): | |
598 np.random.seed(self.seed) | |
599 actual = np.random.standard_t(df=10, size=(3, 2)) | |
600 desired = np.array([[0.97140611862659965, -0.08830486548450577], | |
601 [1.36311143689505321, -0.55317463909867071], | |
602 [-0.18473749069684214, 0.61181537341755321]]) | |
603 np.testing.assert_array_almost_equal(actual, desired, decimal=15) | |
604 | |
605 def test_triangular(self): | |
606 np.random.seed(self.seed) | |
607 actual = np.random.triangular(left=5.12, mode=10.23, right=20.34, | |
608 size=(3, 2)) | |
609 desired = np.array([[12.68117178949215784, 12.4129206149193152], | |
610 [16.20131377335158263, 16.25692138747600524], | |
611 [11.20400690911820263, 14.4978144835829923]]) | |
612 np.testing.assert_array_almost_equal(actual, desired, decimal=14) | |
613 | |
614 def test_uniform(self): | |
615 np.random.seed(self.seed) | |
616 actual = np.random.uniform(low=1.23, high=10.54, size=(3, 2)) | |
617 desired = np.array([[6.99097932346268003, 6.73801597444323974], | |
618 [9.50364421400426274, 9.53130618907631089], | |
619 [5.48995325769805476, 8.47493103280052118]]) | |
620 np.testing.assert_array_almost_equal(actual, desired, decimal=15) | |
621 | |
622 def test_vonmises(self): | |
623 np.random.seed(self.seed) | |
624 actual = np.random.vonmises(mu=1.23, kappa=1.54, size=(3, 2)) | |
625 desired = np.array([[2.28567572673902042, 2.89163838442285037], | |
626 [0.38198375564286025, 2.57638023113890746], | |
627 [1.19153771588353052, 1.83509849681825354]]) | |
628 np.testing.assert_array_almost_equal(actual, desired, decimal=15) | |
629 | |
630 def test_vonmises_small(self): | |
631 # check infinite loop, gh-4720 | |
632 np.random.seed(self.seed) | |
633 r = np.random.vonmises(mu=0., kappa=1.1e-8, size=10**6) | |
634 np.testing.assert_(np.isfinite(r).all()) | |
635 | |
636 def test_wald(self): | |
637 np.random.seed(self.seed) | |
638 actual = np.random.wald(mean=1.23, scale=1.54, size=(3, 2)) | |
639 desired = np.array([[3.82935265715889983, 5.13125249184285526], | |
640 [0.35045403618358717, 1.50832396872003538], | |
641 [0.24124319895843183, 0.22031101461955038]]) | |
642 np.testing.assert_array_almost_equal(actual, desired, decimal=14) | |
643 | |
644 def test_weibull(self): | |
645 np.random.seed(self.seed) | |
646 actual = np.random.weibull(a=1.23, size=(3, 2)) | |
647 desired = np.array([[0.97097342648766727, 0.91422896443565516], | |
648 [1.89517770034962929, 1.91414357960479564], | |
649 [0.67057783752390987, 1.39494046635066793]]) | |
650 np.testing.assert_array_almost_equal(actual, desired, decimal=15) | |
651 | |
652 def test_zipf(self): | |
653 np.random.seed(self.seed) | |
654 actual = np.random.zipf(a=1.23, size=(3, 2)) | |
655 desired = np.array([[66, 29], | |
656 [1, 1], | |
657 [3, 13]]) | |
658 np.testing.assert_array_equal(actual, desired) | |
659 | |
660 | |
661 class TestThread(object): | |
662 # make sure each state produces the same sequence even in threads | |
663 def setUp(self): | |
664 self.seeds = range(4) | |
665 | |
666 def check_function(self, function, sz): | |
667 from threading import Thread | |
668 | |
669 out1 = np.empty((len(self.seeds),) + sz) | |
670 out2 = np.empty((len(self.seeds),) + sz) | |
671 | |
672 # threaded generation | |
673 t = [Thread(target=function, args=(np.random.RandomState(s), o)) | |
674 for s, o in zip(self.seeds, out1)] | |
675 [x.start() for x in t] | |
676 [x.join() for x in t] | |
677 | |
678 # the same serial | |
679 for s, o in zip(self.seeds, out2): | |
680 function(np.random.RandomState(s), o) | |
681 | |
682 # these platforms change x87 fpu precision mode in threads | |
683 if (np.intp().dtype.itemsize == 4 and | |
684 (sys.platform == "win32" or | |
685 sys.platform.startswith("gnukfreebsd"))): | |
686 np.testing.assert_array_almost_equal(out1, out2) | |
687 else: | |
688 np.testing.assert_array_equal(out1, out2) | |
689 | |
690 def test_normal(self): | |
691 def gen_random(state, out): | |
692 out[...] = state.normal(size=10000) | |
693 self.check_function(gen_random, sz=(10000,)) | |
694 | |
695 def test_exp(self): | |
696 def gen_random(state, out): | |
697 out[...] = state.exponential(scale=np.ones((100, 1000))) | |
698 self.check_function(gen_random, sz=(100, 1000)) | |
699 | |
700 def test_multinomial(self): | |
701 def gen_random(state, out): | |
702 out[...] = state.multinomial(10, [1/6.]*6, size=10000) | |
703 self.check_function(gen_random, sz=(10000,6)) | |
704 | |
705 | |
706 if __name__ == "__main__": | |
707 run_module_suite() |