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 13 files changed, 298 insertions(+), 175 deletions(-) [+]
line wrap: on
line diff
--- a/NEWS	Sat Jul 16 18:00:15 2011 -0700
+++ b/NEWS	Sat Jul 16 20:38:00 2011 -0700
@@ -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	Sat Jul 16 18:00:15 2011 -0700
+++ b/doc/interpreter/stats.txi	Sat Jul 16 20:38:00 2011 -0700
@@ -168,9 +168,7 @@
 
 @DOCSTRING(cov)
 
-@DOCSTRING(cor)
-
-@DOCSTRING(corrcoef)
+@DOCSTRING(corr)
 
 @DOCSTRING(spearman)
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/deprecated/cor.m	Sat Jul 16 20:38:00 2011 -0700
@@ -0,0 +1,54 @@
+## Copyright (C) 1995-2011 Kurt Hornik
+##
+## 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} {} cor (@var{x})
+## @deftypefnx {Function File} {} cor (@var{x}, @var{y})
+## Compute matrix of correlation coefficients.
+##
+## This is an alias for @code{corrcoef}.
+## @seealso{corrcoef}
+## @end deftypefn
+
+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
+
+  retval = corrcoef (x, y);
+
+endfunction
+
+
+%!test
+%! x = rand (10, 2);
+%! assert (cor (x), corrcoef (x), 5*eps);
+%! assert (cor (x(:,1), x(:,2)) == corrcoef (x(:,1), x(:,2)));
+
+%% Test input validation
+%!error corrcoef ();
+%!error corrcoef (1, 2, 3);
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/deprecated/corrcoef.m	Sat Jul 16 20:38:00 2011 -0700
@@ -0,0 +1,119 @@
+## 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} {} corrcoef (@var{x})
+## @deftypefnx {Function File} {} corrcoef (@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{corrcoef (@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 corrcoef}(x,y) = {{\rm cov}(x,y) \over {\rm std}(x) {\rm std}(y)}
+## $$
+## @end tex
+## @ifnottex
+##
+## @example
+## corrcoef(x,y) = cov(x,y)/(std(x)*std(y))
+## @end example
+##
+## @end ifnottex
+## If called with one argument, compute @code{corrcoef (@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 = 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
+
+  ## 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 = corrcoef (x);
+%! cc2 = corrcoef (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 (corrcoef (x,y), -1, 5*eps)
+%! assert (corrcoef (x,flipud (y)), 1, 5*eps)
+%! assert (corrcoef ([x, y]), [1 -1; -1 1], 5*eps)
+
+%!test
+%! x = single ([1:3]');
+%! y = single ([3:-1:1]');
+%! assert (corrcoef (x,y), single (-1), 5*eps)
+%! assert (corrcoef (x,flipud (y)), single (1), 5*eps)
+%! assert (corrcoef ([x, y]), single ([1 -1; -1 1]), 5*eps)
+
+%!assert (corrcoef (5), 1);
+%!assert (corrcoef (single(5)), single(1));
+
+%% Test input validation
+%!error corrcoef ();
+%!error corrcoef (1, 2, 3);
+%!error corrcoef ([1; 2], ["A", "B"]);
+%!error corrcoef (ones (2,2,2));
+%!error corrcoef (ones (2,2), ones (2,2,2));
+
--- a/scripts/deprecated/module.mk	Sat Jul 16 18:00:15 2011 -0700
+++ b/scripts/deprecated/module.mk	Sat Jul 16 20:38:00 2011 -0700
@@ -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 \
--- a/scripts/statistics/base/cor.m	Sat Jul 16 18:00:15 2011 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,47 +0,0 @@
-## Copyright (C) 1995-2011 Kurt Hornik
-##
-## 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} {} cor (@var{x})
-## @deftypefnx {Function File} {} cor (@var{x}, @var{y})
-## Compute matrix of correlation coefficients.
-##
-## This is an alias for @code{corrcoef}.
-## @seealso{corrcoef}
-## @end deftypefn
-
-function retval = cor (x, y = x)
-
-  if (nargin < 1 || nargin > 2)
-    print_usage ();
-  endif
-
-  retval = corrcoef (x, y);
-
-endfunction
-
-
-%!test
-%! x = rand (10, 2);
-%! assert (cor (x), corrcoef (x), 5*eps);
-%! assert (cor (x(:,1), x(:,2)) == corrcoef (x(:,1), x(:,2)));
-
-%% Test input validation
-%!error corrcoef ();
-%!error corrcoef (1, 2, 3);
-
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/statistics/base/corr.m	Sat Jul 16 20:38:00 2011 -0700
@@ -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/corrcoef.m	Sat Jul 16 18:00:15 2011 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,112 +0,0 @@
-## 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} {} corrcoef (@var{x})
-## @deftypefnx {Function File} {} corrcoef (@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{corrcoef (@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 corrcoef}(x,y) = {{\rm cov}(x,y) \over {\rm std}(x) {\rm std}(y)}
-## $$
-## @end tex
-## @ifnottex
-##
-## @example
-## corrcoef(x,y) = cov(x,y)/(std(x)*std(y))
-## @end example
-##
-## @end ifnottex
-## If called with one argument, compute @code{corrcoef (@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 = corrcoef (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 = corrcoef (x);
-%! cc2 = corrcoef (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 (corrcoef (x,y), -1, 5*eps)
-%! assert (corrcoef (x,flipud (y)), 1, 5*eps)
-%! assert (corrcoef ([x, y]), [1 -1; -1 1], 5*eps)
-
-%!test
-%! x = single ([1:3]');
-%! y = single ([3:-1:1]');
-%! assert (corrcoef (x,y), single (-1), 5*eps)
-%! assert (corrcoef (x,flipud (y)), single (1), 5*eps)
-%! assert (corrcoef ([x, y]), single ([1 -1; -1 1]), 5*eps)
-
-%!assert (corrcoef (5), 1);
-%!assert (corrcoef (single(5)), single(1));
-
-%% Test input validation
-%!error corrcoef ();
-%!error corrcoef (1, 2, 3);
-%!error corrcoef ([1; 2], ["A", "B"]);
-%!error corrcoef (ones (2,2,2));
-%!error corrcoef (ones (2,2), ones (2,2,2));
-
--- a/scripts/statistics/base/cov.m	Sat Jul 16 18:00:15 2011 -0700
+++ b/scripts/statistics/base/cov.m	Sat Jul 16 20:38:00 2011 -0700
@@ -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	Sat Jul 16 18:00:15 2011 -0700
+++ b/scripts/statistics/base/kendall.m	Sat Jul 16 20:38:00 2011 -0700
@@ -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	Sat Jul 16 18:00:15 2011 -0700
+++ b/scripts/statistics/base/module.mk	Sat Jul 16 20:38:00 2011 -0700
@@ -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	Sat Jul 16 18:00:15 2011 -0700
+++ b/scripts/statistics/base/spearman.m	Sat Jul 16 20:38:00 2011 -0700
@@ -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
--- a/scripts/statistics/tests/cor_test.m	Sat Jul 16 18:00:15 2011 -0700
+++ b/scripts/statistics/tests/cor_test.m	Sat Jul 16 20:38:00 2011 -0700
@@ -91,7 +91,7 @@
   m = method (1);
 
   if (m == "p")
-    r = cor (x, y);
+    r = corr (x, y);
     df = n - 2;
     t.method = "Pearson's product moment correlation";
     t.params = df;