annotate scripts/statistics/base/statistics.m @ 11436:e151e23f73bc

Overhaul base statistics functions and documentation of same. Add or improve input validation. Add input validation tests. Add functional tests. Improve or re-write documentation strings.
author Rik <octave@nomad.inbox5.com>
date Mon, 03 Jan 2011 21:23:08 -0800
parents fe3c3dfc07eb
children fd0a3ac60b0e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7017
a1dbe9d80eee [project @ 2007-10-12 21:27:11 by jwe]
jwe
parents: 7016
diff changeset
1 ## Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2004, 2005,
8920
eb63fbe60fab update copyright notices
John W. Eaton <jwe@octave.org>
parents: 8430
diff changeset
2 ## 2006, 2007, 2008 Kurt Hornik
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3273
diff changeset
3 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3456
diff changeset
4 ## This file is part of Octave.
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3456
diff changeset
5 ##
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3456
diff changeset
6 ## 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
7 ## 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: 6046
diff changeset
8 ## the Free Software Foundation; either version 3 of the License, or (at
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
9 ## your option) any later version.
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3273
diff changeset
10 ##
3922
38c61cbf086c [project @ 2002-05-01 06:48:35 by jwe]
jwe
parents: 3456
diff changeset
11 ## 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
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3273
diff changeset
14 ## General Public License for more details.
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3273
diff changeset
15 ##
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
16 ## 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: 6046
diff changeset
17 ## along with Octave; see the file COPYING. If not, see
93c65f2a5668 [project @ 2007-10-12 06:40:56 by jwe]
jwe
parents: 6046
diff changeset
18 ## <http://www.gnu.org/licenses/>.
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
19
3453
71d2e09c15a2 [project @ 2000-01-18 08:32:09 by jwe]
jwe
parents: 3426
diff changeset
20 ## -*- texinfo -*-
10669
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
21 ## @deftypefn {Function File} {} statistics (@var{x})
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
22 ## @deftypefnx {Function File} {} statistics (@var{x}, @var{dim})
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
23 ## Return a vector with the minimum, first quartile, median, third quartile,
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
24 ## maximum, mean, standard deviation, skewness, and kurtosis of the elements of
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
25 ## the vector @var{x}.
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
26 ##
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
27 ## If @var{x} is a matrix, calculate statistics over the first
7643
0220da981c2a Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
28 ## non-singleton dimension.
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
29 ## If the optional argument @var{dim} is given, operate along this dimension.
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
30 ## @seealso{min,max,median,mean,std,skewness,kurtosis}
3453
71d2e09c15a2 [project @ 2000-01-18 08:32:09 by jwe]
jwe
parents: 3426
diff changeset
31 ## @end deftypefn
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3273
diff changeset
32
5428
2a16423e4aa0 [project @ 2005-08-23 18:38:27 by jwe]
jwe
parents: 5307
diff changeset
33 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
3456
434790acb067 [project @ 2000-01-19 06:58:51 by jwe]
jwe
parents: 3453
diff changeset
34 ## Description: Compute basic statistics
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
35
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
36 function stats = statistics (x, dim)
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3273
diff changeset
37
4885
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 4030
diff changeset
38 if (nargin != 1 && nargin != 2)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 5428
diff changeset
39 print_usage ();
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
40 endif
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
41
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
42 if (!isnumeric(x))
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
43 error ("statistics: X must be a numeric vector or matrix");
10669
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
44 endif
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
45
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
46 nd = ndims (x);
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
47 sz = size (x);
4885
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 4030
diff changeset
48 if (nargin != 2)
4886
54b076a24718 [project @ 2004-04-30 04:31:51 by jwe]
jwe
parents: 4885
diff changeset
49 ## Find the first non-singleton dimension.
10669
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
50 dim = find (sz > 1, 1);
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
51 if (isempty (dim))
4885
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 4030
diff changeset
52 dim = 1;
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 4030
diff changeset
53 endif
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 4030
diff changeset
54 else
11149
fe3c3dfc07eb style fix: break lines before && and ||, not after
John W. Eaton <jwe@octave.org>
parents: 10669
diff changeset
55 if (!(isscalar (dim) && dim == round (dim))
fe3c3dfc07eb style fix: break lines before && and ||, not after
John W. Eaton <jwe@octave.org>
parents: 10669
diff changeset
56 || !(1 <= dim && dim <= nd))
10669
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
57 error ("statistics: DIM must be an integer and a valid dimension");
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
58 endif
4885
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 4030
diff changeset
59 endif
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 4030
diff changeset
60
10669
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
61 if (sz(dim) < 2)
cab3b148d4e4 Improve validation of input arguments for base statistics functions.
Rik <octave@nomad.inbox5.com>
parents: 10549
diff changeset
62 error ("statistics: dimension of X is too small (<2)");
4885
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 4030
diff changeset
63 endif
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 4030
diff changeset
64
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
65 emp_inv = quantile (x, [0.25; 0.5; 0.75], dim, 7);
4885
28ab079d8f0e [project @ 2004-04-30 04:21:33 by jwe]
jwe
parents: 4030
diff changeset
66
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
67 stats = cat (dim, min (x, [], dim), emp_inv, max (x, [], dim), mean (x, dim),
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
68 std (x, [], dim), skewness (x, dim), kurtosis (x, dim));
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3273
diff changeset
69
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
70 endfunction
7643
0220da981c2a Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
71
0220da981c2a Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
72 %!test
0220da981c2a Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
73 %! x = rand(7,5);
0220da981c2a Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
74 %! s = statistics (x);
0220da981c2a Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
75 %! m = median (x);
0220da981c2a Modified statistics to calculate consistent median.
Ben Abbott <bpabbott@mac.com>
parents: 7017
diff changeset
76 %! assert (m, s(3,:), eps);
11436
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
77
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
78 %% Test input validation
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
79 %!error statistics ()
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
80 %!error statistics (1, 2, 3)
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
81 %!error statistics ([true true])
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
82 %!error statistics (1, ones(2,2))
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
83 %!error statistics (1, 1.5)
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
84 %!error statistics (1, 0)
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
85 %!error statistics (1, 3)
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
86 %!error statistics (1)
e151e23f73bc Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents: 11149
diff changeset
87