annotate scripts/statistics/base/std.m @ 8977:f464119ec165

further simplify some stats funcs
author Jaroslav Hajek <highegg@gmail.com>
date Fri, 13 Mar 2009 21:26:13 +0100
parents eb63fbe60fab
children 1bf0ce0930be
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8920
eb63fbe60fab update copyright notices
John W. Eaton <jwe@octave.org>
parents: 8507
diff changeset
1 ## Copyright (C) 1996, 1997, 1998, 1999, 2000, 2004, 2005, 2006, 2007, 2008,
eb63fbe60fab update copyright notices
John W. Eaton <jwe@octave.org>
parents: 8507
diff changeset
2 ## 2009 John W. Eaton
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
3 ##
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
4 ## This file is part of Octave.
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
5 ##
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
6 ## Octave is free software; you can redistribute it and/or modify it
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
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: 6754
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: 6754
diff changeset
9 ## your option) any later version.
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
10 ##
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
11 ## Octave is distributed in the hope that it will be useful, but
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
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
14 ## General Public License for more details.
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
15 ##
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: 6754
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: 6754
diff changeset
18 ## <http://www.gnu.org/licenses/>.
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
19
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
20 ## -*- texinfo -*-
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
21 ## @deftypefn {Function File} {} std (@var{x})
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
22 ## @deftypefnx {Function File} {} std (@var{x}, @var{opt})
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
23 ## @deftypefnx {Function File} {} std (@var{x}, @var{opt}, @var{dim})
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
24 ## If @var{x} is a vector, compute the standard deviation of the elements
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
25 ## of @var{x}.
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
26 ## @iftex
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
27 ## @tex
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
28 ## $$
6754
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6046
diff changeset
29 ## {\rm std} (x) = \sigma (x) = \sqrt{{\sum_{i=1}^N (x_i - \bar{x})^2 \over N - 1}}
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
30 ## $$
6754
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6046
diff changeset
31 ## where $\bar{x}$ is the mean value of $x$.
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
32 ## @end tex
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
33 ## @end iftex
6754
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6046
diff changeset
34 ## @ifnottex
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3408
diff changeset
35 ##
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
36 ## @example
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
37 ## @group
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
38 ## std (x) = sqrt (sumsq (x - mean (x)) / (n - 1))
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
39 ## @end group
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
40 ## @end example
6754
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6046
diff changeset
41 ## @end ifnottex
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
42 ## If @var{x} is a matrix, compute the standard deviation for
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
43 ## each column and return them in a row vector.
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
44 ##
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
45 ## The argument @var{opt} determines the type of normalization to use. Valid values
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
46 ## are
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
47 ##
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
48 ## @table @asis
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
49 ## @item 0:
6754
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6046
diff changeset
50 ## normalizes with @math{N-1}, provides the square root of best unbiased estimator of
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
51 ## the variance [default]
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
52 ## @item 1:
6754
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6046
diff changeset
53 ## normalizes with @math{N}, this provides the square root of the second moment around
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
54 ## the mean
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
55 ## @end table
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
56 ##
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
57 ## The third argument @var{dim} determines the dimension along which the standard
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
58 ## deviation is calculated.
5642
2618a0750ae6 [project @ 2006-03-06 21:26:48 by jwe]
jwe
parents: 5307
diff changeset
59 ## @seealso{mean, median}
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
60 ## @end deftypefn
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
61
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
62 ## Author: jwe
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
63
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
64 function retval = std (a, opt, dim)
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
65
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
66 if (nargin < 1 || nargin > 3)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 6024
diff changeset
67 print_usage ();
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
68 endif
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
69 if nargin < 3
6024
500d884ae373 [project @ 2006-10-03 14:27:33 by jwe]
jwe
parents: 5642
diff changeset
70 dim = find (size (a) > 1, 1);
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7411
diff changeset
71 if (isempty (dim))
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7411
diff changeset
72 dim = 1;
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7411
diff changeset
73 endif
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
74 endif
8507
cadc73247d65 style fixes
John W. Eaton <jwe@octave.org>
parents: 7411
diff changeset
75 if (nargin < 2 || isempty (opt))
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
76 opt = 0;
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
77 endif
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
78
8977
f464119ec165 further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
79 n = size (a, dim);
f464119ec165 further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
80 if (n == 1)
f464119ec165 further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
81 retval = zeros (sz);
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
82 elseif (numel (a) > 0)
8977
f464119ec165 further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
83 retval = sqrt (sumsq (center (a, dim), dim) / (n + opt - 1));
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
84 else
8977
f464119ec165 further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents: 8920
diff changeset
85 error ("std: x must not be empty");
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
86 endif
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
87
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
88 endfunction
7411
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
89
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
90 %!test
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
91 %! x = ones (10, 2);
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
92 %! y = [1, 3];
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
93 %! assert(std (x) == [0, 0] && abs (std (y) - sqrt (2)) < sqrt (eps));
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
94
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
95 %!error std ();
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
96
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
97 %!error std (1, 2, 3, 4);
83a8781b529d [project @ 2008-01-22 21:52:25 by jwe]
jwe
parents: 7017
diff changeset
98