def test_random_integers(self): self._set_common_state() self._is_state_common() with suppress_warnings() as sup: sup.record(DeprecationWarning) compare_2_input(self.nprs.random_integers, self.rg.random_integers, is_scalar=True) self._is_state_common()
def test_multivariate_normal_method(seed, method): from numpy.random import MT19937 as NPMT19937 random = ExtendedGenerator(NPMT19937(seed)) mean = (0.123456789, 10) cov = [[1, 0], [0, 1]] size = (3, 2) actual = random.multivariate_normal(mean, cov, size, method=method) desired = np.array([ [ [-1.747478062846581, 11.25613495182354], [-0.9967333370066214, 10.342002097029821], ], [ [0.7850019631242964, 11.181113712443013], [0.8901349653255224, 8.873825399642492], ], [ [0.7130260107430003, 9.551628690083056], [0.7127098726541128, 11.991709234143173], ], ]) assert_array_almost_equal(actual, desired, decimal=15) # Check for default size, was raising deprecation warning actual = random.multivariate_normal(mean, cov, method=method) desired = np.array([0.233278563284287, 9.424140804347195]) assert_array_almost_equal(actual, desired, decimal=15) # Check path with scalar size works correctly scalar = random.multivariate_normal(mean, cov, 3, method=method) tuple1d = random.multivariate_normal(mean, cov, (3, ), method=method) assert scalar.shape == tuple1d.shape == (3, 2) # Check that non symmetric covariance input raises exception when # check_valid='raises' if using default svd method. mean = [0, 0] cov = [[1, 2], [1, 2]] assert_raises(ValueError, random.multivariate_normal, mean, cov, check_valid="raise") # Check that non positive-semidefinite covariance warns with # RuntimeWarning cov = [[1, 2], [2, 1]] assert_warns(RuntimeWarning, random.multivariate_normal, mean, cov) assert_warns(RuntimeWarning, random.multivariate_normal, mean, cov, method="eigh") assert_raises(LinAlgError, random.multivariate_normal, mean, cov, method="cholesky") # and that it doesn't warn with RuntimeWarning check_valid='ignore' assert_no_warnings(random.multivariate_normal, mean, cov, check_valid="ignore") # and that it raises with RuntimeWarning check_valid='raises' assert_raises(ValueError, random.multivariate_normal, mean, cov, check_valid="raise") assert_raises( ValueError, random.multivariate_normal, mean, cov, check_valid="raise", method="eigh", ) # check degenerate samples from singular covariance matrix cov = [[1, 1], [1, 1]] if method in ("svd", "eigh"): samples = random.multivariate_normal(mean, cov, size=(3, 2), method=method) assert_array_almost_equal(samples[..., 0], samples[..., 1], decimal=6) else: assert_raises(LinAlgError, random.multivariate_normal, mean, cov, method="cholesky") cov = np.array([[1, 0.1], [0.1, 1]], dtype=np.float32) with suppress_warnings() as sup: random.multivariate_normal(mean, cov, method=method) w = sup.record(RuntimeWarning) assert len(w) == 0 mu = np.zeros(2) cov = np.eye(2) assert random.multivariate_normal(mu, cov, size=3).shape == (3, 2) assert_raises(ValueError, random.multivariate_normal, mean, cov, check_valid="other") assert_raises(ValueError, random.multivariate_normal, np.zeros((2, 1, 1)), cov) assert_raises(ValueError, random.multivariate_normal, mu, np.empty((3, 2))) assert_raises(ValueError, random.multivariate_normal, mu, np.eye(3))
def test_random_integers(self): with suppress_warnings() as sup: sup.record(DeprecationWarning) vals = self.rg.random_integers(10, 20, 10) assert_(len(vals) == 10)
def test_multivariate_normal(seed): random.bit_generator.seed(seed) mean = (0.123456789, 10) cov = [[1, 0], [0, 1]] size = (3, 2) actual = random.multivariate_normal(mean, cov, size) desired = np.array([ [ [-3.34929721161096100, 9.891061435770858], [-0.12250896439641100, 9.295898449738300], ], [ [0.48355927611635563, 10.127832101772366], [3.11093021424924300, 10.283109168794352], ], [ [-0.20332082341774727, 9.868532121697195], [-1.33806889550667330, 9.813657233804179], ], ]) assert_array_almost_equal(actual, desired, decimal=15) # Check for default size, was raising deprecation warning actual = random.multivariate_normal(mean, cov) desired = np.array([-1.097443117192574, 10.535787051184261]) assert_array_almost_equal(actual, desired, decimal=15) # Check that non positive-semidefinite covariance warns with # RuntimeWarning mean = [0, 0] cov = [[1, 2], [2, 1]] assert_warns(RuntimeWarning, random.multivariate_normal, mean, cov) # and that it doesn"t warn with RuntimeWarning check_valid="ignore" assert_no_warnings(random.multivariate_normal, mean, cov, check_valid="ignore") # and that it raises with RuntimeWarning check_valid="raises" assert_raises(ValueError, random.multivariate_normal, mean, cov, check_valid="raise") cov = np.array([[1, 0.1], [0.1, 1]], dtype=np.float32) with suppress_warnings() as sup: random.multivariate_normal(mean, cov) w = sup.record(RuntimeWarning) assert len(w) == 0 mu = np.zeros(2) cov = np.eye(2) assert_raises(ValueError, random.multivariate_normal, mean, cov, check_valid="other") assert_raises(ValueError, random.multivariate_normal, np.zeros((2, 1, 1)), cov) assert_raises(ValueError, random.multivariate_normal, mu, np.empty((3, 2))) assert_raises(ValueError, random.multivariate_normal, mu, np.eye(3))