4
$\begingroup$

I'm using Pairwise Mahalanobis distance in R as code to calculate the Mahalanobis distance:

# express difference (X1-X2) as atomic row vector
d <- as.matrix(X1-X2)[1,] 

# solve  (covariance matrix) %*% x = d for x
x <- solve(cov(R),d)

# Mahalanobis calculation forced in two steps
Ma <- sum(d*x)

with X and Y as the individual vectors and R as the population covariance matrix. The distance itself is defined as:

Equation

And as far as I can see the square root is missing in the upper code. Right?

edit: To add some information: I have vectors with four parameters:

Device1:
Voltage Slope Voltage_irr Slope_irr
  355    6.8    354.2       6.67
Device2:
Voltage Slope Voltage_irr Slope_irr
  357.2  6.3    356.7       6.11
Device3:
(..)

Each vector represents a device and I want to estimate/calculate how similar the devices are to each other. I wonder now if there is a difference in using the squared Mahalanobis distance or using the root of the Mahalanobis distance.

$\endgroup$
2
  • 3
    $\begingroup$ Yes, there is no square root in the code. $\endgroup$
    – jbowman
    Commented Nov 23, 2017 at 21:07
  • 1
    $\begingroup$ The code you link to is contained in (a) a barely upvoted question to (b) a closed thread that (c) refers to a much more highly duplicate thread whose answers (d) make it very clear they are discussing squared Mahalanobis distances! One lesson is to select your resources carefully and study them before using them. $\endgroup$
    – whuber
    Commented Nov 24, 2017 at 13:49

1 Answer 1

7
$\begingroup$

The whole thread you linked, and the code you showed which was provided as an answer there, is in terms of Mahalanobis Distance squared, not Mahalanobis Distance. For certain purposes, it is convenient to work in terms of Mahalanobis Distance squared, but if you want Mahalanobis Distance, you need to take the square root of Mahalanobis Distance squared.

Note to close voters, there is a "statistical" issue here, the distinction between Mahalanobis Distance squared and Mahalanobis Distance, both of which are widely used.

$\endgroup$
7
  • 1
    $\begingroup$ Thanks! I wasn't aware of that. When/why is a squared distance useful? $\endgroup$
    – Ben
    Commented Nov 23, 2017 at 21:43
  • 4
    $\begingroup$ Perhaps "used" is a better term than "useful". For instance, if you have a pre-established threshold for Mahalanobis Distance of 3, there is no need to take the square root, just compare Mahalanobis Distance squared to 9 (as a chi-squared threshold). I've seen this done in practice. Some people contribute to the confusion by cavalierly calling Mahalanobis Distance squared as Mahalanobis Distance. $\endgroup$ Commented Nov 23, 2017 at 21:57
  • 1
    $\begingroup$ @MarkL.Stone I agree with you, the question wasn't so much about coding but about a user seeing two different definitions of Mahalanobis distance and needing help to make sense of that. Your answer clarified his doubts. $\endgroup$
    – DeltaIV
    Commented Nov 24, 2017 at 10:14
  • 1
    $\begingroup$ @Ben for $x\ge 0$, $f(x)=x^2$ is a monotone increasing function. Putting a threshold $c$ on $y=f(x)$ or a threshold $f^{-1}(c)$ on $x=f^{-1}(y)$ is exactly the same thing: i.e., the points for which $f(x)<c$ are exactly the same for which $f^{-1}(y)<f^{-1}(c)$. What's your confusion? Maybe you should add some details on how you plan to apply Mahalanobis distance in your analysis. $\endgroup$
    – DeltaIV
    Commented Nov 24, 2017 at 10:20
  • 1
    $\begingroup$ @DeltaIV Hi, thanks! I guess I understand this but I still struggle with the circumstance that some use squared distances and other not :) Isn't it arbitray/meaningless? $\endgroup$
    – Ben
    Commented Nov 24, 2017 at 10:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.