Hi,
I am new to Turing PPL.
I have a random variable X which is Gamma distributed, i.e., X ~Ga(α, β). And the scale β parameter of X is also obeys a Gamma law, namely, β ~ Ga(α0, β0). Then I want to infer the above parameters from the observed data of X. I was thinking the parameters are α, α0, β0.
I implemented the following solution based on the NUTS sampler in Turing.
using Turing, MCMCChains, Distributions
using StatsPlots
# construct the probabilistic model
@model function rdeg(; N::Int)
# define prior
α ~ Gamma(0.1, 0.2)
α0 ~ Gamma(0.1, 0.2)
β0 ~ Gamma(0.1, 0.2)
# Likelihood
β ~ Gamma(α0, β0)
y ~ filldist(Gamma(α, β), N)
# y ~ filldist(Gamma(α, Gamma(α0, β0)), N)
return y
end
rdeg(y::AbstractVector{<:Real}) = rdeg(; N=length(y)) | (; y);
# collect data
data = Float64[]
for i in 1:120
β = rand(Gamma(0.6, 0.04))
push!(data, rand(Gamma(0.4, β)))
end
model = rdeg(data)
# sampler = HMC(0.09, 15)
sampler = NUTS() # a nice sampler
chain = sample(model, sampler, MCMCSerial(), 10_000, 4, progress=false)
As seen in above program, I have set α, α0, β0 = 0.4, 0.6, 0.04; And I got output of:
First, I see the estimation of α, α0, β0 are not so accurate, α is nearly Ok, but α0, β0 are not.
The second problem is I am not sure if my implementation of inference model in @model block is correct.
I see in the results, the parameters are four: α, β, α0, β0.
Thank you very much for checking my problems!