Skip to content

fix: correct covariance computation in Ensemble Kalman Filter - #1528

Open
Zero-State-Logic wants to merge 1 commit into
AtsushiSakai:masterfrom
Zero-State-Logic:fix/enkf-covariance-broadcasting
Open

Zero-State-Logic wants to merge 1 commit into
AtsushiSakai:masterfrom
Zero-State-Logic:fix/enkf-covariance-broadcasting

Conversation

@Zero-State-Logic

@Zero-State-Logic Zero-State-Logic commented Nov 27, 2026 •

Copy link
Copy Markdown

Reference issue

N/A — defect found by code review (no existing issue; the ensemble_kalman_filter module had no tests).

What does this implement/fix?

calc_covariance in Localization/ensemble_kalman_filter computed px[:, i] - xEst, subtracting a (4, 1) column (xEst) from a 1-D (4,) particle. NumPy broadcasts these to a (4, 4) array instead of a per-particle residual, so the 3x3 result summed squared differences over all four state components (including the unrelated velocity) and filled every entry identically. The returned matrix was therefore not the position/orientation covariance, and the covariance ellipse drawn in the simulation was a degenerate (rank-1) shape.

The fix takes the first three state components of both the particle and the ensemble mean explicitly, builds the residual as a (3, 1) column, accumulates the proper outer product, and normalizes by the ensemble size. Behavior of the real simulation call is unchanged except for the now-correct covariance.

Additional information

Verified with a known 2-particle ensemble: before, calc_covariance returned a constant matrix dominated by the velocity component; after, it matches the population covariance of [x, y, yaw]. The module had no test file, so this PR adds tests/test_ensemble_kalman_filter.py with a regression test (fails on the old code, passes on the new), a symmetry/PSD check, and a short simulation smoke test (run without animation).

CheckList

  • Did you add an unittest for your new example or defect fix?
  • Did you add documents for your new example? (N/A — bug fix to an existing example; no doc/equation change)
  • All CIs are green? (You can check it after submitting)

calc_covariance computed `px[:, i] - xEst`, subtracting a (4, 1) column
vector from a 1-D (4,) particle. NumPy broadcasts these to a (4, 4)
array instead of a per-particle residual, so the 3x3 result summed
squared differences over all four state components (including the
unrelated velocity) and filled every entry identically. The returned
matrix was therefore not the position/orientation covariance, and the
covariance ellipse drawn in the simulation was wrong (a degenerate,
rank-1 shape).

Take the first three components of both the particle and the ensemble
mean explicitly, build the residual as a (3, 1) column, accumulate the
proper outer product, and normalize by the actual ensemble size.

Also add tests for this previously untested module: a regression test
pinning the covariance for a known ensemble, a symmetry / PSD check,
and a short simulation smoke test.
Copilot AI balanced review requested due to automatic review settings November 27, 2026 11:35

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants