I did some experimentation with the glasso
function and I find that it does not return the covariance matrix w
, and the inverse covariance matrix wi
lacks the diagonal terms and is not symmetric.
Setting up ...
library(glasso)
n <- 20
k <- 3
x <- matrix(rnorm(n*k),ncol=k)
Since n > k, the covariance matrix can be inverted directly.
> var(x)
[,1] [,2] [,3]
[1,] 1.37818760 -0.03676559 -0.0446064
[2,] -0.03676559 1.15692561 -0.4592093
[3,] -0.04460640 -0.45920931 1.1981595
> solve(var(x))
[,1] [,2] [,3]
[1,] 0.72803016 0.03997524 0.04242491
[2,] 0.03997524 1.02163778 0.39304343
[3,] 0.04242491 0.39304343 0.98683155
With approx=FALSE
the glasso
function uses the lasso method and gets the right answer.
> glasso(var(x),rho=0,approx=FALSE)[c('w','wi')]
$w
[,1] [,2] [,3]
[1,] 1.37818760 -0.03676557 -0.04460639
[2,] -0.03676557 1.15692561 -0.45920931
[3,] -0.04460639 -0.45920931 1.19815952
$wi
[,1] [,2] [,3]
[1,] 0.72803010 0.03997522 0.04242489
[2,] 0.03997386 1.02163778 0.39304343
[3,] 0.04242438 0.39304343 0.98683155
Setting approx=TRUE
causes the function to use the Meinshausen-Buhlmann method, and that seems to be poorly implemented.
> glasso(var(x),rho=0,approx=TRUE)[c('w','wi')]
$w
[,1] [,2] [,3]
[1,] 0 0 0
[2,] 0 0 0
[3,] 0 0 0
$wi
[,1] [,2] [,3]
[1,] 0.00000000 -0.03912856 -0.04299102
[2,] -0.05490688 0.00000000 -0.39828827
[3,] -0.05827283 -0.38471896 0.00000000
Now that I look at it, the wi
returned by setting approx=TRUE
looks like the covariance, not inverse covariance, matrix. This is a serious error in glasso
function. Or maybe it's an error in the documentation. Either way, it's serious.
glasso(var(X), rho = 0.01, zero = info[-c(1000:1950000),], approx=TRUE)
, the estimated cov matrix is still zero!! $\endgroup$