annotate scripts/statistics/corrcoef.m @ 283:f3ce570869fc

[project @ 1994-01-10 20:29:54 by jwe] Initial revision
author jwe
date Mon, 10 Jan 1994 20:30:01 +0000
parents
children 3c23b8ea9099
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
283
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
1 # Copyright (C) 1993 John W. Eaton
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
2 #
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
3 # This file is part of Octave.
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
4 #
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
5 # Octave is free software; you can redistribute it and/or modify it
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
6 # under the terms of the GNU General Public License as published by the
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
7 # Free Software Foundation; either version 2, or (at your option) any
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
8 # later version.
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
9 #
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
10 # Octave is distributed in the hope that it will be useful, but WITHOUT
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
13 # for more details.
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
14 #
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
15 # You should have received a copy of the GNU General Public License
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
16 # along with Octave; see the file COPYING. If not, write to the Free
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
17 # Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
18
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
19 function retval = corrcoef (X, Y)
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
20
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
21 # usage: corrcoef (X [, Y])
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
22 #
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
23 # If each row of X and Y is an observation and each column is a variable,
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
24 # the (i,j)-th entry of corrcoef(X, Y) is the correlation between the
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
25 # i-th variable in X and the j-th variable in Y.
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
26 # corrcoef(X) is corrcoef(X, X).
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
27
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
28 # Written by Kurt Hornik (hornik@neuro.tuwien.ac.at) March 1993.
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
29 # Dept of Probability Theory and Statistics TU Wien, Austria.
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
30
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
31 if (nargin < 1 || nargin > 2)
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
32 error ("usage: corrcoef (X [, Y])");
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
33 endif
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
34
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
35 if (nargin == 2)
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
36 C = cov (X, Y);
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
37 S = std (X)' * std (Y);
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
38 retval = C ./ S;
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
39 elseif (nargin == 1)
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
40 C = cov (X);
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
41 s = diag (C);
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
42 retval = C ./ sqrt (s*s');
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
43 endif
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
44
f3ce570869fc [project @ 1994-01-10 20:29:54 by jwe]
jwe
parents:
diff changeset
45 endfunction