# Weighted moving variance

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 ).

• What do you mean by "exponentially weighted moving average"? A kernel density estimator with a Gaussian kernel? – cdalitz Sep 18 '19 at 13:42

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);