Prior#

class pymc_extras.prior.Prior(distribution: str, *, dims: str | Sequence[str | None] | None = None, centered: bool = True, transform: str | None = None, core_dims: Sequence[str] | str | None = (), **parameters)[source]#

A class to represent a prior distribution.

Make use of the various helper methods to understand the distributions better.

  • preliz attribute to get the equivalent distribution in preliz

  • sample_prior method to sample from the prior

  • to_graph get a dummy model graph with the distribution

  • constrain to shift the distribution to a different range

Parameters:
  • distribution (str) – The name of PyMC distribution.

  • dims (Dims, optional) – The dimensions of the variable, by default None

  • centered (bool, optional) – Whether the variable is centered or not, by default True. Only allowed for Normal distribution.

  • transform (str, optional) – The name of the transform to apply to the variable after it is created, by default None or no transform. The transformation must be registered with register_tensor_transform function or be available in either pytensor.tensor or pymc.math.

Examples

Create a normal prior.

from pymc_extras.prior import Prior

normal = Prior("Normal")

Create a hierarchical normal prior by using distributions for the parameters and specifying the dims.

hierarchical_normal = Prior(
    "Normal",
    mu=Prior("Normal"),
    sigma=Prior("HalfNormal"),
    dims="channel",
)

Create a non-centered hierarchical normal prior with the centered parameter.

non_centered_hierarchical_normal = Prior(
    "Normal",
    mu=Prior("Normal"),
    sigma=Prior("HalfNormal"),
    dims="channel",
    # Only change needed to make it non-centered
    centered=False,
)

Create a hierarchical beta prior by using Beta distribution, distributions for the parameters, and specifying the dims.

hierarchical_beta = Prior(
    "Beta",
    alpha=Prior("HalfNormal"),
    beta=Prior("HalfNormal"),
    dims="channel",
)

Create a transformed hierarchical normal prior by using the transform parameter. Here the “sigmoid” transformation comes from pm.math.

transformed_hierarchical_normal = Prior(
    "Normal",
    mu=Prior("Normal"),
    sigma=Prior("HalfNormal"),
    transform="sigmoid",
    dims="channel",
)

Create a prior with a custom transform function by registering it with register_tensor_transform().

from pymc_extras.prior import register_tensor_transform

def custom_transform(x):
    return x**2


register_tensor_transform("square", custom_transform)

custom_distribution = Prior("Normal", transform="square")
__init__(distribution: str, *, dims: str | Sequence[str | None] | None = None, centered: bool = True, transform: str | None = None, core_dims: Sequence[str] | str | None = (), **parameters) None[source]#

Methods

__init__(distribution, *[, dims, centered, ...])

constrain(lower, upper[, mass, kwargs])

Create a new prior with a given mass constrained within the given bounds.

create_likelihood_variable(name, mu, observed)

Create a likelihood variable from the prior.

create_variable(name[, xdist])

Create a PyMC variable from the prior.

deepcopy()

Return a deep copy of the prior.

from_dict(data)

Create a Prior from the dictionary format.

sample_prior([coords, name, xdist])

Sample the prior distribution for the variable.

to_dict()

Convert the prior to dictionary format.

to_graph()

Generate a graph of the variables.

Attributes

dims

The dimensions of the variable.

distribution

The name of the PyMC distribution.

non_centered_distributions

Available non-centered distributions and their default parameters.

preliz

Create an equivalent preliz distribution.

transform

The name of the transform to apply to the variable after it is created.

pymc_distribution

The PyMC distribution class.