Testing Independence
Purpose
Well, the purpose of this script is very simple
I have a n by p matrix which alterate says that they are dependent and null says that the p dim are independent
> set.seed(1977) > library(mnormt) > sample.mean <- c(1, 1) > sample.cov <- matrix(c(1, 0.5, 0.5, 1), nrow = 2) > n <- 1000 > x <- rmnorm(n, mean = sample.mean, varcov = sample.cov) > test.cov <- cov(x) > y <- diag(2) > diag(y) <- 1/diag(test.cov) > test.stat <- det(sqrt(y) %*% test.cov %*% sqrt(y)) > test.stat <- -n * log(test.stat) > p <- 2 > qchisq(0.95, p * (p - 1)/2) [1] 3.841459 > test.stat [1] 325.0098 |
Obviously the test.stat is outside the Null region and independence is rejected
> set.seed(1977) > library(mnormt) > sample.mean <- c(1, 1) > sample.cov <- matrix(c(1, -0.005, -0.005, 1), nrow = 2) > n <- 1000 > x <- rmnorm(n, mean = sample.mean, varcov = sample.cov) > test.cov <- cov(x) > y <- diag(2) > diag(y) <- 1/diag(test.cov) > test.stat <- det(sqrt(y) %*% test.cov %*% sqrt(y)) > test.stat <- -n * log(test.stat) > p <- 2 > qchisq(0.95, p * (p - 1)/2) [1] 3.841459 > test.stat [1] 0.3620388 |
Obviously the test.stat is well with in the Null region and independence is not rejected
Let me try with NIFTY and GOLD as assets and check for the independence
> set.seed(1977) > x <- coredata(temp.ret[, 1:2]) > n <- dim(x)[1] > test.cov <- cov(x) > y <- diag(2) > diag(y) <- 1/diag(test.cov) > test.stat <- det(sqrt(y) %*% test.cov %*% sqrt(y)) > test.stat <- -n * log(test.stat) > p <- 2 > qchisq(0.95, p * (p - 1)/2) [1] 3.841459 > test.stat [1] 1.176335 |
Clearly it is in the acceptance region in the recent 300 days of data
> set.seed(1977) > x <- coredata(temp.ret[1200:1500, 1:2]) > n <- dim(x)[1] > test.cov <- cov(x) > y <- diag(2) > diag(y) <- 1/diag(test.cov) > test.stat <- det(sqrt(y) %*% test.cov %*% sqrt(y)) > test.stat <- -n * log(test.stat) > p <- 2 > qchisq(0.95, p * (p - 1)/2) [1] 3.841459 > test.stat [1] 9.415422e-05 |
Clearly it is in the acceptance region in the recent 300 days of data