Skip to content

Fix numpyro wrongly conditioning on non-sample sites #58

Merged
aloctavodia merged 4 commits into
arviz-devs:mainfrom
Cab14bacc:fix/no_observed_numpyro_neg_test
May 26, 2026
Merged

Fix numpyro wrongly conditioning on non-sample sites #58
aloctavodia merged 4 commits into
arviz-devs:mainfrom
Cab14bacc:fix/no_observed_numpyro_neg_test

Conversation

@Cab14bacc
Copy link
Copy Markdown
Contributor

@Cab14bacc Cab14bacc commented May 22, 2026

This fixes test test_sbc_numpyro_fail_no_observed_variable. The expected behavior is to raise an ValueError since there is no actual observed variable to condition on.

Factor sites in numpyro is classified as a observed site when the criterion is the existence of is_observed attr.

This means the prior predicitve draw will output Factor sites values for the model to condition on. This is however not only against the user's intention as the provided model function itself does not have the corresponding args, which would yield a TypeError, we also can't supply observed data to factor sites.

The previous test was failing silently as the try except block below catches the error raised in _get_posterior_samples_numpyro on the first iteration, making the self.simulations dict empty, in turn raising another error when attempting to stack an empty array in the finally block, which is conveniently also an ValueError. And the negative test, see that an error is raised and interpreted the eerror handling as correct.

simuk/simuk/sbc.py

Lines 316 to 333 in 6ed2d1c

try:
while self._simulations_complete < self.num_simulations:
idx = self._simulations_complete
prior_predictive_draw = {k: v[idx] for k, v in prior_pred.items()}
posterior = self._get_posterior_samples_numpyro(prior_predictive_draw)
for name in self.var_names:
self.simulations[name].append(
(posterior[name].sel(chain=0) < prior[name][idx]).sum(axis=0).values
)
self._simulations_complete += 1
progress.update()
finally:
self.simulations = {
k: np.stack(v[: self._simulations_complete])[None, :]
for k, v in self.simulations.items()
}
self._convert_to_datatree()
progress.close()

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 22, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.79%. Comparing base (6ed2d1c) to head (b0241be).

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #58      +/-   ##
==========================================
+ Coverage   94.63%   94.79%   +0.15%     
==========================================
  Files           3        3              
  Lines         261      288      +27     
==========================================
+ Hits          247      273      +26     
- Misses         14       15       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@aloctavodia aloctavodia merged commit 44af07f into arviz-devs:main May 26, 2026
3 of 4 checks passed
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