Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Distributions"
uuid = "31c24e10-a181-5473-b8eb-7969acd0382f"
version = "0.25.134"
version = "0.25.135"
authors = ["JuliaStats"]

[deps]
Expand Down Expand Up @@ -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"

Expand Down
16 changes: 1 addition & 15 deletions src/univariate/continuous/laplace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +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)
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))

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))
@_delegate_statsfuns Laplace laplace μ θ

function gradlogpdf(d::Laplace, x::Real)
μ, θ = params(d)
Expand Down
40 changes: 40 additions & 0 deletions test/univariate/continuous/laplace.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,45 @@
using StatsFuns: loghalf, logtwo

@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

@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
Loading