-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Check for observed variables in the trace #7641
Check for observed variables in the trace #7641
Conversation
ca7c8f8
to
567fb2a
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #7641 +/- ##
=======================================
Coverage 92.77% 92.78%
=======================================
Files 107 107
Lines 18178 18185 +7
=======================================
+ Hits 16865 16873 +8
+ Misses 1313 1312 -1
|
567fb2a
to
662595b
Compare
tests/sampling/test_forward.py
Outdated
# test that trace is used in ppc | ||
with pm.Model() as model_ppc: | ||
mu = pm.Normal("mu", 0.0, 1.0) | ||
a = pm.Normal("a", mu=mu, sigma=1) | ||
|
||
ppc = pm.sample_posterior_predictive( | ||
trace=trace, model=model_ppc, return_inferencedata=False | ||
) | ||
assert "a" in ppc |
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.
Can you put this in its own test?
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.
I would also make the test more stringent. Test that only the variables that you want are actually included in the trace. Also add a case where one node is conditionally dependent on the trace.observed_data
so that you see that the auto added variables include conditional nodes.
@@ -817,6 +819,8 @@ def sample_posterior_predictive( | |||
vars_ = [model[x] for x in var_names] | |||
else: | |||
vars_ = model.observed_RVs + observed_dependent_deterministics(model) | |||
if observed_data is not None: |
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.
BTW, the the observed_dependent_deterministics
above is not going to work if these variables are not observed in the model.
That happens with auto-imputation models, which I assume the as_model wrapper won't handle correctly either because the models are different depending on whether you pass data or not.
Just something to keep in mind
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.
I agree with this. You'll have to adapt observed_dependent_deterministics
to also accept a list of extra variables that will depend on your observed_data
Can you give an example of an auto-imputation model?
…On Tue, 14 Jan 2025, 17:01 Ricardo Vieira, ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In tests/sampling/test_forward.py
<#7641 (comment)>:
> + # test that trace is used in ppc
+ with pm.Model() as model_ppc:
+ mu = pm.Normal("mu", 0.0, 1.0)
+ a = pm.Normal("a", mu=mu, sigma=1)
+
+ ppc = pm.sample_posterior_predictive(
+ trace=trace, model=model_ppc, return_inferencedata=False
+ )
+ assert "a" in ppc
Can you put this in its own test?
------------------------------
In pymc/sampling/forward.py
<#7641 (comment)>:
> @@ -817,6 +819,8 @@ def sample_posterior_predictive(
vars_ = [model[x] for x in var_names]
else:
vars_ = model.observed_RVs + observed_dependent_deterministics(model)
+ if observed_data is not None:
BTW, the the observed_dependent_deterministics above is not going to work
if these variables are not observed in the model.
That happens with auto-imputation models, which I assume the as_model
wrapper won't handle correctly either because the models are different
depending on whether you pass data or not.
Just something to keep in mind
—
Reply to this email directly, view it on GitHub
<#7641 (review)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAACCULTFSIDHCNH5OMFAVT2KUYCHAVCNFSM6AAAAABVBF3DQWVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMZDKNJQGE4TQNJWGI>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
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.
I second Ricardo with his comments. Just a few changes and I think this will be ok to merge.
@@ -817,6 +819,8 @@ def sample_posterior_predictive( | |||
vars_ = [model[x] for x in var_names] | |||
else: | |||
vars_ = model.observed_RVs + observed_dependent_deterministics(model) | |||
if observed_data is not None: |
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.
I agree with this. You'll have to adapt observed_dependent_deterministics
to also accept a list of extra variables that will depend on your observed_data
pymc/sampling/forward.py
Outdated
@@ -817,6 +819,8 @@ def sample_posterior_predictive( | |||
vars_ = [model[x] for x in var_names] | |||
else: | |||
vars_ = model.observed_RVs + observed_dependent_deterministics(model) | |||
if observed_data is not None: | |||
vars_ += [model[x] for x in observed_data if x in model] |
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.
maybe also add and if x not in vars_
to the list comprehension.
tests/sampling/test_forward.py
Outdated
# test that trace is used in ppc | ||
with pm.Model() as model_ppc: | ||
mu = pm.Normal("mu", 0.0, 1.0) | ||
a = pm.Normal("a", mu=mu, sigma=1) | ||
|
||
ppc = pm.sample_posterior_predictive( | ||
trace=trace, model=model_ppc, return_inferencedata=False | ||
) | ||
assert "a" in ppc |
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.
I would also make the test more stringent. Test that only the variables that you want are actually included in the trace. Also add a case where one node is conditionally dependent on the trace.observed_data
so that you see that the auto added variables include conditional nodes.
Thanks! Can you give an example model where a node is conditionally
dependent?
…On Tue, 14 Jan 2025, 17:27 Luciano Paz, ***@***.***> wrote:
***@***.**** requested changes on this pull request.
I second Ricardo with his comments. Just a few changes and I think this
will be ok to merge.
------------------------------
In pymc/sampling/forward.py
<#7641 (comment)>:
> @@ -817,6 +819,8 @@ def sample_posterior_predictive(
vars_ = [model[x] for x in var_names]
else:
vars_ = model.observed_RVs + observed_dependent_deterministics(model)
+ if observed_data is not None:
+ vars_ += [model[x] for x in observed_data if x in model]
maybe also add and if x not in vars_ to the list comprehension.
------------------------------
In tests/sampling/test_forward.py
<#7641 (comment)>:
> + # test that trace is used in ppc
+ with pm.Model() as model_ppc:
+ mu = pm.Normal("mu", 0.0, 1.0)
+ a = pm.Normal("a", mu=mu, sigma=1)
+
+ ppc = pm.sample_posterior_predictive(
+ trace=trace, model=model_ppc, return_inferencedata=False
+ )
+ assert "a" in ppc
I would also make the test more stringent. Test that only the variables
that you want are actually included in the trace. Also add a case where one
node is conditionally dependent on the trace.observed_data so that you
see that the auto added variables include conditional nodes.
------------------------------
In pymc/sampling/forward.py
<#7641 (comment)>:
> @@ -817,6 +819,8 @@ def sample_posterior_predictive(
vars_ = [model[x] for x in var_names]
else:
vars_ = model.observed_RVs + observed_dependent_deterministics(model)
+ if observed_data is not None:
I agree with this. You'll have to adapt observed_dependent_deterministics
to also accept a list of extra variables that will depend on your
observed_data
—
Reply to this email directly, view it on GitHub
<#7641 (review)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAACCUN4ZJSE2S2DWG5FJQL2KU3GBAVCNFSM6AAAAABVBF3DQWVHI2DSMVQWIX3LMV43YUDVNRWFEZLROVSXG5CSMV3GSZLXHMZDKNJQGMYDENBTGI>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
That helper was created specifically for the deterministic created by automatic imputation that joins the observed and unobserved components, so I'm not too worried about it. But if you wanted just add a deterministic that's like y + 1, where y is the variable that was observed during sampling |
f24a55c
to
8dc0945
Compare
a217113
to
e895a5c
Compare
It's not fully clear the best way to make sure the correct conditional nodes are added when the model that produced the trace isn't readily available. So I have written a test we expect to fail in the meanwhile. |
pymc/sampling/forward.py
Outdated
@@ -821,6 +824,7 @@ def sample_posterior_predictive( | |||
vars_ = model.observed_RVs + observed_dependent_deterministics(model) | |||
if observed_data is not None: | |||
vars_ += [model[x] for x in observed_data if x in model and x not in vars_] | |||
vars_ += observed_dependent_deterministics(model, vars_) |
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.
This is going to duplicate deterministics of an observed variable, in case there's a mix of observed model variables and implied observed variables from the idata. the + observed_dependent_determininstics)
should be called only once after the if branch?
@lucianopaz does this address your concerns? |
tests/sampling/test_forward.py
Outdated
@@ -540,6 +540,50 @@ def test_normal_scalar_idata(self): | |||
ppc = pm.sample_posterior_predictive(idata, return_inferencedata=False) | |||
assert ppc["a"].shape == (nchains, ndraws) | |||
|
|||
def test_external_trace(self): |
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.
Remove this test? The one with det
is strictly more comprehensive than this?
4a0aa08
to
2fcf395
Compare
Description
This introduces the enhancement discussed in #7225
This makes it easier to use
sample_posterior_predictive
in model factory workflowsChecklist
Type of change
📚 Documentation preview 📚: https://pymc--7641.org.readthedocs.build/en/7641/