How to run sequential methods

Navigation

How to run sequential methods#

In the previous tutorials, we have inferred the posterior using amortized inference. In amortized inference, we draw parameters from the prior, simulate the corresponding data, and then train a neural network to obtain the posterior. However, if one is interested in only one particular observation x_o sampling from the prior can be inefficient in the number of simulations because one is effectively learning a posterior estimate for all observations in the prior space. In this tutorial, we show how one can alleviate this issue by using sequential methods with sbi.

Sequential methods also starts by drawing parameters from the prior, simulating them, and training a neural network to estimate the posterior distribution. Afterwards, however, it continues inference in multiple rounds, focusing on a particular observation x_o. In each new round of inference, it draws samples from the obtained posterior distribution conditioned at x_o (instead of from the prior), simulates these, and trains the network again. This process can be repeated arbitrarily often to get increasingly good approximations to the true posterior distribution at x_o.

Running multi-round inference can be more efficient in the number of simulations, but it will lead to the posterior no longer being amortized (i.e. it will be accurate only for a specific observation x_o, not for any x).

Main syntax#

inference = NPE(prior)
proposal = prior

for _ in range(num_rounds):
    theta = proposal.sample((100,))
    x = simulate(theta)

    # In `SNLE` and `SNRE`, you should not pass the `proposal` to `.append_simulations()`.
    density_estimator = inference.append_simulations(
        theta, x, proposal=proposal
    ).train()
    posterior = inference.build_posterior(density_estimator)
    proposal = posterior.set_default_x(x_o)

Example#

You can find an example and more explanation in the tutorial here.