Repository navigation
Description
Currently, sbi provides multiple ways to associate an observation x_o with a potential function:
- Through the potential's constructor: Potential(prior, x_o=...)
- Through the new bind() method: potential.bind(x_o)
- 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:
-
Create potential WITHOUT x_o
potential = PosteriorBasedPotential(estimator, prior) -
Bind observation when needed
bound_potential = potential.bind(x_o) -
Use the bound potential
log_prob = bound_potential(theta)
For posteriors:
-
Create posterior
posterior = inference.build_posterior() -
Set default observation (user-facing convenience)
posterior.set_default_x(x_o) -
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:
- PR Add .bind() method to potentials #1943: Introduced bind() API
- PR Separate iid from set x #1945: Decoupled bind() from set_x()
- PR Test set x deprecation #1946: Deprecated set_x(), delegates to bind()
- PR refactor: port the Potentials bind() API from gsoc-2026 (#1937, #1943, #1945, #1946, #1948) #1989: Ported bind() API to main
Activity
mayanksingh-27 commentedon Sep 24, 2026
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:
Deprecate x_o in every potential constructor. Provide a warning and migration guidance while keeping the current behavior during the deprecation period.
Make bind(x_o) the internal and documented mechanism for attaching observations to potentials.
Audit the implementations. Remove any branches that only handle an already‑bound x_o if this can be done without breaking compatibility.
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.
Update all internal call sites so they use the binding contract and avoid duplicate state‑management logic.
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.
Add tests for defaults including sampling and log_prob to confirm that set_default_x() remains correct after the internal migration.
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?
janfb commentedon Sep 30, 2026
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
Jocho-Smith commentedon Nov 4, 2026
Hey @janfb can I unblock you here before 'disappearing' for a while?
janfb commentedon Nov 25, 2026
@Jocho-Smith thanks for the offer, and sorry for the slow reply! Your
bind()work is onmainnow, and we will finish the rest of this issue ourselves, so you can go without worrying about it.The plan: passing
x_oto a potential's constructor gets a deprecation warning in the next release (0.28) and goes away in 0.29, sobind(x_o)becomes the only way to attach an observation. As discussed in July, posteriors will also stop taking their defaultxfrom 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.
deprecation: attach observations to potentials only with bind() (#2248)