Skip to content

DOC: add out-of-sample predictions subsection to NumPyro conversion guide#178

Draft
Shlokpalrecha wants to merge 1 commit into
arviz-devs:mainfrom
Shlokpalrecha:docs/numpyro-oos-predictions
Draft

DOC: add out-of-sample predictions subsection to NumPyro conversion guide#178
Shlokpalrecha wants to merge 1 commit into
arviz-devs:mainfrom
Shlokpalrecha:docs/numpyro-oos-predictions

Conversation

@Shlokpalrecha

Copy link
Copy Markdown
Contributor

Context

This PR adds the missing out-of-sample predictions example requested in arviz-base issue #2452, following guidance from @OriolAbril.

What this adds

  • One focused subsection in the existing NumPyro conversion guide notebook.
  • Reuses the fitted eight schools mcmc object already present in the notebook.
  • Generates 2-3 random sigma values and builds out-of-sample predictions for y.
  • Converts with:
    • predictions=...
    • predictions_constant_data={"sigma": sigma_oos}
  • Shows where results appear by inspecting:
    • idata_mcmc_oos.predictions
    • idata_mcmc_oos.predictions_constant_data

Validation

  • tox -e check passed locally.
  • All cells executed successfully in notebook kernel.

@review-notebook-app

Copy link
Copy Markdown

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@read-the-docs-community

read-the-docs-community Bot commented Mar 21, 2026

Copy link
Copy Markdown

Documentation build overview

📚 arviz-base | 🛠️ Build #31997034 | 📁 Comparing ec46b02 against latest (374cd28)


🔍 Preview build

Show files changed (14 files in total): 📝 14 modified | ➕ 0 added | ➖ 0 deleted
File Status
api/index.html 📝 modified
how_to/ConversionGuideNumPyro.html 📝 modified
_modules/arviz_base/base.html 📝 modified
_modules/arviz_base/converters.html 📝 modified
_modules/arviz_base/rcparams.html 📝 modified
_modules/arviz_base/reorg.html 📝 modified
api/generated/arviz_base.dataset_to_dataarray.html 📝 modified
api/generated/arviz_base.dataset_to_dataframe.html 📝 modified
api/generated/arviz_base.dict_to_dataset.html 📝 modified
api/generated/arviz_base.explode_dataset_dims.html 📝 modified
api/generated/arviz_base.extract.html 📝 modified
api/generated/arviz_base.make_attrs.html 📝 modified
api/generated/arviz_base.references_to_dataset.html 📝 modified
api/generated/arviz_base.xarray_sel_iter.html 📝 modified

@Shlokpalrecha Shlokpalrecha marked this pull request as draft March 21, 2026 08:24
Comment thread src/arviz_base/utils.py Outdated


def _check_tilde_start(x):
"""Check whether an item starts with the negation prefix.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is there any reason why youre making changes to the utils file?

I think it would be best to keep this PR scoped to just the conversion guide

@@ -121,12 +121,12 @@
"name": "stderr",

@kylejcaron kylejcaron Mar 21, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Line #2.    sigma_pool = rng.uniform(8.0, 18.0, size=3)

the two line sampling procedure isnt really necessary, you can just hard code a few sigma values or sample them in 1 line instead of two


Reply via ReviewNB

@@ -121,12 +121,12 @@
"name": "stderr",

@kylejcaron kylejcaron Mar 21, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Line #3.    sigma_oos = rng.choice(sigma_pool, size=J, replace=True)

lets choose a different number of schools to make predictions on than 8 to avoid confusion. maybe just 3?


Reply via ReviewNB

@@ -121,12 +121,12 @@
"name": "stderr",

@kylejcaron kylejcaron Mar 21, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Line #2.    idata_mcmc_oos.predictions_constant_data

you can remove this cell, its covered by the last cell which returns idata_mcmc_oos


Reply via ReviewNB

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I removed that redundant cell and now only return idata_mcmc_oos in this section.

@Shlokpalrecha

Copy link
Copy Markdown
Contributor Author

Thanks, updated. I removed unrelated scope from this PR, simplified the sigma sampling, switched to 3-school out-of-sample predictions to avoid confusion, and removed the redundant display cell

@kylejcaron

kylejcaron commented Mar 25, 2026

Copy link
Copy Markdown
Contributor

Thanks, updated. I removed unrelated scope from this PR, simplified the sigma sampling, switched to 3-school out-of-sample predictions to avoid confusion, and removed the redundant display cell

Im not seeing those changes reflected here - maybe check if you pushed the correct commit?

@@ -35,7 +35,7 @@
},

@OriolAbril OriolAbril Mar 26, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This reads more like a PR review than a tutorial page. How about something like:

A common scenario is to generate predictions for newly obtained data or data that was excluded from the fit. The InferenceData schema has a predictions group to hold this kind of posterior predictive samples.
This example shows how posterior predictive samples generated for newly obtained values of sigma can be incorporated into the generated DataTree 

Reply via ReviewNB

@@ -35,7 +35,7 @@
},

@OriolAbril OriolAbril Mar 26, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

sigma_model = np.random.default_rng(2026).choice(np.array([9.0, 12.0, 16.0]), size=J, replace=True)

I think this is still overly complicated and using model in the variable name is potentially confusing. The other variables called model are the functions that do encode a probabilistic model. This could be something like: new_sigma = np.array([-2.0, 7.0, 17.0])

predictions_oos_full = predictive_oos(random.PRNGKey(2), J=J, sigma=sigma_model)

I have barely used numpyro but doing this here with J=8 and then having to index seems again too complicated, this is a very common usecase. My guess is this can be simplified to ..., J=3, sigma=new_sigma) which in turn will allow deleting lines 6-8 and using the output of predictive_oos directly as the predictions argument. I think this is what @kylejcaron was asking for in the previous review round.


Reply via ReviewNB

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, agreed. I simplified this section accordingly in docs/source/how_to/ConversionGuideNumPyro.ipynb:
renamed to new_sigma,
switched to direct J=3 predictions call,
removed the previous indexing-heavy flow.
I also adjusted the predictive setup so the simplified example runs cleanly while keeping the tutorial code straightforward.

@Shlokpalrecha Shlokpalrecha force-pushed the docs/numpyro-oos-predictions branch from 655e6f6 to ec46b02 Compare March 27, 2026 04:12
@Shlokpalrecha

Copy link
Copy Markdown
Contributor Author

Apologies for the earlier confusion with what was visible in the PR. I’ve now pushed the corrected branch state and aligned the section with all review points:
docs-only scope,
clearer tutorial wording,
simplified out-of-sample code path (new_sigma, direct J=3).
Thanks again for the detailed review.

@@ -35,7 +35,7 @@
},

@kylejcaron kylejcaron Mar 30, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Line #1.    sigma_model = np.random.default_rng(2026).choice(np.array([9.0, 12.0, 16.0]), size=J, replace=True)

Theres no reason to do sampling with replacement here, and you end up with size J=8 instead of J=3 which would be better

you can just hardcode sigma to a size 3 array


Reply via ReviewNB

@@ -35,7 +35,7 @@
},

@kylejcaron kylejcaron Mar 30, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Line #3.    predictive_oos = Predictive(eight_schools_model, mcmc.get_samples())

mcmc.get_samples() contains theta from the fitted schools.

Our goal is to estimate theta for 3 new schools. One of the possible correct patterns for numpyro here would be:

post_samples = mcmc.get_samples()
post_samples.pop("theta") # remove theta so it can be estimated for new schools
predictive_oos = Predictive(eight_schools_model, post_samples)


Reply via ReviewNB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants