]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/base/cov.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / base / cov.m
1 ## Copyright (C) 1995-2012 Kurt Hornik
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} {} cov (@var{x})
21 ## @deftypefnx {Function File} {} cov (@var{x}, @var{opt})
22 ## @deftypefnx {Function File} {} cov (@var{x}, @var{y})
23 ## @deftypefnx {Function File} {} cov (@var{x}, @var{y}, @var{opt})
24 ## Compute the covariance matrix.
25 ##
26 ## If each row of @var{x} and @var{y} is an observation, and each column is
27 ## a variable, then the @w{(@var{i}, @var{j})-th} entry of
28 ## @code{cov (@var{x}, @var{y})} is the covariance between the @var{i}-th
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
36 ## @ifnottex
37 ##
38 ## @example
39 ## cov (x) = 1/N-1 * SUM_i (x(i) - mean(x)) * (y(i) - mean(y))
40 ## @end example
41 ##
42 ## @end ifnottex
43 ##
44 ## If called with one argument, compute @code{cov (@var{x}, @var{x})}, the
45 ## covariance between the columns of @var{x}.
46 ##
47 ## The argument @var{opt} determines the type of normalization to use.
48 ## Valid values are
49 ##
50 ## @table @asis
51 ## @item 0:
52 ##   normalize with @math{N-1}, provides the best unbiased estimator of the
53 ## covariance [default]
54 ##
55 ## @item 1:
56 ##   normalize with @math{N}, this provides the second moment around the mean
57 ## @end table
58 ## @seealso{corr}
59 ## @end deftypefn
60
61 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
62 ## Description: Compute covariances
63
64 function c = cov (x, y = [], opt = 0)
65
66   if (nargin < 1 || nargin > 3)
67     print_usage ();
68   endif
69
70   if (   ! (isnumeric (x) || islogical (x))
71       || ! (isnumeric (y) || islogical (y)))
72     error ("cov: X and Y must be numeric matrices or vectors");
73   endif
74
75   if (ndims (x) != 2 || ndims (y) != 2)
76     error ("cov: X and Y must be 2-D matrices or vectors");
77   endif
78
79   if (nargin == 2 && isscalar (y))
80     opt = y;
81   endif
82
83   if (opt != 0 && opt != 1)
84     error ("cov: normalization OPT must be 0 or 1");
85   endif
86
87   ## Special case, scalar has zero covariance
88   if (isscalar (x))
89     if (isa (x, 'single'))
90       c = single (0);
91     else
92       c = 0;
93     endif
94     return;
95   endif
96
97   if (isrow (x))
98     x = x.';
99   endif
100   n = rows (x);
101
102   if (nargin == 1 || isscalar (y))
103     x = center (x, 1);
104     c = conj (x' * x / (n - 1 + opt));
105   else
106     if (isrow (y))
107       y = y.';
108     endif
109     if (rows (y) != n)
110       error ("cov: X and Y must have the same number of observations");
111     endif
112     x = center (x, 1);
113     y = center (y, 1);
114     c = conj (x' * y / (n - 1 + opt));
115   endif
116
117 endfunction
118
119
120 %!test
121 %! x = rand (10);
122 %! cx1 = cov (x);
123 %! cx2 = cov (x, x);
124 %! assert(size (cx1) == [10, 10] && size (cx2) == [10, 10]);
125 %! assert(cx1, cx2, 1e1*eps);
126
127 %!test
128 %! x = [1:3]';
129 %! y = [3:-1:1]';
130 %! assert (cov (x,y), -1, 5*eps)
131 %! assert (cov (x,flipud (y)), 1, 5*eps)
132 %! assert (cov ([x, y]), [1 -1; -1 1], 5*eps)
133
134 %!test
135 %! x = single ([1:3]');
136 %! y = single ([3:-1:1]');
137 %! assert (cov (x,y), single (-1), 5*eps)
138 %! assert (cov (x,flipud (y)), single (1), 5*eps)
139 %! assert (cov ([x, y]), single ([1 -1; -1 1]), 5*eps)
140
141 %!test
142 %! x = [1:5];
143 %! c = cov (x);
144 %! assert (isscalar (c));
145 %! assert (c, 2.5);
146
147 %!assert(cov (5), 0);
148 %!assert(cov (single(5)), single(0));
149
150 %!test
151 %! x = [1:5];
152 %! c = cov (x, 0);
153 %! assert(c, 2.5);
154 %! c = cov (x, 1);
155 %! assert(c, 2);
156
157 %% Test input validation
158 %!error cov ();
159 %!error cov (1, 2, 3, 4);
160 %!error cov ([1; 2], ["A", "B"]);
161 %!error cov (ones (2,2,2));
162 %!error cov (ones (2,2), ones (2,2,2));
163 %!error cov (1, 3);
164 %!error cov (ones (2,2), ones (3,2));
165