# Order of Matrix Operations in Mahalanobis Calculations

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).

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.

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

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$.
• 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. Aug 14, 2014 at 16:22
• @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. Aug 14, 2014 at 16:52