-
Notifications
You must be signed in to change notification settings - Fork 415
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mean et al. for truncated log normal
Fixes 709
- Loading branch information
Showing
7 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Moments of the truncated log-normal can be computed directly from the moment generating | ||
# function of the truncated normal: | ||
# Let Y ~ LogNormal(μ, σ) truncated to (a, b). Then log(Y) ~ Normal(μ, σ) truncated | ||
# to (log(a), log(b)), and E[Y^n] = E[(e^log(Y))^n] = E[e^(nlog(Y))] = mgf(log(Y), n). | ||
|
||
# Given `truncate(LogNormal(μ, σ), a, b)`, return `truncate(Normal(μ, σ), log(a), log(b))` | ||
function _truncnorm(d::Truncated{<:LogNormal}) | ||
μ, σ = params(d.untruncated) | ||
T = partype(d) | ||
a = d.lower === nothing ? nothing : log(T(d.lower)) | ||
b = d.upper === nothing ? nothing : log(T(d.upper)) | ||
return truncated(Normal(μ, σ), a, b) | ||
end | ||
|
||
mean(d::Truncated{<:LogNormal}) = mgf(_truncnorm(d), 1) | ||
|
||
function var(d::Truncated{<:LogNormal}) | ||
tn = _truncnorm(d) | ||
# Ensure the variance doesn't end up negative, which can occur due to numerical issues | ||
return max(mgf(tn, 2) - mgf(tn, 1)^2, 0) | ||
end | ||
|
||
function skewness(d::Truncated{<:LogNormal}) | ||
tn = _truncnorm(d) | ||
m1 = mgf(tn, 1) | ||
m2 = mgf(tn, 2) | ||
m3 = mgf(tn, 3) | ||
sqm1 = m1^2 | ||
v = m2 - sqm1 | ||
return (m3 + m1 * (-3 * m2 + 2 * sqm1)) / (v * sqrt(v)) | ||
end | ||
|
||
function kurtosis(d::Truncated{<:LogNormal}) | ||
tn = _truncnorm(d) | ||
m1 = mgf(tn, 1) | ||
m2 = mgf(tn, 2) | ||
m3 = mgf(tn, 3) | ||
m4 = mgf(tn, 4) | ||
v = m2 - m1^2 | ||
return @horner(m1, m4, -4m3, 6m2, 0, -3) / v^2 - 3 | ||
end | ||
|
||
# TODO: The entropy can be written "directly" as well, according to Mathematica, but | ||
# the expression for it fills me with regret. There are some recognizable components, | ||
# so a sufficiently motivated person could try to manually simplify it into something | ||
# comprehensible. For reference, you can obtain the entropy with Mathematica like so: | ||
# | ||
# d = TruncatedDistribution[{a, b}, LogNormalDistribution[m, s]]; | ||
# Expectation[-LogLikelihood[d, {x}], Distributed[x, d], | ||
# Assumptions -> Element[x | m | s | a | b, Reals] && s > 0 && 0 < a < x < b] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using Distributions, Test | ||
using Distributions: expectation | ||
|
||
naive_moment(d, n, μ, σ²) = (σ = sqrt(σ²); expectation(x -> ((x - μ) / σ)^n, d)) | ||
|
||
@testset "Truncated log normal" begin | ||
@testset "truncated(LogNormal{$T}(0, 1), ℯ⁻², ℯ²)" for T in (Float32, Float64, BigFloat) | ||
d = truncated(LogNormal{T}(zero(T), one(T)), exp(T(-2)), exp(T(2))) | ||
tn = truncated(Normal{BigFloat}(big(0.0), big(1.0)), -2, 2) | ||
bigmean = mgf(tn, 1) | ||
bigvar = mgf(tn, 2) - bigmean^2 | ||
@test @inferred(mean(d)) ≈ bigmean | ||
@test @inferred(var(d)) ≈ bigvar | ||
@test @inferred(median(d)) ≈ one(T) | ||
@test @inferred(skewness(d)) ≈ naive_moment(d, 3, bigmean, bigvar) | ||
@test @inferred(kurtosis(d)) ≈ naive_moment(d, 4, bigmean, bigvar) - big(3) | ||
@test mean(d) isa T | ||
end | ||
@testset "Bound with no effect" begin | ||
# Uses the example distribution from issue #709, though what's tested here is | ||
# mostly unrelated to that issue (aside from `mean` not erroring). | ||
# The specified left truncation at 0 has no effect for `LogNormal` | ||
d1 = truncated(LogNormal(1, 5), 0, 1e5) | ||
@test mean(d1) ≈ 0 atol=eps() | ||
v1 = var(d1) | ||
@test v1 ≈ 0 atol=eps() | ||
# Without a `max(_, 0)`, this would be within machine precision of 0 (as above) but | ||
# numerically negative, which could cause downstream issues that assume a nonnegative | ||
# variance | ||
@test v1 >= 0 | ||
# Compare results with not specifying a lower bound at all | ||
d2 = truncated(LogNormal(1, 5); upper=1e5) | ||
@test mean(d1) == mean(d2) | ||
@test var(d1) == var(d2) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters