I implemented the msum verb in kdb+ using the sliding window code.
First step is to find the sliding window function in q using scan operator :
q)sw:{{1 _ x, y}\[x#0;y]}
q)y
1 6 21 56
The first argument of the internal diadic function is x 0’s and the second argument is the list y.
On the first scan, the internal function gets applied on x 0’s and y[0]. On the second scan, it gets applied on the output of the first scan and y[1] and hence, it continues.
Results :
q)sw[3;y]
0 0 1
0 1 6
1 6 21
6 21 56
q)sw[2;y]
0 1
1 6
6 21
21 56
q)sw[1;y]
1
6
21
56
Now, using this to calculate the msum function in q :
q)y
1 6 21 56
q)2 msum y
1 7 27 77
q)sum each sw[2;y]
1 7 27 77
msum is used to calculate the N-item moving sum on list y where N is the first argument. In this case, it is 2.
Point to ponder on :
mdev is a function which calculates the moving deviation. However, it cannot be implemented using this sliding window code directly. It needs some tweaks. :-)
Follow @novieq13