3200
|
1 ## Copyright (C) 1995, 1996, 1997 Kurt Hornik |
|
2 ## |
|
3 ## This program is free software; you can redistribute it and/or modify |
|
4 ## it under the terms of the GNU General Public License as published by |
|
5 ## the Free Software Foundation; either version 2, or (at your option) |
|
6 ## any later version. |
|
7 ## |
|
8 ## This program is distributed in the hope that it will be useful, but |
|
9 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
11 ## General Public License for more details. |
|
12 ## |
|
13 ## You should have received a copy of the GNU General Public License |
|
14 ## along with this file. If not, write to the Free Software Foundation, |
|
15 ## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
16 |
3367
|
17 ## -*- texinfo -*- |
|
18 ## @deftypefn {Function File} {} cov (@var{x}, @var{y}) |
|
19 ## If each row of @var{x} and @var{y} is an observation and each column is |
|
20 ## a variable, the (@var{i},@var{j})-th entry of |
|
21 ## @code{cov (@var{x}, @var{y})} is the covariance between the @var{i}-th |
|
22 ## variable in @var{x} and the @var{j}-th variable in @var{y}. If called |
|
23 ## with one argument, compute @code{cov (@var{x}, @var{x})}. |
|
24 ## @end deftypefn |
3200
|
25 |
|
26 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
27 ## Description: Compute covariances |
|
28 |
|
29 function c = cov (x, y) |
|
30 |
|
31 if (nargin < 1 || nargin > 2) |
|
32 usage ("cov (x [, y])"); |
|
33 endif |
|
34 |
|
35 if (rows (x) == 1) |
|
36 x = x'; |
|
37 endif |
|
38 n = rows (x); |
|
39 |
|
40 if (nargin == 2) |
|
41 if (rows (y) == 1) |
|
42 y = y'; |
|
43 endif |
|
44 if (rows (y) != n) |
|
45 error ("cov: x and y must have the same number of observations."); |
|
46 endif |
|
47 x = x - ones (n, 1) * sum (x) / n; |
|
48 y = y - ones (n, 1) * sum (y) / n; |
|
49 c = conj (x' * y / (n - 1)); |
|
50 elseif (nargin == 1) |
|
51 x = x - ones (n, 1) * sum (x) / n; |
|
52 c = conj (x' * x / (n - 1)); |
|
53 endif |
|
54 |
|
55 endfunction |