Skip to content

Documented init_method "prior" is not implemented in posterior.map() #2092

Closed

Description

@SahilKumar75

Bug description

The map() method documents init_method as one of posterior or prior, but the value prior is never handled.

In _calculate_map (sbi/inference/posteriors/base_posterior.py, lines 276 to 283), only three cases are covered:

  1. init_method == "posterior"
  2. init_method == "proposal"
  3. init_method is a Tensor

Any other value, including the documented prior, falls through to a bare raise ValueError on line 283 that carries no message.

As a result, the documented call post.map(init_method="prior") fails on every posterior with an empty ValueError. A search for init_method == "prior" across the package returns no matches, confirming the branch does not exist.

Minimal code example

import torch
from sbi.inference import NPE
from sbi.utils import BoxUniform

prior = BoxUniform(low=torch.zeros(2), high=torch.ones(2))
theta = prior.sample((200,))
x = theta + 0.05 * torch.randn_like(theta)

npe = NPE(prior=prior).append_simulations(theta, x)
npe.train(max_num_epochs=2)
post = npe.build_posterior().set_default_x(torch.zeros(2))

post.map(init_method="prior")  # documented value, but raises

Error message or traceback

ValueError

sbi version

0.27.0

Python version

3.12

Device

CPU

Inference method

Not method-specific

Single-round or multi-round

Not applicable

Additional context

Initialising the MAP optimisation from prior samples is useful when the posterior is leaky or slow to sample from, so the documented behaviour is worth supporting.

I would be glad to open a pull request that adds the prior branch (drawing the initial samples from self.prior), corrects the docstrings, and gives the two bare ValueError raises informative messages.

Activity

  1. added
    bug
    on Oct 25, 2026
  2. alibb007 commented on Oct 26, 2026

    @alibb007

    The prior branch was missing from _calculate_map in base_posterior.py. So init_method="prior" fell through to the bare raise ValueError at the end. This change adds the branch. It draws starting points from self.potential_fn.prior. The ValueError now lists the valid values. Not tested: no existing test covers this code path. A pull request follows.

  3. SahilKumar75 commented on Oct 26, 2026

    @SahilKumar75
    ContributorAuthor

    Hey, thanks for picking this up, @alibb007.

    I mentioned back in #2092 that I'd open a PR for this and had one ready to go. Mine also covers a couple of things this one doesn't:

    • EnsemblePosterior.map, which is missing the same branch
    • tests for init_method="prior" and for the invalid case

    I'm happy to push mine or just review this one, whatever works best for you, @janfb and @michaeldeistler.

    Just flagging two gaps in case this PR moves ahead:

    1. EnsemblePosterior.map still raises an error.
    2. There's no test yet for the new path.
  4. janfb commented on Oct 27, 2026

    @janfb
    Contributor

    @alibb007 thanks for the work on this. @SahilKumar75 reported the bug in issue #2092 and offered the PR there, before this PR was opened. Please follow out Code of Conduct and our contribution guideline, e.g., in this case: respect each others work and wait for maintainer reply under an issue before opening a PR.

    I will close PR #2093 and leave the fix to SahilKumar75.

    cheers, Jan

  5. janfb commented on Oct 27, 2026

    @janfb
    Contributor

    @SahilKumar75 thanks for the clear report, and for the friendly note on the overlap. I closed PR #2093, so please go ahead with your PR.

    I checked the history: "prior" is a stale docstring. In 2022 we renamed the option to "proposal" in the code and missed the docstrings. For MCMC, rejection and importance sampling posteriors, the proposal is the prior unless the user passes another one, so init_method="proposal" already does what the docstring promises. So we don't need a new "prior" option.

    Please fix the docstrings of all map() methods, so each lists the options that posterior supports, and replace the bare errors with messages that list them. The direct, vector field and ensemble posteriors have no proposal, so "proposal" should fail there with such a message as well. Starting points from the prior still work everywhere by passing prior samples as a tensor; the docstring can say that.

    cheers, Jan

  6. SahilKumar75 commented on Oct 27, 2026

    @SahilKumar75
    ContributorAuthor

    Went with your approach, no new "prior" option: fixed each posterior's docstring to match what it supports, made "proposal" error clearly where there's no proposal (direct, vector field, ensemble), and noted prior samples work as a tensor. Tests added.

  7. added a commit that references this issue on Oct 29, 2026
    a36c5f5
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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions