cephes: evaluate lgam difference via stable Stirling expansion to pre… #335
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 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
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
dschmitz89
Reference issue
Closes scipy/scipy#26423.
What does this implement/fix?
Fixes catastrophic cancellation and precision loss in
xsf::betaln/xsf::cephes::lbetafor large arguments.1. Problem:
When arguments $a, b$ were large and fell below the asymptotic ratio threshold,
lbetacomputed:$$\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::log1pis 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
mpmathground truth across $a, b \in [10^2, 10^{255}]$:NaNfor extreme ranges due to sub-leading term overflow).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 againstmpmathmulti-precision references.