1
$\begingroup$

i have a time-series and, in analogy with exponentially weighted moving average, i would like to compute the exponentially weighted moving standard deviation or variance in an efficient, numerically stable manner. This is in Octave/MATLAB so vectorized is better than looping.

An incremental algorithm for computing weighted variance in a numerically stable manner is given at https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Weighted_incremental_algorithm but perhaps there is a more efficient vectorized batch solution in the context of Octave/MATLAB? Wikipedia warns that the naive algorithm is numerically unstable ( https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Na%C3%AFve_algorithm ).

$\endgroup$
  • $\begingroup$ What do you mean by "exponentially weighted moving average"? A kernel density estimator with a Gaussian kernel? $\endgroup$ – cdalitz 2 hours ago
1
$\begingroup$

I believe that we can compute using the vectorized form, assuming a vector of weights w and a vector of values x:

wSum = sum(w);
meanx = sum(w .* x) / wSum;
varx = ( sum(w .* x .* x) - wSum * meanx * meanx ) / (wSum - 1);
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.