From 84c52d80b05510e10e730ce9c8c026aadb429594 Mon Sep 17 00:00:00 2001 From: Joseph Martinot-Lagarde Date: Tue, 8 Oct 2013 23:33:26 +0200 Subject: BUG: cov/corrcoef complex input and empty arrays This preserves the complex (and higher precision float or object) type of the input array, so that the complex covariance and correlation coefficients can be calculated. It also fixes the the behaviour of empty arrays. These will now either result in a 0x0 result, or a NxN result filled with NaNs. A warning is now issued when ddof is too large and the factor set to 0 so that in this case the result is always NaN or infinity/negative infinity and never a negative number. Closes gh-597 and gh-2680 Closes gh-3882 (original pull request) --- numpy/lib/tests/test_function_base.py | 52 +++++++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 5 deletions(-) (limited to 'numpy/lib/tests/test_function_base.py') diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index 8b239d7c0..f91ab8aa1 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -1189,19 +1189,61 @@ class TestCorrCoef(TestCase): assert_almost_equal(corrcoef(self.A, ddof=-1), self.res1) assert_almost_equal(corrcoef(self.A, self.B, ddof=-1), self.res2) + def test_complex(self): + x = np.array([[1, 2, 3], [1j, 2j, 3j]]) + assert_allclose(corrcoef(x), np.array([[1., -1.j], [1.j, 1.]])) + + def test_xy(self): + x = np.array([[1, 2, 3]]) + y = np.array([[1j, 2j, 3j]]) + assert_allclose(np.corrcoef(x, y), np.array([[1., -1.j], [1.j, 1.]])) + def test_empty(self): - assert_equal(corrcoef(np.array([])).size, 0) - assert_equal(corrcoef(np.array([]).reshape(0, 2)).shape, (0, 2)) + with warnings.catch_warnings(): + warnings.simplefilter('ignore', RuntimeWarning) + assert_array_equal(corrcoef(np.array([])), np.nan) + assert_array_equal(corrcoef(np.array([]).reshape(0, 2)), + np.array([]).reshape(0, 0)) + assert_array_equal(corrcoef(np.array([]).reshape(2, 0)), + np.array([[np.nan, np.nan], [np.nan, np.nan]])) + + def test_wrong_ddof(self): + x = np.array([[0, 2], [1, 1], [2, 0]]).T + with warnings.catch_warnings(): + warnings.simplefilter('ignore', RuntimeWarning) + assert_array_equal(corrcoef(x, ddof=5), + np.array([[np.nan, np.nan], [np.nan, np.nan]])) class TestCov(TestCase): def test_basic(self): x = np.array([[0, 2], [1, 1], [2, 0]]).T - assert_allclose(np.cov(x), np.array([[1., -1.], [-1., 1.]])) + assert_allclose(cov(x), np.array([[1., -1.], [-1., 1.]])) + + def test_complex(self): + x = np.array([[1, 2, 3], [1j, 2j, 3j]]) + assert_allclose(cov(x), np.array([[1., -1.j], [1.j, 1.]])) + + def test_xy(self): + x = np.array([[1, 2, 3]]) + y = np.array([[1j, 2j, 3j]]) + assert_allclose(cov(x, y), np.array([[1., -1.j], [1.j, 1.]])) def test_empty(self): - assert_equal(cov(np.array([])).size, 0) - assert_equal(cov(np.array([]).reshape(0, 2)).shape, (0, 2)) + with warnings.catch_warnings(): + warnings.simplefilter('ignore', RuntimeWarning) + assert_array_equal(cov(np.array([])), np.nan) + assert_array_equal(cov(np.array([]).reshape(0, 2)), + np.array([]).reshape(0, 0)) + assert_array_equal(cov(np.array([]).reshape(2, 0)), + np.array([[np.nan, np.nan], [np.nan, np.nan]])) + + def test_wrong_ddof(self): + x = np.array([[0, 2], [1, 1], [2, 0]]).T + with warnings.catch_warnings(): + warnings.simplefilter('ignore', RuntimeWarning) + assert_array_equal(cov(x, ddof=5), + np.array([[np.inf, -np.inf], [-np.inf, np.inf]])) class Test_I0(TestCase): -- cgit v1.2.1