depth
Problems / q / kdb+ / Medium

Three-period moving average

Return the 3-period moving average of a list.

f 1 2 3 4 5

should return 1 1.5 2 3 4.

Hint

mavg takes the window size on the left. Note how the first two results are computed.

Solution

f:{3 mavg x}

At the start of the list mavg averages the items available so far, which is why the first result is 1 and the second is 1.5.

Answer

f:{3 mavg x}