I have looked into how to calculate the weighted standard deviation or variance of a sample and found this very helpful post referencing the paper by Gatz and Smith that suggests several methods: https://math.stackexchange.com/questions/823125/sampling-error-with-weighted-mean
Now I am trying to understand how Matlab calculates the weighted variance when using "std(A,w)". I looked through the Matlab help and the original code using "edit var", but failed to understand the syntax of how the weighted variance is calculated. Could someone write down the equation for me or describe the crucial lines of below code in a few words? (you find the complete code of the function when you type "edit var" in Matlab).
% Weighted variance
else
if ~isvector(w) || ~isreal(w) || ~isfloat(w) || ...
(omitnan && ~all(w(~isnan(w)) >= 0)) || (~omitnan && ~all(w >= 0))
error(message('MATLAB:var:invalidWgts'));
end
if numel(w) ~= n
if isscalar(w)
error(message('MATLAB:var:invalidWgts'));
else
error(message('MATLAB:var:invalidSizeWgts'));
end
end
if ~omitnan
% Normalize W, and embed it in the right number of dims. Then
% replicate it out along the non-working dims to match X's size.
wresize = ones(1,max(ndims(x),dim)); wresize(dim) = n;
w = reshape(w ./ sum(w), wresize);
y = sum(w .* abs(x - sum(w .* x, dim)).^2, dim); % abs guarantees a real result
else
% Repeat vector W, such that new W has the same size as X
sz = size(x); sz(end+1:dim) = 1;
wresize = ones(size(sz)); wresize(dim) = sz(dim);
wtile = sz; wtile(dim) = 1;
w = repmat(reshape(w, wresize), wtile);
% Count up non-NaN weights at non-NaN elements
w(isnan(x)) = NaN;
denom = sum(w, dim, 'omitnan'); % contains no NaN, since w >= 0
x = x - (sum(w .* x, dim, 'omitnan') ./ denom);
wx2 = w .* abs(x).^2;
y = sum(wx2, dim, 'omitnan') ./ denom; % abs guarantees a real result
% Don't omit NaNs caused by computation (not missing data)
ind = any(isnan(wx2) & ~isnan(w), dim);
y(ind) = NaN;