7017
|
1 ## Copyright (C) 1995, 1996, 1997, 1998, 2000, 2002, 2004, 2005, 2006, |
9245
|
2 ## 2007, 2009 Kurt Hornik |
3426
|
3 ## |
3922
|
4 ## This file is part of Octave. |
|
5 ## |
|
6 ## Octave is free software; you can redistribute it and/or modify it |
|
7 ## under the terms of the GNU General Public License as published by |
7016
|
8 ## the Free Software Foundation; either version 3 of the License, or (at |
|
9 ## your option) any later version. |
3426
|
10 ## |
3922
|
11 ## Octave is distributed in the hope that it will be useful, but |
3200
|
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
14 ## General Public License for more details. |
|
15 ## |
3200
|
16 ## You should have received a copy of the GNU General Public License |
7016
|
17 ## along with Octave; see the file COPYING. If not, see |
|
18 ## <http://www.gnu.org/licenses/>. |
3200
|
19 |
3453
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} cor (@var{x}, @var{y}) |
6754
|
22 ## Compute correlation. |
|
23 ## |
4855
|
24 ## The (@var{i}, @var{j})-th entry of @code{cor (@var{x}, @var{y})} is |
3453
|
25 ## the correlation between the @var{i}-th variable in @var{x} and the |
|
26 ## @var{j}-th variable in @var{y}. |
3200
|
27 ## |
6754
|
28 ## @tex |
|
29 ## $$ |
|
30 ## {\rm corrcoef}(x,y) = {{\rm cov}(x,y) \over {\rm std}(x) {\rm std}(y)} |
|
31 ## $$ |
|
32 ## @end tex |
|
33 ## @ifnottex |
|
34 ## @example |
|
35 ## corrcoef(x,y) = cov(x,y)/(std(x)*std(y)) |
|
36 ## @end example |
|
37 ## @end ifnottex |
|
38 ## |
3200
|
39 ## For matrices, each row is an observation and each column a variable; |
|
40 ## vectors are always observations and may be row or column vectors. |
|
41 ## |
3453
|
42 ## @code{cor (@var{x})} is equivalent to @code{cor (@var{x}, @var{x})}. |
6754
|
43 ## |
|
44 ## Note that the @code{corrcoef} function does the same as @code{cor}. |
3453
|
45 ## @end deftypefn |
3200
|
46 |
5428
|
47 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456
|
48 ## Description: Compute correlations |
3200
|
49 |
|
50 function retval = cor (x, y) |
|
51 |
|
52 if (nargin < 1 || nargin > 2) |
6046
|
53 print_usage (); |
3200
|
54 endif |
|
55 |
|
56 if (nargin == 2) |
|
57 c = cov (x, y); |
|
58 s = std (x)' * std (y); |
|
59 retval = c ./ s; |
|
60 elseif (nargin == 1) |
|
61 c = cov (x); |
|
62 s = reshape (sqrt (diag (c)), 1, columns (c)); |
|
63 retval = c ./ (s' * s); |
|
64 endif |
|
65 |
|
66 endfunction |