Dear fellow pyromants,
i have a question regarding how to obtain the posterior predictive distribution using MCMC techniques.
Here is the little example code i am trying to run:
# generate some data np.random.seed(1) xs = to.tensor(np.linspace(-2,2,23)).float() f = lambda z : z*.5 + 1 ys = f(xs) + to.tensor(np.random.normal(0, .5, size=len(xs))).float() def y_model(x, y_obs=None): ''' very simple linreg model''' # priors w = pyro.sample('w', pyro.distributions.Normal(loc=0, scale=10)) b = pyro.sample('b', pyro.distributions.Normal(loc=0, scale=10)) f_x = w*x + b y = pyro.sample('y', pyro.distributions.Normal(loc=f_x, scale=1), obs=y_obs) return f_x
I can now sample models from the prior by just running
samples_from_prior = y_model(xs, None)
For inference, as described in the title, i want to use MCMC techniques:
from pyro.infer.mcmc import MCMC, NUTS posterior_trace = MCMC(kernel=NUTS(y_model), num_samples=1337, warmup_steps=100) posterior_trace.run(xs, ys)
posterior_trace is now an
pyro.infer.mcmc.mcmc object, on which i can call
posterior_trace.marginal(sites=..).empirical[..] to get the samples for the respective random variables “w” and “b” (and also ‘y’). And this is about how far the tutorial goes.
But now i would like to sample models
y_model given the parameter configurations in the
posterior_trace. I am not sure how this is ment to be done.
In case of SVI and a given guide i usually did the following:
model_predictions = 
for _ in range(100): # sample 100 models (i.e. posteriors given y_obs)
## trace of the guide (samples all variables given x from the guide) guide_trace = pyro.poutine.trace(guide).get_trace(xs, None) # runs the model using samples from the guide for the latent variables replay_result = pyro.poutine.replay(y_model, guide_trace)(xs, None) # append to list model_predictions.append(replay_result.detach().numpy())
which would give me 100 sampled models from the (guide-approximated) posterior predictive distribution.
How can i achieve the same using the results obtained from MCMC ?
Thanks in advance !
- What tutorial are you running? (a very slim version of)
- What version of Pyro are you using?