Reputation: 1
I have a matrix (V) with daily stock returns between a certain time period on which I try to get the variance of the preceding 1-month (=21 days) and 6-months (=126 days).
This is how I would do it based on future 1-month and 6-months data.
Variance.x <- rollapply(data=V, width=21, var, by.column=F, align="right")
Variance.x <- rollapply(data=V, width=126, var, by.column=F, align="right")
Any suggestions?
Upvotes: 0
Views: 667
Reputation: 269481
This gives the variance covering the future 21 periods assuming z
is a multivariate zoo series:
lag(rollapply(z, 21, function(x) c(var(x)), by.column = FALSE, align = "left"))
Since the variance is a matrix we should flatten it in the function as shown here and this should be done whether we calculate future or past variances.
The above does not include the current point in the future points. If the 21 points starting at and including the present were desired then we would omit the lag
.
Upvotes: 3