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)
myChol <- chol(mcov)
rbind(head(maha(X, mu, mcov), 10),
head(maha(X, mu, myChol, isChol = TRUE), 10),
head(mahalanobis(X, mu, mcov), 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(microbenchmark)
a <- cbind(
maha(X, mu, mcov),
maha(X, mu, myChol, isChol = TRUE),
mahalanobis(X, mu, mcov))
# Same output as mahalanobis
a[ , 1] / a[, 3]
a[ , 2] / a[, 3]
microbenchmark(maha(X, mu, mcov),
maha(X, mu, myChol, isChol = TRUE),
mahalanobis(X, mu, mcov))
}
}
Run the code above in your browser using DataLab