Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 814
Barry Avatar asked Dec 11 '14 16:12

Barry


People also ask

How do you find the variance in Python?

Using Python's pvariance() and variance() variance() are the functions that we can use to calculate the variance of a population and of a sample respectively. We just need to import the statistics module and then call pvariance() with our data as an argument. That will return the variance of the population.

How do you calculate moving average in python?

Method 1: Using Numpy It provides a method called numpy. cumsum() which returns the array of the cumulative sum of elements of the given array. A moving average can be calculated by dividing the cumulative sum of elements by window size.

How does Python Numpy calculate variance?

var() in Python. numpy. var(arr, axis = None) : Compute the variance of the given data (array elements) along the specified axis(if any). This Result is Variance.


2 Answers

The Pandas rolling_mean and rolling_std functions have been deprecated and replaced by a more general "rolling" framework. @elyase's example can be modified to:

import pandas as pd import numpy as np %matplotlib inline  # 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 ts.rolling(window=60).mean().plot(style='k')  # add the 20 day rolling standard deviation: ts.rolling(window=20).std().plot(style='b') 

The rolling function supports a number of different window types, as documented here. A number of functions can be called on the rolling object, including var and other interesting statistics (skew, kurt, quantile, etc.). I've stuck with std since the plot is on the same graph as the mean, which makes more sense unit-wise.

like image 133
sfjac Avatar answered Oct 12 '22 10:10

sfjac


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

like image 31
elyase Avatar answered Oct 12 '22 11:10

elyase