1 ## Copyright (C) 2001 Paul Kienzle <pkienzle@users.sf.net>
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
17 ## @deftypefn {Function File} {@var{v} =} nanstd (@var{X})
18 ## @deftypefnx{Function File} {@var{v} =} nanstd (@var{X}, @var{opt})
19 ## @deftypefnx{Function File} {@var{v} =} nanstd (@var{X}, @var{opt}, @var{dim})
20 ## Compute the standard deviation while ignoring NaN values.
22 ## @code{nanstd} is identical to the @code{std} function except that NaN values are
23 ## ignored. If all values are NaN, the standard deviation is returned as NaN.
24 ## If there is only a single non-NaN value, the deviation is returned as 0.
26 ## The argument @var{opt} determines the type of normalization to use. Valid values
31 ## normalizes with @math{N-1}, provides the square root of best unbiased estimator of
32 ## the variance [default]
34 ## normalizes with @math{N}, this provides the square root of the second moment around
38 ## The third argument @var{dim} determines the dimension along which the standard
39 ## deviation is calculated.
41 ## @seealso{std, nanmin, nanmax, nansum, nanmedian, nanmean}
44 function v = nanstd (X, opt, varargin)
49 dim = min(find(size(X)>1));
50 if isempty(dim), dim=1; endif;
54 if ((nargin < 2) || isempty(opt))
58 ## determine the number of non-missing points in each data set
59 n = sum (!isnan(X), varargin{:});
61 ## replace missing data with zero and compute the mean
63 meanX = sum (X, varargin{:}) ./ n;
65 ## subtract the mean from the data and compute the sum squared
66 sz = ones(1,length(size(X)));
67 sz(dim) = size(X,dim);
68 v = sumsq (X - repmat(meanX,sz), varargin{:});
70 ## because the missing data was set to zero each missing data
71 ## point will contribute (-meanX)^2 to sumsq, so remove these
72 v = v - (meanX .^ 2) .* (size(X,dim) - n);
75 ## compute the standard deviation from the corrected sumsq using
76 ## max(n-1,1) in the denominator so that the std for a single point is 0
77 v = sqrt ( v ./ max(n - 1, 1) );
79 ## compute the standard deviation from the corrected sumsq
82 error ("std: unrecognized normalization type");