Skip to content

Make the proposal argument types explicit #2011

Closed

Description

@vitorbborges

What problem does this solve?

While reading the MCMC code I kept hitting proposal: Any, first in
mcmc_posterior.py:50.
It took me a while to figure out what a proposal has to implement, and the
answer depends on where it is used:

The trainer side already writes the contract down as Optional[Union[Distribution, NeuralPosterior]] (_contracts.py:176, :202).

I would like to add a Proposal Protocol in sbi/sbi_types.py, next to SampleProposal and CustomPrior, that captures sample() and log_prob(), and use it for the sampler functions, with the existing union in check_if_proposal_has_default_x. CustomPrior already describes those two methods, so reusing or renaming it is fine with me if you prefer not to add another type.

One thing I could not decide on my own: MCMCPosterior.to() needs to(), which neither base class declares. Should the protocol include to, should NeuralPosterior declare to and log_prob, or should those calls stay as they are?

The pyright config disables the argument and attribute checks, so I am not proposing this for CI coverage. I just think the signatures should tell a reader what a proposal is. No runtime behavior changes.

Proposed solution

I would like to add a Proposal Protocol in sbi/sbi_types.py, next to SampleProposal and CustomPrior, that captures sample() and log_prob(), and use it for the sampler functions, with the existing union in check_if_proposal_has_default_x. CustomPrior already describes those two methods, so reusing or renaming it is fine with me if you prefer not to add another type.

class Proposal(Protocol):
    """Anything that can be drawn from and scored, e.g. a prior or a posterior."""

    def sample(self, sample_shape, /) -> Tensor: ...
    def log_prob(self, value, /) -> Tensor: ...

The parameters are positional-only so that both NeuralPosterior.log_prob(theta) and Distribution.log_prob(value) satisfy the protocol.
One thing I could not decide on my own: MCMCPosterior.to() needs to(), which neither base class declares. Should the protocol include to, should NeuralPosterior declare to and log_prob, or should those calls stay as they are?

This is type annotations only. No runtime behavior changes.

Alternatives you considered

No response

Additional context

No response

Contribution

  • [ x ] I would like to work on this myself.

Activity

  1. added
    feature request
    on Sep 15, 2026
  2. CaptainAni187 commented on Sep 22, 2026

    @CaptainAni187

    Sorry, I missed that you ticked that you'd like to work on this yourself. It's all yours, ignore my comment.

  3. vitorbborges commented on Sep 22, 2026

    @vitorbborges
    ContributorAuthor

    Thanks @CaptainAni187! I will keep to out of the Proposal protocol (torch Distributions do not declare it) and leave the existing .to(device) calls untouched — PR coming shortly.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions