Turing: Sampling from posterior parameters

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)


using Turing, Plots
# model
	@model function linear_reg(x, y)
           β ~ Normal(0, 1)
		   y .~ Normal.(β .* x, 0.1)
# 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]


  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

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

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


Thanks. Thinking about it now I am like Duh! Obviously! :man_facepalming:
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

1 Like