-
Notifications
You must be signed in to change notification settings - Fork 2
/
nanvar.m
83 lines (72 loc) · 3.2 KB
/
nanvar.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
function y = nanvar(x,w,dim)
%NANVAR Variance, ignoring NaNs.
% Y = NANVAR(X) returns the sample variance of the values in X, treating
% NaNs as missing values. For a vector input, Y is the variance of the
% non-NaN elements of X. For a matrix input, Y is a row vector
% containing the variance of the non-NaN elements in each column of X.
% For N-D arrays, NANVAR operates along the first non-singleton dimension
% of X.
%
% NANVAR normalizes Y by N-1 if N>1, where N is the sample size of the
% non-NaN elements. This is an unbiased estimator of the variance of the
% population from which X is drawn, as long as X consists of independent,
% identically distributed samples, and data are missing at random. For
% N=1, Y is normalized by N.
%
% Y = NANVAR(X,1) normalizes by N and produces the second moment of the
% sample about its mean. NANVAR(X,0) is the same as NANVAR(X).
%
% Y = NANVAR(X,W) computes the variance using the weight vector W. The
% length of W must equal the length of the dimension over which NANVAR
% operates, and its non-NaN elements must be nonnegative. Elements of X
% corresponding to NaN elements of W are ignored.
%
% Y = NANVAR(X,W,DIM) takes the variance along dimension DIM of X.
%
% See also VAR, NANSTD, NANMEAN, NANMEDIAN, NANMIN, NANMAX, NANSUM.
% Copyright 1984-2005 The MathWorks, Inc.
% $Revision: 1.1.6.4 $ $Date: 2005/03/23 20:25:41 $
if nargin < 2 || isempty(w), w = 0; end
sz = size(x);
if nargin < 3 || isempty(dim)
% The output size for [] is a special case when DIM is not given.
if isequal(x,[]), y = NaN(class(x)); return; end
% Figure out which dimension sum will work along.
dim = find(sz ~= 1, 1);
if isempty(dim), dim = 1; end
elseif dim > length(sz)
sz(end+1:dim) = 1;
end
% Need to tile the mean of X to center it.
tile = ones(size(sz));
tile(dim) = sz(dim);
if isequal(w,0) || isequal(w,1)
% Count up non-NaNs.
n = sum(~isnan(x),dim);
if w == 0
% The unbiased estimator: divide by (n-1). Can't do this when
% n == 0 or 1, so n==1 => we'll return zeros
denom = max(n-1, 1);
else
% The biased estimator: divide by n.
denom = n; % n==1 => we'll return zeros
end
denom(n==0) = NaN; % Make all NaNs return NaN, without a divideByZero warning
x0 = x - repmat(nanmean(x, dim), tile);
y = nansum(abs(x0).^2, dim) ./ denom; % abs guarantees a real result
% Weighted variance
elseif numel(w) ~= sz(dim)
error('MATLAB:nanvar:InvalidSizeWgts','The length of W must be compatible with X.');
elseif ~(isvector(w) && all(w(~isnan(w)) >= 0))
error('MATLAB:nanvar:InvalidWgts','W must be a vector of nonnegative weights, or a scalar 0 or 1.');
else
% Embed W in the right number of dims. Then replicate it out along the
% non-working dims to match X's size.
wresize = ones(size(sz)); wresize(dim) = sz(dim);
wtile = sz; wtile(dim) = 1;
w = repmat(reshape(w, wresize), wtile);
% Count up non-NaNs.
n = nansum(~isnan(x).*w,dim);
x0 = x - repmat(nansum(w.*x, dim) ./ n, tile);
y = nansum(w .* abs(x0).^2, dim) ./ n; % abs guarantees a real result
end