samples_true_posterior_linear_gaussian_mvn_prior_different_dims ignores prior correlations of the discarded dimensions

Open
#2,255 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 6 days

@clarkf19 is already working on this.

Since Nov 30, 2026.

  • #2261 by @clarkf19 — open

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
machine-learning, python

Research direction

Start with sbi/simulators/linear_gaussian.py and inspect samples_true_posterior_linear_gaussian_mvn_prior_different_dims; run the issue's minimal example to reproduce the mismatch with Gaussian conditioning. Locate the existing tests that use this function and verify that they only cover identity prior covariance. The change is done when correlated dropped dimensions match the full Gaussian posterior and a regression test covers that case.

Written by the indexing model from the issue text.

Description

bug
Bug description

While reading sbi/simulators/linear_gaussian.py, I noticed how samples_true_posterior_linear_gaussian_mvn_prior_different_dims samples the dropped dimensions: they come from the prior alone, independently of the dimensions that x sees.

This is only correct when the prior covariance is diagonal. If the prior correlates the dropped dimensions with the kept ones, the data also tells us about the dropped ones through that correlation. In the example below (prior correlation 0.9, x only sees theta[0]), the dropped dimension should have mean 1.64 and std 0.51, but the function gives mean 0 and std 1.

This function is the reference that checks whether NPE learned the right posterior, so with a correlated prior, a correct NPE would fail the check.

A fix would be to compute the full Gaussian posterior directly, as in the example (x = A theta + noise, with A = [I, 0]). Could I open a PR with this fix and a test with a correlated prior?

Minimal code example
import torch

from sbi.simulators.linear_gaussian import (
    samples_true_posterior_linear_gaussian_mvn_prior_different_dims,
)

torch.manual_seed(0)

# theta has 2 dims, x only sees theta[0]; the prior correlates theta[0] and theta[1]
prior_mean = torch.zeros(2)
prior_cov = torch.tensor([[1.0, 0.9], [0.9, 1.0]])
likelihood_shift = torch.zeros(1)
likelihood_cov = 0.1 * torch.eye(1)
x_o = torch.tensor([[2.0]])

samples = samples_true_posterior_linear_gaussian_mvn_prior_different_dims(
    x_o, likelihood_shift, likelihood_cov, prior_mean, prior_cov,
    num_discarded_dims=1, num_samples=100_000,
)

# exact posterior: Gaussian conditioning with x = A theta + noise, A = [1, 0]
A = torch.tensor([[1.0, 0.0]])
post_cov = torch.linalg.inv(torch.linalg.inv(prior_cov) + A.T @ torch.linalg.inv(likelihood_cov) @ A)
post_mean = post_cov @ (A.T @ torch.linalg.inv(likelihood_cov) @ (x_o[0] - likelihood_shift))

print("sbi   mean:", samples.mean(0).numpy().round(3), " std:", samples.std(0).numpy().round(3))
print("exact mean:", post_mean.numpy().round(3), " std:", post_cov.diag().sqrt().numpy().round(3))
Error message or traceback
No error, but the samples are wrong. Output:
sbi   mean: [ 1.817 -0.005]  std: [0.302 1.001]
exact mean: [1.818 1.636]  std: [0.302 0.513]
sbi version

0.27.0 (main, 54c18ee)

Python version

3.12.16

Device

CPU

Inference method

Not method-specific

Single-round or multi-round

Not applicable

Additional context

I ran this on WSL Ubuntu with sbi installed from source. The tests that use this function have an identity prior covariance, so they don't catch it.

Dominant language
Python
Stars
894
Forks
306
Avg merge
5d 21h
Merged PRs (30d)
61

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from sbi-dev/sbi

All issues in sbi-dev/sbi

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.