Posterior predictive distributions in Turing

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:

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.

1 Like

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).

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.)

1 Like

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.

1 Like

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?

1 Like

I created an issue here. Thanks!

Just for anyone who finds this, there is this issue in github which discusses the predict function.

2 Likes