I've got an extended Kalman filter with innovation covariance defined as $\mathbf{W}=\mathbf{H}\mathbf{P}\mathbf{H}^\textrm{T} + \mathbf{R}$. I want to know the squared Mahalanobis distance $\|z\|^2$ between a (vector) measurement residual $\mathbf{y}$ and the matrix $\mathbf{W}$, e.g., $\|z\|^2 = \mathbf{y}^\textrm{T} \mathbf{W}^{-1} \mathbf{y}$.
At first I was using the Cholesky decomposition and then forward substitution to solve for $\mathbf{L}\mathbf{z}=\mathbf{y}$. Then, $\|z\|^2 = \mathbf{z}^\textrm{T}\mathbf{z}$.
However, sometimes $\mathbf{W}$ does not appear to be positive definite, though it is symmetric. This seems to happen when — based on the residuals — the Mahalanobis distance should be quite large. I can't do a Cholesky decomposition in these cases, so instead I do an LDL' decomposition, and say that $\|z\|^2 = \mathbf{z}^\textrm{T} \mathbf{D}^{-1} \mathbf{z} = \sum_i^{n-1} \frac{z_i^2}{d_i}$.
That gives me a reasonable value in some cases, but sometimes diagonal elements are negative. That gives me a very large negative squared Mahalanobis distance, which doesn't make a lot of sense to me.
I figure I'm making some math error, but I'm also unsure that it should ever be necessary to do an LDL' decomposition. Might something be wrong with my computation of $\mathbf{W}$?
What does it mean when my sample covariance is not positive definite? How should I decompose this in order to compute the squared Mahalanobis distance?