# How to work out a Mahalanobis distance

Currently going through my Lecturer's notes and came across the Mahalanobis distance but nothing in the notes on how to work this out and googling hasn't really turned up anything either. My maths isn't great but I've been at it for 20 minutes now trying to come up with the same answer and can't.

The Mahalanobis norm is defined by $D_M^2(x) = (x-\mu)^T C^{-1}(x-\mu)$, and for my calculation, I have $(x-\mu)^T = [-90, -100]$ with covariance matrix

$C=\begin{pmatrix} 0.00025 & -0.00015 \\ -0.00015 & 0.00025 \end{pmatrix}$

-
I get a negative answer from your vectors (but your covariance matrix is indeed positive-definite); are you sure you computed $\mathbf x-\mu$ correctly? –  Guess who it is. May 16 '11 at 15:38
Yes... the figures are from the lecture notes. (x-m)^T is the vector transposed, and they're -90 and -100 respectively –  Dark Star1 May 16 '11 at 15:59
@J.M. If the matrix is positive definite, then there is no way to get a negative answer, regardless of what $x-\mu$ is. Every positive definite matrix gives rise to a positive definite bilinear form, by $B(x,y)=x^T A y$, (and in the finite dimensional case, every positive definite bilinear form comes from such a matrix). –  Aaron May 16 '11 at 16:17
Yes, that was an error @Aaron; I misread the vectors. I'm getting a Mahalanobis distance of $4250\sqrt{10}\approx 1.34397\times 10^4$. –  Guess who it is. May 16 '11 at 16:21

Plugging in, we have $C^{-1}=\begin{pmatrix} 6250 & 3750 \\ 3750 & 6250 \end{pmatrix}$, and so $D_M^2= \begin{pmatrix}-90 & -100 \end{pmatrix}\begin{pmatrix} 6250 & 3750 \\ 3750 & 6250 \end{pmatrix} \begin{pmatrix}-90 \\ -100\end{pmatrix}=1.80625*10^8.$ Taking square roots, we see that the norm of $x$ is approximately $13439.7$.

-
Thanks. I got 1.825. We're not required to normalise x and the matrix was already inverted I should've been more specific with my notation. –  Dark Star1 May 16 '11 at 22:17

I found myself factorising things to make the computation easier.

We have $x-\mu=-10[9\ 10]^T$ and $C=0.00005\begin{bmatrix}5&-3\\-3&5\end{bmatrix}$. So $C^{-1}=0.00005^{-1}\times \frac1{16}\begin{bmatrix}5&3\\3&5\end{bmatrix}$ and so $$(x-\mu)^TC^{-1}(x-\mu)= 0.00005^{-1} \times 1/16 \times (-10) \times (-10) \times [9\ 10]\begin{bmatrix}5&3\\3&5\end{bmatrix}\begin{bmatrix}9\\10\end{bmatrix}=125000 \times [9\ 10]\begin{bmatrix}75\\77\end{bmatrix}=125000 \times 1445=180625000.$$ So $D=\sqrt{180625000}\approx 13440$.

[Maybe you should look up matrix multiplication and inversion of $2\times 2$ matrices, since that's all that's going on here].

-
You forgot to invert $C$. –  Aaron May 16 '11 at 16:30
Thanks! Since I couldn't see how to delete my answer, I fixed it. (I hope.) –  mac May 16 '11 at 16:45
More exactly(?) $D = \sqrt {180625000} = 4250 * \sqrt {10}$ –  Gottfried Helms May 26 '11 at 13:52