Skip to content

fix(mobile): sample particles from weighted CDF intervals - #926

Open
Afloat16 wants to merge 1 commit into
petercorke:mainfrom
Afloat16:fix/particle-weight-resampling
Open

Afloat16 wants to merge 1 commit into
petercorke:mainfrom
Afloat16:fix/particle-weight-resampling

Conversation

@Afloat16

@Afloat16 Afloat16 commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Problem

Particle resampling chooses the particle whose cumulative weight is nearest to a uniform draw. That does not sample the intended categorical distribution: for weights [0.8, 0.2], the boundary is 0.9 rather than 0.8. Repeated CDF entries can also select zero-weight particles. This biases the next particle generation.

Change

Use the inverse CDF intervals via searchsorted(..., side="right"). Normalising by the last cumulative sum makes the final CDF entry exactly one. Remove the interpolation dependency that this code no longer needs.

This corrects resampling probabilities and can change seeded particle-filter trajectories.

The regressions use a seeded generator, two independently specified probability thresholds, and intervening zero-weight particles. Expected particle indices are computed directly from those thresholds.

Validation

  • Both new regression cases fail on the original implementation.
  • PYTHONPATH=src python -m pytest tests/test_particle_filter_api.py tests/test_particle_heading_moments.py -q: 41 passed, with 3 animation cleanup warnings.
  • PYTHONPATH=src python -m pytest tests/test_vehicle_estimators.py -k TestParticleFilter -q: 7 passed, 40 deselected, checking the corrected resampling against the existing estimator tracking regressions.
  • Changed-file formatting and git diff --check passed.
  • Ruff reports the same three existing wildcard-import diagnostics in the module's demonstration block as on the base revision; the regression file passes Ruff.

@Afloat16
Afloat16 marked this pull request as ready for review November 3, 2026 13:56
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 0 complexity · 0 duplication

Metric Results
Complexity 0
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

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.

1 participant