annotate scripts/statistics/base/var.m @ 21309:7fbecef105ca

Allow statistics functions to work over non-existent dimension (bug #33523). * var.m, center.m, kurtosis.m, mean.m, meansq.m, median.m, moment.m, skewness.m, std.m: Remove check for input dimension being within the range of ndims (x). Add BIST tests for new behavior and update input validation tests.
author Rik <rik@octave.org>
date Fri, 19 Feb 2016 21:27:03 -0800
parents 516bb87ea72e
children bac0d6f07a3e
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: 19593
diff changeset
1 ## Copyright (C) 1995-2015 Kurt Hornik
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
2 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3456
diff changeset
3 ## This file is part of Octave.
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3456
diff changeset
4 ##
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3456
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: 3456
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: 7001
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: 7001
diff changeset
8 ## your option) any later version.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
9 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3456
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: 3200
diff changeset
13 ## General Public License for more details.
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
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: 7001
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: 7001
diff changeset
17 ## <http://www.gnu.org/licenses/>.
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
18
3453
71d2e09c15a2 [project @ 2000-01-18 08:32:09 by jwe]
jwe
parents: 3426
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 {} {} var (@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 {} {} var (@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 {} {} var (@var{x}, @var{opt}, @var{dim})
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
23 ## Compute the variance of the elements of the vector @var{x}.
20174
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
24 ##
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
25 ## The variance is defined as
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
26 ## @tex
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
27 ## $$
13795
50ceb5c51271 var.m: Fix bug in docstring referring to std (Bug #34729).
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
28 ## {\rm var} (x) = \sigma^2 = {\sum_{i=1}^N (x_i - \bar{x})^2 \over N - 1}
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
29 ## $$
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
30 ## where $\bar{x}$ is the mean value of $x$.
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
31 ## @end tex
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
32 ## @ifnottex
4849
a3440ff5eb14 [project @ 2004-04-06 15:23:56 by jwe]
jwe
parents: 4171
diff changeset
33 ##
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
34 ## @example
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
35 ## @group
13795
50ceb5c51271 var.m: Fix bug in docstring referring to std (Bug #34729).
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
36 ## var (x) = 1/(N-1) SUM_i (x(i) - mean(x))^2
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
37 ## @end group
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
38 ## @end example
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
39 ##
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
40 ## @end ifnottex
20174
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
41 ## If @var{x} is a matrix, compute the variance for each column and return
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
42 ## them in a row vector.
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
43 ##
5435
66ad03c58880 [project @ 2005-08-31 20:41:47 by jwe]
jwe
parents: 5428
diff changeset
44 ## The argument @var{opt} determines the type of normalization to use.
66ad03c58880 [project @ 2005-08-31 20:41:47 by jwe]
jwe
parents: 5428
diff changeset
45 ## Valid values are
4849
a3440ff5eb14 [project @ 2004-04-06 15:23:56 by jwe]
jwe
parents: 4171
diff changeset
46 ##
11587
c792872f8942 all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents: 11523
diff changeset
47 ## @table @asis
4849
a3440ff5eb14 [project @ 2004-04-06 15:23:56 by jwe]
jwe
parents: 4171
diff changeset
48 ## @item 0:
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
49 ## 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: 10821
diff changeset
50 ## variance [default]
10821
693e22af08ae Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
51 ##
4849
a3440ff5eb14 [project @ 2004-04-06 15:23:56 by jwe]
jwe
parents: 4171
diff changeset
52 ## @item 1:
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
53 ## normalizes with @math{N}, this provides the second moment around the mean
4849
a3440ff5eb14 [project @ 2004-04-06 15:23:56 by jwe]
jwe
parents: 4171
diff changeset
54 ## @end table
a3440ff5eb14 [project @ 2004-04-06 15:23:56 by jwe]
jwe
parents: 4171
diff changeset
55 ##
20174
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
56 ## If @math{N==1} the value of @var{opt} is ignored and normalization by
d9341b422488 doc: Update more docstrings to have one sentence summary as first line.
Rik <rik@octave.org>
parents: 19833
diff changeset
57 ## @math{N} is used.
14273
bedccd0abe39 Add comment in var.m docs about behaviour when input is a scalar
Carlo de Falco <kingcrimson@tiscali.it>
parents: 14138
diff changeset
58 ##
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
59 ## If the optional argument @var{dim} is given, operate along this dimension.
12575
d0b799dafede Grammarcheck files for 3.4.1 release.
Rik <octave@nomad.inbox5.com>
parents: 11587
diff changeset
60 ## @seealso{cov, std, skewness, kurtosis, moment}
3453
71d2e09c15a2 [project @ 2000-01-18 08:32:09 by jwe]
jwe
parents: 3426
diff changeset
61 ## @end deftypefn
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
62
5428
2a16423e4aa0 [project @ 2005-08-23 18:38:27 by jwe]
jwe
parents: 5307
diff changeset
63 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
3456
434790acb067 [project @ 2000-01-19 06:58:51 by jwe]
jwe
parents: 3453
diff changeset
64 ## Description: Compute variance
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
65
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
66 function retval = var (x, opt = 0, dim)
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
67
4849
a3440ff5eb14 [project @ 2004-04-06 15:23:56 by jwe]
jwe
parents: 4171
diff changeset
68 if (nargin < 1 || nargin > 3)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 6024
diff changeset
69 print_usage ();
4849
a3440ff5eb14 [project @ 2004-04-06 15:23:56 by jwe]
jwe
parents: 4171
diff changeset
70 endif
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
71
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
72 if (! (isnumeric (x) || islogical (x)))
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
73 error ("var: X must be a numeric vector or matrix");
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
74 endif
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
75
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
76 if (isempty (opt))
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
77 opt = 0;
21309
7fbecef105ca Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents: 20852
diff changeset
78 elseif (opt != 0 && opt != 1)
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
79 error ("var: normalization OPT must be 0 or 1");
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
80 endif
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
81
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
82 nd = ndims (x);
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
83 sz = size (x);
4849
a3440ff5eb14 [project @ 2004-04-06 15:23:56 by jwe]
jwe
parents: 4171
diff changeset
84 if (nargin < 3)
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
85 ## Find the first non-singleton dimension.
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
86 (dim = find (sz > 1, 1)) || (dim = 1);
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
87 else
21309
7fbecef105ca Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents: 20852
diff changeset
88 if (! (isscalar (dim) && dim == fix (dim) && dim > 0))
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
89 error ("var: DIM must be an integer and a valid dimension");
4849
a3440ff5eb14 [project @ 2004-04-06 15:23:56 by jwe]
jwe
parents: 4171
diff changeset
90 endif
a3440ff5eb14 [project @ 2004-04-06 15:23:56 by jwe]
jwe
parents: 4171
diff changeset
91 endif
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
92
21309
7fbecef105ca Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents: 20852
diff changeset
93 n = size (x, dim);
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
94 if (n == 1)
14336
34af9f9ff98b Use Octave coding conventions (double-quote " in preference to single quote ')
Rik <octave@nomad.inbox5.com>
parents: 14273
diff changeset
95 if (isa (x, "single"))
34af9f9ff98b Use Octave coding conventions (double-quote " in preference to single quote ')
Rik <octave@nomad.inbox5.com>
parents: 14273
diff changeset
96 retval = zeros (sz, "single");
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
97 else
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
98 retval = zeros (sz);
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
99 endif
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
100 elseif (numel (x) > 0)
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
101 retval = sumsq (center (x, dim), dim) / (n - 1 + opt);
9371
56751dfc6ebd fix var for scalar and empty arguments
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
102 else
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
103 error ("var: X must not be empty");
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
104 endif
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3200
diff changeset
105
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
106 endfunction
9371
56751dfc6ebd fix var for scalar and empty arguments
John W. Eaton <jwe@octave.org>
parents: 9245
diff changeset
107
12656
6b2f14af2360 Overhaul functions in statistics/base directory.
Rik <octave@nomad.inbox5.com>
parents: 12575
diff changeset
108
14363
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14336
diff changeset
109 %!assert (var (13), 0)
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14336
diff changeset
110 %!assert (var (single (13)), single (0))
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14336
diff changeset
111 %!assert (var ([1,2,3]), 1)
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14336
diff changeset
112 %!assert (var ([1,2,3], 1), 2/3, eps)
f3d52523cde1 Use Octave coding conventions in all m-file %!test blocks
Rik <octave@nomad.inbox5.com>
parents: 14336
diff changeset
113 %!assert (var ([1,2,3], [], 1), [0,0,0])
21309
7fbecef105ca Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents: 20852
diff changeset
114 %!assert (var ([1,2,3], [], 3), [0,0,0])
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
115
19833
9fc020886ae9 maint: Clean up m-files to follow Octave coding conventions.
Rik <rik@octave.org>
parents: 19697
diff changeset
116 ## Test input validation
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
117 %!error var ()
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
118 %!error var (1,2,3,4)
21309
7fbecef105ca Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents: 20852
diff changeset
119 %!error <X must be a numeric> var (['A'; 'B'])
7fbecef105ca Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents: 20852
diff changeset
120 %!error <OPT must be 0 or 1> var (1, -1)
7fbecef105ca Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents: 20852
diff changeset
121 %!error <FLAG must be 0 or 1> skewness (1, 2)
7fbecef105ca Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents: 20852
diff changeset
122 %!error <FLAG must be 0 or 1> skewness (1, [1 0])
7fbecef105ca Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents: 20852
diff changeset
123 %!error <DIM must be an integer> var (1, [], ones (2,2))
7fbecef105ca Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents: 20852
diff changeset
124 %!error <DIM must be an integer> var (1, [], 1.5)
7fbecef105ca Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents: 20852
diff changeset
125 %!error <DIM must be .* a valid dimension> var (1, [], 0)
7fbecef105ca Allow statistics functions to work over non-existent dimension (bug #33523).
Rik <rik@octave.org>
parents: 20852
diff changeset
126 %!error <X must not be empty> var ([], 1)
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 10821
diff changeset
127