ImportanceSamplingPosterior#
- class ImportanceSamplingPosterior(potential_fn, proposal, theta_transform=None, method='sir', oversampling_factor=32, max_sampling_batch_size=10000, device=None, x_shape=None)[source]#
Bases:
NeuralPosteriorProvides importance sampling to sample from the posterior.
SNLE or SNRE train neural networks to approximate the likelihood(-ratios). ImportanceSamplingPosterior allows to estimate the posterior log-probability by estimating the normlalization constant with importance sampling. It also allows to perform importance sampling (with .sample()) and to draw approximate samples with sampling-importance-resampling (SIR) (with .sir_sample())
- Parameters:
- to(device)[source]#
Move the potential, the proposal and x_o to a new device.
It also reinstantiates the posterior with the new device.
- log_prob(theta, x=None, track_gradients=False, normalization_constant_params=None)[source]#
Returns the log-probability of theta under the posterior.
The normalization constant is estimated with importance sampling.
- Parameters:
theta (Tensor) – Parameters \(\theta\).
track_gradients (bool) – Whether the returned tensor supports tracking gradients. This can be helpful for e.g. sensitivity analysis, but increases memory consumption.
normalization_constant_params (dict | None) – Parameters passed on to estimate_normalization_constant().
x (Tensor | None)
- Returns:
len(\(\theta\))-shaped log-probability.
- Return type:
- estimate_normalization_constant(x, num_samples=10000, force_update=False)[source]#
Returns the normalization constant via importance sampling.
- sample(sample_shape=(), x=None, method=None, oversampling_factor=32, max_sampling_batch_size=10000, show_progress_bars=False)[source]#
Draw samples from the approximate posterior distribution \(p( heta|x)\).
- Parameters:
sample_shape (Size | Tuple[int, ...]) – Shape of samples that are drawn from posterior.
x (Tensor | None) – Conditioning observation \(x_o\). If not provided, uses the default x set via .set_default_x().
method (str | None) – Either of [sir`|`importance]. This sets the behavior of the .sample() method. With sir, approximate posterior samples are generated with sampling importance resampling (SIR). With importance, the .sample() method returns a tuple of samples and corresponding importance weights.
oversampling_factor (int) – Number of proposed samples from which only one is selected based on its importance weight.
max_sampling_batch_size (int) – The batch size of samples being drawn from the proposal at every iteration.
show_progress_bars (bool) – Whether to show a progressbar during sampling.
- Return type:
- sample_batched(sample_shape, x, max_sampling_batch_size=10000, show_progress_bars=True)[source]#
Draw samples from the posteriors for a batch of different xs.
Given a batch of observations [x_1, …, x_B], this method samples from posteriors \(p(\theta|x_1), \ldots, p(\theta|x_B)\) in a vectorized manner.
- Parameters:
sample_shape (Size | Tuple[int, ...]) – Shape of samples to draw for each observation.
x (Tensor) – Batch of observations with shape (batch_dim, *event_shape_x).
show_progress_bars (bool) – Whether to show a progress bar during sampling.
**kwargs – Additional keyword arguments passed to the specific posterior’s sampling method.
max_sampling_batch_size (int)
- Returns:
Samples with shape (*sample_shape, batch_dim, *theta_shape).
- Return type:
- map(x=None, num_iter=1000, num_to_optimize=100, learning_rate=0.01, init_method='proposal', num_init_samples=1000, save_best_every=10, show_progress_bars=False, force_update=False)[source]#
Returns the maximum-a-posteriori estimate (MAP).
The method can be interrupted (Ctrl-C) when the user sees that the log-probability converges. The best estimate will be saved in self._map and can be accessed with self.map(). The MAP is obtained by running gradient ascent from a given number of starting positions (samples from the posterior with the highest log-probability). After the optimization is done, we select the parameter set that has the highest log-probability after the optimization.
Warning: The default values used by this function are not well-tested. They might require hand-tuning for the problem at hand.
For developers: if the prior is a BoxUniform, we carry out the optimization in unbounded space and transform the result back into bounded space.
- Parameters:
x (Tensor | None) – Deprecated - use .set_default_x() prior to .map().
num_iter (int) – Number of optimization steps that the algorithm takes to find the MAP.
learning_rate (float) – Learning rate of the optimizer.
init_method (str | Tensor) – How to select the starting parameters for the optimization. If it is a string, it can be either [posterior, prior], which samples the respective distribution num_init_samples times. If it is a tensor, the tensor will be used as init locations.
num_init_samples (int) – Draw this number of samples from the posterior and evaluate the log-probability of all of them.
num_to_optimize (int) – From the drawn num_init_samples, use the num_to_optimize with highest log-probability as the initial points for the optimization.
save_best_every (int) – The best log-probability is computed, saved in the map-attribute, and printed every save_best_every-th iteration. Computing the best log-probability creates a significant overhead (thus, the default is 10.)
show_progress_bars (bool) – Whether to show a progressbar during sampling from the posterior.
force_update (bool) – Whether to re-calculate the MAP when x is unchanged and have a cached value.
log_prob_kwargs – Will be empty for SNLE and SNRE. Will contain {‘norm_posterior’: True} for SNPE.
- Returns:
The MAP estimate.
- Return type:
- property default_x: Tensor | None#
Return default x used by .sample(), .log_prob as conditioning context.
- potential(theta, x=None, track_gradients=False)#
Evaluates \(\theta\) under the potential that is used to sample the posterior.
The potential is the unnormalized log-probability of \(\theta\) under the posterior.
- set_default_x(x)#
Set new default x for .sample(), .log_prob to use as conditioning context.
Reset the MAP stored for the old default x if applicable.
This is a pure convenience to avoid having to repeatedly specify x in calls to .sample() and .log_prob() - only $ heta$ needs to be passed.
This convenience is particularly useful when the posterior is focused, i.e. has been trained over multiple rounds to be accurate in the vicinity of a particular x=x_o (you can check if your posterior object is focused by printing it).
NOTE: this method is chainable, i.e. will return the NeuralPosterior object so that calls like posterior.set_default_x(my_x).sample(mytheta) are possible.
- Parameters:
x (Tensor) – The default observation to set for the posterior \(p( heta|x)\).
- Returns:
NeuralPosterior that will use a default x when not explicitly passed.
- Return type:
NeuralPosterior