# Unscented Kalman filter-negative covariance matrix

I have recently started working on the unscented Kalman filter. I coded the numerically stable version (i.e., square root Kalman filter) and used MATLAB for implementing. In the final update step, where we update the state covariance matrix using cholupdate I get an error.

Pk = cholupdate(expected_S,K*Sy,'-')


Basically, expected_S'*expected_S-(K*Sy)*(K*Sy)' is not positive semi-definite (where expected_P=expected_S'*expected_S;) and so cholupdate returns an error.

I cannot understand why this is happening in the square root implementation. (I have checked the derivations and coding didn't seem to find an error.)

PS: I am currently testing linear models on UKF just to check if its optimal for linear models, but I keep getting the above error (for local linear trend model).

• This is probably due to some numerical instability (that is: your derivations are probably correct, just an issue with losing precision from repeated computations) which tends to occur when dealing with covariance matrices. There are a few tricks that may help you out - for starters, have a look at stats.stackexchange.com/questions/6364/…
– Nick
Commented Jan 21, 2013 at 20:24
• Hi Nick, Thanks. I did what you suggested. It works but not in all cases. The results I get are ok and I can work with them. But even if I set the non zero eigen values to 0 it doesnt gaurantee a semi positive definite matrix. What I would like to understand or do is a) How to avoid this permanently or b) How to know which cases it will work and which it wont.... Any thoughts on this would really be helpful... Again Thanks. Commented Jan 27, 2013 at 19:34
• Yea, this is definitely a work-around. I typically set the negative eigenvalues to 1e-8 (or some small positive value). I'm not sure this problem is avoidable, especially when you're doing repeated operations on a covariance matrix due to the imprecision of floating point variables.
– Nick
Commented Jan 27, 2013 at 19:54
• aah...Thanks. I will do some more testing and revert to you. Commented Jan 28, 2013 at 10:06
• This question appears to be off-topic because it is about coding. Commented Sep 11, 2013 at 17:07