← Back to Pull requests

cephes: evaluate lgam difference via stable Stirling expansion to pre… #335

Open
unknown opened this pull request with 243 lines diff across 3 files
View on GitHub
unknown unknown commented on Oct 21, 2026 04:42 UTC
Author

Reference issue


Closes scipy/scipy#26423.

What does this implement/fix?


Fixes catastrophic cancellation and precision loss in xsf::betaln / xsf::cephes::lbeta for large arguments.

1. Problem:
When arguments $a, b$ were large and fell below the asymptotic ratio threshold, lbeta computed:
$$\ln \Gamma(a) + \ln \Gamma(b) - \ln \Gamma(a + b)$$
Subtracting $\ln \Gamma(a)$ and $\ln \Gamma(a + b)$ directly caused catastrophic cancellation of leading digits, degrading accuracy to $\sim 10^{-9}$ relative error.

2. Fix:
Evaluates $\ln \Gamma(a) - \ln \Gamma(a + b)$ analytically using Stirling's asymptotic expansion:
$$\ln\Gamma(a) - \ln\Gamma(a+b) = -b\ln a + b - \left(a + b - \frac{1}{2}\right)\ln\left(1 + \frac{b}{a}\right) + \frac{b/a}{12(a+b)} - \mathcal{O}\left(\frac{1}{a^3}\right)$$
- For small $x = b/a$ ($x < 10^{-4}$), $b - a\ln(1+x)$ is expanded as $b(x/2 - x^2/3 + \dots)$ to eliminate cancellation.
- For larger $x$, cxx::log1p is used.
- The first Stirling correction term is factored as $\frac{b/a}{12(a+b)}$ to avoid intermediate product overflow.
- Adds regression tests in tests/xsf_tests/test_betaln.cpp.

Additional information


Benchmarked against 80-digit mpmath ground truth across $a, b \in [10^2, 10^{255}]$:
  • Previously: Relative error $\sim 1.9 \times 10^{-9}$ (and NaN for extreme ranges due to sub-leading term overflow).

  • Now: Maximum relative error $< 2.1 \times 10^{-14}$ (median $< 10^{-15}$, full double-precision machine epsilon).
  • AI Generation Disclosure


    AI tools (LLM assistant) were used to help identify the issue in scipy/scipy#26423, derive the analytical Stirling series expansion for $\ln\Gamma(a) - \ln\Gamma(a+b)$, and draft the C++ implementation and Catch2 test cases. All mathematical derivations, numerical stability bounds, and relative error tolerances were validated against mpmath multi-precision references.

    Diff: 243 lines (+182 / -61) across 3 files Reviewer: dschmitz89 Review duration: 39523m
    All Vouch review confidence checks passed
    Residual risk 0.22 = Change Risk (0.39) × (1 − Review Confidence (0.43)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.39 · 243 lines diff across 3 files
    0.39
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    42%
    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
    Shallow Review Depth shallow_review_depth
    0.344
    Diff Size diff_size
    0.329
    Real PR Conversation Timeline
    1 comments
    "Review active on #335"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 43.0% 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: 38.8% (MEDIUM) · Higher = larger blast radius
    38.8%
    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: 43.0% · Lower = rushed or shallow review
    43.0%
    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: 22.1% · Safe to merge
    22.1%
    Repository