Mercurial > hg > octave-terminal
changeset 12856:cad4cba03f19
Deprecate corrcoef, cor and replace with Matlab equivalent corr
The value calculated by Octave's corrcoef and cor are the
same as the value calculated by the Matlab function corr.
Use MathWorks naming convention for this functionality.
* corr.m: New file with functionality of corrcoef.m
* cov.m, kendall.m, spearman.m, cor_test.m: Adjust scripts to call corr()
* statistics/base/module.mk, deprecated/module.mk: Inform Automake about
deprecated functions
* NEWS: Inform users about deprecation
* stats.txi: Add corr() to documentation.
author | Rik <octave@nomad.inbox5.com> |
---|---|
date | Sat, 16 Jul 2011 20:38:00 -0700 |
parents | 8f5bd903ba68 |
children | 4061a9ec9c40 |
files | NEWS doc/interpreter/stats.txi scripts/deprecated/cor.m scripts/deprecated/corrcoef.m scripts/deprecated/module.mk scripts/statistics/base/cor.m scripts/statistics/base/corr.m scripts/statistics/base/corrcoef.m scripts/statistics/base/cov.m scripts/statistics/base/kendall.m scripts/statistics/base/module.mk scripts/statistics/base/spearman.m scripts/statistics/tests/cor_test.m |
diffstat | 11 files changed, 139 insertions(+), 16 deletions(-) [+] |
line wrap: on
line diff
--- a/NEWS +++ b/NEWS @@ -30,13 +30,11 @@ be removed from Octave 3.10 (or whatever version is the second major release after 3.6): - cut - __error_text__ + cut is_duplicate_entry + cor polyderiv + corrcoef studentize + __error_text__ sylvester_matrix error_text - is_duplicate_entry - polyderiv - studentize - sylvester_matrix Summary of important user-visible changes for version 3.4.2: -----------------------------------------------------------
--- a/doc/interpreter/stats.txi +++ b/doc/interpreter/stats.txi @@ -168,9 +168,7 @@ @DOCSTRING(cov) -@DOCSTRING(cor) - -@DOCSTRING(corrcoef) +@DOCSTRING(corr) @DOCSTRING(spearman)
rename from scripts/statistics/base/cor.m rename to scripts/deprecated/cor.m --- a/scripts/statistics/base/cor.m +++ b/scripts/deprecated/cor.m @@ -27,6 +27,13 @@ function retval = cor (x, y = x) + persistent warned = false; + if (! warned) + warned = true; + warning ("Octave:deprecated-function", + "cor is obsolete and will be removed from a future version of Octave; please use corr instead"); + endif + if (nargin < 1 || nargin > 2) print_usage (); endif
rename from scripts/statistics/base/corrcoef.m rename to scripts/deprecated/corrcoef.m --- a/scripts/statistics/base/corrcoef.m +++ b/scripts/deprecated/corrcoef.m @@ -48,6 +48,13 @@ function retval = corrcoef (x, y = []) + persistent warned = false; + if (! warned) + warned = true; + warning ("Octave:deprecated-function", + "corrcoef is not equivalent to Matlab and will be removed from a future version of Octave; for similar functionality see corr"); + endif + if (nargin < 1 || nargin > 2) print_usage (); endif
--- a/scripts/deprecated/module.mk +++ b/scripts/deprecated/module.mk @@ -6,6 +6,8 @@ deprecated/betai.m \ deprecated/cellidx.m \ deprecated/clg.m \ + deprecated/cor.m \ + deprecated/corrcoef.m \ deprecated/cquad.m \ deprecated/cut.m \ deprecated/dispatch.m \
new file mode 100644 --- /dev/null +++ b/scripts/statistics/base/corr.m @@ -0,0 +1,112 @@ +## Copyright (C) 1996-2011 John W. Eaton +## +## This file is part of Octave. +## +## Octave is free software; you can redistribute it and/or modify it +## under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 3 of the License, or (at +## your option) any later version. +## +## Octave is distributed in the hope that it will be useful, but +## WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +## General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with Octave; see the file COPYING. If not, see +## <http://www.gnu.org/licenses/>. + +## -*- texinfo -*- +## @deftypefn {Function File} {} corr (@var{x}) +## @deftypefnx {Function File} {} corr (@var{x}, @var{y}) +## Compute matrix of correlation coefficients. +## +## If each row of @var{x} and @var{y} is an observation and each column is +## a variable, then the @w{(@var{i}, @var{j})-th} entry of +## @code{corr (@var{x}, @var{y})} is the correlation between the +## @var{i}-th variable in @var{x} and the @var{j}-th variable in @var{y}. +## @tex +## $$ +## {\rm corr}(x,y) = {{\rm cov}(x,y) \over {\rm std}(x) {\rm std}(y)} +## $$ +## @end tex +## @ifnottex +## +## @example +## corr(x,y) = cov(x,y)/(std(x)*std(y)) +## @end example +## +## @end ifnottex +## If called with one argument, compute @code{corr (@var{x}, @var{x})}, +## the correlation between the columns of @var{x}. +## @seealso{cov} +## @end deftypefn + +## Author: Kurt Hornik <hornik@wu-wien.ac.at> +## Created: March 1993 +## Adapted-By: jwe + +function retval = corr (x, y = []) + + if (nargin < 1 || nargin > 2) + print_usage (); + endif + + ## Input validation is done by cov.m. Don't repeat tests here + + ## Special case, scalar is always 100% correlated with itself + if (isscalar (x)) + if (isa (x, 'single')) + retval = single (1); + else + retval = 1; + endif + return; + endif + + ## No check for division by zero error, which happens only when + ## there is a constant vector and should be rare. + if (nargin == 2) + c = cov (x, y); + s = std (x)' * std (y); + retval = c ./ s; + else + c = cov (x); + s = sqrt (diag (c)); + retval = c ./ (s * s'); + endif + +endfunction + + +%!test +%! x = rand (10); +%! cc1 = corr (x); +%! cc2 = corr (x, x); +%! assert (size (cc1) == [10, 10] && size (cc2) == [10, 10]); +%! assert (cc1, cc2, sqrt (eps)); + +%!test +%! x = [1:3]'; +%! y = [3:-1:1]'; +%! assert (corr (x,y), -1, 5*eps) +%! assert (corr (x,flipud (y)), 1, 5*eps) +%! assert (corr ([x, y]), [1 -1; -1 1], 5*eps) + +%!test +%! x = single ([1:3]'); +%! y = single ([3:-1:1]'); +%! assert (corr (x,y), single (-1), 5*eps) +%! assert (corr (x,flipud (y)), single (1), 5*eps) +%! assert (corr ([x, y]), single ([1 -1; -1 1]), 5*eps) + +%!assert (corr (5), 1); +%!assert (corr (single(5)), single(1)); + +%% Test input validation +%!error corr (); +%!error corr (1, 2, 3); +%!error corr ([1; 2], ["A", "B"]); +%!error corr (ones (2,2,2)); +%!error corr (ones (2,2), ones (2,2,2)); +
--- a/scripts/statistics/base/cov.m +++ b/scripts/statistics/base/cov.m @@ -55,7 +55,7 @@ ## @item 1: ## normalize with @math{N}, this provides the second moment around the mean ## @end table -## @seealso{corrcoef, cor} +## @seealso{corr} ## @end deftypefn ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
--- a/scripts/statistics/base/kendall.m +++ b/scripts/statistics/base/kendall.m @@ -106,7 +106,7 @@ endif r = ranks (x); m = sign (kron (r, ones (n, 1, cls)) - kron (ones (n, 1, cls), r)); - tau = corrcoef (m); + tau = corr (m); if (nargin == 2) tau = tau(1 : c, (c + 1) : columns (x));
--- a/scripts/statistics/base/module.mk +++ b/scripts/statistics/base/module.mk @@ -3,8 +3,7 @@ statistics_base_FCN_FILES = \ statistics/base/center.m \ statistics/base/cloglog.m \ - statistics/base/cor.m \ - statistics/base/corrcoef.m \ + statistics/base/corr.m \ statistics/base/cov.m \ statistics/base/gls.m \ statistics/base/histc.m \
--- a/scripts/statistics/base/spearman.m +++ b/scripts/statistics/base/spearman.m @@ -57,7 +57,7 @@ endif if (nargin == 1) - rho = corrcoef (ranks (x)); + rho = corr (ranks (x)); else if (isrow (y)) y = y.'; @@ -65,7 +65,7 @@ if (rows (x) != rows (y)) error ("spearman: X and Y must have the same number of observations"); endif - rho = corrcoef (ranks (x), ranks (y)); + rho = corr (ranks (x), ranks (y)); endif ## Restore class cleared by ranks