Hello, I looked and I looked and I couldn’t find an implementation in ‘q’. Fintan at kx.com helped me out and I found an implementation. Here goes.
/// Exponentially weighted moving average
/// Always some debate about this. This is the “starting value is one” version.
/// @note
/// In the use of scan, x is the prior and y the current. I’ve renamed them to make it
/// look like the Wikipedia, they call lambda alpha and here I had to anti the lambda
/// (1-lambda) is passed as a constant ‘z’ to the interior function for scan.
/// @note
/// You can pass N in place of lambda, if greater than one, it will derive lambda
/// for you. N is a sort of period. It’s best to calibrate against a Impulse Response
/// viz. .f00.ewma1[(1,20#0); 2]
/// @note
/// Calibrated against and, once again, puts it to shame on execution times.
.f00.ewma1: { [s0; lambda]
lambda: $[lambda >= 1; 2 % lambda + 1; lambda];
{ [now0;past0;z] past0 + z*(now0 - past0) }[;;1 - lambda] scan s0 }
Great, isn’t it? More comments than implementation.
Works as a table update
x.lambda:0.95
data1: update u20:.f00.ewma1[u00;x.lambda] by folio0 from data1
I’ve attached some results. The trick to the formulation is the formula given at
https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average
And this formula https://upload.wikimedia.org/math/7/d/9/7d9048ec588c5f82276d177c4fccd8c2.png
More of my code is http://code.kx.com/wsvn/code/contrib/weaves/qsys/ but I haven’t added this EWMA to it yet. Nor have I have had the chance to update to 3.3 yet. But there are many more comments than code there too!
Mes regardes a tous! N.