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

UserWarning if doing predictive sampling with models containing Potentials #4419

Merged
merged 4 commits into from
Jan 16, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions RELEASE-NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ It also brings some dreadfully awaited fixes, so be sure to go through the chang
- Fixed `MatrixNormal` random method to work with parameters as random variables. (see [#4368](https://github.com/pymc-devs/pymc3/pull/4368))
- Update the `logcdf` method of several continuous distributions to return -inf for invalid parameters and values, and raise an informative error when multiple values cannot be evaluated in a single call. (see [4393](https://github.com/pymc-devs/pymc3/pull/4393))
- Improve numerical stability in `logp` and `logcdf` methods of `ExGaussian` (see [#4407](https://github.com/pymc-devs/pymc3/pull/4407))
- Issue UserWarning when doing prior or posterior predictive sampling with models containing Potential factors (see [#4419](https://github.com/pymc-devs/pymc3/pull/4419))

## PyMC3 3.10.0 (7 December 2020)

Expand Down
8 changes: 8 additions & 0 deletions pymc3/distributions/posterior_predictive.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,14 @@ def fast_sample_posterior_predictive(

model = modelcontext(model)
assert model is not None

if model.potentials:
warnings.warn(
"The effect of Potentials on other parameters is ignored during posterior predictive sampling. "
"This is likely to lead to invalid or biased predictive samples.",
UserWarning,
)

with model:

if keep_size and samples is not None:
Expand Down
23 changes: 23 additions & 0 deletions pymc3/sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -1692,6 +1692,13 @@ def sample_posterior_predictive(

model = modelcontext(model)

if model.potentials:
warnings.warn(
"The effect of Potentials on other parameters is ignored during posterior predictive sampling. "
"This is likely to lead to invalid or biased predictive samples.",
UserWarning,
)

if var_names is not None:
vars_ = [model[x] for x in var_names]
else:
Expand Down Expand Up @@ -1791,6 +1798,15 @@ def sample_posterior_predictive_w(
if models is None:
models = [modelcontext(models)] * len(traces)

for model in models:
if model.potentials:
warnings.warn(
"The effect of Potentials on other parameters is ignored during posterior predictive sampling. "
"This is likely to lead to invalid or biased predictive samples.",
UserWarning,
)
break

if weights is None:
weights = [1] * len(traces)

Expand Down Expand Up @@ -1903,6 +1919,13 @@ def sample_prior_predictive(
"""
model = modelcontext(model)

if model.potentials:
warnings.warn(
"The effect of Potentials on other parameters is ignored during prior predictive sampling. "
"This is likely to lead to invalid or biased predictive samples.",
UserWarning,
)

if var_names is None:
prior_pred_vars = model.observed_RVs
prior_vars = (
Expand Down
36 changes: 36 additions & 0 deletions pymc3/tests/test_sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,22 @@ def test_variable_type(self):
assert ppc["a"].dtype.kind == "f"
assert ppc["b"].dtype.kind == "i"

def test_potentials_warning(self):
warning_msg = "The effect of Potentials on other parameters is ignored during"
with pm.Model() as m:
a = pm.Normal("a", 0, 1)
p = pm.Potential("p", a)
obs = pm.Normal("obs", a, 1, observed=5)
trace = pm.sample()

with pytest.warns(UserWarning, match=warning_msg):
with m:
pm.sample_posterior_predictive(trace, samples=5)

with pytest.warns(UserWarning, match=warning_msg):
with m:
pm.fast_sample_posterior_predictive(trace, samples=5)


class TestSamplePPCW(SeededTest):
def test_sample_posterior_predictive_w(self):
Expand Down Expand Up @@ -773,6 +789,17 @@ def test_sample_posterior_predictive_w(self):
):
pm.sample_posterior_predictive_w([trace_0, trace_2], 100, [model_0, model_2])

def test_potentials_warning(self):
warning_msg = "The effect of Potentials on other parameters is ignored during"
with pm.Model() as m:
a = pm.Normal("a", 0, 1)
p = pm.Potential("p", a)
obs = pm.Normal("obs", a, 1, observed=5)
trace = pm.sample()

with pytest.warns(UserWarning, match=warning_msg):
pm.sample_posterior_predictive_w(samples=5, traces=[trace, trace], models=[m, m])


@pytest.mark.parametrize(
"method",
Expand Down Expand Up @@ -1012,6 +1039,15 @@ def test_bounded_dist(self):
prior_trace = pm.sample_prior_predictive(5)
assert prior_trace["x"].shape == (5, 3, 1)

def test_potentials_warning(self):
warning_msg = "The effect of Potentials on other parameters is ignored during"
with pm.Model() as m:
a = pm.Normal("a", 0, 1)
p = pm.Potential("p", a)

with pytest.warns(UserWarning, match=warning_msg):
pm.sample_prior_predictive(samples=5)


class TestSamplePosteriorPredictive:
def test_point_list_arg_bug_fspp(self, point_list_arg_bug_fixture):
Expand Down