Glad you like it. However, be aware that a zero in the multiplier vector will cause an error.
Mike Powell

On 8-Mar-08, at 8:33 PM, Xu Zuoqian wrote:

Oh,Mike,your solution is so beautiful!

recur=:[ (] * [:+/\ [ % ]) [: */\ ]
EMA2=:([EMAIL PROTECTED] , [ * [EMAIL PROTECTED]) recur (1, (<:@[EMAIL 
PROTECTED]) $ [EMAIL PROTECTED])

(0.1&EMA -: 0.1&EMA2) p:i.10
1

   6!:2 '0.1&EMA i.1e6'
2.80876

   6!:2 '0.1&EMA1 i.1e6'
3.781998

   6!:2 '0.1&EMA2 i.1e6'
0.314285

2008/3/9, Mike Powell <[EMAIL PROTECTED]>:
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

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

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

Reply via email to