Playing with Matrices

We want to convert a covariance matrix \( \Sigma \) into a corresponding correlation matrix \( \Gamma \).

(sigma <- c(1, 2, 3))
## [1] 1 2 3
(Gamma <- as.matrix(toeplitz(10:8)/10, nrow = 3))
##      [,1] [,2] [,3]
## [1,]  1.0  0.9  0.8
## [2,]  0.9  1.0  0.9
## [3,]  0.8  0.9  1.0
(Sigma <- diag(sqrt(sigma)) %*% Gamma %*% diag(sqrt(sigma)))
##       [,1]  [,2]  [,3]
## [1,] 1.000 1.273 1.386
## [2,] 1.273 2.000 2.205
## [3,] 1.386 2.205 3.000

This we can achieve through \( diag(\Sigma)^{-\frac{1}{2}} \ \Sigma \ diag(\Sigma)^{-\frac{1}{2}} \).

sqrt(solve(diag(diag(Sigma)))) %*% Sigma %*% sqrt(solve(diag(diag(Sigma))))
##      [,1] [,2] [,3]
## [1,]  1.0  0.9  0.8
## [2,]  0.9  1.0  0.9
## [3,]  0.8  0.9  1.0

Ta-dah!

Now we build from a data covariance matrix \( \Sigma \) the corresponding covariance matrix of test statistics \( \tilde\Sigma \) using the appropriate coefficient matrix \( C \).

(C <- cbind(matrix(c(-1, -1), ncol = 1), diag(2)))
##      [,1] [,2] [,3]
## [1,]   -1    1    0
## [2,]   -1    0    1
(SigmaTilde <- C %*% Sigma %*% t(C))
##        [,1]   [,2]
## [1,] 0.4544 0.5461
## [2,] 0.5461 1.2287

Fine.

Now we can compute \( \tilde\Gamma \) directly from \( \tilde\Sigma \).

(GammaTilde <- sqrt(solve(diag(diag(SigmaTilde)))) %*% SigmaTilde %*% sqrt(solve(diag(diag(SigmaTilde)))))
##        [,1]   [,2]
## [1,] 1.0000 0.7308
## [2,] 0.7308 1.0000