Mercurial > hg > octave-lyh
annotate scripts/statistics/base/std.m @ 9051:1bf0ce0930be
Grammar check TexInfo in all .m files
Cleanup documentation sources to follow a few consistent rules.
Spellcheck was NOT done. (but will be in another changeset)
author | Rik <rdrider0-list@yahoo.com> |
---|---|
date | Fri, 27 Mar 2009 22:31:03 -0700 |
parents | f464119ec165 |
children | f0c3d3fc4903 |
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 -*- |
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 ## @iftex | |
27 ## @tex | |
28 ## $$ | |
6754 | 29 ## {\rm std} (x) = \sigma (x) = \sqrt{{\sum_{i=1}^N (x_i - \bar{x})^2 \over N - 1}} |
3367 | 30 ## $$ |
6754 | 31 ## where $\bar{x}$ is the mean value of $x$. |
3367 | 32 ## @end tex |
33 ## @end iftex | |
6754 | 34 ## @ifnottex |
3426 | 35 ## |
3367 | 36 ## @example |
37 ## @group | |
38 ## std (x) = sqrt (sumsq (x - mean (x)) / (n - 1)) | |
39 ## @end group | |
40 ## @end example | |
6754 | 41 ## @end ifnottex |
3367 | 42 ## If @var{x} is a matrix, compute the standard deviation for |
43 ## each column and return them in a row vector. | |
4844 | 44 ## |
9051
1bf0ce0930be
Grammar check TexInfo in all .m files
Rik <rdrider0-list@yahoo.com>
parents:
8977
diff
changeset
|
45 ## The argument @var{opt} determines the type of normalization to use. Valid values |
4844 | 46 ## are |
47 ## | |
48 ## @table @asis | |
49 ## @item 0: | |
6754 | 50 ## normalizes with @math{N-1}, provides the square root of best unbiased estimator of |
4844 | 51 ## the variance [default] |
52 ## @item 1: | |
6754 | 53 ## normalizes with @math{N}, this provides the square root of the second moment around |
4844 | 54 ## the mean |
55 ## @end table | |
56 ## | |
57 ## The third argument @var{dim} determines the dimension along which the standard | |
58 ## deviation is calculated. | |
5642 | 59 ## @seealso{mean, median} |
3367 | 60 ## @end deftypefn |
3200 | 61 |
62 ## Author: jwe | |
63 | |
4844 | 64 function retval = std (a, opt, dim) |
3200 | 65 |
4844 | 66 if (nargin < 1 || nargin > 3) |
6046 | 67 print_usage (); |
4844 | 68 endif |
69 if nargin < 3 | |
6024 | 70 dim = find (size (a) > 1, 1); |
8507 | 71 if (isempty (dim)) |
72 dim = 1; | |
73 endif | |
4844 | 74 endif |
8507 | 75 if (nargin < 2 || isempty (opt)) |
4844 | 76 opt = 0; |
3200 | 77 endif |
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 | 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 | 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 | 86 endif |
87 | |
88 endfunction | |
7411 | 89 |
90 %!test | |
91 %! x = ones (10, 2); | |
92 %! y = [1, 3]; | |
93 %! assert(std (x) == [0, 0] && abs (std (y) - sqrt (2)) < sqrt (eps)); | |
94 | |
95 %!error std (); | |
96 | |
97 %!error std (1, 2, 3, 4); | |
98 |