Skip to content

Implement ExpGamma sampler (draft) #1958

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

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions src/samplers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ for fname in ["aliastable.jl",
"poisson.jl",
"exponential.jl",
"gamma.jl",
"expgamma.jl",
"multinomial.jl",
"vonmises.jl",
"vonmisesfisher.jl",
Expand Down
57 changes: 57 additions & 0 deletions src/samplers/expgamma.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# These are used to bypass subnormals when sampling from

# Inverse Power sampler
# uses the x*u^(1/a) trick from Marsaglia and Tsang (2000) for when shape < 1
struct ExpGammaIPSampler{S<:Sampleable{Univariate,Continuous},T<:Real} <: Sampleable{Univariate,Continuous}
s::S #sampler for Gamma(1+shape,scale)
nia::T #-1/scale
end

ExpGammaIPSampler(d::Gamma) = ExpGammaIPSampler(d, GammaMTSampler)
function ExpGammaIPSampler(d::Gamma, ::Type{S}) where {S<:Sampleable}
shape_d = shape(d)
sampler = S(Gamma{partype(d)}(1 + shape_d, scale(d)))
return ExpGammaIPSampler(sampler, -inv(shape_d))
end

function rand(rng::AbstractRNG, s::ExpGammaIPSampler)
x = log(rand(rng, s.s))
e = randexp(rng, typeof(x))
return muladd(s.nia, e, x)
end


# Small Shape sampler
# From Liu, C., Martin, R., and Syring, N. (2015) for when shape < 0.3
struct ExpGammaSSSampler{T<:Real} <: Sampleable{Univariate,Continuous}
α::T
θ::T
λ::T
ω::T
ωω::T
end

function ExpGammaSSSampler(d::Gamma)
α = shape(d)
ω = α / MathConstants.e / (1 - α)
return ExpGammaSSSampler(promote(
α,
scale(d),
inv(α) - 1,
ω,
inv(ω + 1)
)...)
end

function rand(rng::AbstractRNG, s::ExpGammaSSSampler{T})::Float64 where T
flT = float(T)
while true
U = rand(rng, flT)
z = (U <= s.ωω) ? -log(U / s.ωω) : log(rand(rng, flT)) / s.λ
h = exp(-z - exp(-z / s.α))
η = z >= zero(T) ? exp(-z) : s.ω * s.λ * exp(s.λ * z)
if h / η > rand(rng, flT)
return s.θ - z / s.α
end
end
end
Loading