1 function [o,v]=std(x,opt,DIM,W)
2 % STD calculates the standard deviation.
4 % [y,v] = std(x [, opt[, DIM [, W]]])
7 % 0: normalizes with N-1 [default]
8 % provides the square root of best unbiased estimator of the variance
9 % 1: normalizes with N,
10 % this provides the square root of the second moment around the mean
12 % best unbiased estimator of the standard deviation (see [1])
15 % N STD of N-th dimension
16 % default or []: first DIMENSION, with more than 1 element
17 % W weights to compute weighted s.d. (default: [])
18 % if W=[], all weights are 1.
19 % number of elements in W must match size(x,DIM)
21 % y estimated standard deviation
24 % - provides an unbiased estimation of the S.D.
25 % - can deal with NaN's (missing values)
27 % - dimension argument also in Octave
28 % - compatible to Matlab and Octave
30 % see also: RMS, SUMSKIPNAN, MEAN, VAR, MEANSQ,
34 % [1] http://mathworld.wolfram.com/StandardDeviationDistribution.html
37 % This program is free software; you can redistribute it and/or modify
38 % it under the terms of the GNU General Public License as published by
39 % the Free Software Foundation; either version 3 of the License, or
40 % (at your option) any later version.
42 % This program is distributed in the hope that it will be useful,
43 % but WITHOUT ANY WARRANTY; without even the implied warranty of
44 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
45 % GNU General Public License for more details.
47 % You should have received a copy of the GNU General Public License
48 % along with this program; If not, see <http://www.gnu.org/licenses/>.
50 % $Id: std.m 8223 2011-04-20 09:16:06Z schloegl $
51 % Copyright (C) 2000-2003,2006,2009,2010 by Alois Schloegl <alois.schloegl@gmail.com>
52 % This is part of the NaN-toolbox for Octave and Matlab
53 % http://pub.ist.ac.at/~schloegl/matlab/NaN/
62 DIM = find(size(x)>1,1);
63 if isempty(DIM), DIM=1; end;
67 [y,n,ssq] = sumskipnan(x,DIM,W);
68 if all(ssq(:).*n(:) > 2*(y(:).^2))
69 %% rounding error is neglectable
72 %% rounding error is not neglectable
75 if length(szy)<length(szx);
76 szy(length(szy)+1:length(szx)) = 1;
78 [y,n] = sumskipnan((x-repmat(y./n,szx./szy)).^2,DIM,W);
91 % square root if the best unbiased estimator of the variance
93 o = sqrt(y./max(n-1,0)); % normalize
100 % best unbiased estimator of the mean
101 if exist('unique','file'),
102 % usually only a few n's differ
103 [N,tmp,tix] = unique(n(:)); % compress n and calculate ib(n)
104 ib = sqrt(N/2).*gamma((N-1)./2)./gamma(N./2); %inverse b(n) [1]
105 ib = ib(reshape(tix,size(y))); % expand ib to correct size
107 elseif exist('histo3','file'),
108 % usually only a few n's differ
109 [N,tix] = histo3(n(:)); N = N.X;
110 ib = sqrt(N/2).*gamma((N-1)./2)./gamma(N./2); %inverse b(n) [1]
111 ib = ib(reshape(tix,size(y))); % expand ib to correct size
113 else % gamma is called prod(size(n)) times
114 ib = sqrt(n/2).*gamma((n-1)./2)./gamma(n./2); %inverse b(n) [1]
116 ib = reshape(ib,size(y));
121 v = y.*((max(n-1,0)./(n.*n))-1./(n.*ib.*ib)); % variance of the estimated S.D. ??? needs further checks