Skip to content

Simplify x_o workflow: enforce bind() as the canonical way to associate observations with potentials #1994

Open

Description

@Jocho-Smith

Currently, sbi provides multiple ways to associate an observation x_o with a potential function:

  1. Through the potential's constructor: Potential(prior, x_o=...)
  2. Through the new bind() method: potential.bind(x_o)
  3. Through the posterior's set_default_x(): posterior.set_default_x(x_o)

This redundancy can confuse users and creates maintenance burden. The bind() API was introduced specifically to provide a clean, immutable, stateless way to associate observations with potentials. However, the old constructor-based approach still works, and the relationship between set_default_x() (posterior level) and bind() (potential level) is unclear.

The problem at the potential level:

Users can set x_o in three ways:

Way 1: Constructor
potential = PosteriorBasedPotential(estimator, prior, x_o=obs)

Way 2: bind() - the new canonical way
potential = PosteriorBasedPotential(estimator, prior, x_o=None)
bound_potential = potential.bind(obs)

The constructor approach means potentials are created in a "half-configured" state. Many places in the codebase check "if x_o is not None" to decide whether to build flows, set defaults, etc. This conditional logic adds complexity and is a common source of bugs.

The problem at the posterior level:

The set_default_x() method exists on posteriors, but it's not clear how it relates to potential-level binding. Users don't understand when to use set_default_x vs bind(), and the internal implementation has to coordinate between them.

Proposed solution:

Step 1: Deprecate x_o parameter in potential constructors

Deprecate the x_o parameter in all potential class constructors (Note that the code internally sets x_o=None already anyway). This affects:

  • BasePotential.init
  • PosteriorBasedPotential.init
  • LikelihoodBasedPotential.init
  • RatioBasedPotential.init
  • VectorFieldBasedPotential.init
  • EnsemblePotential.init

Step 2: Simplify constructor logic

After deprecation, constructors can assume x_o is None. No conditional flow building - bind() handles this as a second step

Step 3: Document the canonical workflow

The canonical workflow should be:

  1. Create potential WITHOUT x_o
    potential = PosteriorBasedPotential(estimator, prior)

  2. Bind observation when needed
    bound_potential = potential.bind(x_o)

  3. Use the bound potential
    log_prob = bound_potential(theta)

For posteriors:

  1. Create posterior
    posterior = inference.build_posterior()

  2. Set default observation (user-facing convenience)
    posterior.set_default_x(x_o)

  3. Sample/log_prob use the default
    samples = posterior.sample((1000,)) # uses x_o set via set_default_x

Step 4: Clarify set_default_x relationship

Document that set_default_x() is the user-facing API for posteriors, and internally it uses bind() on the underlying potential.

Related PRs:

Activity

  1. mayanksingh-27 commented on Sep 24, 2026

    @mayanksingh-27

    Hi I looked at the observation‑binding flow and found the overlap between potential constructors, bind() and posterior.set_default_x().

    I would like to implement this as one migration PR instead of changing isolated call sites. The goal is to make the potential lifecycle explicit and consistent: first create an unbound potential then bind an observation with bind(x_o) and finally use the resulting bound potential.

    The PR would:

    1. Deprecate x_o in every potential constructor. Provide a warning and migration guidance while keeping the current behavior during the deprecation period.

    2. Make bind(x_o) the internal and documented mechanism for attaching observations to potentials.

    3. Audit the implementations. Remove any branches that only handle an already‑bound x_o if this can be done without breaking compatibility.

    4. Keep posterior.set_default_x(x_o) as the user‑facing posterior API. Ensure that its implementation follows the binding path instead of keeping a separate observation workflow.

    5. Update all internal call sites so they use the binding contract and avoid duplicate state‑management logic.

    6. Add regression tests for both paths. The legacy constructor‑level x_o must continue to work with a deprecation warning. The bind(x_o) path must produce the numerical behavior.

    7. Add tests for defaults including sampling and log_prob to confirm that set_default_x() remains correct after the internal migration.

    8. Update API documentation, examples and deprecation messages with before/after migration snippets.

    I will keep the scope limited to the observation‑binding migration itself. I will not change device handling, constructors outside this workflow or broader posterior refactors.

    Before I begin does this match the intended direction, for making bind() the API?

  2. janfb commented on Sep 30, 2026

    @janfb
    Contributor

    Thanks for the detailed plan, @mayanksingh-27, it matches the direction we have in mind.

    However, this is already in progress internally as part of the potentials and bind() work with @Jocho-Smith, and it touches many parts of the posterior API at once. So we will keep it in that stream and not take an external PR for it.

    Cheers, Jan

  3. self-assigned this
    on Sep 30, 2026
  4. Jocho-Smith commented on Nov 4, 2026

    @Jocho-Smith
    ContributorAuthor

    Hey @janfb can I unblock you here before 'disappearing' for a while?

  5. janfb commented on Nov 25, 2026

    @janfb
    Contributor

    @Jocho-Smith thanks for the offer, and sorry for the slow reply! Your bind() work is on main now, and we will finish the rest of this issue ourselves, so you can go without worrying about it.

    The plan: passing x_o to a potential's constructor gets a deprecation warning in the next release (0.28) and goes away in 0.29, so bind(x_o) becomes the only way to attach an observation. As discussed in July, posteriors will also stop taking their default x from a bound potential; set_default_x() stays the way to set it.

    I will tag you on the PR. A review would be great if you find the time, but no pressure.

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions