Skip to content

Fix group selection in sample_posterior_predictive when predictions=True is passed in kwargs #426

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
15 changes: 10 additions & 5 deletions pymc_extras/model_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@ def predict(
self,
X_pred: np.ndarray | pd.DataFrame | pd.Series,
extend_idata: bool = True,
predictions: bool = False,
**kwargs,
) -> np.ndarray:
"""
Expand Down Expand Up @@ -559,7 +560,7 @@ def predict(
"""

posterior_predictive_samples = self.sample_posterior_predictive(
X_pred, extend_idata, combined=False, **kwargs
X_pred, extend_idata, predictions, combined=False, **kwargs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pass by keyword to be on the safe side

)

if self.output_var not in posterior_predictive_samples:
Expand Down Expand Up @@ -624,7 +625,7 @@ def sample_prior_predictive(

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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Provide default

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

"""
Sample from the model's posterior predictive distribution.

Expand All @@ -646,12 +647,15 @@ def sample_posterior_predictive(self, X_pred, extend_idata, combined, **kwargs):
self._data_setter(X_pred)

with self.model: # sample with new input data
post_pred = pm.sample_posterior_predictive(self.idata, **kwargs)
post_pred = pm.sample_posterior_predictive(self.idata, predictions=predictions, **kwargs)
if extend_idata:
self.idata.extend(post_pred, join="right")

# Determine the correct group
group_name = "predictions" if predictions else "posterior_predictive"

posterior_predictive_samples = az.extract(
post_pred, "posterior_predictive", combined=combined
post_pred, group_name, combined=combined
)

return posterior_predictive_samples
Expand Down Expand Up @@ -700,6 +704,7 @@ def predict_posterior(
X_pred: np.ndarray | pd.DataFrame | pd.Series,
extend_idata: bool = True,
combined: bool = True,
predictions: bool = False,
**kwargs,
) -> xr.DataArray:
"""
Expand All @@ -723,7 +728,7 @@ def predict_posterior(

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pass by keyword argument

Copy link
Author

@butterman0 butterman0 Feb 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ricardoV94, let me know what you think and I can adjust.

)

if self.output_var not in posterior_predictive_samples:
Expand Down