Skip to content

Potentials

posterior_estimator_based_potential(posterior_estimator, prior, x_o, enable_transform=True)

Returns the potential for posterior-based methods.

It also returns a transformation that can be used to transform the potential into unconstrained space.

The potential is the same as the log-probability of the posterior_estimator, but it is set to \(-\inf\) outside of the prior bounds.

Parameters:

Name Type Description Default
posterior_estimator ConditionalDensityEstimator

The neural network modelling the posterior.

required
prior Distribution

The prior distribution.

required
x_o Optional[Tensor]

The observed data at which to evaluate the posterior.

required
enable_transform bool

Whether to transform parameters to unconstrained space. When False, an identity transform will be returned for theta_transform.

True

Returns:

Type Description
PosteriorBasedPotential

The potential function and a transformation that maps

TorchTransform

to unconstrained space.

Source code in sbi/inference/potentials/posterior_based_potential.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
def posterior_estimator_based_potential(
    posterior_estimator: ConditionalDensityEstimator,
    prior: Distribution,
    x_o: Optional[Tensor],
    enable_transform: bool = True,
) -> Tuple[PosteriorBasedPotential, TorchTransform]:
    r"""Returns the potential for posterior-based methods.

    It also returns a transformation that can be used to transform the potential into
    unconstrained space.

    The potential is the same as the log-probability of the `posterior_estimator`, but
    it is set to $-\inf$ outside of the prior bounds.

    Args:
        posterior_estimator: The neural network modelling the posterior.
        prior: The prior distribution.
        x_o: The observed data at which to evaluate the posterior.
        enable_transform: Whether to transform parameters to unconstrained space.
            When False, an identity transform will be returned for `theta_transform`.

    Returns:
        The potential function and a transformation that maps
        to unconstrained space.
    """

    device = str(next(posterior_estimator.parameters()).device)

    potential_fn = PosteriorBasedPotential(
        posterior_estimator, prior, x_o, device=device
    )

    theta_transform = mcmc_transform(
        prior, device=device, enable_transform=enable_transform
    )

    return potential_fn, theta_transform

likelihood_estimator_based_potential(likelihood_estimator, prior, x_o, enable_transform=True)

Returns potential :math:\log(p(x_o|\theta)p(\theta)) for likelihood estimator.

It also returns a transformation that can be used to transform the potential into unconstrained space.

Parameters:

Name Type Description Default
likelihood_estimator ConditionalDensityEstimator

The density estimator modelling the likelihood.

required
prior Distribution

The prior distribution.

required
x_o Optional[Tensor]

The observed data at which to evaluate the likelihood.

required
enable_transform bool

Whether to transform parameters to unconstrained space. When False, an identity transform will be returned for theta_transform.

True

Returns:

Type Description
Callable

The potential function \(p(x_o|\theta)p(\theta)\) and a transformation that maps

TorchTransform

to unconstrained space.

Source code in sbi/inference/potentials/likelihood_based_potential.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
def likelihood_estimator_based_potential(
    likelihood_estimator: ConditionalDensityEstimator,
    prior: Distribution,  # type: ignore
    x_o: Optional[Tensor],
    enable_transform: bool = True,
) -> Tuple[Callable, TorchTransform]:
    r"""Returns potential :math:`\log(p(x_o|\theta)p(\theta))` for likelihood estimator.

    It also returns a transformation that can be used to transform the potential into
    unconstrained space.

    Args:
        likelihood_estimator: The density estimator modelling the likelihood.
        prior: The prior distribution.
        x_o: The observed data at which to evaluate the likelihood.
        enable_transform: Whether to transform parameters to unconstrained space.
             When False, an identity transform will be returned for `theta_transform`.

    Returns:
        The potential function $p(x_o|\theta)p(\theta)$ and a transformation that maps
        to unconstrained space.
    """

    device = str(next(likelihood_estimator.parameters()).device)

    potential_fn = LikelihoodBasedPotential(
        likelihood_estimator, prior, x_o, device=device
    )
    theta_transform = mcmc_transform(
        prior, device=device, enable_transform=enable_transform
    )

    return potential_fn, theta_transform

ratio_estimator_based_potential(ratio_estimator, prior, x_o, enable_transform=True)

Returns the potential for ratio-based methods.

It also returns a transformation that can be used to transform the potential into unconstrained space.

Parameters:

Name Type Description Default
ratio_estimator Module

The neural network modelling likelihood-to-evidence ratio.

required
prior Distribution

The prior distribution.

required
x_o Optional[Tensor]

The observed data at which to evaluate the likelihood-to-evidence ratio.

required
enable_transform bool

Whether to transform parameters to unconstrained space. When False, an identity transform will be returned for theta_transform.

True

Returns:

Type Description
Callable

The potential function and a transformation that maps

TorchTransform

to unconstrained space.

Source code in sbi/inference/potentials/ratio_based_potential.py
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
def ratio_estimator_based_potential(
    ratio_estimator: nn.Module,
    prior: Distribution,
    x_o: Optional[Tensor],
    enable_transform: bool = True,
) -> Tuple[Callable, TorchTransform]:
    r"""Returns the potential for ratio-based methods.

    It also returns a transformation that can be used to transform the potential into
    unconstrained space.

    Args:
        ratio_estimator: The neural network modelling likelihood-to-evidence ratio.
        prior: The prior distribution.
        x_o: The observed data at which to evaluate the likelihood-to-evidence ratio.
        enable_transform: Whether to transform parameters to unconstrained space.
            When False, an identity transform will be returned for `theta_transform`.

    Returns:
        The potential function and a transformation that maps
        to unconstrained space.
    """

    device = str(next(ratio_estimator.parameters()).device)

    potential_fn = RatioBasedPotential(ratio_estimator, prior, x_o, device=device)
    theta_transform = mcmc_transform(
        prior, device=device, enable_transform=enable_transform
    )

    return potential_fn, theta_transform

vector_field_estimator_based_potential(vector_field_estimator, prior, x_o, enable_transform=True, **kwargs)

Returns the potential function gradient for vector field estimators.

Parameters:

Name Type Description Default
vector_field_estimator ConditionalVectorFieldEstimator

The neural network modelling the vector field.

required
prior Optional[Distribution]

The prior distribution.

required
x_o Optional[Tensor]

The observed data at which to evaluate the vector field.

required
enable_transform bool

Whether to enable transforms. Not supported yet.

True
**kwargs

Additional keyword arguments passed to VectorFieldBasedPotential.

{}
Source code in sbi/inference/potentials/vector_field_potential.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
def vector_field_estimator_based_potential(
    vector_field_estimator: ConditionalVectorFieldEstimator,
    prior: Optional[Distribution],
    x_o: Optional[Tensor],
    enable_transform: bool = True,
    **kwargs,
) -> Tuple["VectorFieldBasedPotential", TorchTransform]:
    r"""Returns the potential function gradient for vector field estimators.

    Args:
        vector_field_estimator: The neural network modelling the vector field.
        prior: The prior distribution.
        x_o: The observed data at which to evaluate the vector field.
        enable_transform: Whether to enable transforms. Not supported yet.
        **kwargs: Additional keyword arguments passed to
            `VectorFieldBasedPotential`.
    """
    device = str(next(vector_field_estimator.parameters()).device)

    potential_fn = VectorFieldBasedPotential(
        vector_field_estimator, prior, x_o, device=device, **kwargs
    )

    if prior is not None:
        theta_transform = mcmc_transform(
            prior, device=device, enable_transform=enable_transform
        )
    else:
        theta_transform = torch.distributions.transforms.identity_transform

    return potential_fn, theta_transform