Mercurial > hg > octave-nkf
annotate scripts/statistics/base/std.m @ 10830:b4ebfd675321
avoid static initialization disaster in dim_vector
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Thu, 29 Jul 2010 08:47:26 +0200 |
parents | 693e22af08ae |
children | e151e23f73bc |
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}) | |
3367 | 24 ## If @var{x} is a vector, compute the standard deviation of the elements |
25 ## of @var{x}. | |
26 ## @tex | |
27 ## $$ | |
6754 | 28 ## {\rm std} (x) = \sigma (x) = \sqrt{{\sum_{i=1}^N (x_i - \bar{x})^2 \over N - 1}} |
3367 | 29 ## $$ |
6754 | 30 ## where $\bar{x}$ is the mean value of $x$. |
3367 | 31 ## @end tex |
6754 | 32 ## @ifnottex |
3426 | 33 ## |
3367 | 34 ## @example |
35 ## @group | |
36 ## std (x) = sqrt (sumsq (x - mean (x)) / (n - 1)) | |
37 ## @end group | |
38 ## @end example | |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
39 ## |
6754 | 40 ## @end ifnottex |
3367 | 41 ## If @var{x} is a matrix, compute the standard deviation for |
42 ## each column and return them in a row vector. | |
4844 | 43 ## |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
9500
diff
changeset
|
44 ## The argument @var{opt} determines the type of normalization to use. Valid |
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
9500
diff
changeset
|
45 ## values are |
4844 | 46 ## |
47 ## @table @asis | |
48 ## @item 0: | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
9500
diff
changeset
|
49 ## normalizes with @math{N-1}, provides the square root of best unbiased |
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
9500
diff
changeset
|
50 ## estimator of the variance [default] |
10821
693e22af08ae
Grammarcheck documentation of m-files
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
51 ## |
4844 | 52 ## @item 1: |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
9500
diff
changeset
|
53 ## normalizes with @math{N}, this provides the square root of the second |
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
9500
diff
changeset
|
54 ## moment around the mean |
4844 | 55 ## @end table |
56 ## | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
9500
diff
changeset
|
57 ## The third argument @var{dim} determines the dimension along which the |
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
9500
diff
changeset
|
58 ## standard |
4844 | 59 ## deviation is calculated. |
5642 | 60 ## @seealso{mean, median} |
3367 | 61 ## @end deftypefn |
3200 | 62 |
63 ## Author: jwe | |
64 | |
4844 | 65 function retval = std (a, opt, dim) |
3200 | 66 |
4844 | 67 if (nargin < 1 || nargin > 3) |
6046 | 68 print_usage (); |
4844 | 69 endif |
9500
bb37822e9b82
std.m: correctly work along singleton dimension
John W. Eaton <jwe@octave.org>
parents:
9211
diff
changeset
|
70 if (nargin < 3) |
6024 | 71 dim = find (size (a) > 1, 1); |
8507 | 72 if (isempty (dim)) |
73 dim = 1; | |
74 endif | |
4844 | 75 endif |
8507 | 76 if (nargin < 2 || isempty (opt)) |
4844 | 77 opt = 0; |
3200 | 78 endif |
79 | |
8977
f464119ec165
further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
80 n = size (a, dim); |
f464119ec165
further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
81 if (n == 1) |
9500
bb37822e9b82
std.m: correctly work along singleton dimension
John W. Eaton <jwe@octave.org>
parents:
9211
diff
changeset
|
82 retval = zeros (size (a)); |
4844 | 83 elseif (numel (a) > 0) |
8977
f464119ec165
further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
84 retval = sqrt (sumsq (center (a, dim), dim) / (n + opt - 1)); |
3200 | 85 else |
8977
f464119ec165
further simplify some stats funcs
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
86 error ("std: x must not be empty"); |
3200 | 87 endif |
88 | |
89 endfunction | |
7411 | 90 |
91 %!test | |
92 %! x = ones (10, 2); | |
93 %! y = [1, 3]; | |
94 %! 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
|
95 %! 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
|
96 %! assert (std (ones (3, 1, 2), 0, 2), zeros (3, 1, 2)) |
7411 | 97 |
98 %!error std (); | |
99 | |
100 %!error std (1, 2, 3, 4); |