Skip to content

NPE potentials mishandle iid observations: wrong trial count and batch size errors #2125

Closed

Description

@janfb

Bug description

NPE can use several iid observations through its potential: with x_is_iid=True, it sums the log posteriors of the trials and corrects for the prior. Three things go wrong around this.

  1. The potential counts the trials by the first dimension of x, not against the condition shape of the estimator. For a 2x2 observation passed without batch dimension, it counts two trials. MCMC binds observations as iid, so it then samples q(θ|x_o)² / p(θ) instead of q(θ|x_o), without an error.
  2. map() binds x without x_is_iid, and the NPE potential defaults to a batch. So map() of an MCMC posterior on an NPE potential fails with a batch size assertion, while sample() of the same posterior works. With init_method="posterior" it works by accident, because sample() rebinds the potential as iid before the gradient ascent. The joint map() of an ensemble of such posteriors fails the same way.
  3. The rejection and importance sampling posteriors bind without x_is_iid too, so their sample() and log_prob() fail with the same assertion.

Minimal code example

posterior = npe.build_posterior(sample_with="mcmc")
posterior.set_default_x(x_iid)  # two iid observations
posterior.sample((1000,))  # works
posterior.map()  # AssertionError; sample_with="rejection" or "importance" fails in sample()

Error message or traceback

AssertionError: Batch size mismatch: 1000 and 2. When performing batched sampling for multiple `x`, the batch size of `theta` must match the batch size of `x`.

Proposed fix

I think the potential should count the trials against the condition shape of the estimator, in one place. Then map(), MCMC, rejection and importance sampling can all bind several observations as iid trials. I tried the map() part in PR #2106 and took it out again, because it needs that count first.

sbi version

main (2026-10-29)

Activity

  1. added
    bug
    on Oct 29, 2026
  2. aniketpandey05 commented on Oct 30, 2026

    @aniketpandey05
    Contributor

    @janfb if not assigned may i pick it up?

  3. self-assigned this
    on Oct 31, 2026
  4. janfb commented on Oct 31, 2026

    @janfb
    ContributorAuthor

    @aniketpandey05 thanks for the offer. This goes together with issue #2122 into the posterior refactoring we do internally, so it is not open for a PR. Your other PRs are welcome: I will review PR #2128 and PR #2129 after November 9.

    Thanks, Jan

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

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions