Skip to content

Fix catastrophic cancellation error in poisson logpdf when k and mu are large - #1438

Open
rahulb0802 wants to merge 3 commits into
boostorg:developfrom
rahulb0802:poisson-logpmf
Open

Fix catastrophic cancellation error in poisson logpdf when k and mu are large#1438
rahulb0802 wants to merge 3 commits into
boostorg:developfrom
rahulb0802:poisson-logpmf

Conversation

@rahulb0802

@rahulb0802 rahulb0802 commented Aug 26, 2026

Copy link
Copy Markdown

This replaces the current direct-formula method in logpdf with Loader's (2000) saddle-point approx, ported from a change I've been working on in SciPy's scipy.stats.poisson.logpmf. The current formula loses precision due to cancellation when k is close to mu and both are large. The new form implemented avoids this.

Two new helpers are added to poisson_detail:

  • stirlerr(n): Stirling's series remainder. Direct path for small n, asymptotic series for large n
  • bd0(mean, k): Deviance term. Direct path and series expansion for |v| < 0.1 to avoid cancellation when k and mu are close

All existing poisson tests pass (across all real types). I also added accuracy checks for other values, validated against mpmath (1000-digit) reference values, including large-magnitude cases. No regressions as per CI checks.

@rahulb0802
rahulb0802 marked this pull request as draft August 26, 2026 03:50
@codecov

codecov Bot commented Aug 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.40%. Comparing base (64a8d75) to head (487009b).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff            @@
##           develop    #1438   +/-   ##
========================================
  Coverage    95.39%   95.40%           
========================================
  Files          829      829           
  Lines        69186    69219   +33     
========================================
+ Hits         66003    66039   +36     
+ Misses        3183     3180    -3     
Files with missing lines Coverage Δ
include/boost/math/distributions/poisson.hpp 86.03% <100.00%> (+2.16%) ⬆️
test/test_poisson.cpp 99.19% <100.00%> (+0.06%) ⬆️

... and 1 file with indirect coverage changes


Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 64a8d75...487009b. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@rahulb0802
rahulb0802 marked this pull request as ready for review August 26, 2026 16:20
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