I got a 4-years time series of asset returns and I'm trying to perform a rolling-window in order to estimate the variance-covariance matrix with a calibration period of 6 months.
in general, consider as dataset a matrix which includes the returns of 5 assets over 20 days
data <- matrix(rnorm(100), 20, 5) #data represents the returns of 5 assets over 20 days
I want to calibrate the covariance matrix of returns over 5 days, so taking into consideration days 1, 2, 3, 4, 5. Then I want to calibrate another covariance matrix taking into consideration days 4, 5, 6, 7, 8. And so on, using a rolling window ( I have tried to this using the loop for).
window.size <- 5
But setting the windows size equal to 5, the code considers, for the first matrix, days 1, 2, 3, 4, 5, but for the second matrix the code considers days 2, 3, 4, 5, 6 (not 4, 5, 6, 7, 8 that I want). This is my problem. I do not know how to modify the code in order this "split" from day 2 to day 4. How can I manage this problem?
window.size <- 5 #set the size of the window equal to 5 days
windows <- embed(1:nrow(data), window.size)
forApproach <- function(data, windows) {
l <- vector(mode="list", length=nrow(windows))
for (i in 1:nrow(data)) {
l[[i]] <- cov(data[windows[i, ], ])
}
}