samples_true_posterior_linear_gaussian_mvn_prior_different_dims ignores prior correlations of the discarded dimensions
Maintainers usually reply within 6 days
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
- Domain
- machine-learning
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 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
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from sbi-dev/sbi
-
simulate_for_sbi(num_simulations=0) returns 1D empty theta/x tensors and breaks append_simulationsOpenbug
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
sbi-dev/sbi#2310 · 3 comments ·
Maintainers usually reply within 6 days
-
bug: RestrictionEstimator.train(subsample_invalid_sims="auto") raises TypeErrorPossibly taken @Nitesh780 claimed this 2 days ago. Openbug
Difficulty 1/5 1-3 hours Newbie friendliness 85/100
sbi-dev/sbi#2309 · 1 comment ·
Maintainers usually reply within 6 days
-
TARP reference sampling fails when a parameter coordinate is fixedPossibly taken @Jayant-kernel claimed this 4 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 77/100
sbi-dev/sbi#2264 · 2 comments ·
Maintainers usually reply within 6 days
-
fix: broadcast UniformNormalConvolution batch shapePossibly taken @studentarijitpaul claimed this 32 days ago. Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
sbi-dev/sbi#2151 · 2 comments ·
Maintainers usually reply within 6 days
-
bug
sbi-dev/sbi#2321 · 1 comment ·
Maintainers usually reply within 6 days
Similar issues
-
docs(contributing): the two things that silently block a PRPossibly taken A pull request linked to this issue is open or already merged. Openavailable documentation good first issue
Difficulty 1/5 1-3 hours Newbie friendliness 95/100
Nixitov/Riptide-Native#944 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
JTHCode/secretguard#24 ·
Maintainers usually reply within 1 day
-
area: middleware documentation good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
enhancement good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
Maintainers usually reply within 1 day
-
good first issue
Difficulty 1/5 Under an hour Newbie friendliness 95/100
omarelshamy25x/shamy-hijri#63 ·
Maintainers usually reply within 1 day