annotate scripts/statistics/base/std.m @ 7016:93c65f2a5668

[project @ 2007-10-12 06:40:56 by jwe]
author jwe
date Fri, 12 Oct 2007 06:41:26 +0000
parents 451b346d8c2f
children a1dbe9d80eee
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
1 ## Copyright (C) 1996, 1997 John W. Eaton
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
2 ##
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
3 ## This file is part of Octave.
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
4 ##
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
5 ## 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
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.
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
9 ##
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
10 ## 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
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
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
13 ## General Public License for more details.
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
14 ##
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 -*-
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
20 ## @deftypefn {Function File} {} std (@var{x})
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
21 ## @deftypefnx {Function File} {} std (@var{x}, @var{opt})
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
22 ## @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
23 ## 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
24 ## of @var{x}.
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
25 ## @iftex
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
26 ## @tex
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
27 ## $$
6754
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6046
diff changeset
28 ## {\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
29 ## $$
6754
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6046
diff changeset
30 ## 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
31 ## @end tex
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
32 ## @end iftex
6754
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6046
diff changeset
33 ## @ifnottex
3426
f8dde1807dee [project @ 2000-01-13 08:40:00 by jwe]
jwe
parents: 3408
diff changeset
34 ##
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
35 ## @example
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
36 ## @group
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
37 ## std (x) = sqrt (sumsq (x - mean (x)) / (n - 1))
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
38 ## @end group
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
39 ## @end example
6754
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6046
diff changeset
40 ## @end ifnottex
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
41 ## 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
42 ## 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
43 ##
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
44 ## 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
45 ## are
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
46 ##
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
47 ## @table @asis
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
48 ## @item 0:
6754
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6046
diff changeset
49 ## 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
50 ## the variance [default]
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
51 ## @item 1:
6754
451b346d8c2f [project @ 2007-06-25 17:31:46 by jwe]
jwe
parents: 6046
diff changeset
52 ## 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
53 ## the mean
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
54 ## @end table
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
55 ##
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
56 ## 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
57 ## deviation is calculated.
5642
2618a0750ae6 [project @ 2006-03-06 21:26:48 by jwe]
jwe
parents: 5307
diff changeset
58 ## @seealso{mean, median}
3367
0748b03c3510 [project @ 1999-11-20 14:52:38 by jwe]
jwe
parents: 3200
diff changeset
59 ## @end deftypefn
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
60
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
61 ## Author: jwe
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
62
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
63 function retval = std (a, opt, dim)
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
64
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
65 if (nargin < 1 || nargin > 3)
6046
34f96dd5441b [project @ 2006-10-10 16:10:25 by jwe]
jwe
parents: 6024
diff changeset
66 print_usage ();
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
67 endif
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
68 if nargin < 3
6024
500d884ae373 [project @ 2006-10-03 14:27:33 by jwe]
jwe
parents: 5642
diff changeset
69 dim = find (size (a) > 1, 1);
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
70 if isempty(dim), dim=1; endif;
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
71 endif
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
72 if ((nargin < 2) || isempty(opt))
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
73 opt = 0;
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
74 endif
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
75
4844
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
76 sz = size(a);
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
77 if (sz (dim) == 1)
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
78 retval = zeros(sz);
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
79 elseif (numel (a) > 0)
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
80 rng = ones (1, length (sz));
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
81 rng (dim) = sz (dim);
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
82 if (opt == 0)
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
83 retval = sqrt (sumsq (a - repmat(mean (a, dim), rng), dim) / (sz(dim) - 1));
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
84 else
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
85 retval = sqrt (sumsq (a - repmat(mean (a, dim), rng), dim) / sz(dim));
9f7ef92b50b0 [project @ 2004-04-02 17:26:53 by jwe]
jwe
parents: 3426
diff changeset
86 endif
3200
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
87 else
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
88 error ("std: invalid matrix argument");
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
89 endif
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
90
781c930425fd [project @ 1998-10-29 05:23:08 by jwe]
jwe
parents:
diff changeset
91 endfunction