[Numpy-discussion] trailing max

John Hunter jdh2358 at gmail.com
Tue Oct 30 13:31:41 EDT 2007


In financial time series, it is very common to keep track of things
like a trailing N day max, trailing N day average, etc.  Generally,
for a 1D array x, I'd like to be able to efficiently compute a new
len(x) vector where y[i] = func(x[i-N:])  and I need to be able to
handle edge effects (eg where i<N).  I'm thinking of writing some
extension code to handle this, but since this is a common task, I
thought I'd elicit some suggestions about how to best handle this.
One approach might to create a very large 2D array with each row the
appropriate masked (to handle edge effects) shifted array, and then
one could do sums and maxes over the columns.  For a length 1000
vector with a max lag of 250, you'd have 250K elements, which is
probably manageable, but I'm still not sure how to best create this
shifted array and if there are better approaches.

JDH



More information about the NumPy-Discussion mailing list