Over / Scan puzzles in Q

When reading Q Tips there is an introduction about Over / Scan using an example of computing EWMA (exponentially weighted moving average):

ewma: {{(y*1-x)+z*x}[x]\[y]}

Note that for the inner function, we should read it from right side to the left side: x*z+(1-x)*y.

The formula for EWMA calculation is conceptually simple:

EWMA(t) = Weight * X(t) + (1 - Weight)*EWMA(t-1)

while to understand the example in q is really brain burning.

First the difference between over (/) and scan() is that, the previous one will only return the final result nevertheless for the latter one each of the intermediate values will be returned. And scan is used in this example.

q)ewma[.25] 1 2 3 4 5f
1 1.25 1.6875 2.265625 2.949219

In the example above, [x] is an atom of [.25] and [y] is a vector of (1;2;3;4;5f). The scan operator will change a dyadic(two parameter) function into a monadic function that takes a single list of values, and each call to the modified function will take the previous value as the first argument and the second argument will be the next value from the list. And if the initial value is not given, the first value of the list after the scan operator will be offered as the initial value, in the meanwhile the other params will be set to 0.

Base on the explanation, the passed arguments will be

t x y (y(t) = EWMA(t-1)) z EWMA (EWMA(t) = x*z+(1-x)*y)
0 0 1 0 1
1 0.25 1 1 1
2 0.25 1 2 1.25
3 0.25 1.25 3 1.6875
4 0.25 1.6875 4 2.265625
5 0.25 2.265625 5 2.949219

which matches the result of ewma[.25] 1 2 3 4 5f.

More to read: Accumulators.

results matching ""

    No results matching ""