← Back to Pull requests

Fix `InverseCumulativePoisson` for large means and probabilities near one #2984

Closed
unknown closed this pull request without merging on Nov 03, 2026 14:02 UTC
View on GitHub
unknown unknown commented on Oct 30, 2026 16:59 UTC
Author

InverseCumulativePoisson::calcSummand computes each term as exp(-λ)·λ^k/k! from a separate power and factorial
(poissondistribution.hpp:136-139 on d2fcd5136). The separate power and factorial can overflow even when the
Poisson 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 BigNatural and Natural. These checks prevent out-of-range
conversions, counter wraparound, and overflow in the i+1 evaluated by Factorial::ln. p = 0 and p = 1 still return
0 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):

  • testInverseCumulativePoissonLargeMean checks the table above for exact equality. For the rows with λ = 100, 130,

  • or 150, it also checks through CumulativePoissonDistribution that CDF(k) >= p and CDF(k-1) < p, skipping both
    checks only when both CDF values are within 1e-12 of p.
  • testInverseCumulativePoissonIndexRange checks that an infinite mean and means of 2^digits(Natural) and

  • 2^digits(BigNatural) raise "exceeds the supported index range", and that a mean one below the largest Natural,
    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 values
    in the table), the λ = 5 control passes, and the index-range checks see no error; with this patch both tests pass.
    The existing testInverseCumulativePoisson passes before and after. QuantLibTests/DistributionTests (27 test
    cases), QuantLibTests/RngTraitsTests (4, which include the Poisson random sequence) and
    QuantLibTests/BatesModelTests (4; the Bates process draws its jump counts from this class) pass on Windows with
    MSVC 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

    Diff: 511 lines (+383 / -128) across 6 files Reviewer: Jackmeson1 Review duration: 5582m
    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
    0.48
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 47.6% (MEDIUM) · Higher = larger blast radius
    47.6%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 18.8% · Safe to merge
    18.8%