Predictive as an easy way to sample from posterior conditioned on observations?

If I want to generate samples from a hidden Markov process conditioned on observations then I can use Predictive is that correct? Since for those sites for which the latent variable values are not specified it samples from the posterior conditioned on the observations? This was my understanding but unless I’ve made a mistake (quite possible) it doesn’t seem to be conditioning on the observations. Thanks.

Hi, if your aim is to draw posterior samples for discrete latent variables that are marginalized during training, you will need to use pyro.infer.discrete.infer_discrete; see the Gaussian mixture model tutorial for a complete example. pyro.infer.Predictive does not do this automatically.

If not, please be more specific (e.g. by providing some runnable code to reproduce errors and a clear description of both the current and desired behaviors of that code), otherwise it’s difficult for us to be helpful.

Hi,

As far as I was aware infer_discrete produces maximum likelihood estimates of hidden states (using a forward backward algorithm if temperature=0)?

Sorry the code is pretty long, it does feature entirely discrete variables though. I got this impression from the docs where it said:

“EXPERIMENTAL class used to construct predictive distribution. The predictive distribution is obtained by running the model conditioned on latent samples from posterior_samples. If a guide is provided, then posterior samples from all the latent sites are also returned.”

https://docs.pyro.ai/en/dev/inference_algos.html

So in that case when it predicts and you give it samples of latent sites, it purely uses these and not the observations that are supplied as an argument to the model/guide?

So in that case when it predicts and you give it samples of latent sites, it purely uses these and not the observations that are supplied as an argument to the model/guide?

If you do not pass a posterior sample or a guide for a site to Predictive or observe the site in the model, a value for that site will be sampled from the distribution object at that site in the model. This is how the posterior predictive distribution is defined. If you would like to use Predictive in conjunction with collapsed discrete latent variables, you will need to draw posterior samples for those variables yourself using infer_discrete and include them in the samples argument to Predictive.

As far as I was aware infer_discrete produces maximum likelihood estimates of hidden states (using a forward backward algorithm if temperature=0)?

The default setting in infer_discrete of temperature=1 corresponds to sampling from the posterior distribution over discrete latent variables, while setting temperature=0 finds the maximum a posteriori configuration of discrete variables. From the infer_discrete documentation I linked to above:

  • temperature ( int ) – Either 1 (sample via forward-filter backward-sample) or 0 (optimize via Viterbi-like MAP inference). Defaults to 1 (sample).

From the Gaussian mixture model tutorial I linked to above:

To generate random posterior assignments rather than MAP assignments, we could set temperature=1 .

If you do not pass a posterior sample or a guide for a site to Predictive or observe the site in the model, a value for that site will be sampled from the distribution object at that site in the model. This is how the posterior predictive distribution is defined. If you would like to use Predictive in conjunction with collapsed discrete latent variables, you will need to draw posterior samples for those variables yourself using infer_discrete and include them in the samples argument to Predictive .

Thanks :slightly_smiling_face:, so if I do pass a guide (and a model with observations of the observed variables) it should sample the latent variables from the posterior (conditioned on the observations) not the original model then?

so if I do pass a guide (and a model with observations of the observed variables) it should sample the latent variables from the posterior (conditioned on the observations) not the original model then?

Predictive will use samples from the guide for every latent variable included in the guide. However, for any latent variable not included in the guide, it will draw samples from the distribution at that site in the model.

1 Like

Thanks, that was what I thought :smiley: