I have a simple time series and I am struggling to estimate the variance within a moving window. More specifically, I cannot figure some issues out relating to the way of implementing a sliding window function. For example, when using NumPy and window size = 20:
def rolling_window(a, window):
shape = a.shape[:-1] + (a.shape[-1] - window + 1, window)
strides = a.strides + (a.strides[-1],)
return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)
rolling_window(data, 20)
np.var(rolling_window(data, 20), -1)
datavar=np.var(rolling_window(data, 20), -1)
Perhaps I am mistaken somewhere, in this line of thought. Does anyone know a straightforward way to do this? Any help/advice would be most welcome.
The Pandas
rolling_mean
androlling_std
functions have been deprecated and replaced by a more general "rolling" framework. @elyase's example can be modified to:The
rolling
function supports a number of different window types, as documented here. A number of functions can be called on therolling
object, includingvar
and other interesting statistics (skew
,kurt
,quantile
, etc.). I've stuck withstd
since the plot is on the same graph as the mean, which makes more sense unit-wise.Despite being an old thread, I'll add another method modified from this, that doesn't rely on pandas, nor python loops. Essentially, using numpy's stride tricks you can first create a view of an array with striding such that computing a statistic of the function along the last axis is equivalent to performing the rolling statistic. I've modified the original code so that the output shape is the same as the input shape by padding add the start of the last axis.
You should take a look at pandas. For example: