← Back to Pull requests

Bound adaptive Gauss-Kronrod recursion and work #2958

Closed
unknown closed this pull request without merging on Oct 30, 2026 09:34 UTC
View on GitHub
unknown unknown commented on Oct 27, 2026 15:51 UTC
Author

GaussKronrodAdaptive can recurse until the stack overflows. On e96579fd4, when the error estimate of an interval
misses the tolerance, integrateRecursively splits the interval and halves the tolerance, with no check that the split
still advances or that the halved tolerance is still representable, and no bound on the recursion depth. The default
evaluation limit is Null<Size>(), a very large finite cap that does not prevent these deep-recursion failures.

Standalone reproduction I ran on Linux with gcc 13.3 (not part of this PR), each case in a child process:

| Call | e96579fd4 | This PR |
|---|---|---|
| GaussKronrodAdaptive(1e-10), 1e5exp(x) on [0, 1] | segmentation fault | throws "maximum subdivision depth exceeded" after 3855 evaluations |
| GaussKronrodAdaptive(1e-12), 100
cos(x) on [0, 10] | segmentation fault | the same |
| GaussKronrodAdaptive(1e-15), exp(x) on [0, 1] | segmentation fault | the same |
| GaussKronrodAdaptive(1e-10, 10000), 1e5exp(x) on [0, 1] | throws "maximum number of function evaluations exceeded" after 9975 evaluations | throws "maximum subdivision depth exceeded" after 3855 evaluations |
| GaussKronrodAdaptive(1e-8, 100), x == 0.5 ? 1e8 : 0 on [0, 1] | returns 0 after 45 evaluations | the same |

The last row is a control: the first estimate samples the spike, but after one split neither half samples it.

This PR keeps the existing acceptance test and adds checks before further subdivision, together with limits on
recursion and work:

  • the midpoint must lie strictly inside the interval ("interval subdivision no longer advances");

  • the halved tolerance must be positive and smaller than the tolerance ("tolerance underflow");

  • an unaccepted interval at depth 256 fails instead of subdividing again ("maximum subdivision depth exceeded");

  • the evaluation budget is checked before each interval, and a null maxFunctionEvaluations passed to the

  • constructor now means a budget of 1000000 evaluations (documented on the constructor; setMaxEvaluations is
    unchanged).

    The quadrature formula and the acceptance comparison are unchanged. The new depth and work limits can reject requests
    that previously returned a value. Which check fires first can differ between platforms: on Windows with MSVC 19.41
    the first three calls above stop at the subdivision-progress check after 795 to 810 evaluations.

    Tests. In test-suite/integrals.cpp, next to testGaussKronrodAdaptive:

  • testGaussKronrodAdaptiveUnattainableAccuracy: the first three calls above throw Error within 10000 evaluations.

  • testGaussKronrodAdaptiveAttainableAccuracy: the spike control (exactly 0 after 45 evaluations); 1e5(e − 1) and

  • 100*sin(10) to 1e-8 and 1e-9, and a reversed interval; an explicit 15-evaluation limit that still throws the
    evaluation-limit error.
  • testGaussKronrodAdaptiveEvaluationBudget: an integrator constructed with the default evaluation limit reports

  • maxEvaluations() == 1000000; with a budget of 60, an integrand whose halves both need subdivision is called exactly
    60 times before the error; setMaxEvaluations(Null<Size>()) still stores the sentinel.
  • testGaussKronrodAdaptiveNoSubdivisionProgress: an interval between adjacent representable values, checking an

  • exact zero result, a constant integral within a roundoff tolerance, and the subdivision error when refinement is
    needed.
  • testGaussKronrodAdaptiveLimits: the depth, tolerance-underflow and default evaluation-limit errors.
  • From a configured and built Linux CMake build directory, run:

    ./test-suite/quantlib-test-suite \
        --run_test=QuantLibTests/IntegralTests \
        --log_level=message
    

    With the new tests added to the unchanged e96579fd4 library (Windows, MSVC 19.41), UnattainableAccuracy and Limits
    abort with a stack overflow on their first integration. NoSubdivisionProgress aborts on its third integration, after
    the zero and constant controls pass. EvaluationBudget fails (maxEvaluations() is 2147483647, and the integrand is
    called 75 times); AttainableAccuracy passes there, as a control. All five pass with the fix.

    I also ran IntegralTests, CmsTests, CmsNormalTests, LiborMarketModelTests, LiborMarketModelProcessTests and
    HestonModelTests, which pass on Windows with MSVC 19.41 (C++17) and on Linux with gcc 13.3, Boost 1.83, Release and
    QL_COMPILE_WARNING_AS_ERROR=ON (no warnings). Library callers include the conundrum pricer, the legacy LMM
    covariance implementations, and FourierIntegration::gaussKronrod, which is available to Heston-family engines. The
    Heston tests exercise its Gatheral and Andersen–Piterbarg paths.

    AI disclosure: this was prepared with AI assistance (Claude Code and OpenAI Codex).

    🤖 Generated with Claude Code

    Diff: 603 lines (+452 / -151) across 7 files Reviewer: Jackmeson1 Review duration: 3942m
    All Vouch review confidence checks passed
    Residual risk 0.20 = Change Risk (0.50) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 603 lines diff across 7 files
    0.50
    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.383
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #2958"
    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: 50.1% (MEDIUM) · Higher = larger blast radius
    50.1%
    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: 19.8% · Safe to merge
    19.8%