[SciPy-user] accumulate with custom functions

Stefan van der Walt stefan at sun.ac.za
Sun Dec 24 13:11:18 CST 2006


On Sun, Dec 24, 2006 at 11:17:30AM -0500, Matt Knox wrote:
> 
> I work quite a bit with another language called FAME, which is very vector based. In FAME I calculate exponentially weighted moving averages (exp mave) in the following way:
> 
> suppose X is a 1 dimensional array, then to Calculate an exp mave I would do:
> 
> myExpMave = X
> Case firstvalue(X)+1 to lastvalue(X) #sets global operating range
> set myExpMave[N] = myExpMave[N-1] + k *
> (myExpMave[N]-myExpMave[N-1])  #where k is some constant smoothing
> factor, N is an index placeholder recognized by FAME

This can be done with standard indexing:

y = x[:-1] + k * (x[1:] - x[:-1])

or

y = x[:-1] + k * numpy.diff(x)

You may also be interested in

numpy.vectorize

Cheers
Stéfan


More information about the SciPy-user mailing list