0

I have to compute the inverse of the covariance matrix [variance covariance covariance variance] between 2 assets (SPY and TLT) over the time horizon 2006 2010 I did this

ret1=diff(SPY1) %2006-31/12/2009 of log of SPY
ret2=diff(TLT1)
dim(ret1)
dim(ret2)
dim(m1)
dim(r1)
m1 = t(m1)
r1 = t(r1)
inv_C = matrix(0,nrow = NCOL(ret1),ncol = 1)

for(i in 1:NCOL(ret1)) {
  cov1 = cov(ret1[,i],ret2[,i])
  inv_C[i,] = 1/cov1
}

but this give me only a single value for each I need all the matrix HOW can I do that? Thanks

Jaap
  • 81,064
  • 34
  • 182
  • 193
  • please provide a reproducible data set. But I think u can just do `cov(ret1, ret2)` to get the covariance matrix – rahul Oct 08 '18 at 09:05
  • You can calculate a covariance matrix between two variables by giving the matrix of the two variables as an argument to cov. E.g. `cov1 = cov(cbind(ret1, ret2))`. Its inverse matrix can be obtained by `solve(cov1)`. (If for some reason you want to get element-wise inverses of `cov1`, you can get them by `cov1^-1`.) – Otto Kässi Oct 08 '18 at 09:06
  • no I don't have to do the only covariance but build up a variance covariance matrix – Tommaso Dellolmo Oct 08 '18 at 09:09
  • I answered with a new post below – Tommaso Dellolmo Oct 08 '18 at 09:19
  • Tommaso, you should edit your original question to include your explanation you have included as an answer. That way others will not think that you have answered your own question. I see that your `dim(ret1) = dim(ret2) = 1006 x 252`. I suspect that these should be 1258 x 1? After the objects are of correct dimension, you can use `cov(cbind(ret1, ret2))`. – Otto Kässi Oct 08 '18 at 10:36

0 Answers0