I have problem requiring me to calculate the rolling product of a series of 1 period returns. The length of the rolling window is variable. The purpose is to obtain the rolling product of the 1 period returns that covers as closely as possible a 12 months window.
I have been able to produce a working solution using brute force through for
loops and if
statements, however I'm wondering if there is an elegant solution. I have spent a lot of time trying with rollapply
and other similar functions but I haven't been able to obtain a solution.
The data below illustrates the problem.
date rt_1_period rt_12_mth_window
1 04-04-13 NA NA
2 10-04-13 0.729096362 NA
3 24-05-13 1.002535647 NA
4 30-05-13 0.993675716 NA
5 21-07-13 1.002662843 NA
6 03-08-13 1.009516582 NA
7 01-09-13 0.963099395 NA
8 20-10-13 1.012470278 NA
9 25-10-13 1.01308502 NA
10 03-11-13 1.005440704 NA
11 01-01-14 1.024208021 NA
12 11-01-14 0.996613924 NA
13 17-02-14 1.009811368 NA
14 24-02-14 1.008139557 NA
15 30-03-14 1.002794709 NA
16 30-04-14 0.998745849 1.042345473
17 02-05-14 1.002324076 1.044767963
18 27-06-14 0.997741026 1.046389027
19 24-08-14 1.015767546 1.050072129
20 05-09-14 1.014405005 1.106010894
21 02-11-14 1.013830296 1.09319212
22 09-11-14 1.013127219 1.101549487
23 16-11-14 1.012614177 1.115444628
24 18-01-15 0.986893629 1.078458006
25 24-01-15 1.028120919 1.108785236
26 10-04-15 0.912452762 0.991025615
27 09-08-15 1.004676152 0.981376513
28 07-01-16 1.004236123 0.934086003
29 01-04-16 1.02341302 0.94215696
In the example the 12 months return for row 29 is calculated as the product of 1 period return from row 26 to 29 because 02-04-15 (365 days from 01-04-16) is contained between rows 25 and 26. On the other side the 12 months return for row 15 is NA because 30-03-13 (365 days from 30-03-14) is outside the time window for which I have observable 1 period returns.
I would be glad if somebody could suggest some way of approaching this problem.
Just for clarity, if the data provided do not make much sense is because this is a cut down version of a larger database that I have created for illustration purposes.