I have a data matrix $X$ with shape $p\times n$.
It might not matter but I interpret $X$ is $n$ vectors each containing $p$ features.
Then I compute $Q = X X^{T} / n$. This implies that $Q$ is positive definite.
I interpret $Q$ as covariance matrix of data which are columns of $X$. (Normally mean should be subtracted from $X$ before computing covariance this way. However the mean of $X$ is 0 in my case. That is why the formula for covariance is simpler in my case.)
Then I compute $Q^{-1}$, the inverse of $Q$. Which should be also positive definite.
I want to compute Mahalanobis distance for each column $x$ of $X$ as follows:
$\sqrt{x^T Q^{-1} x}$
However for some columns I get that $x^T Q^{-1} x$ is negative. This should not be possible in theory. So I suspect that the error is caused by numerical errors caused by software.
I use the Math.Net numerics library in C#. How do I avoid this problem?
I tried replacing $Q$ and $Q^{-1}$ by their transpose. The result should be the same in theory but slightly differs in practice. But the difference wasn't big enough I still get negative number for the same column.
What else I can do?