# CVglasso Tutorial

## 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. `CVglasso` is designed to build upon this package and allow for further flexibility and rapid experimentation for the end user.

We will illustrate this with a short simulation.

\vspace{0.5cm}

## Simulation

Let us generate some data.

\vspace{0.5cm}

    library(CVglasso)

    # 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)

    ##            [,1]         [,2]         [,3]       [,4]        [,5]
    ## [1,] -0.4311177 -0.217744186  1.276826576 -0.1061308 -0.02363953
    ## [2,] -0.0418538  0.304253474  0.688201742 -0.5976510 -1.06758924
    ## [3,]  1.1344174  0.004493877 -0.440059159 -0.9793198 -0.86953222
    ## [4,] -0.0738241 -0.286438212  0.009577281 -0.7850619 -0.32351261
    ## [5,] -0.2905499 -0.906939891 -0.656034183 -0.4324413  0.28516534
    ## [6,]  1.3761967  0.276942730 -0.297518545 -0.2634814 -1.35944340

\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

    ##        [,1]  [,2] [,3]  [,4]   [,5]
    ## [1,] 1.0000 0.700 0.49 0.343 0.2401
    ## [2,] 0.7000 1.000 0.70 0.490 0.3430
    ## [3,] 0.4900 0.700 1.00 0.700 0.4900
    ## [4,] 0.3430 0.490 0.70 1.000 0.7000
    ## [5,] 0.2401 0.343 0.49 0.700 1.0000

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

    ##          [,1]     [,2]     [,3]     [,4]     [,5]
    ## [1,]  1.96078 -1.37255  0.00000  0.00000  0.00000
    ## [2,] -1.37255  2.92157 -1.37255  0.00000  0.00000
    ## [3,]  0.00000 -1.37255  2.92157 -1.37255  0.00000
    ## [4,]  0.00000  0.00000 -1.37255  2.92157 -1.37255
    ## [5,]  0.00000  0.00000  0.00000 -1.37255  1.96078

\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)

    ##          [,1]     [,2]     [,3]     [,4]     [,5]
    ## [1,]  2.32976 -1.55033  0.22105 -0.08607  0.24309
    ## [2,] -1.55033  3.27561 -1.68026 -0.14277  0.18949
    ## [3,]  0.22105 -1.68026  3.19897 -1.25158 -0.11016
    ## [4,] -0.08607 -0.14277 -1.25158  2.76790 -1.37226
    ## [5,]  0.24309  0.18949 -0.11016 -1.37226  2.05377

\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 `CVglasso` to estimate \(\Omega\).

By default, `CVglasso` will estimate \(\Omega\) using a lasso penalty (like `glasso`) but choose the optimal `lam` tuning parameters using k-fold cross validation.

\vspace{0.5cm}

    # cross validation for lam (small grid/folds for vignette speed)
    CVglasso(X, nlam = 4, K = 3, trace = "none")

    ##
    ##
    ## Call: CVglasso(X = X, nlam = 4, K = 3, trace = "none")
    ##
    ## Iterations:
    ## [1] 3
    ##
    ## Tuning parameter:
    ##       log10(lam)    lam
    ## [1,]      -1.544  0.029
    ##
    ## Log-likelihood: -110.16675
    ##
    ## Omega:
    ##          [,1]     [,2]     [,3]     [,4]     [,5]
    ## [1,]  2.13225 -1.24667  0.00000  0.00000  0.18710
    ## [2,] -1.24669  2.75120 -1.29907 -0.07345  0.00000
    ## [3,]  0.00000 -1.29915  2.81735 -1.15679 -0.00114
    ## [4,]  0.00000 -0.07339 -1.15673  2.46461 -1.17086
    ## [5,]  0.18707  0.00000 -0.00116 -1.17087  1.86326

\vspace{0.5cm}

`CVglasso` 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 from the same small vignette fit
    CV = CVglasso(X, nlam = 4, K = 3, 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}

`CVglasso` 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(CVglasso(X, nlam = 4, K = 3, crit.cv = "AIC", trace = "none"))

    # BIC
    plot(CVglasso(X, nlam = 4, K = 3, 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 = CVglasso(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 `CVglasso` that can be used to improve computation speed:

- Reduce the number of `lam` values during cross validation. The default number is 10.

\vspace{0.5cm}

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

\vspace{0.5cm}

- Reduce the number of `K` folds during cross validation. The default number is 5.

\vspace{0.5cm}

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

\vspace{0.5cm}

- Relax the convergence critera for the graphical lasso algorithm using the `tol` option. The default for each is 1e-4.

\vspace{0.5cm}

    # relax convergence criteria
    CV = CVglasso(X, tol = 0.001)

\vspace{0.5cm}

- Adjust the maximum number of iterations. The default is 1e4.

\vspace{0.5cm}

    # adjust maximum number of iterations
    CV = CVglasso(X, maxit = 1000)

\vspace{0.5cm}

- We can also opt to run our cross validation procedure in parallel. For reproducible CRAN vignette builds, this remains a non-evaluated example.

\vspace{0.5cm}

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

\vspace{0.5cm}