How to sample the posterior given iid observations (for NLE and NRE)

How to sample the posterior given iid observations (for NLE and NRE)#

Important: This how-to guide is only relevant if you have iid observations and you are using NLE or NRE. You can use NPE with iid observations directly by passing sample_with="mcmc" or sample_with="vi" to build_posterior. If you want to use the default direct sampling (sample_with="direct") for NPE with iid data, you still need to construct a permutation-invariant embedding net, which is explained in this how-to guide.

In many cases, you want to estimate a parameter set given multiple observations. NLE and NRE can naturally deal with this scenario. Both of these methods can be trained on single observations (i.e., just one simulation per parameter set), and can then be used to sample the posterior given an arbitrary number of observations. In sbi, this can be done as shown below:

# Generate training dataset.
theta = prior.sample((100,))  # Example shape: (100, 3)
x = simulate(theta)  # Example shape: (100, 4)

# Train NLE or NRE network.
trainer = NLE(prior=prior)
trainer.append_simulations(theta, x).train()
posterior = trainer.build_posterior()

x_o_iid = torch.ones((20, 4))  # 20 iid observations.
samples = posterior.sample((1_000,), x=x_o_iid)

Example and further explanation#

For more details, see this tutorial.