knitr::opts_chunk$set(echo = TRUE, cache = TRUE, tidy = TRUE)

Introduction

In many statistical applications, estimating the covariance for a set of random variables is a critical task. The covariance is useful because it characterizes the relationship between variables. For instance, suppose we have three variables $X, Y, \mbox{ and } Z$ and their covariance matrix is of the form

[ \Sigma_{xyz} = \begin{pmatrix} 1 & 0 & 0.5 \ 0 & 1 & 0 \ 0.5 & 0 & 1 \end{pmatrix} ]

We can gather valuable information from this matrix. First of all, we know that each of the variables has an equal variance of 1. Second, we know that variables $X$ and $Y$ are likely independent because the covariance between the two is equal to 0. This implies that any information in $X$ is useless in trying to gather information about $Y$. Lastly, we know that variables $X$ and $Z$ are moderately, positively correlated because their covariance is 0.5.

Unfortunately, estimating $\Sigma$ well is often computationally expensive and, in a few settings, extremely challenging. For this reason, emphasis in the literature and elsewhere has been placed on estimating the inverse of $\Sigma$ which we like to denote as $\Omega \equiv \Sigma^{-1}$.

glasso is a popular R package which estimates $\Omega$ extremely efficiently using the graphical lasso algorithm. GLASSOO is a re-design of that package (re-written in C++) with the aim of giving the user more control over the underlying algorithm. The hope is that this package will allow for further flexibility and rapid experimentation for the end user.

We will demonstrate some of the functionality of GLASSOO with a short simulation.


\vspace{0.5cm}

Simulation

Let us generate some data.


\vspace{0.5cm}

library(GLASSOO)

#  generate data from a sparse matrix
# first compute covariance matrix
S = matrix(0.7, nrow = 5, ncol = 5)
for (i in 1:5){
  for (j in 1:5){
    S[i, j] = S[i, j]^abs(i - j)
  }
}

# generate 100 x 5 matrix with rows drawn from iid N_p(0, S)
set.seed(123)
Z = matrix(rnorm(100*5), nrow = 100, ncol = 5)
out = eigen(S, symmetric = TRUE)
S.sqrt = out$vectors %*% diag(out$values^0.5) %*% t(out$vectors)
X = Z %*% S.sqrt

# snap shot of data
head(X)


\vspace{0.5cm}

We have generated 100 samples (5 variables) from a normal distribution with mean equal to zero and an oracle covariance matrix $S$.


\vspace{0.5cm}

# print oracle covariance matrix
S

# print inverse covariance matrix (omega)
round(qr.solve(S), 5)


\vspace{0.5cm}

It turns out that this particular oracle covariance matrix (tapered matrix) has an inverse - or precision matrix - that is sparse (tri-diagonal). That is, the precision matrix has many zeros.

In this particular setting, we could estimate $\Omega$ by taking the inverse of the sample covariance matrix $\hat{S} = \sum_{i = 1}^{n}(X_{i} - \bar{X})(X_{i} - \bar{X})^{T}/n$:


\vspace{0.5cm}

# print inverse of sample precision matrix (perhaps a bad estimate)
round(qr.solve(cov(X)*(nrow(X) - 1)/nrow(X)), 5)


\vspace{0.5cm}

However, because $\Omega$ is sparse, this estimator will likely perform very poorly. Notice the number of zeros in our oracle precision matrix compared to the inverse of the sample covariance matrix. Instead, we will use GLASSOO to estimate $\Omega$.

By default, GLASSOO will estimate $\Omega$ using a lasso penalty and choose the optimal lam tuning parameters using k-fold cross validation.


\vspace{0.5cm}

# cross validation for lam
GLASSO(X, trace = "none")


\vspace{0.5cm}

GLASSOO also has the capability to provide plots for the cross validation errors. This allows the user to analyze and select the appropriate tuning parameters.

In the heatmap plot below, the more bright (white) areas of the heat map correspond to a better tuning parameter selection.


\vspace{0.5cm}

# produce CV heat map
CV = GLASSO(X, trace = "none")
plot(CV, type = "heatmap")


\vspace{0.5cm}

We can also produce a line graph of the cross validation errors:


\vspace{0.5cm}

# produce line graph for CV errors
plot(CV, type = "line")


\vspace{0.5cm}

GLASSOO contains a number of different criteria for selecting the optimal tuning parameters during cross validation. The package default is to choose the tuning parameters that maximize the log-likelihood (crit.cv = loglik). Other options include AIC and BIC.


\vspace{0.5cm}

# AIC
plot(GLASSO(X, crit.cv = "AIC", trace = "none"))

# BIC
plot(GLASSO(X, crit.cv = "BIC", trace = "none"))


\vspace{0.5cm}

This allows the user to select appropriate tuning parameters under various decision criteria. We also have the option to print all of the estimated precision matrices for each tuning parameter combination using the path option. This option should be used with extreme care when the dimension and sample size is large -- you may run into memory issues.


\vspace{0.5cm}

# keep all estimates using path
CV = GLASSO(X, path = TRUE, trace = "none")

# print only first three objects
CV$Path[,,1:3]


\vspace{0.5cm}

More advanced options

A huge issue in precision matrix estimation is the computational complexity when the sample size and dimension of our data is particularly large. There are a number of built-in options in GLASSO that can be used to improve computation speed:


\vspace{0.5cm}

# reduce number of lam to 5
CV = GLASSO(X, nlam = 5)


\vspace{0.5cm}


\vspace{0.5cm}

# reduce number of folds to 3
CV = GLASSO(X, K = 3)


\vspace{0.5cm}


\vspace{0.5cm}

# relax convergence criteria
CV = GLASSO(X, tol.out = 1e-3)


\vspace{0.5cm}


\vspace{0.5cm}

# adjust maximum number of iterations
CV = GLASSO(X, maxit = 1e3)


\vspace{0.5cm}


\vspace{0.5cm}

# parallel CV
CV = GLASSO(X, cores = 3)


\vspace{0.5cm}



MGallow/GLASSOO documentation built on May 8, 2019, 3:13 a.m.