Improve accuracy of logexpm1, logcosh, log1pmx and logmxp1 #136
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 · 606 lines diff across 7 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.383
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #136"
general
weight: 0.35
oscardssmith
Fixes #46, fixes #135.
Changes
logexpm1(andinvsoftplus,logitexp,logit1mexp)- Thresholds are now derived from the precision by reusing the positive thresholds of
log1pexp(_log1pexp_thresholdsis split into_log1pexp_thresholds_negativeand_log1pexp_thresholds_positive).log(expm1(x)) = x - e⁻ˣ - e⁻²ˣ/2 - …has the same truncation errors as thex + e⁻ˣbranch oflog1pexp, so the same cutoffs apply. This replaces the Float64-tuned18.0/33.3cutoffs.- A BigFloat method mirrors
_log1pexp(::BigFloat).- Also fixes a Float16 bug:
expm1overflowed for x ≳ 11.09 and returnedInf.logcosh- Generic types use
log1p(2 sinh(x/2)^2)for |x| < 2. This is cancellation-free at any precision. For |x| ≥ 2 the large-|x| formula loses less than one bit.- Float16 keeps its native polynomial kernel. Only the large-|x| branch, which cancels in Float16, is widened to Float32.
log1pmx/logmxp1- Float16 is computed via Float32.
- Generic types (e.g. BigFloat) use the atanh series
log1p(x) - x = -r x + 2 r t ∑ tᵏ/(2k+3)withr = x/(x+2)andt = r². The series is used for |r| < 1/2, i.e. −2/3 < x < 2; outside, the naive formula loses at most ~2 bits. Inside, cancellation between the two terms amplifies the relative error by at most ~1.11. Summation stops as soon as the sum no longer changes, with an upper bound ofcld(precision(x), 2)iterations (t < 1/4).log1pexppattern, all four functions dispatch onfloat(x), soIntinputs now use the specialised Float64 implementations.Accuracy (max ulp error)
| | before | after |
|---|---|---|
|
log1pmx(::Float16), exhaustive | 127 | 0.5 ||
logmxp1(::Float16), exhaustive | 63 | 0.5 ||
logcosh(::Float16), exhaustive | 3.2 | 1.4 ||
logexpm1(::Float16), x ≥ 11.09 |Inf| exact || BigFloat (256/512 bits), all 22 functions of #135 on its full grid | up to 77 digits lost | ≤ 1.03 eps × max(1, cond) |
Float32 was already fine (#46). An exhaustive check over all Float32 inputs, using the Float64 implementation as reference, gives max errors of 2.05 ulp (
log1pmxon 0, 10)), 2.19 ulp (logmxp1on [1e-10, 10)), and 2.56 ulp (logcoshon [0, 3)). These exhaustive Float32 checks are not part of the test suite.ForwardDiff
logexpm1,log1pmx,logmxp1(and the wrappers that calllogexpm1): ForwardDiff overloads these through DiffRules. The primal value goes through the fixed Float64/BigFloat paths, and the derivative comes from the DiffRules formula, so these benefit without any Dual-specific change.logcoshhas no DiffRule, soDuals use the generic path. On master they always took the cancelling large-|x| formula, becauseDual isa Float64is false.- At x = 1e-8, the value had 122% relative error and the derivative a relative error of 6e-9.
- Both are now within ~1 eps.
- A DiffRule for
logcosh(andlogabssinh/logabstanh) in DiffRules.jl is left for a follow-up.Performance
map!over 10k random inputs for Float32/Float64 is unchanged within noise, andlogexpm1(::Float32)is about 15% faster. For BigFloat with 1 ≤ |x| < 2, genericlogcoshis 31–38% faster than the large-|x| formula (fewer MPFR temporaries); forDual{Float64}it is ~3 ns slower on that interval.Tests
- As in the issue, the tolerance is 4 eps scaled by the condition number
|x f'(x)/f(x)|(via ForwardDiff on the reference), so points near roots such aslogitexp(-log(2))only test the algorithmic error. Exact zeros must be returned exactly.- The naive references of
logabstanhandcexpexpround to zero at |x| = 1e5 even at high precision (the issue's script silently skipped these points), so they are replaced by cancellation-free references.- Against master's implementation this testset fails for exactly the six functions flagged in #135 (
logexpm1,invsoftplus,logitexp,logit1mexp,log1pmx,logcosh) and passes for the other 16.@inferredreturn-type checks.Notes
log1pmxnow requiresprecision(x)and comparisons. Non-AbstractFloatReals without them (e.g. symbolic types) previously went throughlog1p(x) - x. AD types are unaffected since DiffRules/ChainRules interceptlog1pmx.Not addressed
logexpm1near its root x = log(2) is still inaccurate in ulp terms: about 600–900 ulp for Float32/Float64. A possible fix islog1p(2 expm1(x - log(2)))with a split constant for log(2); that is left for a follow-up PR.🤖 Generated with [Claude Code