Mercurial > hg > octave-nkf
annotate scripts/statistics/base/cov.m @ 11523:fd0a3ac60b0e
update copyright notices
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 14 Jan 2011 05:47:45 -0500 |
parents | e151e23f73bc |
children | c792872f8942 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 1995-2011 Kurt Hornik |
3426 | 2 ## |
3922 | 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
3426 | 9 ## |
3922 | 10 ## Octave is distributed in the hope that it will be useful, but |
3200 | 11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
3426 | 13 ## General Public License for more details. |
14 ## | |
3200 | 15 ## You should have received a copy of the GNU General Public License |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
3200 | 18 |
3367 | 19 ## -*- texinfo -*- |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
20 ## @deftypefn {Function File} {} cov (@var{x}) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
21 ## @deftypefnx {Function File} {} cov (@var{x}, @var{opt}) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
22 ## @deftypefnx {Function File} {} cov (@var{x}, @var{y}) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
23 ## @deftypefnx {Function File} {} cov (@var{x}, @var{y}, @var{opt}) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
24 ## Compute the covariance matrix. |
6754 | 25 ## |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
26 ## If each row of @var{x} and @var{y} is an observation, and each column is |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
27 ## a variable, then the @w{(@var{i}, @var{j})-th} entry of |
3367 | 28 ## @code{cov (@var{x}, @var{y})} is the covariance between the @var{i}-th |
6754 | 29 ## variable in @var{x} and the @var{j}-th variable in @var{y}. |
30 ## @tex | |
31 ## $$ | |
32 ## \sigma_{ij} = {1 \over N-1} \sum_{i=1}^N (x_i - \bar{x})(y_i - \bar{y}) | |
33 ## $$ | |
34 ## where $\bar{x}$ and $\bar{y}$ are the mean values of $x$ and $y$. | |
35 ## @end tex | |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
36 ## @ifnottex |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
37 ## |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
38 ## @example |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
39 ## cov (x) = 1/N-1 * SUM_i (x(i) - mean(x)) * (y(i) - mean(y)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
40 ## @end example |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
41 ## |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
42 ## @end ifnottex |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
43 ## |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
44 ## If called with one argument, compute @code{cov (@var{x}, @var{x})}, the |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
45 ## covariance between the columns of @var{x}. |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
46 ## |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
47 ## The argument @var{opt} determines the type of normalization to use. |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
48 ## Valid values are |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
49 ## |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
50 ## @table @asis |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
51 ## @item 0: |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
52 ## normalize with @math{N-1}, provides the best unbiased estimator of the |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
53 ## covariance [default] |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
54 ## |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
55 ## @item 1: |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
56 ## normalize with @math{N}, this provides the second moment around the mean |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
57 ## @end table |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
58 ## @seealso{corrcoef, cor} |
3367 | 59 ## @end deftypefn |
3200 | 60 |
5428 | 61 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456 | 62 ## Description: Compute covariances |
3200 | 63 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
64 function c = cov (x, y = [], opt = 0) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
65 |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
66 if (nargin < 1 || nargin > 3) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
67 print_usage (); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
68 endif |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
69 |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
70 if (! (isnumeric (x) && isnumeric (y))) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
71 error ("cov: X and Y must be numeric matrices or vectors"); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
72 endif |
3200 | 73 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
74 if (ndims (x) != 2 || ndims (y) != 2) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
75 error ("cov: X and Y must be 2-D matrices or vectors"); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
76 endif |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
77 |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
78 if (nargin == 2 && isscalar(y)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
79 opt = y; |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
80 endif |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
81 |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
82 if (opt != 0 && opt != 1) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
83 error ("cov: normalization OPT must be 0 or 1"); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
84 endif |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
85 |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
86 if (isscalar (x)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
87 c = 0; |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
88 return; |
3200 | 89 endif |
90 | |
91 if (rows (x) == 1) | |
92 x = x'; | |
93 endif | |
94 n = rows (x); | |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
95 |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
96 if (nargin == 1 || isscalar(y)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
97 x = center (x, 1); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
98 c = conj (x' * x / (n - 1 + opt)); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
99 else |
3200 | 100 if (rows (y) == 1) |
101 y = y'; | |
102 endif | |
103 if (rows (y) != n) | |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
104 error ("cov: X and Y must have the same number of observations"); |
3200 | 105 endif |
8977
f464119ec165
further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
106 x = center (x, 1); |
f464119ec165
further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
107 y = center (y, 1); |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
108 c = conj (x' * y / (n - 1 + opt)); |
3200 | 109 endif |
110 | |
111 endfunction | |
7411 | 112 |
113 %!test | |
114 %! x = rand (10); | |
115 %! cx1 = cov (x); | |
116 %! cx2 = cov (x, x); | |
7939 | 117 %! assert(size (cx1) == [10, 10] && size (cx2) == [10, 10] && norm(cx1-cx2) < 1e1*eps); |
7411 | 118 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
119 %!test |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
120 %! x = [1:5]; |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
121 %! c = cov (x); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
122 %! assert(isscalar (c)); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
123 %! assert(c, 2.5); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
124 |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
125 %!test |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
126 %! x = [1:5]; |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
127 %! c = cov (x, 0); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
128 %! assert(c, 2.5); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
129 %! c = cov (x, 1); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
130 %! assert(c, 2); |
7411 | 131 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
132 %!assert(cov (5), 0); |
7411 | 133 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
134 %% Test input validation |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
135 %!error cov (); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
136 %!error cov (1, 2, 3, 4); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
137 %!error cov ([true, true]); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
138 %!error cov ([1, 2], [true, true]); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
139 %!error cov (ones (2,2,2)); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
140 %!error cov (ones (2,2), ones (2,2,2)); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
141 %!error cov (1, 3); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
142 %!error cov (ones (2,2), ones (3,2)); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
9245
diff
changeset
|
143 |