]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/base/corr.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / base / corr.m
1 ## Copyright (C) 1996-2012 John W. Eaton
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn  {Function File} {} corr (@var{x})
21 ## @deftypefnx {Function File} {} corr (@var{x}, @var{y})
22 ## Compute matrix of correlation coefficients.
23 ##
24 ## If each row of @var{x} and @var{y} is an observation and each column is
25 ## a variable, then the @w{(@var{i}, @var{j})-th} entry of
26 ## @code{corr (@var{x}, @var{y})} is the correlation between the
27 ## @var{i}-th variable in @var{x} and the @var{j}-th variable in @var{y}.
28 ## @tex
29 ## $$
30 ## {\rm corr}(x,y) = {{\rm cov}(x,y) \over {\rm std}(x) {\rm std}(y)}
31 ## $$
32 ## @end tex
33 ## @ifnottex
34 ##
35 ## @example
36 ## corr (x,y) = cov (x,y) / (std (x) * std (y))
37 ## @end example
38 ##
39 ## @end ifnottex
40 ## If called with one argument, compute @code{corr (@var{x}, @var{x})},
41 ## the correlation between the columns of @var{x}.
42 ## @seealso{cov}
43 ## @end deftypefn
44
45 ## Author: Kurt Hornik <hornik@wu-wien.ac.at>
46 ## Created: March 1993
47 ## Adapted-By: jwe
48
49 function retval = corr (x, y = [])
50
51   if (nargin < 1 || nargin > 2)
52     print_usage ();
53   endif
54
55   ## Input validation is done by cov.m.  Don't repeat tests here
56
57   ## Special case, scalar is always 100% correlated with itself
58   if (isscalar (x))
59     if (isa (x, 'single'))
60       retval = single (1);
61     else
62       retval = 1;
63     endif
64     return;
65   endif
66
67   ## No check for division by zero error, which happens only when
68   ## there is a constant vector and should be rare.
69   if (nargin == 2)
70     c = cov (x, y);
71     s = std (x)' * std (y);
72     retval = c ./ s;
73   else
74     c = cov (x);
75     s = sqrt (diag (c));
76     retval = c ./ (s * s');
77   endif
78
79 endfunction
80
81
82 %!test
83 %! x = rand (10);
84 %! cc1 = corr (x);
85 %! cc2 = corr (x, x);
86 %! assert (size (cc1) == [10, 10] && size (cc2) == [10, 10]);
87 %! assert (cc1, cc2, sqrt (eps));
88
89 %!test
90 %! x = [1:3]';
91 %! y = [3:-1:1]';
92 %! assert (corr (x,y), -1, 5*eps)
93 %! assert (corr (x,flipud (y)), 1, 5*eps)
94 %! assert (corr ([x, y]), [1 -1; -1 1], 5*eps)
95
96 %!test
97 %! x = single ([1:3]');
98 %! y = single ([3:-1:1]');
99 %! assert (corr (x,y), single (-1), 5*eps)
100 %! assert (corr (x,flipud (y)), single (1), 5*eps)
101 %! assert (corr ([x, y]), single ([1 -1; -1 1]), 5*eps)
102
103 %!assert (corr (5), 1);
104 %!assert (corr (single(5)), single(1));
105
106 %% Test input validation
107 %!error corr ();
108 %!error corr (1, 2, 3);
109 %!error corr ([1; 2], ["A", "B"]);
110 %!error corr (ones (2,2,2));
111 %!error corr (ones (2,2), ones (2,2,2));
112