diff options
author | Blake Griffith <blake.a.griffith@gmail.com> | 2015-03-26 17:31:27 -0500 |
---|---|---|
committer | Blake Griffith <blake.a.griffith@gmail.com> | 2015-05-19 16:21:18 -0500 |
commit | f555826ac776e0866e1edfc1804c88c2a23dab3b (patch) | |
tree | 9219f6d0b9cfeac089e0a05d1754a14b83a256f4 /numpy/random/tests/test_random.py | |
parent | fc57915afddad49f49611a623ed8360a3b44ed25 (diff) | |
download | numpy-f555826ac776e0866e1edfc1804c88c2a23dab3b.tar.gz |
TST: test multivariate_normal check_valid kw
Test that warnings are or are not raised when check_valid kwarg is used.
Diffstat (limited to 'numpy/random/tests/test_random.py')
-rw-r--r-- | numpy/random/tests/test_random.py | 40 |
1 files changed, 28 insertions, 12 deletions
diff --git a/numpy/random/tests/test_random.py b/numpy/random/tests/test_random.py index 596c218a2..f73991267 100644 --- a/numpy/random/tests/test_random.py +++ b/numpy/random/tests/test_random.py @@ -1,4 +1,5 @@ from __future__ import division, absolute_import, print_function +import warnings import numpy as np from numpy.testing import ( @@ -460,27 +461,42 @@ 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]] + cov = [[1, 0], [0, 1]] 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.463620246718631, 11.73759122771936 ], + [1.622445133300628, 9.771356667546383]], + [[2.154490787682787, 12.170324946056553], + [1.719909438201865, 9.230548443648306]], + [[0.689515026297799, 9.880729819607714], + [-0.023054015651998, 9.201096623542879]]]) + 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.]) + desired = np.array([0.895289569463708, 9.17180864067987]) np.testing.assert_array_almost_equal(actual, desired, decimal=15) - # Check that non positive-semidefinite covariance raises warning + # Check that non positive-semidefinite covariance warns with + # RuntimeWarning mean = [0, 0] - cov = [[1, 1 + 1e-10], [1 + 1e-10, 1]] - assert_warns(RuntimeWarning, np.random.multivariate_normal, mean, cov) + cov = [[1, 2], [3, 4]] + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter('always') + np.random.multivariate_normal(mean, cov) + assert len(w) == 1 + assert issubclass(w[0].category, RuntimeWarning) + + # and that it doesn't warn with RuntimeWarning check_valid='ignore' + with warnings.catch_warnings(record=True) as w: + warnings.simplefilter('always') + np.random.multivariate_normal(mean, cov, check_valid='ignore') + assert len(w) == 0 + + # and that it raises with RuntimeWarning check_valid='raises' + assert_raises(ValueError, np.random.multivariate_normal, mean, cov, + check_valid='raise') def test_negative_binomial(self): np.random.seed(self.seed) |