]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/base/std.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / base / std.m
1 ## Copyright (C) 1996-2012 John W. Eaton
2 ##
3 ## This file is part of Octave.
4 ##
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
9 ##
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 ## General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING.  If not, see
17 ## <http://www.gnu.org/licenses/>.
18
19 ## -*- texinfo -*-
20 ## @deftypefn  {Function File} {} std (@var{x})
21 ## @deftypefnx {Function File} {} std (@var{x}, @var{opt})
22 ## @deftypefnx {Function File} {} std (@var{x}, @var{opt}, @var{dim})
23 ## Compute the standard deviation of the elements of the vector @var{x}.
24 ## @tex
25 ## $$
26 ## {\rm std} (x) = \sigma = \sqrt{{\sum_{i=1}^N (x_i - \bar{x})^2 \over N - 1}}
27 ## $$
28 ## where $\bar{x}$ is the mean value of $x$ and $N$ is the number of elements.
29 ## @end tex
30 ## @ifnottex
31 ##
32 ## @example
33 ## @group
34 ## std (x) = sqrt ( 1/(N-1) SUM_i (x(i) - mean(x))^2 )
35 ## @end group
36 ## @end example
37 ##
38 ## @noindent
39 ## where @math{N} is the number of elements.
40 ## @end ifnottex
41 ##
42 ## If @var{x} is a matrix, compute the standard deviation for
43 ## each column and return them in a row vector.
44 ##
45 ## The argument @var{opt} determines the type of normalization to use.
46 ## Valid values are
47 ##
48 ## @table @asis
49 ## @item 0:
50 ##   normalize with @math{N-1}, provides the square root of the best unbiased
51 ## estimator of the variance [default]
52 ##
53 ## @item 1:
54 ##   normalize with @math{N}, this provides the square root of the second
55 ## moment around the mean
56 ## @end table
57 ##
58 ## If the optional argument @var{dim} is given, operate along this dimension.
59 ## @seealso{var, range, iqr, mean, median}
60 ## @end deftypefn
61
62 ## Author: jwe
63
64 function retval = std (x, opt = 0, dim)
65
66   if (nargin < 1 || nargin > 3)
67     print_usage ();
68   endif
69
70   if (! (isnumeric (x) || islogical (x)))
71     error ("std: X must be a numeric vector or matrix");
72   endif
73
74   if (isempty (opt))
75     opt = 0;
76   endif
77   if (opt != 0 && opt != 1)
78     error ("std: normalization OPT must be 0 or 1");
79   endif
80
81   nd = ndims (x);
82   sz = size (x);
83   if (nargin < 3)
84     ## Find the first non-singleton dimension.
85     (dim = find (sz > 1, 1)) || (dim = 1);
86   else
87     if (!(isscalar (dim) && dim == fix (dim))
88         || !(1 <= dim && dim <= nd))
89       error ("std: DIM must be an integer and a valid dimension");
90     endif
91   endif
92
93   n = sz(dim);
94   if (n == 1 || isempty (x))
95     if (isa (x, 'single'))
96       retval = zeros (sz, 'single');
97     else
98       retval = zeros (sz);
99     endif
100   else
101     retval = sqrt (sumsq (center (x, dim), dim) / (n - 1 + opt));
102   endif
103
104 endfunction
105
106
107 %!test
108 %! x = ones (10, 2);
109 %! y = [1, 3];
110 %! assert(std (x) == [0, 0]);
111 %! assert(std (y), sqrt (2), sqrt (eps));
112 %! assert(std (x, 0, 2), zeros (10, 1));
113
114 %!assert(std (ones (3, 1, 2), 0, 2), zeros (3, 1, 2));
115 %!assert(std ([1 2], 0), sqrt(2)/2, 5*eps);
116 %!assert(std ([1 2], 1), 0.5, 5*eps);
117 %!assert(std(1), 0);
118 %!assert(std(single(1)), single(0));
119 %!assert(std([]), []);
120 %!assert(std(ones (1,3,0,2)), ones (1,3,0,2));
121
122 %% Test input validation
123 %!error std ();
124 %!error std (1, 2, 3, 4);
125 %!error std (['A'; 'B'])
126 %!error std (1, -1);
127