rmatrixvariancecalibrationrolling-computation

Rolling window for co-variance matrix


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, ], ])
  }
}

Solution

  • A solution by extending OP's approach is to use another variable skip. Based on feedback it seems OP wants to calculate cov for first 5 rows(1:5) and then wants to skip 3 rows to calculate cov for rows(4:9) and so on.

    The use of embed created window of size 5 but skipping 1. We can skip rows to find selected_windows of row index and then apply cov function.

    window.size <- 5
    skip  <- 3
    windows <- embed(1:nrow(data), window.size)
    selected_windows <- windows[(1:nrow(windows) %% skip) == 1, ]
    
    #       [,1] [,2] [,3] [,4] [,5]
    # [1,]    5    4    3    2    1
    # [2,]    8    7    6    5    4
    # [3,]   11   10    9    8    7
    # [4,]   14   13   12   11   10
    # [5,]   17   16   15   14   13
    # [6,]   20   19   18   17   16
    
    
    #One can use for-loop or apply to calculate "cov" on group of rows and get the result
    apply(selected_windows, 1, function(x)list(cov(data[x,])))