Fix `InverseCumulativePoisson` for large means and probabilities near one #2984
All Vouch review confidence checks passed
Residual risk 0.19 = Change Risk (0.48) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.48 · 511 lines diff across 6 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.373
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #2984"
general
weight: 0.35
Jackmeson1
InverseCumulativePoisson::calcSummandcomputes each term as exp(-λ)·λ^k/k! from a separate power and factorial(
poissondistribution.hpp:136-139ond2fcd5136). The separate power and factorial can overflow even when thePoisson probability is finite. This can make the accumulated sum infinite or NaN and stop the loop before the
requested quantile: for λ = 150 and p = 0.5 it returns 142, where
CumulativePoissonDistribution(150)(142)is 0.273.Separately, for p within a few ulps of 1 the summed CDF can stay below p because of numerical error: λ = 20 with
p =
std::nextafter(1.0, 0.0)returns 237 instead of 67.This PR computes each term in the log domain, as
PoissonDistribution::operator()in the same file already does.Probabilities at or below exp(-λ) return zero before either summation. For p > 0.5 it finds the quantile from the
upper tail instead: starting at floor(λ), it moves up until a geometric bound on the remaining terms is at most
(1 - p)·ε, then sums the terms downwards and returns the first index at which the accumulated tail exceeds 1 - p.
For p <= 0.5 the forward summation is kept. After the endpoint and zero-quantile returns, means and summation
indices must be strictly below the maxima of both
BigNaturalandNatural. These checks prevent out-of-rangeconversions, counter wraparound, and overflow in the
i+1evaluated byFactorial::ln. p = 0 and p = 1 still return0 and
QL_MAX_REAL, and the constructor checks are unchanged.Quantiles (smallest k with P(X <= k) >= p) on Windows with MSVC 19.41; the references use the regularized upper
incomplete gamma Q(k+1, λ), with the exact double value of p. The original twelve references were calculated with
mpmath at 50 digits. The rows (150,
std::nextafter(0.5, 1.0)), (1000,std::nextafter(1.0, 0.0)) and (100000, 0.5)were calculated with mpmath 1.3.0 at 60 digits:
| λ | p |
d2fcd5136| this PR | Reference ||---|---|---|---|---|
| 100 | 0.999999999 | 155 | 166 | 166 |
| 130 | 0.999 | 146 | 167 | 167 |
| 150 | 0.5 | 142 | 150 | 150 |
| 150 |
std::nextafter(0.5, 1.0)| 142 | 150 | 150 || 150 | 0.9 | 142 | 166 | 166 |
| 200 | 0.5 | 134 | 200 | 200 |
| 200 | 0.9 | 134 | 218 | 218 |
| 500 | 0.5 | 115 | 500 | 500 |
| 800 | 0.5 | 107 | 800 | 800 |
| 1000 | 0.5 | 103 | 1000 | 1000 |
| 1000 | 1e-10 | 103 | 806 | 806 |
| 1000 |
std::nextafter(1.0, 0.0)| 103 | 1270 | 1270 || 100000 | 0.5 | 62 | 100000 | 100000 |
| 20 |
std::nextafter(1.0, 0.0)| 237 | 67 | 67 || 5 | 0.3 | 4 | 4 | 4 |
Tests (
test-suite/distributions.cpp):testInverseCumulativePoissonLargeMeanchecks the table above for exact equality. For the rows with λ = 100, 130,or 150, it also checks through
CumulativePoissonDistributionthat CDF(k) >= p and CDF(k-1) < p, skipping bothchecks only when both CDF values are within 1e-12 of p.
testInverseCumulativePoissonIndexRangechecks that an infinite mean and means of 2^digits(Natural) and2^digits(
BigNatural) raise "exceeds the supported index range", and that a mean one below the largestNatural,whose upper-tail scan reaches the index limit, raises an error as well.
Outside-sandbox validation: with the header from
d2fcd5136, the fourteen large-λ and near-one rows fail (the valuesin the table), the λ = 5 control passes, and the index-range checks see no error; with this patch both tests pass.
The existing
testInverseCumulativePoissonpasses before and after.QuantLibTests/DistributionTests(27 testcases),
QuantLibTests/RngTraitsTests(4, which include the Poisson random sequence) andQuantLibTests/BatesModelTests(4; the Bates process draws its jump counts from this class) pass on Windows withMSVC 19.41 (C++17) and on Linux with g++ 13.3.0, Release and
QL_COMPILE_WARNING_AS_ERROR=ON.With a CMake build in
build/(Linux), run the suite with:./build/test-suite/quantlib-test-suite --run_test=QuantLibTests/DistributionTests --log_level=message
AI disclosure: this was prepared with AI assistance (Claude Opus 5.5).
🤖 Generated with Claude Code