Skip to content

Use StatsFuns for Laplace evaluations - #2115

Merged
devmotion merged 4 commits into
masterfrom
dmw/laplace-logcdf
Nov 17, 2026
Merged

devmotion merged 4 commits into
masterfrom
dmw/laplace-logcdf

Conversation

@devmotion

@devmotion devmotion commented Nov 14, 2026 •

Copy link
Copy Markdown
Member

This PR implements pdf, logpdf, cdf, ccdf, logcdf, logccdf, quantile, cquantile, invlogcdf, and invlogccdf of Laplace with the functions added in StatsFuns 2.3.0 (JuliaStats/StatsFuns.jl#244), via @_delegate_statsfuns as for Normal, Gamma, etc. The StatsFuns compat is bumped to 2.3.

This fixes the accuracy of logcdf, logccdf, invlogcdf, and invlogccdf:

  • For z = (x - μ) / θ ≥ 0, logcdf(::Laplace, x) computed log(1 - exp(-z) / 2) as loghalf + log2mexp(-z). This cancels and returns 0.0 for large z, where the result is ≈ -exp(-z) / 2 (e.g., about -2e-18 for z = 40). The same holds for logccdf with z ≤ 0. StatsFuns computes it as log1p(-exp(-z) / 2).
  • Close to the median, invlogcdf and invlogccdf cancelled: e.g., invlogcdf(Laplace(), log(0.5)) returned 0.0 instead of 2.3e-17 (log(0.5) is rounded). StatsFuns adds the low part of log(2) and uses -log1p(-expm1(⋯)) in the range where -log(2(1 - p)) would cancel.

Further changes from StatsFuns:

  • logpdf, quantile, cquantile, invlogcdf, and invlogccdf promote the parameters and the argument before computing, so that, e.g., log(θ) is not computed in a lower precision than the result.

This also removes the only use of log2mexp in Distributions (see JuliaStats/LogExpFunctions.jl#140).

Examples

Float64 results, relative error w.r.t. a 256-bit BigFloat reference in parentheses:

master this PR reference
logcdf(Laplace(), 1.0) -0.2032670549151953 (1.1e-16) -0.20326705491519534 (3.0e-17) -0.203267…
logcdf(Laplace(), 5.0) -0.003374661268985668 (1.0e-14) -0.0033746612689856343 (5.6e-17) -0.00337466…
logcdf(Laplace(), 20.0) -1.0305767350615724e-9 (7.4e-8) -1.0305768117503233e-9 (1.1e-16) -1.03058…e-9
logcdf(Laplace(), 40.0) 0.0 (1.0) -2.1241771276457944e-18 (3.0e-17) -2.12418…e-18
logcdf(Laplace(), 100.0) 0.0 (1.0) -1.860037988010418e-44 (4.2e-17) -1.86004…e-44
logccdf(Laplace(), -40.0) 0.0 (1.0) -2.1241771276457944e-18 (3.0e-17) -2.12418…e-18
invlogcdf(Laplace(), log(0.5)) 0.0 (1.0) 2.3190468138462996e-17 (4.8e-17) 2.31905…e-17
invlogcdf(Laplace(), nextfloat(log(0.5))) 1.1102230246251565e-16 (0.17) 1.3421277060097865e-16 (1.2e-16) 1.34213…e-16
invlogcdf(Laplace(), prevfloat(log(0.5), 16)) -1.7763568394002505e-15 (0.013) -1.7531663712617875e-15 (1.4e-18) -1.75317…e-15
invlogcdf(Laplace(), log(0.5) - 0.01) -0.010000000000000009 (2.3e-15) -0.009999999999999986 (6.4e-17) -0.01
invlogccdf(Laplace(), log(0.5)) 0.0 (1.0) -2.3190468138462996e-17 (4.8e-17) -2.31905…e-17
logpdf(Laplace(0f0, 3f0), 1.0) -2.1250928243001304 (1.0e-8) -2.1250928025613884 (4.9e-17) -2.12509…
quantile(Laplace(0.0, 3.0), 0.3f0) -1.5324767231941223 (1.9e-8) -1.5324767520886846 (1.7e-16) -1.53248…

Unchanged in the other tail, e.g., logcdf(Laplace(), -5.0) and invlogcdf(Laplace(), -20.0) return the same values as on master.

Timings (Float64, mean over 1000 random inputs): logcdf 6.8 ns → 4.3 ns, invlogcdf 1.4 ns → 4.0 ns.

Script
using Pkg
Pkg.activate(; temp = true, io = devnull)
Pkg.add([PackageSpec(; name = "StatsFuns", version = "2.3"), PackageSpec(; name = "LogExpFunctions"), PackageSpec(; name = "BenchmarkTools")]; io = devnull)

using StatsFuns, LogExpFunctions, BenchmarkTools, Printf
using StatsFuns: loghalf, logtwo

# Implementations on master (μ, θ as arguments)
old_logpdf(μ, θ, x) = -(abs((x - μ) / θ) + log(2θ))
old_quantile(μ, θ, p) = (z = p < 1 / 2 ? log(2p) : -log(2(1 - p)); μ + z * θ)
old_logcdf(μ, θ, x) = (z = (x - μ) / θ; z < 0 ? loghalf + z : loghalf + log2mexp(-z))
old_logccdf(μ, θ, x) = (z = (x - μ) / θ; z > 0 ? loghalf - z : loghalf + log2mexp(z))
old_invlogcdf(μ, θ, lp) = μ + θ * (lp < loghalf ? logtwo + lp : -(logtwo + log1mexp(lp)))
old_invlogccdf(μ, θ, lp) = μ + θ * (lp > loghalf ? logtwo + log1mexp(lp) : -(logtwo + lp))

# BigFloat references
withbig(f) = setprecision(f, BigFloat, 256)
ref_logpdf(μ, θ, x) = withbig(() -> -(abs((big(x) - big(μ)) / big(θ)) + log(2 * big(θ))))
ref_quantile(μ, θ, p) = withbig(() -> (p = big(p); big(μ) + big(θ) * (p < 1 / 2 ? log(2p) : -log(2(1 - p)))))
ref_logcdf(μ, θ, x) = withbig() do
    z = (big(x) - big(μ)) / big(θ)
    z < 0 ? z - log(big(2)) : log1p(-exp(-z) / 2)
end
ref_logccdf(μ, θ, x) = ref_logcdf(-μ, θ, -x)
ref_invlogcdf(μ, θ, lp) = withbig() do
    l = big(lp)
    big(μ) + big(θ) * (2 * exp(l) < 1 ? log(big(2)) + l : -log(-2 * expm1(l)))
end
ref_invlogccdf(μ, θ, lp) = -ref_invlogcdf(-μ, θ, lp)

function relerr(v, r)
    iszero(r) && return iszero(v) ? "0" : "∞"
    e = Float64(abs((big(v) - r) / r))
    return iszero(e) ? "0" : @sprintf("%.1e", e)
end

const ROWS = [
    ("logcdf", old_logcdf, StatsFuns.laplacelogcdf, ref_logcdf, [(0.0, 1.0, x) for x in (-5.0, 1.0, 5.0, 20.0, 40.0, 100.0)]),
    ("logccdf", old_logccdf, StatsFuns.laplacelogccdf, ref_logccdf, [(0.0, 1.0, x) for x in (-20.0, -40.0)]),
    ("invlogcdf", old_invlogcdf, StatsFuns.laplaceinvlogcdf, ref_invlogcdf,
        [(0.0, 1.0, lp) for lp in (log(0.5), nextfloat(log(0.5)), prevfloat(log(0.5), 16), log(0.5) - 0.01, log(0.5) + 0.01, -20.0)]),
    ("invlogccdf", old_invlogccdf, StatsFuns.laplaceinvlogccdf, ref_invlogccdf,
        [(0.0, 1.0, lp) for lp in (log(0.5), nextfloat(log(0.5)))]),
    ("logpdf", old_logpdf, StatsFuns.laplacelogpdf, ref_logpdf, [(0.0f0, 3.0f0, 1.0)]),
    ("quantile", old_quantile, StatsFuns.laplaceinvcdf, ref_quantile, [(0.0, 3.0, 0.3f0)]),
]

println("| function | input | before | rel. error | after | rel. error | reference |")
println("|---|---|---|---|---|---|---|")
for (name, old, new, ref, args) in ROWS, (μ, θ, x) in args
    o, n, r = old(μ, θ, x), new(μ, θ, x), ref(μ, θ, x)
    d = (μ, θ) == (0.0, 1.0) ? "Laplace()" : "Laplace($(repr(μ)), $(repr(θ)))"
    @printf("| `%s(%s, x)` | `%s` | `%s` | %s | `%s` | %s | `%.6g` |\n",
        name, d, repr(x), repr(o), relerr(o, r), repr(n), relerr(n, r), Float64(r))
end

println()
for (name, old, new) in (("logcdf", old_logcdf, StatsFuns.laplacelogcdf), ("invlogcdf", old_invlogcdf, StatsFuns.laplaceinvlogcdf))
    xs = name == "logcdf" ? 20 .* randn(1000) : -10 .* rand(1000)
    to = @belapsed map(x -> $old(0.0, 1.0, x), $xs)
    tn = @belapsed map(x -> $new(0.0, 1.0, x), $xs)
    @printf("%s: before %.2f ns/eval, after %.2f ns/eval\n", name, 1e9 * to / 1000, 1e9 * tn / 1000)
end

Tests

  • logcdf and logccdf are compared with a BigFloat reference for Float32 and Float64.
  • invlogcdf and invlogccdf are compared with a BigFloat reference close to the median, around log(1/√2), and in both tails for Float16, Float32, Float64, and BigFloat.

🤖 Generated with Claude Code

For `z ≥ 0` the log cdf `log(1 - exp(-z) / 2)` was computed as
`loghalf + log2mexp(-z)`, which cancels and returns zero for large `z`.
It is now computed as `log1p(-exp(-z) / 2)`, which is well-conditioned
since `0 < exp(-z) / 2 ≤ 1/2` (and analogously for `logccdf`).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.35%. Comparing base (dd5ad8e) to head (514d879).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2115      +/-   ##
==========================================
- Coverage   87.37%   87.35%   -0.02%     
==========================================
  Files         149      149              
  Lines        8948     8936      -12     
==========================================
- Hits         7818     7806      -12     
  Misses       1130     1130              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

…e median

Close to the median the quantile is small and both `lp + logtwo` and
`logtwo + log1mexp(lp)` cancel. `lp + logtwo` is exact there, so adding the
low part of `log(2)` yields an accurate result; for `log(1/2) ≤ lp < log(1/√2)`
the quantile is computed as `-log1p(-expm1(lp + log(2)))`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@devmotion devmotion changed the title Improve accuracy of logcdf and logccdf of Laplace Improve accuracy of logcdf, logccdf, invlogcdf, and invlogccdf of Laplace Nov 15, 2026
StatsFuns 2.3.0 provides `laplacepdf`, ..., `laplaceinvlogccdf` with the
accurate `logcdf`/`logccdf`/`invlogcdf`/`invlogccdf` implementations, so
delegate to them instead of maintaining them here.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@devmotion devmotion changed the title Improve accuracy of logcdf, logccdf, invlogcdf, and invlogccdf of Laplace Use StatsFuns for Laplace evaluations Nov 17, 2026
@devmotion
devmotion marked this pull request as ready for review November 17, 2026 13:08
@devmotion
devmotion merged commit 264883f into master Nov 17, 2026
14 checks passed
@devmotion
devmotion deleted the dmw/laplace-logcdf branch November 17, 2026 14:04
nsiccha added a commit to nsiccha/Distributions.jl that referenced this pull request Nov 18, 2026
… (+3 more)

Lands 4 commits:
- d202c57 Test latest patch of the minimum Julia version in CI (JuliaStats#2110)
- e433723 Replace CompatHelper with Dependabot for Julia compat updates (JuliaStats#2109)
- dd5ad8e Fix missing power in `KSDist` Durbin matrix (JuliaStats#2112)
- 264883f Use StatsFuns for `Laplace` evaluations (JuliaStats#2115)

KB-Branch: kb-impl/Distributions-rebase
KB-Committer-Id: Distributions:rebase
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants