RestrictedPrior#

class RestrictedPrior(prior, accept_reject_fn, posterior=None, sample_with='rejection', device='cpu')[source]#

Bases: Distribution

Distribution that restricts the prior distribution to a smaller region.

Parameters:
sample(sample_shape=(), sample_with=None, max_sampling_batch_size=10000, oversampling_factor=1024, save_acceptance_rate=False, show_progress_bars=False, print_rejected_frac=True)[source]#

Return samples from the RestrictedPrior.

Samples are obtained by sampling from the prior, evaluating them under the trained classifier (RestrictionEstimator) and using only those that were accepted.

Parameters:
  • sample_shape (Size | Tuple[int, ...]) – Shape of the returned samples.

  • sample_with (str | None) – Either of [rejection`|`sir]. Sets that method that is used to sample from the restricted prior. If sir, youu must have passed a posterior at initialization.

  • max_sampling_batch_size (int) – Batch size for drawing samples from the posterior. Takes effect only in the second iteration of the loop below, i.e., in case of leakage or num_samples>max_sampling_batch_size. Larger batch size speeds up sampling.

  • oversampling_factor (int) – Number of proposed samples for sir from which only one is selected based on its importance weight.

  • save_acceptance_rate (bool) – If True, the acceptance rate is saved and such that it can potentially be used later in log_prob().

  • show_progress_bars (bool) – Whether to show a progressbar during sampling.

  • print_rejected_frac (bool) – Whether to print the rejection rate of the restriction estimator during sampling.

Returns:

Samples from the RestrictedPrior.

Return type:

Tensor

arg_constraints = {}#
property batch_shape: Size#

Returns the shape over which parameters are batched.

cdf(value)#

Returns the cumulative density/mass function evaluated at value.

Parameters:

value (Tensor)

Return type:

Tensor

entropy()#

Returns entropy of distribution, batched over batch_shape.

Returns:

Tensor of shape batch_shape.

Return type:

Tensor

enumerate_support(expand=True)#

Returns tensor containing all values supported by a discrete distribution. The result will enumerate over dimension 0, so the shape of the result will be (cardinality,) + batch_shape + event_shape (where event_shape = () for univariate distributions).

Note that this enumerates over all batched tensors in lock-step [[0, 0], [1, 1], …]. With expand=False, enumeration happens along dim 0, but with the remaining batch dimensions being singleton dimensions, [[0], [1], ...

To iterate over the full Cartesian product use itertools.product(m.enumerate_support()).

Parameters:

expand (bool) – whether to expand the support over the batch dims to match the distribution’s batch_shape.

Returns:

Tensor iterating over dimension 0.

Return type:

Tensor

property event_shape: Size#

Returns the shape of a single sample (without batching).

expand(batch_shape, _instance=None)#

Returns a new distribution instance (or populates an existing instance provided by a derived class) with batch dimensions expanded to batch_shape. This method calls expand on the distribution’s parameters. As such, this does not allocate new memory for the expanded distribution instance. Additionally, this does not repeat any args checking or parameter broadcasting in __init__.py, when an instance is first created.

Parameters:
  • batch_shape (torch.Size) – the desired expanded size.

  • _instance – new instance provided by subclasses that need to override .expand.

Returns:

New distribution instance with batch dimensions expanded to batch_size.

has_enumerate_support = False#
has_rsample = False#
icdf(value)#

Returns the inverse cumulative density/mass function evaluated at value.

Parameters:

value (Tensor)

Return type:

Tensor

log_prob(theta, norm_restricted_prior=True, track_gradients=False, prior_acceptance_params=None)[source]#

Returns the log-probability of the restricted prior.

Parameters:
  • theta (Tensor) – Parameters \(\theta\).

  • norm_restricted_prior (bool) – Whether to enforce a normalized restricted prior density. The normalizing factor is calculated via rejection sampling, so if you need speedier but unnormalized log probability estimates set here norm_restricted_prior=False. The returned log probability is set to -∞ outside of the restriceted prior support regardless of this setting.

  • track_gradients (bool) – Whether the returned tensor supports tracking gradients. This can be helpful for e.g. sensitivity analysis, but increases memory consumption.

  • prior_acceptance_params (dict | None) – A dict of keyword arguments to override the default values of prior_acceptance(). Possible options are: num_rejection_samples, force_update, show_progress_bars, and rejection_sampling_batch_size. These parameters only have an effect if norm_restricted_prior=True.

Returns:

(len(θ),)-shaped log probability for θ in the support of the restricted prior, -∞ (corresponding to 0 probability) outside.

Return type:

Tensor

property mode: Tensor#

Returns the mode of the distribution.

perplexity()#

Returns perplexity of distribution, batched over batch_shape.

Returns:

Tensor of shape batch_shape.

Return type:

Tensor

rsample(sample_shape=())#

Generates a sample_shape shaped reparameterized sample or sample_shape shaped batch of reparameterized samples if the distribution parameters are batched.

Parameters:

sample_shape (Size | list[int] | tuple[int, ...])

Return type:

Tensor

sample_n(n)#

Generates n samples or n batches of samples if the distribution parameters are batched.

Parameters:

n (int)

Return type:

Tensor

static set_default_validate_args(value)#

Sets whether validation is enabled or disabled.

The default behavior mimics Python’s assert statement: validation is on by default, but is disabled if Python is run in optimized mode (via python -O). Validation may be expensive, so you may want to disable it once a model is working.

Parameters:

value (bool) – Whether to enable validation.

Return type:

None

property stddev: Tensor#

Returns the standard deviation of the distribution.

prior_acceptance(num_rejection_samples=10000, force_update=False, show_progress_bars=False, rejection_sampling_batch_size=10000)[source]#

Return the fraction of prior samples accepted by the classifier.

The factor is estimated from the acceptance probability during rejection sampling from the prior.

Parameters:
  • num_rejection_samples (int) – Number of samples to estimate the acceptance rate.

  • force_update (bool) – Whether to force update the acceptance rate.

  • show_progress_bars (bool) – Whether to show progress bars during sampling.

  • rejection_sampling_batch_size (int) – Batch size for rejection sampling.

Returns:

Tensor of the estimated acceptance rate.

Return type:

Tensor

property mean: Tensor#

Mean of the restricted prior (not implemented).

property variance: Tensor#

Variance of the restricted prior (not implemented).

property support#

Returns a Constraint object representing this distribution’s support.