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

Update docs to make Predictive behavior more clear #1850

Merged
merged 3 commits into from
Sep 6, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,13 @@
"samples_predictive = predictive(random.PRNGKey(0), patient_code, Weeks, None)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that for [`Predictive`](http://num.pyro.ai/en/latest/utilities.html#numpyro.infer.util.Predictive) to work as expected, the response variable of the model (in this case, `FVC_obs`) must be set to `None`."
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
8 changes: 8 additions & 0 deletions notebooks/source/bayesian_regression.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -1507,6 +1507,14 @@
"ax.set(xlabel=\"Marriage rate\", ylabel=\"Divorce rate\", title=\"Predictions with 90% CI\");"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that for `Predictive` to work as expected, the response variable of the model (in this case, `divorce`) must be set to `None`.\n",
"In the code above this is done implicitly by not passing a value for `divorce` to the model in the call to `prior_predictive`, which due to the model definition, sets `divorce=None`."
]
},
{
"cell_type": "markdown",
"metadata": {
Expand Down
7 changes: 7 additions & 0 deletions numpyro/infer/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -857,6 +857,9 @@ class Predictive(object):
The interface for the `Predictive` class is experimental, and
might change in the future.
Note that for the predictive distribution to be returned as intended, observed
variables in the model (constraining the likelihood term) must be set to `None` (see Example).
:param model: Python callable containing Pyro primitives.
:param dict posterior_samples: dictionary of samples from the posterior.
:param callable guide: optional guide to get posterior samples of sites not present
Expand Down Expand Up @@ -908,6 +911,10 @@ def model(X, y=None):
predictive = Predictive(model, num_samples=1000)
y_pred = predictive(rng_key, X)["obs"]
Note how above, no value for `y` is passed to `predictive`, resulting in `y`
being set to `None`. Setting the observed variable(s) to `None` when using
`Predictive` is required for the method to function as expected.
If you also have posterior samples, you can sample from the posterior predictive::
predictive = Predictive(model, posterior_samples=posterior_samples)
Expand Down
Loading