diff options
author | Joseph Martinot-Lagarde <contrebasse@gmail.com> | 2013-10-08 23:33:26 +0200 |
---|---|---|
committer | Sebastian Berg <sebastian@sipsolutions.net> | 2013-10-16 20:54:21 +0200 |
commit | 84c52d80b05510e10e730ce9c8c026aadb429594 (patch) | |
tree | 26ce2b0445ce81bde4ad82a2e418de0ed102a800 /numpy/lib/tests/test_function_base.py | |
parent | 95a5219774818eb754053a8c45023e2e185642e7 (diff) | |
download | numpy-84c52d80b05510e10e730ce9c8c026aadb429594.tar.gz |
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)
Diffstat (limited to 'numpy/lib/tests/test_function_base.py')
-rw-r--r-- | numpy/lib/tests/test_function_base.py | 52 |
1 files changed, 47 insertions, 5 deletions
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): |