From 963f442a2c1dc7745b94bcef38680b7583798ec4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20M=C3=BCller-Widmann?= Date: Sun, 15 Nov 2026 00:38:47 +0100 Subject: [PATCH 1/4] Improve accuracy of `logcdf` and `logccdf` of `Laplace` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/univariate/continuous/laplace.jl | 7 +++++-- test/univariate/continuous/laplace.jl | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/univariate/continuous/laplace.jl b/src/univariate/continuous/laplace.jl index 9a8099b48e..29e80c7d4f 100644 --- a/src/univariate/continuous/laplace.jl +++ b/src/univariate/continuous/laplace.jl @@ -88,8 +88,11 @@ logpdf(d::Laplace, x::Real) = - (abs(zval(d, x)) + log(2scale(d))) cdf(d::Laplace, x::Real) = (z = zval(d, x); z < 0 ? exp(z)/2 : 1 - exp(-z)/2) ccdf(d::Laplace, x::Real) = (z = zval(d, x); z > 0 ? exp(-z)/2 : 1 - exp(z)/2) -logcdf(d::Laplace, x::Real) = (z = zval(d, x); z < 0 ? loghalf + z : loghalf + log2mexp(-z)) -logccdf(d::Laplace, x::Real) = (z = zval(d, x); z > 0 ? loghalf - z : loghalf + log2mexp(z)) +# For `z ≥ 0`, `cdf(d, x) = 1 - exp(-z) / 2` (and symmetrically `ccdf(d, x) = 1 - exp(z) / 2` for `z ≤ 0`). +# Its logarithm is computed with `log1p`, which is well-conditioned since `0 < exp(-z) / 2 ≤ 1/2`, and keeps +# the tiny result `≈ -exp(-z) / 2` for large `z` (the sum `loghalf + log(2 - exp(-z))` would cancel to zero). +logcdf(d::Laplace, x::Real) = (z = zval(d, x); z < 0 ? loghalf + z : log1p(-exp(-z) / 2)) +logccdf(d::Laplace, x::Real) = (z = zval(d, x); z > 0 ? loghalf - z : log1p(-exp(z) / 2)) quantile(d::Laplace, p::Real) = p < 1/2 ? xval(d, log(2p)) : xval(d, -log(2(1 - p))) cquantile(d::Laplace, p::Real) = p > 1/2 ? xval(d, log(2(1 - p))) : xval(d, -log(2p)) diff --git a/test/univariate/continuous/laplace.jl b/test/univariate/continuous/laplace.jl index 9288927982..0e8d871223 100644 --- a/test/univariate/continuous/laplace.jl +++ b/test/univariate/continuous/laplace.jl @@ -1,5 +1,21 @@ +using StatsFuns: loghalf + @testset "laplace.jl" begin # affine transformations test_cgf(Laplace(1, 1), (0.99, -0.99, 1f-2, -1f-5)) test_affine_transformations(Laplace, randn(), randn()^2) + + @testset "logcdf and logccdf accuracy (T = $T)" for T in (Float32, Float64) + d = Laplace(zero(T), one(T)) + # reference: log(1 - exp(-|z|) / 2) for z = x ≥ 0 (logcdf) or z = -x ≥ 0 (logccdf) + ref(z) = T(setprecision(() -> log1p(-exp(-big(z)) / 2), BigFloat, 256)) + for z in T[0, 1.0e-10, 0.1, 1, 5, 20, 40, 100, 700] + # `atol` only matters if the result is subnormal or underflows + @test @inferred(logcdf(d, z)) ≈ ref(z) rtol = 4 * eps(T) atol = floatmin(T) + @test @inferred(logccdf(d, -z)) ≈ ref(z) rtol = 4 * eps(T) atol = floatmin(T) + # the other tail + @test logcdf(d, -z) ≈ T(loghalf) - z rtol = 4 * eps(T) + @test logccdf(d, z) ≈ T(loghalf) - z rtol = 4 * eps(T) + end + end end From 8d29314cdd7ba686a881acab5a7b49da714ca899 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20M=C3=BCller-Widmann?= Date: Sun, 15 Nov 2026 23:12:19 +0100 Subject: [PATCH 2/4] Improve accuracy of `invlogcdf` and `invlogccdf` of `Laplace` near the median MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/univariate/continuous/laplace.jl | 22 ++++++++++++++++++++-- test/univariate/continuous/laplace.jl | 26 +++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/univariate/continuous/laplace.jl b/src/univariate/continuous/laplace.jl index 29e80c7d4f..8e85c2bd61 100644 --- a/src/univariate/continuous/laplace.jl +++ b/src/univariate/continuous/laplace.jl @@ -96,8 +96,26 @@ logccdf(d::Laplace, x::Real) = (z = zval(d, x); z > 0 ? loghalf - z : log1p(-exp quantile(d::Laplace, p::Real) = p < 1/2 ? xval(d, log(2p)) : xval(d, -log(2(1 - p))) cquantile(d::Laplace, p::Real) = p > 1/2 ? xval(d, log(2(1 - p))) : xval(d, -log(2p)) -invlogcdf(d::Laplace, lp::Real) = lp < loghalf ? xval(d, logtwo + lp) : xval(d, -(logtwo + log1mexp(lp))) -invlogccdf(d::Laplace, lp::Real) = lp > loghalf ? xval(d, logtwo + log1mexp(lp)) : xval(d, -(logtwo + lp)) + +# low part of `log(2)`, i.e., `log(2) - T(log(2))` +for T in (Float16, Float32, Float64) + @eval _logtwo_lo(::Type{$T}) = $(T(big(logtwo) - T(logtwo))) +end +_logtwo_lo(::Type{T}) where {T} = T(BigFloat(logtwo; precision = 2 * precision(T) + 32) - T(logtwo)) + +# Quantile of `Laplace(0, 1)` for log-probability `lp`. Close to the median the result is small: +# `lp + logtwo` is exact there and the low part of `log(2)` is added to avoid cancellation. +function _laplace_invlogcdf(lp::Real) + return if lp < loghalf + (lp + logtwo) + _logtwo_lo(typeof(lp)) + elseif -2 * lp > logtwo + -log1p(-expm1((lp + logtwo) + _logtwo_lo(typeof(lp)))) + else + -(logtwo + log1mexp(lp)) + end +end +invlogcdf(d::Laplace, lp::Real) = xval(d, _laplace_invlogcdf(float(last(promote(d.μ, d.θ, lp))))) +invlogccdf(d::Laplace, lp::Real) = xval(d, -_laplace_invlogcdf(float(last(promote(d.μ, d.θ, lp))))) function gradlogpdf(d::Laplace, x::Real) μ, θ = params(d) diff --git a/test/univariate/continuous/laplace.jl b/test/univariate/continuous/laplace.jl index 0e8d871223..0657ea862a 100644 --- a/test/univariate/continuous/laplace.jl +++ b/test/univariate/continuous/laplace.jl @@ -1,4 +1,4 @@ -using StatsFuns: loghalf +using StatsFuns: loghalf, logtwo @testset "laplace.jl" begin # affine transformations @@ -18,4 +18,28 @@ using StatsFuns: loghalf @test logccdf(d, z) ≈ T(loghalf) - z rtol = 4 * eps(T) end end + + @testset "invlogcdf and invlogccdf accuracy (T = $T)" for T in (Float16, Float32, Float64, BigFloat) + d = Laplace(zero(T), one(T)) + # reference: log(2p) for p < 1/2 and -log(2(1 - p)) otherwise + ref(lp) = T( + setprecision(BigFloat, 2 * precision(T) + 64) do + l = big(lp) + 2 * exp(l) < 1 ? logtwo + l : -log(-2 * expm1(l)) + end + ) + m = T(loghalf) + lps = [ + m, prevfloat(m), nextfloat(m), prevfloat(m, 16), nextfloat(m, 16), m - T(1 // 100), m + T(1 // 100), + T(-logtwo / 2), T(-1 // 10), T(-1 // 1000), -one(T), -floatmin(T), T(-20), T(-200), + ] + for lp in lps + @test @inferred(invlogcdf(d, lp)) ≈ ref(lp) rtol = 4 * eps(T) + @test @inferred(invlogccdf(d, lp)) ≈ -ref(lp) rtol = 4 * eps(T) + end + @test invlogcdf(d, zero(T)) == T(Inf) + @test invlogcdf(d, T(-Inf)) == T(-Inf) + @test invlogccdf(d, zero(T)) == T(-Inf) + @test invlogccdf(d, T(-Inf)) == T(Inf) + end end From c8df569454aa0cdbc25d41cdb66482a1e4d2a4bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20M=C3=BCller-Widmann?= Date: Tue, 17 Nov 2026 13:22:38 +0100 Subject: [PATCH 3/4] Use StatsFuns 2.3 for `Laplace` evaluations 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 --- Project.toml | 2 +- src/univariate/continuous/laplace.jl | 37 +--------------------------- 2 files changed, 2 insertions(+), 37 deletions(-) diff --git a/Project.toml b/Project.toml index 63b86dbe5a..ad4dd1224e 100644 --- a/Project.toml +++ b/Project.toml @@ -59,7 +59,7 @@ StaticArrays = "1" Statistics = "1" StatsAPI = "1.6" StatsBase = "0.32, 0.33, 0.34" -StatsFuns = "1.5.2, 2.0.1" +StatsFuns = "2.3" Test = "<0.0.1, 1" julia = "1.10" diff --git a/src/univariate/continuous/laplace.jl b/src/univariate/continuous/laplace.jl index 8e85c2bd61..fc01c4d32a 100644 --- a/src/univariate/continuous/laplace.jl +++ b/src/univariate/continuous/laplace.jl @@ -80,42 +80,7 @@ end #### Evaluations -zval(d::Laplace, x::Real) = (x - d.μ) / d.θ -xval(d::Laplace, z::Real) = d.μ + z * d.θ - -pdf(d::Laplace, x::Real) = exp(-abs(zval(d, x))) / 2scale(d) -logpdf(d::Laplace, x::Real) = - (abs(zval(d, x)) + log(2scale(d))) - -cdf(d::Laplace, x::Real) = (z = zval(d, x); z < 0 ? exp(z)/2 : 1 - exp(-z)/2) -ccdf(d::Laplace, x::Real) = (z = zval(d, x); z > 0 ? exp(-z)/2 : 1 - exp(z)/2) -# For `z ≥ 0`, `cdf(d, x) = 1 - exp(-z) / 2` (and symmetrically `ccdf(d, x) = 1 - exp(z) / 2` for `z ≤ 0`). -# Its logarithm is computed with `log1p`, which is well-conditioned since `0 < exp(-z) / 2 ≤ 1/2`, and keeps -# the tiny result `≈ -exp(-z) / 2` for large `z` (the sum `loghalf + log(2 - exp(-z))` would cancel to zero). -logcdf(d::Laplace, x::Real) = (z = zval(d, x); z < 0 ? loghalf + z : log1p(-exp(-z) / 2)) -logccdf(d::Laplace, x::Real) = (z = zval(d, x); z > 0 ? loghalf - z : log1p(-exp(z) / 2)) - -quantile(d::Laplace, p::Real) = p < 1/2 ? xval(d, log(2p)) : xval(d, -log(2(1 - p))) -cquantile(d::Laplace, p::Real) = p > 1/2 ? xval(d, log(2(1 - p))) : xval(d, -log(2p)) - -# low part of `log(2)`, i.e., `log(2) - T(log(2))` -for T in (Float16, Float32, Float64) - @eval _logtwo_lo(::Type{$T}) = $(T(big(logtwo) - T(logtwo))) -end -_logtwo_lo(::Type{T}) where {T} = T(BigFloat(logtwo; precision = 2 * precision(T) + 32) - T(logtwo)) - -# Quantile of `Laplace(0, 1)` for log-probability `lp`. Close to the median the result is small: -# `lp + logtwo` is exact there and the low part of `log(2)` is added to avoid cancellation. -function _laplace_invlogcdf(lp::Real) - return if lp < loghalf - (lp + logtwo) + _logtwo_lo(typeof(lp)) - elseif -2 * lp > logtwo - -log1p(-expm1((lp + logtwo) + _logtwo_lo(typeof(lp)))) - else - -(logtwo + log1mexp(lp)) - end -end -invlogcdf(d::Laplace, lp::Real) = xval(d, _laplace_invlogcdf(float(last(promote(d.μ, d.θ, lp))))) -invlogccdf(d::Laplace, lp::Real) = xval(d, -_laplace_invlogcdf(float(last(promote(d.μ, d.θ, lp))))) +@_delegate_statsfuns Laplace laplace μ θ function gradlogpdf(d::Laplace, x::Real) μ, θ = params(d) From 514d8799c6c838fc1b46f32cace22e5727a6c0d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20M=C3=BCller-Widmann?= Date: Tue, 17 Nov 2026 14:28:58 +0100 Subject: [PATCH 4/4] Bump version from 0.25.134 to 0.25.135 --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index ad4dd1224e..2909ee50cd 100644 --- a/Project.toml +++ b/Project.toml @@ -1,6 +1,6 @@ name = "Distributions" uuid = "31c24e10-a181-5473-b8eb-7969acd0382f" -version = "0.25.134" +version = "0.25.135" authors = ["JuliaStats"] [deps]