If the covariance matrix isn't full rank, there exists a linear combination of your variables which has zero variance. That linear combination should always equal some constant! (Note: any eigenvector of covariance matrix associated with a zero eigenvalue defines a linear combination that should have zero variance.)
If in your data you have a point where the linear combination does not equal that constant, in some sense, the Mahlanobis distance is infinite: in units of the standard deviation (which is zero), the point is infinitely far from the mean.
On the other hand, if the linear combination that should be zero is in fact zero, there's no problem. But how would you calculate the distance? Two basic approaches are:
- Use pseudo inverse instead of inverse. (Simple!)
md = x' * pinv(Sigma) * x
- Reduce the number of dimensions until everything is full rank. Your data is actually in a lower dimensional space than the current number of variables, and you can transform your data and covariance matrix to operate directly in that lower dimensional space.
- Eg. Use singular value decomposition
[U, S, V] = svd(X)
. $Y = X V$ and $\Sigma_Y = V' * \Sigma_X * V$ and then drop dimensions associated with zero (or near zero) singular values.
(Note: above formulas assume everything already demeaned)
Example:
Let's imagine the a simple two variable case where $x_1$ and $x_2$ are mean zero, $2 x_1 = x_2 $, and the covariance matrix is given by:
$$\Sigma = \left[ \begin{array}{cc} 1 & 2 \\ 2 & 4\end{array}\right] $$
$2x_1 - x_2$ should always be zero! It has zero variance:
$$ \left[ \begin{array}{c} 2& -1\end{array}\right] \left[ \begin{array}{cc} 1 & 2 \\ 2 & 4\end{array}\right] \left[ \begin{array}{c} 2\\-1\end{array}\right] = 0 $$
In this case, for computing the distance, we could either:
- Use the pseudo inverse of $\Sigma$
- Only use one of the variables (either $x_1$ or $x_2$) and use the appropriate submatrix of $\Sigma$
- Do svd stuff mentioned earlier. (Makes sense for high dimensions but rather silly for two dimensions.)