[Numpy-discussion] trailing max

Stefan van der Walt stefan@sun.ac...
Tue Oct 30 16:05:50 CDT 2007


On Tue, Oct 30, 2007 at 12:31:41PM -0500, John Hunter wrote:
> 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.

A while ago Anne posted the attached 'segmentaxis'.  This is a very
useful piece of code!

Regards
Stéfan
-------------- next part --------------
A non-text attachment was scrubbed...
Name: segmentaxis.py
Type: text/x-python
Size: 5434 bytes
Desc: not available
Url : http://projects.scipy.org/pipermail/numpy-discussion/attachments/20071030/dc0212dd/attachment.py 


More information about the Numpy-discussion mailing list