---
title: "Verifying the variances."
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Verifying the variances.}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

## Introduction

Combining the `type` and `parallel` options of `alphaci` yields a total of $6$ different asymptotic variance estimates. In this file we informally verify the computations of these estimates.

| Distribution | Parallel |
|--------------|----------|
| Normal       | Yes      |
| Normal       | No       |
| Elliptical   | Yes      |
| Elliptical   | No       |
| Arbitrary    | Yes      |
| Arbitrary    | No       |

In our setup we have $n = 1,000,000$, which should suffice. We use $k = 5$ across the board.


```r
library("future.apply")
plan(multisession)
avar <- alphaci:::avar
avar_std <- alphaci:::avar_std
set.seed(313)
k <- 5
n <- 10 ** 6
print_table <- function(x) {
  table <- rbind(
  c("normal" = avar(x, sigma_mat, type = "normal", parallel = FALSE),
    "adf" = avar(x, sigma_mat, type = "adf", parallel = FALSE),
    "elliptical" = avar(x, sigma_mat, type = "elliptical", parallel = FALSE),
    "normal_par" = avar(x, sigma_mat, type = "normal", parallel = TRUE),
    "adf_par" = avar(x, sigma_mat, type = "adf", parallel = TRUE),
    "elliptical_par" = avar(x, sigma_mat, type = "elliptical", parallel = TRUE)),
  c("normal" = avar_std(x, sigma_mat, type = "normal", parallel = FALSE),
    "adf" = avar_std(x, sigma_mat, type = "adf", parallel = FALSE),
    "elliptical" = avar_std(x, sigma_mat, type = "elliptical", parallel = FALSE),
    "normal_par" = avar_std(x, sigma_mat, type = "normal", parallel = TRUE),
    "adf_par" = avar_std(x, sigma_mat, type = "adf", parallel = TRUE),
    "elliptical_par" = avar_std(x, sigma_mat, type = "elliptical", parallel = TRUE))
  )
  rownames(table) <- c("alpha", "alpha_std")
  knitr::kable(round(table, 3))
}
```

## Normality
First we simulate from a normal model that isn't parallel. Then the normal model should not be equal to the normal parallel method. Moreover, the adf method should be approximately equal to the normal method, but the adpf paralell model should not be equal to the adf model. The table below confirms these points.

```r
lambda <- 1/(5:1)^2
sigma <- 1/(1:5)^2
x <- alphaci:::simulate_congeneric(n, k, sigma, lambda)
sigma_mat <- cov(x)
print_table(x)
```



|          | normal|   adf| elliptical| normal_par| adf_par| elliptical_par|
|:---------|------:|-----:|----------:|----------:|-------:|--------------:|
|alpha     |  0.666| 0.665|      0.665|      0.865|   0.019|          0.865|
|alpha_std |  0.187| 0.187|      0.187|      0.233|   0.153|          0.233|

If the normal parallel model is true, all of the methods should yield approximately the same result. Again, this is the case.

```r
lambda <- 1
sigma <- 2
x <- alphaci:::simulate_congeneric(n, k, sigma, lambda)
sigma_mat <- cov(x)
print_table(x)
```



|          | normal|   adf| elliptical| normal_par| adf_par| elliptical_par|
|:---------|------:|-----:|----------:|----------:|-------:|--------------:|
|alpha     |  0.495| 0.494|      0.495|      0.495|   0.494|          0.495|
|alpha_std |  0.495| 0.494|      0.495|      0.495|   0.494|          0.495|

## Ellipticity

We simulate from an elliptical model, the multivariate Laplace, that is neither parallel nor normal. Then the elliptical model should not be equal to the elliptical parallel method, the normal models should not equal the elliptical models. Moreover, the adf method should be approximately equal to the elliptical method, but the adpf paralell model should not be equal to the adf model. The table below confirms these points.

```r
lambda <- 1/(5:1)^2
sigma <- 1/(1:5)^2
mat <- lambda %*% t(lambda) + diag(sigma)
x <- LaplacesDemon::rmvl(n, mu = 0, Sigma = mat)
sigma_mat <- cov(x)
print_table(x)
```



|          | normal|   adf| elliptical| normal_par| adf_par| elliptical_par|
|:---------|------:|-----:|----------:|----------:|-------:|--------------:|
|alpha     |  0.851| 1.700|      1.696|      1.005|   0.335|          2.002|
|alpha_std |  0.611| 1.219|      1.218|      0.679|   0.983|          1.352|

Finally, we check the eliptical parallel model. The adfs and elliptical should be equal, but not equal to the normal. The table below confirms this.

```r
lambda <- 1
sigma <- 2
mat <- rep(1, k) %*% t(rep(1, k)) * lambda^2 + diag(sigma, nrow = k)
x <- LaplacesDemon::rmvl(n, mu = 0, Sigma = mat)
sigma_mat <- cov(x)
print_table(x)
```



|          | normal|   adf| elliptical| normal_par| adf_par| elliptical_par|
|:---------|------:|-----:|----------:|----------:|-------:|--------------:|
|alpha     |  0.205| 0.405|      0.408|      0.205|   0.405|          0.408|
|alpha_std |  0.205| 0.405|      0.408|      0.205|   0.405|          0.408|
