← Back to Pull requests

Improve accuracy of logexpm1, logcosh, log1pmx and logmxp1 #136

Closed
unknown closed this pull request without merging on Nov 10, 2026 07:45 UTC
View on GitHub
unknown unknown commented on Nov 09, 2026 11:40 UTC
Author

Fixes #46, fixes #135.

Changes

  • logexpm1 (and invsoftplus, logitexp, logit1mexp)

  • - Thresholds are now derived from the precision by reusing the positive thresholds of log1pexp (_log1pexp_thresholds is split into _log1pexp_thresholds_negative and _log1pexp_thresholds_positive). log(expm1(x)) = x - e⁻ˣ - e⁻²ˣ/2 - … has the same truncation errors as the x + e⁻ˣ branch of log1pexp, so the same cutoffs apply. This replaces the Float64-tuned 18.0/33.3 cutoffs.
    - A BigFloat method mirrors _log1pexp(::BigFloat).
    - Also fixes a Float16 bug: expm1 overflowed for x ≳ 11.09 and returned Inf.
  • 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) with r = x/(x+2) and t = 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 of cld(precision(x), 2) iterations (t < 1/4).
  • Following the log1pexp pattern, all four functions dispatch on float(x), so Int inputs 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 (log1pmx on 0, 10)), 2.19 ulp (logmxp1 on [1e-10, 10)), and 2.56 ulp (logcosh on [0, 3)). These exhaustive Float32 checks are not part of the test suite.

    ForwardDiff

  • logexpm1, log1pmx, logmxp1 (and the wrappers that call logexpm1): 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.

  • logcosh has no DiffRule, so Duals use the generic path. On master they always took the cancelling large-|x| formula, because Dual isa Float64 is 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 (and logabssinh/logabstanh) in DiffRules.jl is left for a follow-up.

    Performance

    map! over 10k random inputs for Float32/Float64 is unchanged within noise, and logexpm1(::Float32) is about 15% faster. For BigFloat with 1 ≤ |x| < 2, generic logcosh is 31–38% faster than the large-|x| formula (fewer MPFR temporaries); for Dual{Float64} it is ~3 ns slower on that interval.

    Tests

  • Exhaustive Float16 accuracy tests.

  • Dense Float32/Float64 ranges.

  • A BigFloat accuracy testset with all 22 exported single-argument functions of #135 on the issue's full grid, plus points around the thresholds and branch boundaries, at 256 and 512 bits:

  • - 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 as logitexp(-log(2)) only test the algorithmic error. Exact zeros must be returned exactly.
    - The naive references of logabstanh and cexpexp round 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.
  • @inferred return-type checks.
  • Notes

  • The generic log1pmx now requires precision(x) and comparisons. Non-AbstractFloat Reals without them (e.g. symbolic types) previously went through log1p(x) - x. AD types are unaffected since DiffRules/ChainRules intercept log1pmx.
  • Not addressed

    logexpm1 near its root x = log(2) is still inaccurate in ulp terms: about 600–900 ulp for Float32/Float64. A possible fix is log1p(2 expm1(x - log(2))) with a split constant for log(2); that is left for a follow-up PR.

    🤖 Generated with [Claude Code

    Diff: 606 lines (+454 / -152) across 7 files Reviewer: oscardssmith Review duration: 1204m
    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
    0.50
    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.383
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #136"
    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: 50.1% (MEDIUM) · Higher = larger blast radius
    50.1%
    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: 19.8% · Safe to merge
    19.8%