Source code for pyro.infer.importance

from __future__ import absolute_import, division, print_function

import pyro.poutine as poutine

from .abstract_infer import TracePosterior

[docs]class Importance(TracePosterior): """ :param model: probabilistic model defined as a function :param guide: guide used for sampling defined as a function :param num_samples: number of samples to draw from the guide (default 10) This method performs posterior inference by importance sampling using the guide as the proposal distribution. If no guide is provided, it defaults to proposing from the model's prior. """ def __init__(self, model, guide=None, num_samples=None): """ Constructor. default to num_samples = 10, guide = model """ super(Importance, self).__init__() if num_samples is None: num_samples = 10 print("num_samples not provided, defaulting to {}".format(num_samples)) if guide is None: # propose from the prior by making a guide from the model by hiding observes guide = poutine.block(model, hide_types=["observe"]) self.num_samples = num_samples self.model = model = guide def _traces(self, *args, **kwargs): """ Generator of weighted samples from the proposal distribution. """ for i in range(self.num_samples): guide_trace = poutine.trace(*args, **kwargs) model_trace = poutine.trace( poutine.replay(self.model, guide_trace)).get_trace(*args, **kwargs) log_weight = model_trace.log_pdf() - guide_trace.log_pdf() yield (model_trace, log_weight)