Chris@87: from __future__ import division, absolute_import, print_function Chris@87: Chris@87: from numpy.testing import (TestCase, run_module_suite, assert_, Chris@87: assert_array_equal) Chris@87: from numpy import random Chris@87: from numpy.compat import long Chris@87: import numpy as np Chris@87: Chris@87: Chris@87: class TestRegression(TestCase): Chris@87: Chris@87: def test_VonMises_range(self): Chris@87: # Make sure generated random variables are in [-pi, pi]. Chris@87: # Regression test for ticket #986. Chris@87: for mu in np.linspace(-7., 7., 5): Chris@87: r = random.mtrand.vonmises(mu, 1, 50) Chris@87: assert_(np.all(r > -np.pi) and np.all(r <= np.pi)) Chris@87: Chris@87: def test_hypergeometric_range(self): Chris@87: # Test for ticket #921 Chris@87: assert_(np.all(np.random.hypergeometric(3, 18, 11, size=10) < 4)) Chris@87: assert_(np.all(np.random.hypergeometric(18, 3, 11, size=10) > 0)) Chris@87: Chris@87: def test_logseries_convergence(self): Chris@87: # Test for ticket #923 Chris@87: N = 1000 Chris@87: np.random.seed(0) Chris@87: rvsn = np.random.logseries(0.8, size=N) Chris@87: # these two frequency counts should be close to theoretical Chris@87: # numbers with this large sample Chris@87: # theoretical large N result is 0.49706795 Chris@87: freq = np.sum(rvsn == 1) / float(N) Chris@87: msg = "Frequency was %f, should be > 0.45" % freq Chris@87: assert_(freq > 0.45, msg) Chris@87: # theoretical large N result is 0.19882718 Chris@87: freq = np.sum(rvsn == 2) / float(N) Chris@87: msg = "Frequency was %f, should be < 0.23" % freq Chris@87: assert_(freq < 0.23, msg) Chris@87: Chris@87: def test_permutation_longs(self): Chris@87: np.random.seed(1234) Chris@87: a = np.random.permutation(12) Chris@87: np.random.seed(1234) Chris@87: b = np.random.permutation(long(12)) Chris@87: assert_array_equal(a, b) Chris@87: Chris@87: def test_randint_range(self): Chris@87: # Test for ticket #1690 Chris@87: lmax = np.iinfo('l').max Chris@87: lmin = np.iinfo('l').min Chris@87: try: Chris@87: random.randint(lmin, lmax) Chris@87: except: Chris@87: raise AssertionError Chris@87: Chris@87: def test_shuffle_mixed_dimension(self): Chris@87: # Test for trac ticket #2074 Chris@87: for t in [[1, 2, 3, None], Chris@87: [(1, 1), (2, 2), (3, 3), None], Chris@87: [1, (2, 2), (3, 3), None], Chris@87: [(1, 1), 2, 3, None]]: Chris@87: np.random.seed(12345) Chris@87: shuffled = list(t) Chris@87: random.shuffle(shuffled) Chris@87: assert_array_equal(shuffled, [t[0], t[3], t[1], t[2]]) Chris@87: Chris@87: def test_call_within_randomstate(self): Chris@87: # Check that custom RandomState does not call into global state Chris@87: m = np.random.RandomState() Chris@87: res = np.array([0, 8, 7, 2, 1, 9, 4, 7, 0, 3]) Chris@87: for i in range(3): Chris@87: np.random.seed(i) Chris@87: m.seed(4321) Chris@87: # If m.state is not honored, the result will change Chris@87: assert_array_equal(m.choice(10, size=10, p=np.ones(10)/10.), res) Chris@87: Chris@87: def test_multivariate_normal_size_types(self): Chris@87: # Test for multivariate_normal issue with 'size' argument. Chris@87: # Check that the multivariate_normal size argument can be a Chris@87: # numpy integer. Chris@87: np.random.multivariate_normal([0], [[0]], size=1) Chris@87: np.random.multivariate_normal([0], [[0]], size=np.int_(1)) Chris@87: np.random.multivariate_normal([0], [[0]], size=np.int64(1)) Chris@87: Chris@87: if __name__ == "__main__": Chris@87: run_module_suite()