0

my problem is simple. I have a function f(y,x) where y is of dim (T,1) and x of dim (T,k) with k>1. This function return a Numpy array of dim (k,1).

From this I would like to calculate f function on a rolling basis ie : result[i] = f(y[i-window],x[I-window]), i=window+1, T

Of course I can do a for loop but it is very slow.

I found this function on internet but I don't know how to call my function on this:

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)

How can I optimise this sort of problem ?

  • I'd say you have two options: https://stackoverflow.com/questions/14313510/how-to-calculate-rolling-moving-average-using-numpy-scipy and https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.rolling.html – BallpointBen Mar 18 '21 at 16:44
  • You can search for `as_strided` – Quang Hoang Mar 18 '21 at 16:44
  • From rolling_window function how then to apply my function f (which is `def f(y,x):...` type). I don't know how to proceed ? – Jacques Tebeka Mar 18 '21 at 16:54
  • I tried to use `pd.rolling().apply()` but it is not working because my output function is multi-dimensional – Jacques Tebeka Mar 18 '21 at 16:56

0 Answers0