Skip to content

Add IID x support to PosteriorBasedPotential - #1810

Merged
manuelgloeckler merged 11 commits into
sbi-dev:mainfrom
satwiksps:npe_iid_support
Mar 13, 2026
Merged

manuelgloeckler merged 11 commits into
sbi-dev:mainfrom
satwiksps:npe_iid_support

Conversation

@satwiksps

Copy link
Copy Markdown
Collaborator

Related Issues

Summary

This PR adds support for multiple Independent and Identically Distributed (IID) observations to PosteriorBasedPotential. This unlocks the ability to use methods like MCMC, Variational Inference (VI), or importance sampling (sample_with="mcmc", etc.) for Neural Posterior Estimation (NPE) when dealing with IID datasets.

Changes

  • Unblocked set_x: Removed the NotImplementedError that previously blocked IID data. The method now safely passes the x_is_iid flag down to BasePotential.
  • Updated __call__ logic: Added an IID branch that evaluates the log-probability for each individual observation, sums them up, and subtracts the expanded prior.
  • Safe Prior Checking: Added a specific check to raise a ValueError if prior is missing or lacks a log_prob method, but only inside the IID branch.
  • New Unit Tests: Added test_posterior_based_potential_iid_log_prob to tests/posterior_nn_test.py. It parameterizes over iid_batch_size = [1, 2, 5] and compares our new potential calculation directly against an analytically exact Linear Gaussian ground truth to ensure the tensor math is okay.

@codecov

codecov Bot commented Mar 12, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 57.14286% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.80%. Comparing base (bcedb90) to head (8cecefe).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
.../inference/potentials/posterior_based_potential.py 57.14% 9 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1810      +/-   ##
==========================================
- Coverage   87.85%   87.80%   -0.06%     
==========================================
  Files         140      140              
  Lines       12716    12724       +8     
==========================================
  Hits        11172    11172              
- Misses       1544     1552       +8     
Flag Coverage Δ
fast 82.73% <57.14%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
.../inference/potentials/posterior_based_potential.py 76.36% <57.14%> (-13.00%) ⬇️

@manuelgloeckler manuelgloeckler left a comment •

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.

Thanks for the contribution! This looks great already.

I think we can make this a bit more efficient by using log_prob_batched which would vectorize the N evaluations.

Other than that a few more test cases should be considered and this would in addition require updating some documentation files i.e. here. We should mention that this is now also possible with NPE (when using e.g. sample_with="mcmc").

Comment thread tests/posterior_nn_test.py Resolved

@manuelgloeckler manuelgloeckler left a comment

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.

Great! Thanks for implementing this so quickly :)

@manuelgloeckler
manuelgloeckler merged commit 0a70cdf into sbi-dev:main Mar 13, 2026
9 checks passed
@satwiksps
satwiksps deleted the npe_iid_support branch April 25, 2026 03:30
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.

posterior_based_potential should support IID x.

2 participants