0
votes

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. Overall, I should obtain 40 covariance matrices. I have tried to run the code written below but it is wrong. How can I modify this R code?

data 

window.size <- 180 #set the size of the window equal to 6 months
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, ], ])
  }
}

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 6, 7, 8, 9, 10. 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 6, 7, 8, 9, 10 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 6.

1
"I have tried to run the code written below but it is wrong" How is it wrong? Did it throw an error? A warning? It would help if you could make your example reproducible by providing (1) sample data, and (2) your expected output based on the sample data.Maurits Evers
I've tried to explain better my question in the aswer below @MauritsEversRfun2018

1 Answers

2
votes

I think there is a misunderstanding about the term "rolling windows"; usually a rolling window approach refers to calculating some metric across rows within a certain window as you "roll along" row-by-row. So in your case of a 5 day window where every row corresponds to a day, rows 1,2,3,4,5 would be followed by rows 2,3,4,5,6, followed by rows 3,4,5,6,7 and so on.

If I understand you correctly, you instead want to calculate the covariance matrix of non-overlapping row chunks of your data.

Given the sample data, you can do something like this:

# Sample data
set.seed(2017);
df <- matrix(rnorm(100), 20, 5)

# Split into groups of 5 corresponding to 5 days and calculate
# covariance matrix
idx <- rep(1:(nrow(df) / 5), each = 5)
lapply(split(as.data.frame(df), idx), cov)
#$`1`
#            V1          V2          V3          V4         V5
#V1  1.42311854  1.12594509 -0.01635956 -0.02680876 -0.9996623
#V2  1.12594509  1.91104181  0.01600511 -0.50270431 -0.4910714
#V3 -0.01635956  0.01600511  0.21584984  0.04264861  0.5356313
#V4 -0.02680876 -0.50270431  0.04264861  0.80241761 -0.3501894
#V5 -0.99966230 -0.49107141  0.53563126 -0.35018940  2.2617564
#
#$`2`
#           V1          V2          V3          V4          V5
#V1  1.6361650  0.28858744  0.55629684 -0.10309928 -0.56784302
#V2  0.2885874  0.32030225  0.09751046 -0.03968577  0.10521384
#V3  0.5562968  0.09751046  0.21460406  0.06921578 -0.20474838
#V4 -0.1030993 -0.03968577  0.06921578  0.44061198 -0.02624344
#V5 -0.5678430  0.10521384 -0.20474838 -0.02624344  0.35858727
#
#$`3`
#            V1         V2          V3          V4         V5
#V1  1.32188749 -0.2504449  0.02865553 -0.83709045  0.7402660
#V2 -0.25044493  0.4449060 -0.45165482  0.18724720 -0.1684300
#V3  0.02865553 -0.4516548  1.59804827 -0.05257944 -0.2588460
#V4 -0.83709045  0.1872472 -0.05257944  2.08276888  0.1345800
#V5  0.74026604 -0.1684300 -0.25884602  0.13457998  0.7381084
#
#$`4`
#          V1         V2         V3         V4         V5
#V1 1.3825793  1.8348434  0.1367480  0.7553666  0.1722815
#V2 1.8348434  3.0679884 -0.7141430  1.9419513  0.4139003
#V3 0.1367480 -0.7141430  1.3646673 -1.3689109 -0.3962832
#V4 0.7553666  1.9419513 -1.3689109  2.1242897  0.7087351
#V5 0.1722815  0.4139003 -0.3962832  0.7087351  0.4589429

Update

To address the scenario from your comment, here is a possibility:

# Calculate rows by which to calculate the covariance matrix. 
idx <- lapply(seq(1, nrow(df) - 5, by = 3), function(i) seq(i, i + 4));
idx;
#[[1]]
#[1] 1 2 3 4 5
#
#[[2]]
#[1] 4 5 6 7 8
#
#[[3]]
#[1]  7  8  9 10 11
#
#[[4]]
#[1] 10 11 12 13 14
#
#[[5]]
#[1] 13 14 15 16 17

# Calculate covariance matrix
lapply(idx, function(i) cov(df[i, ]))
[[1]]
            [,1]        [,2]        [,3]        [,4]       [,5]
[1,]  1.42311854  1.12594509 -0.01635956 -0.02680876 -0.9996623
[2,]  1.12594509  1.91104181  0.01600511 -0.50270431 -0.4910714
[3,] -0.01635956  0.01600511  0.21584984  0.04264861  0.5356313
[4,] -0.02680876 -0.50270431  0.04264861  0.80241761 -0.3501894
[5,] -0.99966230 -0.49107141  0.53563126 -0.35018940  2.2617564

[[2]]
           [,1]      [,2]       [,3]        [,4]       [,5]
[1,]  1.2276633 0.8120994 0.68757421  0.43389428 -0.2034626
[2,]  0.8120994 0.9467878 0.54971586  0.32442138  0.0417013
[3,]  0.6875742 0.5497159 0.81237637  0.04317779  0.1016797
[4,]  0.4338943 0.3244214 0.04317779  0.28202885 -0.1328829
[5,] -0.2034626 0.0417013 0.10167967 -0.13288293  0.1941425

[[3]]
             [,1]        [,2]        [,3]         [,4]       [,5]
[1,]  1.611594316  0.20860309  0.57449605 -0.009977472 -0.5998735
[2,]  0.208603088  0.37400181  0.02228603 -0.184461638  0.1758137
[3,]  0.574496047  0.02228603  0.25869591  0.192013428 -0.2558926
[4,] -0.009977472 -0.18446164  0.19201343  0.726477219 -0.1542378
[5,] -0.599873476  0.17581368 -0.25589263 -0.154237772  0.4141949

[[4]]
             [,1]         [,2]       [,3]        [,4]       [,5]
[1,]  0.959758045 -0.002570837 -0.2490718 -0.11965574  0.7669619
[2,] -0.002570837  0.413593056 -0.2238722 -0.05783551 -0.1231235
[3,] -0.249071754 -0.223872167  1.3953139  0.56463838 -0.2210563
[4,] -0.119655741 -0.057835506  0.5646384  1.09879770  0.1947360
[5,]  0.766961857 -0.123123489 -0.2210563  0.19473603  0.7653579

[[5]]
            [,1]       [,2]        [,3]       [,4]       [,5]
[1,]  1.02217247  0.8925311 -0.01480308  0.4282321  0.5941764
[2,]  0.89253109  2.8366577 -1.20242470  2.7991809  0.6818609
[3,] -0.01480308 -1.2024247  1.48751111 -1.7348326 -0.1196483
[4,]  0.42823208  2.7991809 -1.73483255  3.8382883  1.0043009
[5,]  0.59417636  0.6818609 -0.11964826  1.0043009  0.8229246