Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DOC Add Predictive examples #1084

Merged
merged 8 commits into from
Jul 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion numpyro/infer/mcmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class MCMCKernel(ABC):
>>> kernel = MetropolisHastings(f)
>>> mcmc = MCMC(kernel, num_warmup=1000, num_samples=1000)
>>> mcmc.run(random.PRNGKey(0), init_params=jnp.array([1., 2.]))
>>> samples = mcmc.get_samples()
>>> posterior_samples = mcmc.get_samples()
>>> mcmc.print_summary() # doctest: +SKIP
"""

Expand Down Expand Up @@ -593,6 +593,15 @@ def get_samples(self, group_by_chain=False):
`dict` keyed on site names if a model containing Pyro primitives is used,
but can be any :func:`jaxlib.pytree`, more generally (e.g. when defining a
`potential_fn` for HMC that takes `list` args).

**Example:**

You can then pass those samples to :class:`~numpyro.infer.util.Predictive`::

posterior_samples = mcmc.get_samples()
predictive = Predictive(model, posterior_samples=posterior_samples)
samples = predictive(rng_key1, *model_args, **model_kwargs)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need an empty string after this to make lint pass.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might be the * - will try making it a r string

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, fixed, it just needed to be put into a code block, so with the previous line ending with :: rather than :

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now it looks like this:

Screenshot 2021-07-11 at 21 17 21

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, good catch!


"""
return (
self._states[self._sample_field]
Expand Down
5 changes: 4 additions & 1 deletion numpyro/infer/svi.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class SVI(object):
>>> import numpyro
>>> import numpyro.distributions as dist
>>> from numpyro.distributions import constraints
>>> from numpyro.infer import SVI, Trace_ELBO
>>> from numpyro.infer import Predictive, SVI, Trace_ELBO

>>> def model(data):
... f = numpyro.sample("latent_fairness", dist.Beta(10, 10))
Expand All @@ -102,6 +102,9 @@ class SVI(object):
>>> svi_result = svi.run(random.PRNGKey(0), 2000, data)
>>> params = svi_result.params
>>> inferred_mean = params["alpha_q"] / (params["alpha_q"] + params["beta_q"])
>>> # get posterior samples
>>> predictive = Predictive(guide, params=params, num_samples=1000)
>>> samples = predictive(random.PRNGKey(1), data)

:param model: Python callable with Pyro primitives for the model.
:param guide: Python callable with Pyro primitives for the guide
Expand Down
21 changes: 21 additions & 0 deletions numpyro/infer/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,27 @@ class Predictive(object):
argument is not None, its value should be equal to `num_chains x N`.

:return: dict of samples from the predictive distribution.

**Example:**

Given a model

def model(X, y=None):
...
return numpyro.sample("obs", likelihood, obs=y)

you can sample from the prior predictive:

predictive = Predictive(model, num_samples=1000)
y_pred = predictive(rng_key, X)["obs"]

If you also have posterior samples, you can sample from the posterior predictive:

predictive = Predictive(model, posterior_samples=posterior_samples)
y_pred = predictive(rng_key, X)["obs"]

See docstrings for :class:`~numpyro.infer.svi.SVI` and :class:`~numpyro.infer.mcmc.MCMCKernel`
to see example code of this in context.
"""

def __init__(
Expand Down