Fix group selection in sample_posterior_predictive when predictions=True is passed in kwargs#426
Fix group selection in sample_posterior_predictive when predictions=True is passed in kwargs#426butterman0 wants to merge 12 commits intopymc-devs:mainfrom
sample_posterior_predictive when predictions=True is passed in kwargs#426Conversation
|
I'm sorry I haven't opened an issue first - I thought it was such a minor change that it wasn't necessary. This is also my first contribution so not 100% on the process! |
| # Determine the correct group dynamically | ||
| group_name = "predictions" if kwargs.get("predictions", False) else "posterior_predictive" |
There was a problem hiding this comment.
Would it be better to make predictions an explicit kwarg (with the same default as PyMC) and use that directly?
There was a problem hiding this comment.
Yes I initially had that! Although I wasn't sure what was best practice. It is nice to make it explicit, but it means passing predictions=False as explicit args through the predict method and then to sample_posterior_predictive which is used in other methods - although this shouldn't be a problem if keeping the same default as PyMC as you suggest.
There was a problem hiding this comment.
In fact, the class method sample_posterior_predictive is called twice, on both occasions it is for prediction: class methods predict and predict_posterior.
I think I would argue that in this case we would like the default to be predictions=True (as opposed to the pymc pm.sample_posterior_predictive default). The default would be set in the predict and predict_posterior methods.
I say this because when False, the posterior_predictive group in the idata object is overridden - meaning we would have to run fit or sample_model again if we wanted to do posterior predictive checks?
There was a problem hiding this comment.
Just checking you agree with setting predictions=True as default @ricardoV94 ?
There was a problem hiding this comment.
Yeah makes sense in the predict oriented methods
ricardoV94
left a comment
There was a problem hiding this comment.
The predictions argument should be mentioned in the docstrings now that it is explicit
| return prior_predictive_samples | ||
|
|
||
| def sample_posterior_predictive(self, X_pred, extend_idata, combined, **kwargs): | ||
| def sample_posterior_predictive(self, X_pred, extend_idata, predictions, combined, **kwargs): |
There was a problem hiding this comment.
The other arguments do not have defaults. The sample_posterior_predictive is only called through the predict functions, which do have defaults.
Would you be able to explain why we would want predictions to have a default, when the other arguments do not?
|
|
||
| posterior_predictive_samples = self.sample_posterior_predictive( | ||
| X_pred, extend_idata, combined=False, **kwargs | ||
| X_pred, extend_idata, predictions, combined=False, **kwargs |
There was a problem hiding this comment.
pass by keyword to be on the safe side
| X_pred = self._validate_data(X_pred) | ||
| posterior_predictive_samples = self.sample_posterior_predictive( | ||
| X_pred, extend_idata, combined, **kwargs | ||
| X_pred, extend_idata, predictions, combined, **kwargs |
There was a problem hiding this comment.
I was aiming to keep it in the same format as current implementation. i.e. x_pred, extend_idata and combined do not use keyword arguments..
Similar question to the one above - should these all be changed to use keyword arguments? Why would we treat predictions differently?
There was a problem hiding this comment.
Hi @ricardoV94, let me know what you think and I can adjust.
|
Hi @ricardoV94, I've made the requested changes. Two commits updating the doc strings. Most recent commit passing by keyword argument and setting default as requested. I'm still a little unclear as to why we would treat the predictions argument differently to the other arguments in the Harry |
|
We should always pass by keyword argument, but since you didn't write the previous code I didn't ask you to change those lines |
ricardoV94
left a comment
There was a problem hiding this comment.
Can you add a test that confirms this is working now?
|
pre-commit.ci autofix |
|
Hi @ricardoV94, let me know if anything else. |
7f84f03 to
7950f99
Compare
|
@butterman0 sorry for the delay, running the tests, we can merge if they pass |
|
pre-commit.ci autofix |
|
Hi @ricardoV94, one main problem (incorrect argument) arose in the testing which was solved. There were a few more under the hood that arose when I added another test: to test the The I assume Let me know if anything is off - test are passing on my side. |
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
f4d04c2 to
776908f
Compare
|
Hi @ricardoV94, I'm not totally sure what the problem is as all the tests are passing on my side. I will have a look at the test output when it runs. |
|
Hi @ricardoV94, what do you think of the updates? I also am considering writing a python package for the biodiversity model I'm making using PyMC, and in particular model builder. There's a few design choices specifically regarding model builder I wanted to ask about to help make this. Where would be the best forum for that? |
Summary
Fixes hard-coded group selection in
sample_posterior_predictivewhich unnecessarily restricts usage of predict functions. Previously, ifpredictions=True(ideally set in pm.sample_posterior_predictive when predicting out-of-sample) is passed as a kwarg to the predict functions, the inference data was extracted fromposterior_predictivegroup which is incorrect whenpredictions = True.Changes
Selects appropriate group depending if
predictionsis passed.