Search code examples
pythonnumpytime-seriesvariancesliding-window

How can I simply calculate the rolling/moving variance of a time series in python?


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.


Solution

  • You should take a look at pandas. For example:

    import pandas as pd
    import numpy as np
    
    # some sample data
    ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000)).cumsum()
    
    #plot the time series
    ts.plot(style='k--')
    
    # calculate a 60 day rolling mean and plot
    pd.rolling_mean(ts, 60).plot(style='k')
    
    # add the 20 day rolling variance:
    pd.rolling_std(ts, 20).plot(style='b')
    

    enter image description here