我获得了4年的资产回报时间序列,并且我试图执行滚动窗口以估计方差 - 协方差矩阵,校准周期为6个月。总的来说,我应该获得40个协方差矩阵。 我试图运行下面写的代码,但这是错误的。 如何修改此R代码?
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, ], ])
}
}
将数据集视为一个矩阵,其中包含20天内5个资产的回报
data <- matrix(rnorm(100), 20, 5) #data represents the returns of 5 assets over 20 days
我想在5天内校准返回的协方差矩阵,因此需要考虑第1,2,3,4,5天。然后我想考虑第6天,第7天,第8天,第9天,校准另一个协方差矩阵,10依此类推,使用滚动窗口(我已尝试使用循环)。
window.size <- 5
但是将窗口大小设置为5,代码会考虑第一个矩阵,第1,2,3,4,5天,但对于第二个矩阵,代码会考虑第2,3,4,5,6天(不是我想要的6,7,8,9,10)。这是我的问题。我不知道如何修改代码,以便&#34; split&#34;从第2天到第6天。
答案 0 :(得分:2)
我认为对“滚动窗户”一词存在误解;通常,滚动窗口方法是指在逐行“滚动”时在某个窗口内的行之间计算某些度量。因此,对于5天窗口,其中每行对应一天,行1,2,3,4,5
后面会有行2,3,4,5,6
,后面是行3,4,5,6,7
,依此类推。
如果我理解正确,您需要计算数据的非重叠行块的协方差矩阵。
根据样本数据,您可以执行以下操作:
# 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
要从评论中解决这个问题,可以采用以下方式:
# 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