---
title: "Benchmarks"
author: "Matt Galloway"
#date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Benchmarks}
  %\VignetteEngine{knitr::knitr}
  %\usepackage[UTF-8]{inputenc}
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, cache = FALSE, tidy = TRUE)
```

This is a short effort to give users an idea of how long the functions take to process. The benchmark examples below are illustrative; exact timings vary by platform and R version.

We will be estimating a tri-diagonal precision matrix with dimension $p = 100$:

<br>\vspace{0.5cm}
```{r, message = FALSE}
library(CVglasso)

#  generate data from tri-diagonal (sparse) matrix
# compute covariance matrix (can confirm inverse is tri-diagonal)
S = matrix(0, nrow = 100, ncol = 100)

for (i in 1:100){
  for (j in 1:100){
    S[i, j] = 0.7^(abs(i - j))
  }
}

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

# calculate sample covariance matrix
sample = (nrow(X) - 1)/nrow(X)*cov(X)

```
<br>\vspace{0.5cm}

The code chunks below show how to run local benchmarks. To keep CRAN vignette
builds fast and deterministic, these chunks are not evaluated during rendering.

 - Default convergence tolerance with specified tuning parameter (no cross validation):

<br>\vspace{0.5cm}
```{r, message = FALSE, eval = FALSE}

# benchmark CVglasso - defaults
library(microbenchmark)
microbenchmark(CVglasso(S = sample, lam = 0.1, trace = "none"))

```
<br>\vspace{0.5cm}

 - Stricter convergence tolerance with specified tuning parameter (no cross validation):

<br>\vspace{0.5cm}
```{r, message = FALSE, eval = FALSE}

# benchmark CVglasso - tolerance 1e-6
microbenchmark(CVglasso(S = sample, lam = 0.1, tol = 1e-6, trace = "none"))

```
<br>\vspace{0.5cm}

 - Default convergence tolerance with cross validation for `lam`:

<br>\vspace{0.5cm}
```{r, message = FALSE, eval = FALSE}

# benchmark CVglasso CV - default parameter grid
microbenchmark(CVglasso(X, trace = "none"), times = 5)

```
<br>\vspace{0.5cm}

 - Parallel (`cores = 2`) cross validation:

<br>\vspace{0.5cm}
```{r, message = FALSE, eval = FALSE}

# benchmark CVglasso parallel CV
microbenchmark(CVglasso(X, cores = 2, trace = "none"), times = 5)

```
<br>\vspace{0.5cm}
