sbi icon indicating copy to clipboard operation
sbi copied to clipboard

Improper convergence for a multidimensional simulator for SNRE and SNLE

Open paarth-dudani opened this issue 1 year ago • 0 comments

Describe the bug I don't get a proper convergence with SNLE and SNRE for a multidimensional prior and simulatoe with noise.

To Reproduce

  1. Versions Python version: 3.9.13 SBI version: 0.23.1

  2. Minimal code example

scale_true = torch.tensor([[2.0, 1.5, 1.0, 2.0]])

# Define the prior
num_dim = 4
num_sim = 1000
num_rounds = 2
initial_cond = torch.tensor([1.0, 1.0, 2.0,1.2])


n = 1000
t = np.linspace(0,10,n)

# prior

uni_prior_2D = BoxUniform(low=0* torch.ones(num_dim), high=3* torch.ones(num_dim))

# Simulators

def simulator_exp(input, theta, num_sim, n_dim):
    x = np.zeros((n_dim, num_sim, len(input)))
    for i in range(n_dim):
            x[i] = (1/initial_cond[i])*torch.exp(-input/theta[:,i].reshape(-1,1)) + 0.5*torch.rand_like(theta[:,i]).reshape(-1,1)
    return torch.tensor(x).reshape(num_sim, n_dim, len(input)).float()

x_o = simulator_exp(t, scale_true, 1, num_dim)
x_o = x_o.reshape(1, num_dim*len(t))


inference = NLE(uni_prior_2D)
proposal = uni_prior_2D
for _ in range(num_rounds):
    theta = proposal.sample((num_sim,))
    x = simulator_exp(t, theta, num_sim, num_dim)  # Simulate data
    x = x.reshape(num_sim, num_dim*len(t))
    x = np.clip(x, 0, 8)
    _ = inference.append_simulations(theta, x).train()
    posterior = inference.build_posterior(mcmc_method="slice_np_vectorized",
                                          mcmc_parameters={"num_chains": 5,
                                                           "thin": 5})
    proposal = posterior.set_default_x(x_o)

All else same for SNLE.

  1. Error: None. Just poor convergence.

SNRE:

Screenshot 2024-10-07 at 04 26 37 Screenshot 2024-10-07 at 03 57 11

SNLE: Screenshot 2024-10-07 at 13 00 25

Expected behavior A sharper and proper convergence of the posterior

paarth-dudani avatar Oct 07 '24 12:10 paarth-dudani