Xu,

Your EMA question is related to algorithms for linear recurrence relations. There is a much loved algorithm (probably because it's usually found to be lean and mean) that I first encountered at IP Sharp in a newsletter quiz. We used it all over the place, particularly wherever a present value or discounted cash flow was in play. Where it came from before that, I know not. It goes like this.

If you have a linear recurrence relation of the form:

r[0] is m[0] times a[0]
r[i] is a[i] + m[i] times r[i-1]          (i >0)

then this can be calculated in vector languages in the following way:

a recur m;t
t is timesscan m
r is t times plusscan a div t

I'd be most interested to see a J algorithm for this, particularly a tacit one. I note that we have a reflexive piece (t) and that there may be an opportunity for an inverse (* and %).

As far as the EMA is concerned, I suggest Xu's original requirement is solved with:

(y[0] , k times 1 drop y) recur (length y) replicate 1 - k

Mike Powell
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to