annotate scripts/statistics/base/cov.m @ 22226:9a0e30e24b9b

Calculate cov as Matlab does, not as its conjugate (bug #48315). * NEWS: Announce change. * cov.m: Remove "conj" from calculation of output. Update compatibility note to explain what Matlab actually does.
author Lachlan Andrew <lachlanbis@gmail.com>
date Thu, 30 Jun 2016 18:30:26 +1000
parents 516bb87ea72e
children 9fc91bb2aec3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19697
4197fc428c7d maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents: 18207
diff changeset
1 ## Copyright (C) 1995-2015 Kurt Hornik
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3367
diff changeset
2 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3458
diff changeset
3 ## This file is part of Octave.
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3458
diff changeset
4 ##
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3458
diff changeset
5 ## Octave is free software; you can redistribute it and/or modify it
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3458
diff changeset
6 ## under the terms of the GNU General Public License as published by
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6754
diff changeset
7 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6754
diff changeset
8 ## your option) any later version.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3367
diff changeset
9 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3458
diff changeset
10 ## Octave is distributed in the hope that it will be useful, but
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3367
diff changeset
13 ## General Public License for more details.
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3367
diff changeset
14 ##
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
15 ## You should have received a copy of the GNU General Public License
7016
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6754
diff changeset
16 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6754
diff changeset
17 ## <http://www.gnu.org/licenses/>.
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
18
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
19 ## -*- texinfo -*-
20852
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20174
diff changeset
20 ## @deftypefn {} {} cov (@var{x})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20174
diff changeset
21 ## @deftypefnx {} {} cov (@var{x}, @var{opt})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20174
diff changeset
22 ## @deftypefnx {} {} cov (@var{x}, @var{y})
516bb87ea72e 2015 Code Sprint: remove class of function from docstring for all m-files.
Rik <rik@octave.org>
parents: 20174
diff changeset
23 ## @deftypefnx {} {} cov (@var{x}, @var{y}, @var{opt})
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 9245
diff changeset
24 ## Compute the covariance matrix.
6754
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6046
diff changeset
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
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
28 ## @code{cov (@var{x}, @var{y})} is the covariance between the @var{i}-th
6754
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6046
diff changeset
29 ## variable in @var{x} and the @var{j}-th variable in @var{y}.
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6046
diff changeset
30 ## @tex
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6046
diff changeset
31 ## $$
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6046
diff changeset
32 ## \sigma_{ij} = {1 \over N-1} \sum_{i=1}^N (x_i - \bar{x})(y_i - \bar{y})
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6046
diff changeset
33 ## $$
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6046
diff changeset
34 ## where $\bar{x}$ and $\bar{y}$ are the mean values of $x$ and $y$.
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6046
diff changeset
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 ##
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
47 ## The argument @var{opt} determines the type of normalization to use.
11436
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
18207
48dafd739840 cov.m: Document intentional Matlab incompatibility (bug #40751).
Rik <rik@octave.org>
parents: 17744
diff changeset
58 ##
22226
9a0e30e24b9b Calculate cov as Matlab does, not as its conjugate (bug #48315).
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20852
diff changeset
59 ## Compatibility Note:: Octave always treats rows of @var{x} and @var{y}
9a0e30e24b9b Calculate cov as Matlab does, not as its conjugate (bug #48315).
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20852
diff changeset
60 ## as multivariate random variables.
9a0e30e24b9b Calculate cov as Matlab does, not as its conjugate (bug #48315).
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20852
diff changeset
61 ## For two inputs, however, @sc{matlab} treats @var{x} and @var{y} as two
9a0e30e24b9b Calculate cov as Matlab does, not as its conjugate (bug #48315).
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20852
diff changeset
62 ## univariate distributions regardless of their shapes, and will calculate
9a0e30e24b9b Calculate cov as Matlab does, not as its conjugate (bug #48315).
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20852
diff changeset
63 ## @code{cov ([@var{x}(:), @var{y}(:)])} whenever the number of elements in
9a0e30e24b9b Calculate cov as Matlab does, not as its conjugate (bug #48315).
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20852
diff changeset
64 ## @var{x} and @var{y} are equal. This will result in a 2x2 matrix.
9a0e30e24b9b Calculate cov as Matlab does, not as its conjugate (bug #48315).
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20852
diff changeset
65 ## Code relying on @sc{Matlab}'s definition will need to be
18207
48dafd739840 cov.m: Document intentional Matlab incompatibility (bug #40751).
Rik <rik@octave.org>
parents: 17744
diff changeset
66 ## changed when running in Octave.
12856
cad4cba03f19 Deprecate corrcoef, cor and replace with Matlab equivalent corr
Rik <octave@nomad.inbox5.com>
parents: 12656
diff changeset
67 ## @seealso{corr}
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
68 ## @end deftypefn
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
69
5428
2a16423e4aa0 [project @ 2005-08-23 18:38:27 by jwe]
jwe
parents: 5307
diff changeset
70 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
3456
434790acb067 [project @ 2000-01-19 06:58:51 by jwe]
jwe
parents: 3426
diff changeset
71 ## Description: Compute covariances
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
72
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 9245
diff changeset
73 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
74
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 9245
diff changeset
75 if (nargin < 1 || nargin > 3)
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 9245
diff changeset
76 print_usage ();
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 9245
diff changeset
77 endif
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 9245
diff changeset
78
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
79 if ( ! (isnumeric (x) || islogical (x))
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
80 || ! (isnumeric (y) || islogical (y)))
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 9245
diff changeset
81 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
82 endif
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
83
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 9245
diff changeset
84 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
85 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
86 endif
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 9245
diff changeset
87
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
88 if (nargin == 2 && isscalar (y))
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 9245
diff changeset
89 opt = y;
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 9245
diff changeset
90 endif
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 9245
diff changeset
91
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 9245
diff changeset
92 if (opt != 0 && opt != 1)
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 9245
diff changeset
93 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
94 endif
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 9245
diff changeset
95
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
96 ## Special case, scalar has zero covariance
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 9245
diff changeset
97 if (isscalar (x))
14336
34af9f9ff98b Use Octave coding conventions (double-quote " in preference to single quote ')
Rik <octave@nomad.inbox5.com>
parents: 14138
diff changeset
98 if (isa (x, "single"))
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
99 c = single (0);
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
100 else
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
101 c = 0;
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
102 endif
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 9245
diff changeset
103 return;
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
104 endif
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
105
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
106 if (isrow (x))
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
107 x = x.';
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
108 endif
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
109 n = rows (x);
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
110
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
111 if (nargin == 1 || isscalar (y))
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 9245
diff changeset
112 x = center (x, 1);
22226
9a0e30e24b9b Calculate cov as Matlab does, not as its conjugate (bug #48315).
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20852
diff changeset
113 c = x' * x / (n - 1 + opt);
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 9245
diff changeset
114 else
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
115 if (isrow (y))
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
116 y = y.';
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
117 endif
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
118 if (rows (y) != n)
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 9245
diff changeset
119 error ("cov: X and Y must have the same number of observations");
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
120 endif
8977
f464119ec165 further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
121 x = center (x, 1);
f464119ec165 further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
122 y = center (y, 1);
22226
9a0e30e24b9b Calculate cov as Matlab does, not as its conjugate (bug #48315).
Lachlan Andrew <lachlanbis@gmail.com>
parents: 20852
diff changeset
123 c = x' * y / (n - 1 + opt);
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
124 endif
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
125
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
126 endfunction
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
127
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
128
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
129 %!test
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
130 %! x = rand (10);
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
131 %! cx1 = cov (x);
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
132 %! cx2 = cov (x, x);
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14336
diff changeset
133 %! assert (size (cx1) == [10, 10] && size (cx2) == [10, 10]);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14336
diff changeset
134 %! assert (cx1, cx2, 1e1*eps);
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
135
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
136 %!test
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
137 %! x = [1:3]';
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
138 %! y = [3:-1:1]';
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14336
diff changeset
139 %! assert (cov (x, y), -1, 5*eps);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14336
diff changeset
140 %! assert (cov (x, flipud (y)), 1, 5*eps);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14336
diff changeset
141 %! assert (cov ([x, y]), [1 -1; -1 1], 5*eps);
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
142
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
143 %!test
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
144 %! x = single ([1:3]');
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
145 %! y = single ([3:-1:1]');
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14336
diff changeset
146 %! assert (cov (x, y), single (-1), 5*eps);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14336
diff changeset
147 %! assert (cov (x, flipud (y)), single (1), 5*eps);
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14336
diff changeset
148 %! assert (cov ([x, y]), single ([1 -1; -1 1]), 5*eps);
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
149
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 9245
diff changeset
150 %!test
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 9245
diff changeset
151 %! x = [1:5];
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 9245
diff changeset
152 %! c = cov (x);
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
153 %! assert (isscalar (c));
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
154 %! assert (c, 2.5);
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
155
14868
5d3a684236b0 maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
156 %!assert (cov (5), 0)
5d3a684236b0 maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
157 %!assert (cov (single (5)), single (0))
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 9245
diff changeset
158
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 9245
diff changeset
159 %!test
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 9245
diff changeset
160 %! x = [1:5];
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 9245
diff changeset
161 %! c = cov (x, 0);
14868
5d3a684236b0 maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
162 %! assert (c, 2.5);
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 9245
diff changeset
163 %! c = cov (x, 1);
14868
5d3a684236b0 maint: Use Octave coding conventions for cuddling parentheses in scripts directory
Rik <octave@nomad.inbox5.com>
parents: 14363
diff changeset
164 %! assert (c, 2);
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
165
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
166 ## Test input validation
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14336
diff changeset
167 %!error cov ()
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14336
diff changeset
168 %!error cov (1, 2, 3, 4)
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14336
diff changeset
169 %!error cov ([1; 2], ["A", "B"])
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14336
diff changeset
170 %!error cov (ones (2,2,2))
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14336
diff changeset
171 %!error cov (ones (2,2), ones (2,2,2))
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14336
diff changeset
172 %!error cov (1, 3)
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14336
diff changeset
173 %!error cov (ones (2,2), ones (3,2))
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 9245
diff changeset
174