TuringLang / AdvancedHMC.jl

Robust, modular and efficient implementation of advanced Hamiltonian Monte Carlo algorithms
https://turinglang.org/AdvancedHMC.jl/
MIT License
237 stars 41 forks source link

Sampling hangs on integer data #229

Closed anhi closed 3 years ago

anhi commented 3 years ago

Hi,

I'm trying to fit a Weibull distrbution to input data that happen to be exact integers, but sampling doesn't terminate. Here is a reduced example:


using Turing, Distributions

@model weibull(x) = begin
    μ ~ Uniform(1, 10)
    σ ~ Uniform(1, 10)

    x ~ Weibull.(σ, μ)
end

w = Weibull(2, 4);
data = rand(w, 1000);
rounded = round.(data)
rounded_noisy = rounded + 0.01 .* rand(length(rounded));

# this works
chain = sample(weibull(data), NUTS(0.65), 1000, progress=true)
# this works, too
chain =  sample(weibull(rounded_noisy), NUTS(0.65), 1000, progress=true)
# this doesn't
chain =  sample(weibull(rounded), NUTS(0.65), 1000, progress=true)```
`
luiarthur commented 3 years ago

I think the behavior is due to rounded containing zeros. Note that pdf(w, 0) == 0. Thus, when zeros are present in x, the likelihood, and thus the joint posterior density, will always evaluate to 0. So all samples of (mu, sigma) will be rejected.

If rounded does not contain any zeros (but only positive integers), it also runs fine.

anhi commented 3 years ago

Ah, that makes perfect sense! You are absolutely right, so I'll close this issue.