N <- 100
d <- 5
mu <- 1:d
X <- t(t(matrix(rnorm(N*d), N, d)) + mu)
tmp <- matrix(rnorm(d^2), d, d)
mcov <- tcrossprod(tmp, tmp) + diag(0.5, d)
myChol <- chol(mcov)
head(dmvn(X, mu, mcov), 10)
head(dmvn(X, mu, myChol, isChol = TRUE), 10)
if (FALSE) {
# Performance comparison: microbenchmark does not work on all
# platforms, hence we need to check whether it is installed
if( "microbenchmark" %in% rownames(installed.packages()) ){
library(mvtnorm)
library(microbenchmark)
a <- cbind(
dmvn(X, mu, mcov),
dmvn(X, mu, myChol, isChol = TRUE),
dmvnorm(X, mu, mcov))
# Check if we get the same output as dmvnorm()
a[ , 1] / a[, 3]
a[ , 2] / a[, 3]
microbenchmark(dmvn(X, mu, myChol, isChol = TRUE),
dmvn(X, mu, mcov),
dmvnorm(X, mu, mcov))
detach("package:mvtnorm", unload=TRUE)
}
}
Run the code above in your browser using DataLab