r:两资产之间在特定时间范围内的方差协方差矩阵

时间:2018-10-08 12:54:20

标签: r covariance variance covariance-matrix

我已经下载了2006-10期间的价格,并计算了SPY的对数价格,通过回归,我找到了m1,我将使用它作为平均值来计算该公式f = C ^ -1(先生) 图书馆(统计)     图书馆(片刻)     图书馆(quantmod)     图书馆(fBasics)     图书馆(预测)     图书馆(tseries)     图书馆(动物园)     图书馆(corpcor)

SPY <- get.hist.quote("SPY",start = "2006-01-01",end = "2010-01-01",quote = "AdjClose", compression = "d",retclass = c("zoo","ts"))
L<-length(SPY)
SPY <- get.hist.quote("SPY",start = "2006-01-01",end = "2011-01-01",quote = "AdjClose", compression = "d",retclass = c("zoo","ts"))
X<-length(SPY)
SPY<-coredata(SPY)
SPY1=matrix(0,ncol = 252,nrow = L) #252esimo giorno di mercato
m1 = matrix(0,ncol = 252,nrow = 1)
t=seq(1,L,by=1)
for(i in 1:252){
 SPY1[,i]<-log(SPY[(0+i):(i+L-1)]) #1007-->>>lunghezza fino 1/1/2010
 co <-lm(SPY1[,i]~t)
 m1[i] <-co$coefficients[2]

}

TLT <- get.hist.quote("TLT",start = "2006-01-01",end = "2010-01-01",quote = "AdjClose", compression = "d",retclass = c("zoo","ts"))
L<-length(TLT)
TLT <- get.hist.quote("TLT",start = "2006-01-01",end = "2011-01-01",quote = "AdjClose", compression = "d",retclass = c("zoo","ts"))
TLT<-coredata(TLT)
TLT1=matrix(0,ncol = 252,nrow = L)
m10 = matrix(0,ncol = 252,nrow = 1)
t=seq(1,L,by=1)
for(i in 1:252){
  TLT1[,i]<-log(TLT[(0+i):(i+L-1)]) #1007-->>>lunghezza fino 1/1/2010
  co <-lm(TLT1[,i]~t)
  m10[i] <-co$coefficients[2]
}

SHY <- get.hist.quote("SHY",start = "2006-01-01",end = "2010-01-01",quote = "AdjClose", compression = "d",retclass = c("zoo","ts"))
L<-length(SHY)
SHY <- get.hist.quote("SHY",start = "2006-01-01",end = "2011-01-01",quote = "AdjClose", compression = "d",retclass = c("zoo","ts"))
SHY<-coredata(SHY)
SHY1=matrix(0,ncol = 252,nrow = L)
r1 = matrix(0,ncol = 252,nrow = 1)
t=seq(1,L,by=1)
for(i in 1:252){
  SHY1[,i]<-log(SHY[(0+i):(i+L-1)]) #1007-->>>lunghezza fino 1/1/2010
  co <-lm(SHY1[,i]~t)
  r1[i] <-co$coefficients[2]
}

ret1=diff(SPY1)
ret2=diff(TLT1)

现在我的问题是如何从所有矩阵ret1和ret2上计算SPY1和TLT1之间的方差协方差矩阵的逆矩阵? 我需要该值进行计算 f = C ^ -1(m-r) r来自SHY

0 个答案:

没有答案