Fix catastrophic cancellation error in poisson logpdf when k and mu are large - #1438
Fix catastrophic cancellation error in poisson logpdf when k and mu are large#1438rahulb0802 wants to merge 4 commits into
Conversation
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:
|
|
Thanks for this @rahulb0802 , this is a good idea, but not quite right just yet. The problem is that the new routines evaluate a fixed number of terms of the Stirling remainder, and therefore are definitely not arbitrary precision. BTW your new test values are limited to double precision too, so the tests should NOT be passing for any long double larger than 64 bits, certainly not for the 128-bit ones, I need to investigate why that is. But for now taking the log of the PDF as the true result, and with Gives: So when evaluated at 200-decimal precision the new routine gives just 17 decimal places compared to nearly full precision before. Evaluating at larger mean looks a little better: But we're further out into the asymptotic region for Sterlings formula so fewer terms generate more digits of precision. So... what to do? I can't see Sterling converging to full arbitrary precision for small means (from memory the terms become alternating and divergent eventually?) Although we might get there if the |
Now we extract the Bernoulli series loop from scaled_tgamma_no_lanczos into a new helper which is reused by both functions. This is used instead of the fixed 5-term series, and bd0 also uses a convergence check rather than a fixed number of 10 terms. Also fixes a bug where k = mean caused convergence check to never trigger. We already know series is trivially zero so just skip the loop.
|
Yep, that is the fix, thanks for pointing this out. The sum variable being accumulated in this function is literally the exact same thing I was trying to compute in
Running your original case: using mp = boost::multiprecision::number<boost::multiprecision::backends::cpp_bin_float<200>>;
std::cout << std::setprecision(18);
std::int64_t mean = 20;
std::int64_t k = 18;
mp val = log(boost::math::pdf(poisson_distribution<mp>(mean), mp(k)));
double dval = (double)val;
std::cout << "mean = " << mean << " k = " << k << std::endl;
std::cout << "MP logpdf: " << boost::math::relative_difference(boost::math::logpdf(poisson_distribution<mp>(mean), mp(k)), val) << std::endl;
std::cout << "double logpdf: " << boost::math::relative_difference(boost::math::logpdf(poisson_distribution<double>(mean), double(k)), dval) << std::endl;
Double results remain unchanged but the mp result is substantially better in terms of precision. |
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.