Fix catastrophic cancellation error in poisson logpdf when k and mu are large - #1438
Open
rahulb0802 wants to merge 3 commits into
Open
Fix catastrophic cancellation error in poisson logpdf when k and mu are large#1438rahulb0802 wants to merge 3 commits into
rahulb0802 wants to merge 3 commits into
Conversation
rahulb0802
marked this pull request as draft
August 26, 2026 03:50
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #1438 +/- ##
========================================
Coverage 95.39% 95.40%
========================================
Files 829 829
Lines 69186 69219 +33
========================================
+ Hits 66003 66039 +36
+ Misses 3183 3180 -3
... and 1 file with indirect coverage changes Continue to review full report in Codecov by Harness.
🚀 New features to boost your workflow:
|
rahulb0802
marked this pull request as ready for review
August 26, 2026 16:20
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This replaces the current direct-formula method in
logpdfwith Loader's (2000) saddle-point approx, ported from a change I've been working on in SciPy'sscipy.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 smalln, asymptotic series for largenbd0(mean, k): Deviance term. Direct path and series expansion for|v| < 0.1to avoid cancellation when k and mu are closeAll 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.