Mercurial > hg > octave-lyh
annotate scripts/statistics/base/std.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 | 693e22af08ae |
children | fd0a3ac60b0e |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 1996, 1997, 1998, 1999, 2000, 2004, 2005, 2006, 2007, 2008, |
2 ## 2009 John W. Eaton | |
3200 | 3 ## |
4 ## This file is part of Octave. | |
5 ## | |
6 ## Octave is free software; you can redistribute it and/or modify it | |
7 ## under the terms of the GNU General Public License as published by | |
7016 | 8 ## the Free Software Foundation; either version 3 of the License, or (at |
9 ## your option) any later version. | |
3200 | 10 ## |
11 ## Octave is distributed in the hope that it will be useful, but | |
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 ## General Public License for more details. | |
15 ## | |
16 ## You should have received a copy of the GNU General Public License | |
7016 | 17 ## along with Octave; see the file COPYING. If not, see |
18 ## <http://www.gnu.org/licenses/>. | |
3200 | 19 |
3367 | 20 ## -*- texinfo -*- |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
9500
diff
changeset
|
21 ## @deftypefn {Function File} {} std (@var{x}) |
4844 | 22 ## @deftypefnx {Function File} {} std (@var{x}, @var{opt}) |
23 ## @deftypefnx {Function File} {} std (@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
|
24 ## Compute the standard deviation of the elements of the vector @var{x}. |
3367 | 25 ## @tex |
26 ## $$ | |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
27 ## {\rm std} (x) = \sigma = \sqrt{{\sum_{i=1}^N (x_i - \bar{x})^2 \over N - 1}} |
3367 | 28 ## $$ |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
29 ## where $\bar{x}$ is the mean value of $x$ and $N$ is the number of elements. |
3367 | 30 ## @end tex |
6754 | 31 ## @ifnottex |
3426 | 32 ## |
3367 | 33 ## @example |
34 ## @group | |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
35 ## std (x) = sqrt ( 1/(N-1) SUM_i (x(i) - mean(x))^2 ) |
3367 | 36 ## @end group |
37 ## @end example | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
38 ## |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
39 ## @noindent |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
40 ## where @math{N} is the number of elements. |
6754 | 41 ## @end ifnottex |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
42 ## |
3367 | 43 ## If @var{x} is a matrix, compute the standard deviation for |
44 ## each column and return them in a row vector. | |
4844 | 45 ## |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
46 ## 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:
10821
diff
changeset
|
47 ## Valid values are |
4844 | 48 ## |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
49 ## @table @asis |
4844 | 50 ## @item 0: |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
51 ## normalize with @math{N-1}, provides the square root of the best unbiased |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
52 ## estimator of the variance [default] |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
53 ## |
4844 | 54 ## @item 1: |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
55 ## normalize with @math{N}, this provides the square root of the second |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
56 ## moment around the mean |
4844 | 57 ## @end table |
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. |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
60 ## @seealso{var, range, iqr, mean, median} |
3367 | 61 ## @end deftypefn |
3200 | 62 |
63 ## Author: jwe | |
64 | |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
65 function retval = std (x, opt = 0, dim) |
3200 | 66 |
4844 | 67 if (nargin < 1 || nargin > 3) |
6046 | 68 print_usage (); |
4844 | 69 endif |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
70 |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
71 if (! (isnumeric (x))) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
72 error ("std: 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
|
73 endif |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
74 |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
75 if (isempty (opt)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
76 opt = 0; |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
77 endif |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
78 if (opt != 0 && opt != 1) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
79 error ("std: 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 |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
82 sz = size (x); |
9500
bb37822e9b82
std.m: correctly work along singleton dimension
John W. Eaton <jwe@octave.org>
parents:
9211
diff
changeset
|
83 if (nargin < 3) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
84 ## Find the first non-singleton dimension. |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
85 dim = find (sz > 1, 1); |
8507 | 86 if (isempty (dim)) |
87 dim = 1; | |
88 endif | |
4844 | 89 endif |
3200 | 90 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
91 n = size (x, dim); |
8977
f464119ec165
further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
92 if (n == 1) |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
93 retval = zeros (sz); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
94 elseif (numel (x) > 0) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
95 retval = sqrt (sumsq (center (x, dim), dim) / (n - 1 + opt)); |
3200 | 96 else |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
97 error ("std: X must not be empty"); |
3200 | 98 endif |
99 | |
100 endfunction | |
7411 | 101 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
102 |
7411 | 103 %!test |
104 %! x = ones (10, 2); | |
105 %! y = [1, 3]; | |
106 %! assert(std (x) == [0, 0] && abs (std (y) - sqrt (2)) < sqrt (eps)); | |
9500
bb37822e9b82
std.m: correctly work along singleton dimension
John W. Eaton <jwe@octave.org>
parents:
9211
diff
changeset
|
107 %! assert (std (x, 0, 3), zeros (10, 2)) |
bb37822e9b82
std.m: correctly work along singleton dimension
John W. Eaton <jwe@octave.org>
parents:
9211
diff
changeset
|
108 %! assert (std (ones (3, 1, 2), 0, 2), zeros (3, 1, 2)) |
7411 | 109 |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
110 %% Test input validation |
7411 | 111 %!error std (); |
11436
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
112 %!error std (1, 2, 3, 4); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
113 %!error std (true(1,2)) |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
114 %!error std (1, -1); |
e151e23f73bc
Overhaul base statistics functions and documentation of same.
Rik <octave@nomad.inbox5.com>
parents:
10821
diff
changeset
|
115 %!error std ([], 1); |
7411 | 116 |