Skip to contents

Mahalanobis Distance using eclairs() decomposition

Usage

mahalanobisDistance(ecl, X, lambda, center = FALSE)

Arguments

ecl

estimate of covariance/correlation matrix from eclairs storing \(U\), \(d_1^2\), \(\lambda\) and \(\nu\)

X

data matrix

lambda

specify lambda and override value from ‘ecl’

center

logical: should columns be centered internally

Value

array of distances

Details

Evaluate quadratic form \((X-\mu)^T \Sigma^{-1} (X-\mu)\) where covariance is estimated from finite sample

Examples

library(Rfast)

n <- 800 # number of samples
p <- 200 # number of features

# create correlation matrix
Sigma <- autocorr.mat(p, .9)

# draw data from correlation matrix Sigma
Y <- rmvnorm(n, rep(0, p), sigma = Sigma * 5.1, seed = 1)

# eclairs decomposition
ecl <- eclairs(Y)

# Mahalanobis distance after mean centering
Y_center <- scale(Y, scale = FALSE)
mu <- colMeans(Y)

# Standard R method
a <- mahalanobis(Y, mu, cov = cov(Y))

# distance using eclairs decomposition, no shrinage
b <- mahalanobisDistance(ecl, Y_center, lambda = 0)
range(a - b)
#> [1] -1.676881e-12  3.069545e-12

# with shrinkage
d <- mahalanobisDistance(ecl, Y_center)

# centering internally
e <- mahalanobisDistance(ecl, Y, center = TRUE)
range(d - e)
#> [1] 0 0
#