cephes: evaluate lbeta_asymp correction terms without overflow (betaln NaN for large arguments) #322
All Vouch review confidence checks passed
Residual risk 0.18 = Change Risk (0.45) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.45 · 420 lines diff across 5 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.362
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #322"
general
weight: 0.35
dschmitz89
Reference issue
Closes the NaN part of scipy/scipy#26423.
What does this implement/fix?
scipy.special.betaln(a, b)returns NaN for large arguments whose ratio exceedsASYMP_FACTOR(1e6), for examplebetaln(1e100, 1e107), although log|B(a, b)| is an ordinary finite number (−1.71e101 here). On 500 pairs (a, b) log-uniform in (1e-3, 1e300)², 259 came back NaN.The cause is in
detail::lbeta_asymp. Its three Stirling correction terms are written asFor |b| above about 1e77 the numerator
bb(1-b)(1-b)overflows to inf; for a above about 5.6e102 the denominatoraa*aoverflows to inf; either way the term becomes inf/inf = NaN.This change evaluates the same three terms through the ratios
t = b/aandu = (1-b)/a, which are at most 1e-6 in this branch:No intermediate can overflow, and the mathematics is unchanged: only the order of the multiplications and divisions differs, so results elsewhere agree with the old code to rounding (identical on every point checked below).
Additional information
The header was compiled standalone before and after the change. The reference is
lgamma(a) + lgamma(b) − lgamma(a+b)computed with mpmath at a precision wherea + bis exact:Test:
tests/xsf_tests/test_betaln.cppchecks these seven cases (finite result, relative error ≤ 1e-13). It was verified by compiling the same assertions standalone against the patched header; I could not run the Catch2 suite locally (no pixi on this machine), so CI is the first full run.Not addressed here: the ~1e-9 relative error of the non-asymptotic path for large arguments with ratio below 1e6 (cancellation in
lgam(a) + lgam(b) − lgam(a+b)), described in the same SciPy issue.AI Generation Disclosure
AI was used. Claude (Anthropic's assistant, run from a terminal on my machine) drafted the three-line change in
lbeta_asymp, the regression test, the standalone before/after comparison, the text of scipy/scipy#26423 and this description. The bug itself was found by specaudit, an accuracy audit of special functions that I built with the same assistant. I reviewed the change and the numbers and I am responsible for them; the mathematics is a reordering of the existing Stirling terms and I can explain any part of it.