# Turing: Sampling from posterior parameters

**URL:** https://discourse.julialang.org/t/turing-sampling-from-posterior-parameters/70318
**Category:** General Usage
**Tags:** turing
**Created:** [October 25, 2021, 5:12am UTC](https://discourse.julialang.org/t/turing-sampling-from-posterior-parameters/70318 "2021-10-25T05:12:42Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![amit1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amit1/32/25318_2.png) [@amit1](https://discourse.julialang.org/u/amit1)
#### Post date: [October 25, 2021, 5:12am UTC](https://discourse.julialang.org/t/turing-sampling-from-posterior-parameters/70318/1 "2021-10-25T05:12:42Z")

</div>

I am bit new to probabilistic programming and following the Statistical Rethinking book. I want to know how to draw samples from posterior parameters distributions? (equivalent to “simulation” in R, as shown in that book). Before chapter 10 it uses quap sampler which comes with the package StatisticalRethinking and it is as simple as

`rand(quap_sampled_obj.distr, N)`

* * *

Example:

```julia
using Turing, Plots
# model
begin
	@model function linear_reg(x, y)
           β ~ Normal(0, 1)
		   y .~ Normal.(β .* x, 0.1)
     end
end
# data
xs_train = collect(0:0.1:10)
ys_train = xs_train*2.0 .+ rand(Normal(0.1),length(xs_train))

# see prior
m_train = linear_reg(xs_train, ys_train);
prior_chain = sample(m_train, Prior(), 200)

# > plot(prior_chain)
# Show prior distribution of beta centered as 0.
# Extracted samples as DataFrame(sample(m_train, Prior(), 200))

posterior_chain = sample(m_train, NUTS(), 200)

# How to draw samples from beta centered at 2 now?
# I tried rand(posterior_chain.name_map.parameters[1],200) and sample(prosterior_chain,10)
# sample method gives error: BoundsError: attempt to access 200×13×1 Array{Float64, 3} at index [[175, 183, 225, 300, 275, 132, 264, 232, 136, 288], 1:13, 1:1]

```

Also

1. Is there any better way sample from priors of model parameters?
2. How to get data out of sampled chains? chains.AxisArray object and I cant get it to give sampled values for plotting on my own?

Please not that I am not asking for data prediction using `predict`. I understood that part. I want to sample from individual parameters (prior and posterior).

Thank you

---

<div class="post-metadata">

### Author: ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)
#### Post date: [October 25, 2021, 7:35pm UTC](https://discourse.julialang.org/t/turing-sampling-from-posterior-parameters/70318/2 "2021-10-25T19:35:29Z")

</div>

> [@amit1](#):
>
> I want to know how to draw samples from posterior parameters distributions?

> [@amit1](#):
>
> `posterior_chain = sample(m_train, NUTS(), 200)`

You’ve answered your own question, just run sample as above.

> [@amit1](#):
>
> How to get data out of sampled chains?

For example to plot a density of all samples in the first chain of the Beta parameter

`density(mychain[:,:β,:1])`

---

<div class="post-metadata">

### Author: ![amit1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amit1/32/25318_2.png) [@amit1](https://discourse.julialang.org/u/amit1)
#### Post date: [October 25, 2021, 8:23pm UTC](https://discourse.julialang.org/t/turing-sampling-from-posterior-parameters/70318/3 "2021-10-25T20:23:07Z")

</div>

Thanks. Thinking about it now I am like Duh! Obviously! 🤦‍♂️  
I still had somewhat of ML mind set where sampling using NUTS constituted “Training” and hence I need to access “learned parameters”. It never occurred to me that here training _is_ the random sampling.  
Thanks again for your time
