diff options
author | Matthew Brett <matthew.brett@gmail.com> | 2015-03-15 11:34:58 -0700 |
---|---|---|
committer | Matthew Brett <matthew.brett@gmail.com> | 2015-03-15 11:45:18 -0700 |
commit | 594c64cedc3e521bc223fb90f29f22b034de3839 (patch) | |
tree | 3b1791109f3a70c0cc68855fe4f5bbe37477e831 /numpy/lib | |
parent | 4b4d8510739c9ea7a80391e2656d84a3c5e4a9c3 (diff) | |
download | numpy-594c64cedc3e521bc223fb90f29f22b034de3839.tar.gz |
ENH: deprecate bias and ddof arguments to corrcoef
The bias and ddof arguments had no effect on the calculation of the
correlation coefficient because the value cancels in the calculation.
Deprecate these arguments to np.corrcoef and np.ma.corrcoef.
Diffstat (limited to 'numpy/lib')
-rw-r--r-- | numpy/lib/function_base.py | 39 | ||||
-rw-r--r-- | numpy/lib/tests/test_function_base.py | 38 |
2 files changed, 50 insertions, 27 deletions
diff --git a/numpy/lib/function_base.py b/numpy/lib/function_base.py index d58492a67..9aec98cc8 100644 --- a/numpy/lib/function_base.py +++ b/numpy/lib/function_base.py @@ -1949,17 +1949,17 @@ def cov(m, y=None, rowvar=1, bias=0, ddof=None): return (dot(X, X.T.conj()) / fact).squeeze() -def corrcoef(x, y=None, rowvar=1, bias=0, ddof=None): +def corrcoef(x, y=None, rowvar=1, bias=np._NoValue, ddof=np._NoValue): """ - Return correlation coefficients. + Return Pearson product-moment correlation coefficients. Please refer to the documentation for `cov` for more detail. The - relationship between the correlation coefficient matrix, `P`, and the + relationship between the correlation coefficient matrix, `R`, and the covariance matrix, `C`, is - .. math:: P_{ij} = \\frac{ C_{ij} } { \\sqrt{ C_{ii} * C_{jj} } } + .. math:: R_{ij} = \\frac{ C_{ij} } { \\sqrt{ C_{ii} * C_{jj} } } - The values of `P` are between -1 and 1, inclusive. + The values of `R` are between -1 and 1, inclusive. Parameters ---------- @@ -1975,28 +1975,33 @@ def corrcoef(x, y=None, rowvar=1, bias=0, ddof=None): variable, with observations in the columns. Otherwise, the relationship is transposed: each column represents a variable, while the rows contain observations. - bias : int, optional - Default normalization is by ``(N - 1)``, where ``N`` is the number of - observations (unbiased estimate). If `bias` is 1, then - normalization is by ``N``. These values can be overridden by using - the keyword ``ddof`` in numpy versions >= 1.5. - ddof : int, optional - .. versionadded:: 1.5 - If not ``None`` normalization is by ``(N - ddof)``, where ``N`` is - the number of observations; this overrides the value implied by - ``bias``. The default value is ``None``. + bias : _NoValue, optional + .. deprecated:: 1.10.0 + Has no affect, do not use. + ddof : _NoValue, optional + .. deprecated:: 1.10.0 + Has no affect, do not use. Returns ------- - out : ndarray + R : ndarray The correlation coefficient matrix of the variables. See Also -------- cov : Covariance matrix + Notes + ----- + This function accepts but discards arguments `bias` and `ddof`. This is + for backwards compatibility with previous versions of this function. These + arguments had no effect on the return values of the function and can be + safely ignored in this and previous versions of numpy. """ - c = cov(x, y, rowvar, bias, ddof) + if bias is not np._NoValue or ddof is not np._NoValue: + warnings.warn('bias and ddof have no affect and are deprecated', + DeprecationWarning) + c = cov(x, y, rowvar) try: d = diag(c) except ValueError: # scalar covariance diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index a37c527d9..cf9fcf5e2 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -8,8 +8,9 @@ from numpy.testing import ( run_module_suite, TestCase, assert_, assert_equal, assert_array_equal, assert_almost_equal, assert_array_almost_equal, assert_raises, assert_allclose, assert_array_max_ulp, assert_warns, - assert_raises_regex, dec + assert_raises_regex, dec, clear_and_catch_warnings ) +import numpy.lib.function_base as nfb from numpy.random import rand from numpy.lib import * from numpy.compat import long @@ -1305,6 +1306,12 @@ class TestCheckFinite(TestCase): assert_(a.dtype == np.float64) +class catch_warn_nfb(clear_and_catch_warnings): + """ Context manager to catch, reset warnings in function_base module + """ + class_modules = (nfb,) + + class TestCorrCoef(TestCase): A = np.array( [[0.15391142, 0.18045767, 0.14197213], @@ -1335,8 +1342,26 @@ class TestCorrCoef(TestCase): assert_almost_equal(corrcoef(self.A, self.B), self.res2) def test_ddof(self): - assert_almost_equal(corrcoef(self.A, ddof=-1), self.res1) - assert_almost_equal(corrcoef(self.A, self.B, ddof=-1), self.res2) + # ddof raises DeprecationWarning + with catch_warn_nfb(): + warnings.simplefilter("always") + assert_warns(DeprecationWarning, corrcoef, self.A, ddof=-1) + warnings.simplefilter("ignore") + # ddof has no or negligible effect on the function + assert_almost_equal(corrcoef(self.A, ddof=-1), self.res1) + assert_almost_equal(corrcoef(self.A, self.B, ddof=-1), self.res2) + assert_almost_equal(corrcoef(self.A, ddof=3), self.res1) + assert_almost_equal(corrcoef(self.A, self.B, ddof=3), self.res2) + + def test_bias(self): + # bias raises DeprecationWarning + with catch_warn_nfb(): + warnings.simplefilter("always") + assert_warns(DeprecationWarning, corrcoef, self.A, self.B, 1, 0) + assert_warns(DeprecationWarning, corrcoef, self.A, bias=0) + warnings.simplefilter("ignore") + # bias has no or negligible effect on the function + assert_almost_equal(corrcoef(self.A, bias=1), self.res1) def test_complex(self): x = np.array([[1, 2, 3], [1j, 2j, 3j]]) @@ -1356,13 +1381,6 @@ class TestCorrCoef(TestCase): 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(record=True): - warnings.simplefilter('always', 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): |