← Back to Pull requests

cephes: evaluate lbeta_asymp correction terms without overflow (betaln NaN for large arguments) #322

Closed
unknown closed this pull request without merging on Oct 26, 2026 04:56 UTC
View on GitHub
unknown unknown commented on Oct 16, 2026 11:24 UTC
Author

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 exceeds ASYMP_FACTOR (1e6), for example betaln(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 as

b*(1-b)/(2*a)
b*(1-b)*(1-2*b)/(12*a*a)
-b*b*(1-b)*(1-b)/(12*a*a*a)

For |b| above about 1e77 the numerator bb(1-b)(1-b) overflows to inf; for a above about 5.6e102 the denominator aa*a overflows to inf; either way the term becomes inf/inf = NaN.

This change evaluates the same three terms through the ratios t = b/a and u = (1-b)/a, which are at most 1e-6 in this branch:

t*(1-b)/2
t*u*(1-2*b)/12
-t*t*u*(1-b)/12

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 where a + b is exact:

lbeta(1e+100, 1e+107): before=nan                     after=-1.711809570095828e+101   ref=-1.711809570095832e+101   (2.2e-15 relative)
lbeta(1e+150, 1e+157): before=nan                     after=-1.7118095700958327e+151  ref=-1.711809570095832e+151   (5.1e-16 relative)
lbeta(1e+200, 1e+230): before=nan                     after=-7.0077552789821352e+201  ref=-7.007755278982136e+201   (1.6e-16 relative)
lbeta(1e+7,   1e+100): before=-2151404143.6245718     after=-2151404143.6245718       ref=-2151404143.624572        (unchanged)
lbeta(1e+10,  1e+20):  before=-240258509310.49857     after=-240258509310.49857       ref=-240258509310.49857       (unchanged)
lbeta(1e+20,  1e+30):  before=-2.402585092999045e+21  after=-2.402585092999045e+21    ref=-2.4025850929990457e+21   (unchanged)
lbeta(2, 3):           before=-2.4849066497880004     after=-2.4849066497880004       ref=-2.4849066497880004       (unchanged)

Test: tests/xsf_tests/test_betaln.cpp checks 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.

Diff: 420 lines (+315 / -105) across 5 files Reviewer: dschmitz89 Review duration: 14012m
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
0.45
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.362
Shallow Review Depth shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #322"
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: 45% (MEDIUM) · Higher = larger blast radius
45%
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: 17.8% · Safe to merge
17.8%
Repository