diff --git a/filterpy/kalman/square_root.py b/filterpy/kalman/square_root.py index fdcb3e4..373a3cc 100644 --- a/filterpy/kalman/square_root.py +++ b/filterpy/kalman/square_root.py @@ -292,7 +292,7 @@ def P_prior(self): @property def P_post(self): """ covariance matrix of the posterior""" - return dot(self._P1_2_prior, self._P1_2_prior.T) + return dot(self._P1_2_post, self._P1_2_post.T) @property def P1_2(self): diff --git a/filterpy/kalman/tests/test_sqrt_posterior_covariance.py b/filterpy/kalman/tests/test_sqrt_posterior_covariance.py new file mode 100644 index 0000000..472913b --- /dev/null +++ b/filterpy/kalman/tests/test_sqrt_posterior_covariance.py @@ -0,0 +1,23 @@ +import numpy as np +from filterpy.kalman import KalmanFilter, SquareRootKalmanFilter + + +def test_saved_posterior_matches_covariance_filter_and_survives_prediction(): + regular, square = KalmanFilter(2, 1), SquareRootKalmanFilter(2, 1) + for f in (regular, square): + f.x = np.array([[0.0], [1.0]]) + f.P = np.array([[3.0, 0.4], [0.4, 2.0]]) + f.F = np.array([[1.0, 0.5], [0.0, 1.0]]) + f.H = np.array([[1.0, 0.0]]) + f.Q = np.array([[0.2, 0.05], [0.05, 0.1]]) + f.R = np.array([[0.7]]) + for f in (regular, square): + f.predict() + f.update(np.array([[1.2]])) + np.testing.assert_allclose(square.P_post, regular.P_post, atol=1e-14) + posterior = regular.P_post.copy() + square.predict() + np.testing.assert_allclose(square.P_post, posterior, atol=1e-14) + assert not np.allclose(square.P_prior, posterior) + square.update(None) + np.testing.assert_allclose(square.P_post, square.P)