Parameter estimation problem in Turing

Note that I have changed the model to accept y directly, instead of conditioning it later like you did.

julia> using Turing, MCMCChains, Distributions
       using StatsPlots
       # construct the probabilistic model
       @model function rdeg2(y)
           N = length(y)
           # define prior
           α ~ Gamma(1.0, 0.2)
           α0 ~ Gamma(1.0, 0.2)
           β0 ~ Gamma(1.0, 0.2)

           # Likelihood
           β ~ filldist(Gamma(α0, β0), N)
           for i in 1:N
             y[i] ~ Gamma(α,β[i])
           end
           return y
       end

       # collect data
       data = Float64[]
       for i in 1:120
           β = rand(Gamma(0.6, 0.04))
           push!(data, rand(Gamma(0.4, β)))
       end
       model = rdeg2(data)
       chain = sample(model, NUTS(), 1000)

It doesn’t make a difference for the inference, but if you define rdeg(;N) like you originally did, you’ll need to define

 y = Vector{Float64}(undef, N) # needs to be declared if not argument of the model
 for i in 1:N
   y[i] ~ Gamma(α,β[i])
 end
1 Like