-
Notifications
You must be signed in to change notification settings - Fork 74
recover_marginals infers model from model context #619
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -11,7 +11,7 @@ | |||||
| from pymc.distributions.discrete import Bernoulli, Categorical, DiscreteUniform | ||||||
| from pymc.distributions.transforms import Chain | ||||||
| from pymc.logprob.transforms import IntervalTransform | ||||||
| from pymc.model import Model | ||||||
| from pymc.model import Model, modelcontext | ||||||
| from pymc.model.fgraph import ( | ||||||
| ModelFreeRV, | ||||||
| ModelValuedVar, | ||||||
|
|
@@ -337,8 +337,8 @@ def transform_posterior_pts(model, posterior_pts): | |||||
|
|
||||||
|
|
||||||
| def recover_marginals( | ||||||
| model: Model, | ||||||
| idata: InferenceData, | ||||||
| model: Model | None = None, | ||||||
| var_names: Sequence[str] | None = None, | ||||||
| return_samples: bool = True, | ||||||
| extend_inferencedata: bool = True, | ||||||
|
|
@@ -389,6 +389,11 @@ def recover_marginals( | |||||
|
|
||||||
|
|
||||||
| """ | ||||||
| if isinstance(idata, Model): | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a comment that this is temporary check due to API change and should be removed eventually |
||||||
| raise TypeError("The first argument of `recover_marginals` must be an idata") | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| model = modelcontext(model) | ||||||
|
|
||||||
| unmarginal_model = unmarginalize(model) | ||||||
|
|
||||||
| # Find the names of the marginalized variables | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -837,7 +837,9 @@ def test_basic(self): | |
| ) | ||
| idata = InferenceData(posterior=dict_to_dataset(prior)) | ||
|
|
||
| idata = recover_marginals(marginal_m, idata, return_samples=True) | ||
| with marginal_m: | ||
| idata = recover_marginals(idata, return_samples=True) | ||
|
|
||
| post = idata.posterior | ||
| assert "k" in post | ||
| assert "lp_k" in post | ||
|
|
@@ -881,7 +883,8 @@ def test_coords(self): | |
| posterior=dict_to_dataset({k: np.expand_dims(prior[k], axis=0) for k in prior}) | ||
| ) | ||
|
|
||
| idata = recover_marginals(marginal_m, idata, return_samples=True) | ||
| with marginal_m: | ||
| idata = recover_marginals(idata, return_samples=True) | ||
| post = idata.posterior | ||
| assert post.idx.dims == ("chain", "draw", "year") | ||
| assert post.lp_idx.dims == ("chain", "draw", "year", "lp_idx_dim") | ||
|
|
@@ -907,7 +910,7 @@ def test_batched(self): | |
| posterior=dict_to_dataset({k: np.expand_dims(prior[k], axis=0) for k in prior}) | ||
| ) | ||
|
|
||
| idata = recover_marginals(marginal_m, idata, return_samples=True) | ||
| idata = recover_marginals(idata, return_samples=True) | ||
| post = idata.posterior | ||
| assert post["y"].shape == (1, 20, 2, 3) | ||
| assert post["idx"].shape == (1, 20, 3, 2) | ||
|
|
@@ -933,7 +936,7 @@ def test_nested(self): | |
| ) | ||
| idata = InferenceData(posterior=dict_to_dataset(prior)) | ||
|
|
||
| idata = recover_marginals(marginal_m, idata, return_samples=True) | ||
| idata = recover_marginals(idata, return_samples=True) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we testing the explicit model kwarg anywhere now? |
||
| post = idata.posterior | ||
| assert "idx" in post | ||
| assert "lp_idx" in post | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make all but idata kwarg only?