# Fast Mahalanobis distance computation with singular covariance matrix

I'm trying to calculate the following Mahlanobis distance.

$x^{T}$pinv($C$)$x$

Since covariance matrix, $C$ is singular, pinv($C$) means pseudo-inverse of C. However, my $C$ is very large, so it's very time-consuming to calculate pinv($C$). Thus, I'm trying to calculate this without pseudo-inverse computation like this. Since C is symmetric, C has eigen decomposition. $C = USU^T = US^{1/2}(US^{1/2})^T=JJ^T$ (here, $J=US^{1/2}$)

Then, $x^T$pinv($C$)$x$ $=$ $x^T$pinv($JJ^T$)$x$$=(pinv(J)x)^T(pinv(J)x)=$$y^Ty$ (here, $y$$=$pinv($J$)$x$)

$y$ can be calculated from $Jy = x$ using QR factorization.

This is my idea. Is there any problem in my logic?

• This should work, but finding $J$ shouldn't really be any faster than $\operatorname{pinv}(C)$: you're doing an eigendecomposition, which is equally expensive as the SVD used in pinv.... It may be more numerically stable, though. – Dougal Jul 3 '15 at 7:59