# Using Turing for MCMC estimation

**URL:** https://discourse.julialang.org/t/using-turing-for-mcmc-estimation/84137
**Category:** Performance
**Tags:** turing, probability
**Created:** [July 13, 2022, 12:13am UTC](https://discourse.julialang.org/t/using-turing-for-mcmc-estimation/84137 "2022-07-13T00:13:09Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Jian\_ZUO](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jian_zuo/32/33738_2.png) [@Jian\_ZUO](https://discourse.julialang.org/u/Jian_ZUO)
#### Post date: [July 13, 2022, 12:13am UTC](https://discourse.julialang.org/t/using-turing-for-mcmc-estimation/84137/1 "2022-07-13T00:13:09Z")

</div>

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.

```julia
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:

 ![turing](https://global.discourse-cdn.com/julialang/original/3X/1/e/1e02312e07ccf8217b265b93493309998e75c3dc.png)  
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!

---

<div class="post-metadata">

### Author: ![Paul\_Schrimpf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/paul_schrimpf/32/10134_2.png) [@Paul\_Schrimpf](https://discourse.julialang.org/u/Paul_Schrimpf)
#### Post date: [July 13, 2022, 12:58am UTC](https://discourse.julialang.org/t/using-turing-for-mcmc-estimation/84137/2 "2022-07-13T00:58:28Z")

</div>

> This program is not working, how can obtain the estimations for all parameters?

You’re more likely to get a helpful reply if you provide more details. What does “not working” mean? Is there an error message? If yes, provide the message. Is there no error, but the output is incorrect? What is the output? How do you know it’s incorrect?

Without that, I’ll guess at one problem: `data = []` creates a `Vector{Any}`, but you’ve define `rdeg` to only work with `AbstractVector{<:Real}`. Initialize `data = Float64[]` instead should fix that problem.

---

<div class="post-metadata">

### Author: ![Jian\_ZUO](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jian_zuo/32/33738_2.png) [@Jian\_ZUO](https://discourse.julialang.org/u/Jian_ZUO)
#### Post date: [July 13, 2022, 6:11am UTC](https://discourse.julialang.org/t/using-turing-for-mcmc-estimation/84137/3 "2022-07-13T06:11:01Z")

</div>

Thank you very much Paul!  
Your reply solves the error in my program, now I can obtain the  
results.

---

<div class="post-metadata">

### Author: ![wc4wc4wc4](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wc4wc4wc4/32/23038_2.png) [@wc4wc4wc4](https://discourse.julialang.org/u/wc4wc4wc4)
#### Post date: [July 13, 2022, 6:23am UTC](https://discourse.julialang.org/t/using-turing-for-mcmc-estimation/84137/4 "2022-07-13T06:23:39Z")

</div>

Also, for future help with Turing, the performance section is not really the best place to ask. You would probably get more answers by asking here: [Probabilistic programming - JuliaLang](https://discourse.julialang.org/c/domain/probprog/48).

---

<div class="post-metadata">

### Author: ![Jian\_ZUO](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jian_zuo/32/33738_2.png) [@Jian\_ZUO](https://discourse.julialang.org/u/Jian_ZUO)
#### Post date: [July 13, 2022, 6:30am UTC](https://discourse.julialang.org/t/using-turing-for-mcmc-estimation/84137/5 "2022-07-13T06:30:38Z")

</div>

@wc4wc4wc4 Thank you very much for sharing this information!  
I will try on the link you shared.
