# Posterior predictive distributions in Turing

**URL:** https://discourse.julialang.org/t/posterior-predictive-distributions-in-turing/21781
**Category:** General Usage
**Created:** [March 12, 2019, 1:19pm UTC](https://discourse.julialang.org/t/posterior-predictive-distributions-in-turing/21781 "2019-03-12T13:19:34Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [March 12, 2019, 1:19pm UTC](https://discourse.julialang.org/t/posterior-predictive-distributions-in-turing/21781/1 "2019-03-12T13:19:34Z")

</div>

I am using Turing and would like to create some posterior predictive distributions. In order to do this, I need to sample from the posterior distribution. I am wondering what is the best way to sample a set of parameters from the MCMCChains object. Here is something that works:

```julia
using MCMCChains
n_iter = 500
n_name = 3
n_chain = 2

# experiment results
val = randn(n_iter, n_name, n_chain) .+ [1, 2, 3]'
val = hcat(val, rand(1:2, n_iter, 1, n_chain))

# construct a Chains object
chn = Chains(val)

function posteriorSample(chain)
    parms = chain.name_map.parameters
    idx = rand(1:length(chain))
    return map(x->chain[x].value[idx],parms)
end

#sample from the chain
postSample = posteriorSample(chn)

```

However, I was wondering if there is (undocumented) built in functionality for sampling from the chain or a better approach.

---

<div class="post-metadata">

### Author: ![goedman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goedman/32/217_2.png) [@goedman](https://discourse.julialang.org/u/goedman)
#### Post date: [March 13, 2019, 5:25pm UTC](https://discourse.julialang.org/t/posterior-predictive-distributions-in-turing/21781/2 "2019-03-13T17:25:45Z")

</div>

Hi Chris, Cameron ( @cpfiffer ) is probably a better person to answer your question, but to the best of my knowledge there is no build-in functionality for that in MCMCChains. For the StatisticalRethinking project I am looking into implementing part of the Sampling API (as in StatsBase). I was primarily interested in sample() with an AbstractWeights vector (with computed posterior densities). Your question is a subset of the sample() functionality. In StatisticalRethinking#master there are a couple of very early tests with that stuff (test/test\_sampling.jl).

---

<div class="post-metadata">

### Author: ![jkbest2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jkbest2/32/7350_2.png) [@jkbest2](https://discourse.julialang.org/u/jkbest2)
#### Post date: [March 13, 2019, 5:47pm UTC](https://discourse.julialang.org/t/posterior-predictive-distributions-in-turing/21781/3 "2019-03-13T17:47:59Z")

</div>

Out of curiousity, is your posterior prediction so computationally expensive that you need to use a subset of the posterior samples? I’ve typically used the entire posterior sample. (Not that it wouldn’t be useful to be able to sample from the posterior for other reasons.)

---

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [March 13, 2019, 6:00pm UTC](https://discourse.julialang.org/t/posterior-predictive-distributions-in-turing/21781/4 "2019-03-13T18:00:45Z")

</div>

Thanks, Rob. I’ll take a look.

jkbest2, yes, in some cases, the models I am using are computationally intensive. I guess in either case, I need a way to map a three dimensional array to the correct function arguments. Its not a difficult undertaking, but I figured I would ask because there were a few times I made my own helper functions, which turned out to be less elegant the ones made by others. Rob and I had one of those situations not too long ago.

---

<div class="post-metadata">

### Author: ![cpfiffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpfiffer/32/208747_2.png) [@cpfiffer](https://discourse.julialang.org/u/cpfiffer)
#### Post date: [March 13, 2019, 8:58pm UTC](https://discourse.julialang.org/t/posterior-predictive-distributions-in-turing/21781/5 "2019-03-13T20:58:23Z")

</div>

I can confirm that no, MCMCChains doesn’t actually have sampling functionality, but it’d be nice to include something like that if you think you’d get some use out of it. Could you open a feature request on [MCMCChains](https://github.com/TuringLang/MCMCChains.jl)?

---

<div class="post-metadata">

### Author: ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)
#### Post date: [March 13, 2019, 9:32pm UTC](https://discourse.julialang.org/t/posterior-predictive-distributions-in-turing/21781/6 "2019-03-13T21:32:46Z")

</div>

I created an issue [here](https://github.com/TuringLang/MCMCChains.jl/issues). Thanks!

---

<div class="post-metadata">

### Author: ![youainti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/youainti/32/30451_2.png) [@youainti](https://discourse.julialang.org/u/youainti)
#### Post date: [September 29, 2022, 10:28pm UTC](https://discourse.julialang.org/t/posterior-predictive-distributions-in-turing/21781/7 "2022-09-29T22:28:00Z")

</div>

Just for anyone who finds this, there is [this issue in github](https://github.com/TuringLang/Turing.jl/issues/1884) which discusses the predict function.
