How to refine posterior estimates with importance sampling#
The sbi toolbox does not require that the simulator can evaluate the likelihood (it only requires samples). If the likelihood can be evaluated, then one can refine the posterior estimate after training with likelihood evaluations. This is typically done with importance sampling (using the posterior estimate as proposal).
In sbi, this can be implemented as follows:
Main syntax#
from sbi.inference import ImportanceSamplingPosterior
log_prob_fn = lambda theta, x_o: simulator.log_likelihood(theta, x_o) + prior.log_prob(theta)
# Obtian posterior with NPE, NLE, or NRE.
posterior_estimate = DirectPosterior(posterior_net, prior).set_default_x(x_o)
# Importance sampling for refining the posterior_estimate.
posterior_sir = ImportanceSamplingPosterior(
potential_fn=log_prob_fn,
proposal=posterior_estimate,
method="sir",
)
theta_inferred_sir = posterior_sir.sample(
(1000,),
oversampling_factor=32,
)
Example#
More details can be found in the tutorial on importance sampling for refining the posterior estimate.