2
$\begingroup$

I'm teaching myself to translate equations to code after many years of letting my math skills atrophy, and am trying to do it on my own as much as possible. I've run into a couple of difficult equations though that I just can't solve without a few pointers though, particularly the one for Mahalanobis distances mentioned at http://en.wikipedia.org/wiki/Mahalanobis_distance (where the equation depicted below was taken from).

enter image description here

enter image description here

enter image description here

The main thing that's slowing me down is the order of operations, in large part because the are matrix operations mixed in. $S^{-1}$ is an inverse covariance matrix, which I've already managed to write code for. The square root it trivial, of course.

I'm not sure if I'm reading the part under the square root correctly, but my first guess is that I should compare every single data point in the X vector to its corresponding mean, transpose that resulting matrix and then multiply it by the inverse covariance matrix, then multiply that result by another matrix consisting of each X vector value compared to the corresponding mean again. If I understand this correctly, it will return one Mahalanobis distance for each X value in the vector. Is there anything in this order of operations I might be doing incorrectly?

Since this is a more advanced topic that some of the stats I've coded to date, it's difficult to find examples on the Web with dummy data I can check my own code against, let alone examples of how to calculate it out step by step. If anyone knows off-hand of any such examples, that also might be helpful. I have the impression that anyone familiar with such equations would find it trivial to solve them, but I don't have any experience with 'em yet.

$\endgroup$
1
  • $\begingroup$ If you have $S$, but don't have $S^{-1}$ you shouldn't explicitly invert $S$ to do this calculation. $\endgroup$
    – Glen_b
    May 25, 2015 at 22:47

1 Answer 1

3
$\begingroup$

"If I understand this correctly, it will return one Mahalanobis distance for each X value in the vector."

A Mahalanobis distance has a scalar value. Suppose $x$ is a $k \times 1$ vector. Then $x-\mu$ is too, while $S$ and therefore $S^{-1}$ are $k \times k$. Hence, taking transposes into account, the dimension of the expression under the square root is $(1 \times k)(k \times k)(k \times 1)=1\times 1$.

$\endgroup$
3
  • 1
    $\begingroup$ It might be helpful to add that Mahalanobis distance measures a distance from a point $x$ to a Gaussian distribution with mean $\mu$ and covariance $S$, so $\mu$ is not a mean of $x$, as OP seems to think. $\endgroup$
    – amoeba
    Aug 14, 2014 at 16:22
  • 1
    $\begingroup$ @amoeba This is a good point. A distance being from $x$ to $y$ - i.e. not just of one variable $x$. I think the expression was lifted from the wikipedia article which is a little unclear. $\endgroup$ Aug 14, 2014 at 16:52
  • $\begingroup$ Thanks conjectures and amoeba, both of those points were helpful - especially the way you reduced it down to (1×k)(k×k)(k×1)=1×1. The thing that threw me off was the use of the distribution mean together with a vector as x−μ when many of the equations I've been looking at lately frequently have x−μ components, except without vector dimensionality. I'm new to a lot of this stuff, but I figure the best way to learn it is to slog through it and make mistakes along the way. Thanks again. $\endgroup$ Aug 14, 2014 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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