rlabbe / filterpy

Python Kalman filtering and optimal estimation library. Implements Kalman filter, particle filter, Extended Kalman filter, Unscented Kalman filter, g-h (alpha-beta), least squares, H Infinity, smoothers, and more. Has companion book 'Kalman and Bayesian Filters in Python'.
MIT License
3.22k stars 612 forks source link

Possible issue with loglikelihood computation in kalman/kalman_filter.py update function #260

Open sortega87 opened 2 years ago

sortega87 commented 2 years ago

The computation of the log-likelihood, on the kalman update function, is currently made after the state vector is updated with the last observation. That is, the log_likelihood is computed in line 1434 using the updated state of line 1420.

I believe the computation should be made with the prior state estimate; moving the log_likelihood computation just after line 1408 should solve the issue.

https://github.com/rlabbe/filterpy/blob/a437893597957764fb6b415bfb5640bb117f5b99/filterpy/kalman/kalman_filter.py#L1329-L1436

Eheran1 commented 2 years ago

Have you compared how this affects the filter?

sortega87 commented 2 years ago

@Eheran1 I had trouble with the current implementation while using the log likelihood to fit the parameters of a Dynamic Linear Model (DLM, as in here). I had no luck in fitting the model parameters with the current implementation. However, I was able to fit the model, and the resulting parameters were consistent with my data, with the suggested changes.

The log likelihood is computed using the posterior state estimate in the current implementation, I believe the log likelihood should be computed using the prior state estimate.