rolling

Syntax

rolling(func, funcArgs, window, [step=1])

Arguments

func is an aggregate or vectorized function.

funcArgs are the parameters of func, which can be vectors, matrices, or tables. It is a tuple if there are more than 1 parameter of func, and all parameters must have the same size (the number of elements of a vector or rows of a matrix).

window is the window size.

step is the count or interval that windows slide. The default value is 1.

Details

Apply the function/operator to a sliding window of the given objects. It is a tumbling window when step = window.

The rolling function starts calculating when the window size is reached for the first time; then it calculates with frequency specified by step.

Note:

  • If func is specified as a vectorized function, the step must be equal to window.
  • The calculation only starts when the number of elements in the window reaches the window size.

Similar to the moving function, windows in rolling function are always along rows.

The func parameter in rolling function supports aggregate or vectorized functions, whereas func in moving function only supports aggregate functions. When func is specified as an aggregate function, the differences of rolling and moving functions lie in:

  1. Parameter step can be specified in rolling function.

  2. The rolling function does not return NULL values of the first (window -1) elements.

Examples

When func is the vectorized function:

m = matrix(3 4 6 8 5 2 0 -2, 2 9 NULL 1 3 -4 2 1, NULL 8 9 8 0 1 9 -3)
rolling(cummax, m, 4, 4)
col1 col2 col3
3 2
4 9 8
6 9 9
8 9 9
5 3 0
5 3 1
5 3 9
5 3 9
rolling(cumsum, m, 3, 3)
col1 col2 col3
3 2
7 11 8
13 11 17
8 1 8
13 4 8
15 0 9

When func is the aggregate function:

rolling(sum, m, 4)
col1 col2 col3
21 12 25
23 13 25
21 0 18
15 2 18
5 2 7

Calculate the rolling beta of AAPL against the market (SPY) with the moving window of size 10 and frequency of 5.

date=2016.08.01..2016.08.31
date=date[1<=weekday(date)<=5]
aaplRet=0.0177 -0.0148 0.0125 0.0008 0.0152 0.0083 0.0041 -0.0074 -0.0006 0.0023 0.0120 -0.0009 -0.0015 -0.0013 0.0026 -0.0078 0.0031 -0.0075 -0.0043 -0.0059 -0.0011 -0.0077 0.0009
spyRet=-0.0008 -0.0064 0.0029 0.0011 0.0082 -0.0006 0.0006 -0.0025 0.0046 -0.0009 0.0029 -0.0052 0.0019 0.0022 -0.0015 0.0000 0.0020 -0.0051 -0.0007 -0.0019 0.0049 -0.0016 -0.0028
t=table(date, aaplRet, spyRet);
t;
date aaplRet spyRet
2016.08.01 0.0177 -0.0008
2016.08.02 -0.0148 -0.0064
2016.08.03 0.0125 0.0029
2016.08.04 0.0008 0.0011
2016.08.05 0.0152 0.0082
2016.08.08 0.0083 -0.0006
2016.08.09 0.0041 0.0006
2016.08.10 -0.0074 -0.0025
2016.08.11 -0.0006 0.0046
2016.08.12 0.0023 -0.0009
2016.08.15 0.012 0.0029
2016.08.16 -0.0009 -0.0052
2016.08.17 -0.0015 0.0019
2016.08.18 -0.0013 0.0022
2016.08.19 0.0026 -0.0015
2016.08.22 -0.0078 0
2016.08.23 0.0031 0.002
2016.08.24 -0.0075 -0.0051
2016.08.25 -0.0043 -0.0007
2016.08.26 -0.0059 -0.0019
2016.08.29 -0.0011 0.0049
2016.08.30 -0.0077 -0.0016
2016.08.31 0.0009 -0.0028
betas = rolling(beta, [aaplRet, spyRet], 10,5);
dates = rolling(last, date, 10,5);
table(dates, betas);
dates betas
2016.08.12 1.601173
2016.08.19 0.512656
2016.08.26 1.064465