How can I just calculate the rolling / moving speed of a time series in python?

I have a simple time series, and I'm struggling to estimate the variance in a moving window. In particular, I can’t understand some of the problems associated with the way the sliding window function is implemented. 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 an easy way to do this? Any help / advice would be most welcome.

+6
source share
3 answers

You should take a look at pandas . For instance:

 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

+13
source

The Pandas rolling_mean and rolling_std are deprecated and replaced by a more general "rolling" structure. The @elyase example can be changed 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 variance: ts.rolling(window=20).std().plot(style='b') 

The rolling function supports several different types of windows, as described here . You can call a number of functions on the rolling object, including var and other interesting statistics ( skew , kurt , quantile , etc.). I am stuck with std since the graph is on the same graph as the average, which makes more sense single.

+1
source

Here is a simple function that provides the average value and variance of the rental according to the size of the size specified by the user.

 import numpy as np import matplotlib.pyplot as plt window = 10 # make fake data to scan: data = np.random.rand(100,10) # define window function: def sliding_window(data, window): current_pos = 0 left_pos = 0 win_size = window right_pos = left_pos + win_size vdata = [] mdata = [] while current_pos < len(data-win_size): left_pos = current_pos right_pos = left_pos + win_size mean = np.mean(np.var(data[left_pos:right_pos,:], axis=0)) var = np.var(data[left_pos:right_pos,:], axis=0) vdata.append(var) mdata.append(mean) current_pos += 1 return vdata, mdata dvar, dmean = sliding_window(data, window) 
The difference of each sample in the data set:
 plt.plot(dvar) plt.show() 

dvar for all samples (10 columns)

The average variance throughout the set:
 plt.plot(dmean) plt.show() 

dmean sample set

0
source

Source: https://habr.com/ru/post/979463/


All Articles