0

I have a 1D-array representing a time series of size N (with index 0 being the newest value). Now I want to calculate another array that contains a moving maximum, i.e. the maximum of the last M < N values (which would have size N-M+1).

Example (X = time series, Y = desired output with max. of the last 3 values):

X = np.array([5,6,4,5,4,6,5,7,8,9,8])
Y = np.array([6,6,5,6,6,7,8,9,9])

How would I do that efficiently? I've found this thread using numpy's convolve-function to calculate a running average, but I must admit I don't really understand what the convolve does and how to apply it to calculate the running max (or min).

Community
  • 1
  • 1
ascripter
  • 5,665
  • 12
  • 45
  • 68
  • 1
    That other question has some good idea, but they are buried in a more complicated problem. – hpaulj Apr 12 '15 at 19:35
  • indeed. The "windowed_view" function should already pave the way to solve my problem. I'm still new to numpy and "thinking in arrays", but the possibilities are really nice :) – ascripter Apr 12 '15 at 20:00
  • 1
    A more succinct duplicate (from 2011): http://stackoverflow.com/questions/6811183/rolling-window-for-1d-arrays-in-numpy (using the `as_strided` solution). Found with a search for 'rolling [window]'. – hpaulj Apr 12 '15 at 20:26
  • The alleged duplicate is about rolling "drawdowns", not about rolling "maximums". I didn't find this question here answered there. – Alfe Apr 10 '17 at 23:47

0 Answers0