> library(mnormt)
> Wi <- function(n) {
+ fun <- function(w, i) {
+ w * (-log(w)) * ((w - w * log(w))^(i - 1)) * ((1 - w +
+ w * log(w))^(n - i))
+ }
+ out <- numeric(n)
+ for (i in 1:n) {
+ out[i] <- integrate(fun, lower = 0, upper = 1, i = i,
+ subdivisions = 10000, rel.tol = .Machine$double.eps^0.001)$value
+ out[i] <- out[i] * n * choose(n - 1, i - 1)
+ }
+ out
+ }
> Hi <- function(z) {
+ n <- dim(z)[1]
+ x <- array(z[, 1])
+ y <- array(z[, 2])
+ Hi.temp <- apply(z, 1, function(temp) length(which(x[which(x !=
+ temp[1])] <= temp[1] & y[which(y != temp[2])] <= temp[2]))/(n -
+ 1))
+ return(as.matrix(Hi.temp))
+ }
> K.plot <- function(data, title = "") {
+ n <- nrow(data)
+ W <- Wi(n)
+ H <- sort(matrix(Hi(data)))
+ xrange <- c(0, 1)
+ yrange <- c(0, 1)
+ seq <- seq(from = 0.01, to = 1, by = 0.01)
+ plot(W, H, xlim = xrange, ylim = yrange, pch = 19, col = "red",
+ xlab = "Wi", ylab = "Hi", main = title)
+ lines(c(0, 1), c(0, 1), lty = 2, lwd = 2, col = "blue")
+ K0 <- seq - seq * log(seq)
+ lines(c(0, seq), c(0, K0), lty = 2, lwd = 2, col = "blue")
+ }
> par(mfrow = c(2, 2))
> ind.data <- cbind(runif(1000), runif(1000))
> K.plot(ind.data, "X and Y independent ")
> runs <- 1000
> sample.mean <- c(1, 2)
> sample.cov <- matrix(c(1, 0.7, 0.7, 1), ncol = 2)
> data <- rmnorm(runs, mean = sample.mean, varcov = sample.cov)
> K.plot(data, "Positive cor = 0.7")
> sample.cov <- matrix(c(1, -0.7, -0.7, 1), ncol = 2)
> data <- rmnorm(runs, mean = sample.mean, varcov = sample.cov)
> K.plot(data, "Negative cor = -0.7")
> sample.cov <- matrix(c(1, -0.9999, -0.9999, 1), ncol = 2)
> data <- rmnorm(runs, mean = sample.mean, varcov = sample.cov)
> K.plot(data, "Perfect Negative Cor") |