-
Notifications
You must be signed in to change notification settings - Fork 419
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error calculating mean of Truncated Log Normal #709
Comments
I don't remember if we have an issue for this but in general, there isn't a closed form solution to the truncated |
We even have QuadGK as a dependency already, so adding a fallback to integration wouldn't even require another dependency. |
I believe there are closed-form results for all moments of a truncated lognormal, but maybe a statistician can correct me. /Paul S |
Correct there are closed form results: https://en.wikipedia.org/wiki/Truncated_normal_distribution |
It is pretty easy to implement. I did it myself. The method I used to distinguish the continuous vs discrete distributions is a bit kludgey. There should be a way to determine if the parent distribution is continuous or discrete.
|
can't you just check if |
julia> d=Truncated(Normal(0,1), 0, Inf)
Truncated(Normal{Float64}(μ=0.0, σ=1.0), range=(0.0, Inf))
julia> typeof(d) <: ContinuousDistribution
true |
Ah, that was the subtype! I was actually wanting to do it that way, but I wrote that code under a tight deadline so I didn't have the chance to thoroughly research the Distributions.jl type system. I updated the code in my previous comment. |
Has this being incorporated into the package? |
bump |
There's a convenient derivation here that relates the moments of the truncated log-normal to the moment generating function for the truncated normal: the and so the mean would be and the variance Then I believe we can define function mgf(d::Truncated{Normal{T}}, t::Real) where {T}
d0 = d.untruncated
μ = mean(d0)
σ = std(d0)
σt = σ * t
a = (minimum(d) - μ) / σ - σt
b = (maximum(d) - μ) / σ - σt
stdnorm = Normal{T}(zero(T), one(T))
return exp(μ * t + σt^2 / 2 + logdiffcdf(stdnorm, b, a) - d.logtp)
end
function _truncnorm(d::Truncated{<:LogNormal})
μ, σ = params(d.untruncated)
a = d.lower === nothing ? nothing : log(minimum(d))
b = d.upper === nothing ? nothing : log(maximum(d))
return truncated(Normal(μ, σ), a, b)
end
mean(d::Truncated{<:LogNormal}) = mgf(_truncnorm(d), 1)
function var(d::Truncated{<:LogNormal})
tn = _truncnorm(d)
m1 = mgf(tn, 1)
m2 = sqrt(mgf(tn, 2))
return (m2 - m1) * (m2 + m1)
end and likewise for |
Your first LaTeX equation should probably have |
Ah yes, thank you! Fixed. |
Any idea why I get the following error with this command:
mean(Truncated(LogNormal(1.0,5.0),0.0,1.0e5))
The text was updated successfully, but these errors were encountered: