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 \(\log(p(x_o|\theta)p(\theta))\) for likelihood-based methods.

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
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
def likelihood_estimator_based_potential(
    likelihood_estimator: ConditionalDensityEstimator,
    prior: Distribution,
    x_o: Optional[Tensor],
    enable_transform: bool = True,
) -> Tuple[Callable, TorchTransform]:
    r"""Returns potential $\log(p(x_o|\theta)p(\theta))$ for likelihood-based methods.

    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

score_estimator_based_potential(score_estimator, prior, x_o, enable_transform=False)

Returns the potential function gradient for score estimators.

Parameters:

Name Type Description Default
score_estimator ConditionalScoreEstimator

The neural network modelling the score.

required
prior Optional[Distribution]

The prior distribution.

required
x_o Optional[Tensor]

The observed data at which to evaluate the score.

required
enable_transform bool

Whether to enable transforms. Not supported yet.

False
Source code in sbi/inference/potentials/score_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
def score_estimator_based_potential(
    score_estimator: ConditionalScoreEstimator,
    prior: Optional[Distribution],
    x_o: Optional[Tensor],
    enable_transform: bool = False,
) -> Tuple["PosteriorScoreBasedPotential", TorchTransform]:
    r"""Returns the potential function gradient for score estimators.

    Args:
        score_estimator: The neural network modelling the score.
        prior: The prior distribution.
        x_o: The observed data at which to evaluate the score.
        enable_transform: Whether to enable transforms. Not supported yet.
    """
    device = str(next(score_estimator.parameters()).device)

    potential_fn = PosteriorScoreBasedPotential(
        score_estimator, prior, x_o, device=device
    )

    assert (
        enable_transform is False
    ), "Transforms are not yet supported for score estimators."

    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