diff options
Diffstat (limited to 'numpy/random/tests')
-rw-r--r-- | numpy/random/tests/test_random.py | 161 |
1 files changed, 151 insertions, 10 deletions
diff --git a/numpy/random/tests/test_random.py b/numpy/random/tests/test_random.py index 4ae7cfeb1..b64c9d6cd 100644 --- a/numpy/random/tests/test_random.py +++ b/numpy/random/tests/test_random.py @@ -1,11 +1,41 @@ from __future__ import division, absolute_import, print_function -from numpy.testing import TestCase, run_module_suite, assert_,\ - assert_raises +import numpy as np +from numpy.testing import ( + TestCase, run_module_suite, assert_, assert_raises, assert_equal, + assert_warns) from numpy import random from numpy.compat import asbytes -import numpy as np +class TestSeed(TestCase): + def test_scalar(self): + s = np.random.RandomState(0) + assert_equal(s.randint(1000), 684) + s = np.random.RandomState(4294967295) + assert_equal(s.randint(1000), 419) + + def test_array(self): + s = np.random.RandomState(range(10)) + assert_equal(s.randint(1000), 468) + s = np.random.RandomState(np.arange(10)) + assert_equal(s.randint(1000), 468) + s = np.random.RandomState([0]) + assert_equal(s.randint(1000), 973) + s = np.random.RandomState([4294967295]) + assert_equal(s.randint(1000), 265) + + def test_invalid_scalar(self): + # seed must be a unsigned 32 bit integers + assert_raises(TypeError, np.random.RandomState, -0.5) + assert_raises(ValueError, np.random.RandomState, -1) + + def test_invalid_array(self): + # seed must be a unsigned 32 bit integers + assert_raises(TypeError, np.random.RandomState, [-0.5]) + assert_raises(ValueError, np.random.RandomState, [-1]) + assert_raises(ValueError, np.random.RandomState, [4294967296]) + assert_raises(ValueError, np.random.RandomState, [1, 2, 4294967296]) + assert_raises(ValueError, np.random.RandomState, [1, -2, 4294967296]) class TestBinomial(TestCase): def test_n_zero(self): @@ -17,6 +47,10 @@ class TestBinomial(TestCase): assert_(random.binomial(0, p) == 0) np.testing.assert_array_equal(random.binomial(zeros, p), zeros) + def test_p_is_nan(self): + # Issue #4571. + assert_raises(ValueError, random.binomial, 1, np.nan) + class TestMultinomial(TestCase): def test_basic(self): @@ -31,6 +65,20 @@ class TestMultinomial(TestCase): assert_(np.all(-5 <= x)) assert_(np.all(x < -1)) + def test_size(self): + # gh-3173 + p = [0.5, 0.5] + assert_equal(np.random.multinomial(1 ,p, np.uint32(1)).shape, (1, 2)) + assert_equal(np.random.multinomial(1 ,p, np.uint32(1)).shape, (1, 2)) + assert_equal(np.random.multinomial(1 ,p, np.uint32(1)).shape, (1, 2)) + assert_equal(np.random.multinomial(1 ,p, [2, 2]).shape, (2, 2, 2)) + assert_equal(np.random.multinomial(1 ,p, (2, 2)).shape, (2, 2, 2)) + assert_equal(np.random.multinomial(1 ,p, np.array((2, 2))).shape, + (2, 2, 2)) + + assert_raises(TypeError, np.random.multinomial, 1 , p, + np.float(1)) + class TestSetState(TestCase): def setUp(self): @@ -230,6 +278,29 @@ class TestRandomDist(TestCase): desired = conv([0, 1, 9, 6, 2, 4, 5, 8, 7, 3]) np.testing.assert_array_equal(actual, desired) + def test_shuffle_flexible(self): + # gh-4270 + arr = [(0, 1), (2, 3)] + dt = np.dtype([('a', np.int32, 1), ('b', np.int32, 1)]) + nparr = np.array(arr, dtype=dt) + a, b = nparr[0].copy(), nparr[1].copy() + for i in range(50): + np.random.shuffle(nparr) + assert_(a in nparr) + assert_(b in nparr) + + def test_shuffle_masked(self): + # gh-3263 + a = np.ma.masked_values(np.reshape(range(20), (5,4)) % 3 - 1, -1) + b = np.ma.masked_values(np.arange(20) % 3 - 1, -1) + ma = np.ma.count_masked(a) + mb = np.ma.count_masked(b) + for i in range(50): + np.random.shuffle(a) + self.assertEqual(ma, np.ma.count_masked(a)) + np.random.shuffle(b) + self.assertEqual(mb, np.ma.count_masked(b)) + def test_beta(self): np.random.seed(self.seed) actual = np.random.beta(.1, .9, size=(3, 2)) @@ -266,6 +337,18 @@ class TestRandomDist(TestCase): [ 0.56974431743975207, 0.43025568256024799]]]) np.testing.assert_array_almost_equal(actual, desired, decimal=15) + def test_dirichlet_size(self): + # gh-3173 + p = np.array([51.72840233779265162, 39.74494232180943953]) + assert_equal(np.random.dirichlet(p, np.uint32(1)).shape, (1, 2)) + assert_equal(np.random.dirichlet(p, np.uint32(1)).shape, (1, 2)) + assert_equal(np.random.dirichlet(p, np.uint32(1)).shape, (1, 2)) + assert_equal(np.random.dirichlet(p, [2, 2]).shape, (2, 2, 2)) + assert_equal(np.random.dirichlet(p, (2, 2)).shape, (2, 2, 2)) + assert_equal(np.random.dirichlet(p, np.array((2, 2))).shape, (2, 2, 2)) + + assert_raises(TypeError, np.random.dirichlet, p, np.float(1)) + def test_exponential(self): np.random.seed(self.seed) actual = np.random.exponential(1.1234, size=(3, 2)) @@ -378,20 +461,32 @@ class TestRandomDist(TestCase): def test_multivariate_normal(self): np.random.seed(self.seed) mean= (.123456789, 10) + # Hmm... not even symmetric. cov = [[1, 0], [1, 0]] size = (3, 2) actual = np.random.multivariate_normal(mean, cov, size) - desired = np.array([[[ -1.47027513018564449, 10. ], - [ -1.65915081534845532, 10. ]], - [[ -2.29186329304599745, 10. ], - [ -1.77505606019580053, 10. ]], - [[ -0.54970369430044119, 10. ], - [ 0.29768848031692957, 10. ]]]) + desired = np.array([[[-1.47027513018564449, 10.], + [-1.65915081534845532, 10.]], + [[-2.29186329304599745, 10.], + [-1.77505606019580053, 10.]], + [[-0.54970369430044119, 10.], + [ 0.29768848031692957, 10.]]]) + np.testing.assert_array_almost_equal(actual, desired, decimal=15) + + # Check for default size, was raising deprecation warning + actual = np.random.multivariate_normal(mean, cov) + desired = np.array([-0.79441224511977482, 10.]) np.testing.assert_array_almost_equal(actual, desired, decimal=15) + # Check that non positive-semidefinite covariance raises warning + mean= [0, 0] + cov = [[1, 1 + 1e-10], [1 + 1e-10, 1]] + rng = np.random.multivariate_normal + assert_warns(RuntimeWarning, np.random.multivariate_normal, mean, cov) + def test_negative_binomial(self): np.random.seed(self.seed) - actual = np.random.negative_binomial(n = 100, p = .12345, size = (3, 2)) + actual = np.random.negative_binomial(n=100, p=.12345, size=(3, 2)) desired = np.array([[848, 841], [892, 611], [779, 647]]) @@ -534,6 +629,12 @@ class TestRandomDist(TestCase): [ 1.19153771588353052, 1.83509849681825354]]) np.testing.assert_array_almost_equal(actual, desired, decimal=15) + def test_vonmises_small(self): + # check infinite loop, gh-4720 + np.random.seed(self.seed) + r = np.random.vonmises(mu=0., kappa=1.1e-8, size=10**6) + np.testing.assert_(np.isfinite(r).all()) + def test_wald(self): np.random.seed(self.seed) actual = np.random.wald(mean = 1.23, scale = 1.54, size = (3, 2)) @@ -558,5 +659,45 @@ class TestRandomDist(TestCase): [ 3, 13]]) np.testing.assert_array_equal(actual, desired) + +class TestThread: + """ make sure each state produces the same sequence even in threads """ + def setUp(self): + self.seeds = range(4) + + def check_function(self, function, sz): + from threading import Thread + + out1 = np.empty((len(self.seeds),) + sz) + out2 = np.empty((len(self.seeds),) + sz) + + # threaded generation + t = [Thread(target=function, args=(np.random.RandomState(s), o)) + for s, o in zip(self.seeds, out1)] + [x.start() for x in t] + [x.join() for x in t] + + # the same serial + for s, o in zip(self.seeds, out2): + function(np.random.RandomState(s), o) + + np.testing.assert_array_equal(out1, out2) + + def test_normal(self): + def gen_random(state, out): + out[...] = state.normal(size=10000) + self.check_function(gen_random, sz=(10000,)) + + def test_exp(self): + def gen_random(state, out): + out[...] = state.exponential(scale=np.ones((100, 1000))) + self.check_function(gen_random, sz=(100, 1000)) + + def test_multinomial(self): + def gen_random(state, out): + out[...] = state.multinomial(10, [1/6.]*6, size=10000) + self.check_function(gen_random, sz=(10000,6)) + + if __name__ == "__main__": run_module_suite() |