Skip to content

Support Joseph-form in Kalman filters #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
franckgaga opened this issue Sep 11, 2024 · 10 comments
Open

Support Joseph-form in Kalman filters #98

franckgaga opened this issue Sep 11, 2024 · 10 comments
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@franckgaga
Copy link
Member

It would be nice to support the Joseph-form covariance update equation in KalmanFilter, ExtendedKalmanFilter and UnscentedKalmanFilter. It theoretically guarantee positive-definite covariance matrices, even with finite precision floating-point arithmetic.

In my experience although, I never had any problems with the classical equation, both with the Kalman filter and the unscented version. I personally don't consider that as a high priority feature.

@franckgaga franckgaga self-assigned this Sep 11, 2024
@baggepinnen
Copy link
Member

Another option is to have the QR form https://arxiv.org/abs/2208.06452
implemented here https://github.com/baggepinnen/LowLevelParticleFilters.jl/blob/master/src/sq_kalman.jl

@franckgaga
Copy link
Member Author

franckgaga commented Jan 22, 2025

I noticed that once and a while in the MovingHorizonEstimator tests, the estimation fails because of the arrival covariance $\mathbf{\bar{P}}$ estimation based on UnscentedKalmanFilter, that internally uses cholesky. The error is PosDefException: matrix is not positive definite; Factorization failed.

It raises two points:

  1. Should I encapsulate arrival covariance estimation of the MHE in a try block ? If yes what would be the fallback ? Do not update the arrival covariance (keep the one from the last iteration $k-1$) and emit a @warn ? I think It would be a good idea since with a reasonably long He, the impact of the arrival estimate term in the cost function is typically quite low. Morever, I frequently used the MHE with a constant arrival covariance estimate in the past and it was simpler and working quite well.

edit: an alternative and presumably better fallback would be to modify the current arrival covariance matrix to the nearest symmetric one with P̄ = 0.5*(P̄ + P̄')

  1. The Joseph-From should be implemented in the package and be the default for the MovingHorizonEstimator arrival covariance estimation. The "numerical noise" produced by the MHE optimization is more risky to produce negative eigenvalues in the covariance matrix.

Do you have an opinion @baggepinnen ?

Thanks!

@baggepinnen
Copy link
Member

I include P̄ = 0.5*(P̄ + P̄') in more or less all code that modifies covariance matrices, I find that this is often all that is required to handle numerical issues in Kalman filters

@franckgaga
Copy link
Member Author

I'll also add this line as another "saftey net" for the MHE.

@franckgaga franckgaga added enhancement New feature or request invalid This doesn't seem right and removed invalid This doesn't seem right labels Feb 28, 2025
@franckgaga franckgaga added the good first issue Good for newcomers label Mar 16, 2025
@1-Bart-1
Copy link

I noticed that once and a while in the MovingHorizonEstimator tests, the estimation fails because of the arrival covariance P ¯ estimation based on UnscentedKalmanFilter, that internally uses cholesky. The error is PosDefException: matrix is not positive definite; Factorization failed.

I have gotten this error several times as well.

@franckgaga
Copy link
Member Author

franckgaga commented Mar 25, 2025

@1-Bart-1 If you have a MWE that systematically produces this error, send it to me. I will try to investigate on the error. Fredrik and I emit the hypothesis that It may be caused by the default values of the UKF $\alpha, \beta, \kappa$ weights (the defaults are not rigorously justified, it's just values that many people use without a real justification).

If that's really the case, a breaking change may be worthwhile.

@baggepinnen
Copy link
Member

@1-Bart-1 see this comment and the linked article for more details on the UT parameters
baggepinnen/LowLevelParticleFilters.jl#191 (comment)

@1-Bart-1
Copy link

I don't have a MWE... If I remember correctly, the error tends to disappear when changing some of the estimator weights. But I will definitely come back here if the error appears again.

@1-Bart-1
Copy link

1-Bart-1 commented Apr 17, 2025

I am getting this issue again. I don't have a lightweight example, but you can recreate the issue by running mwe/online_lin_simple_kite.jl from KitePredictiveControl.jl#cholesky. Let me know if you have any issues recreating this. Is it possible to play around with the cholesky weights in MPC.jl currently?
Also note that I use the KalmanFilter in this example, not the UnscentedKalmanFilter.

@franckgaga
Copy link
Member Author

franckgaga commented Apr 17, 2025

Thanks for the exemple! Above I was mentioning an issue with the MHE that uses the UKF internally for the arrival covariance estimation. So it's not related to the UKF $\alpha, \beta, \kappa$ weights.

Cholesky is used in KalmanFilter for array division without any allocation (/ and
\ operators are allocating). But it's kind of similar as the issue above: the covariance estimate $\mathbf{P}$ becomes no longer positive definite at some points in the simulation, probably because of numerical issues. I'll take a look at you exemple. It's possible that the Joseph-form would be a solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

3 participants