prior#

Class that represents a prior distribution.

The Prior class is a wrapper around PyMC distributions that allows the user to create outside of the PyMC model.

This is the alternative to using the dictionaries in PyMC-Marketing models.

Examples#

Create a normal prior.

from pymc_marketing.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_marketing.prior import register_tensor_transform

def custom_transform(x):
    return x ** 2

register_tensor_transform("square", custom_transform)

custom_distribution = Prior("Normal", transform="square")

Functions

create_dim_handler(desired_dims)

Wrap the handle_dims function to act like the previous create_dim_handler function.

handle_dims(x, dims, desired_dims)

Take a tensor of dims dims and align it to desired_dims.

register_tensor_transform(name, transform)

Register a tensor transform function to be used in the Prior class.

sample_prior(factory[, coords, name, wrap])

Sample the prior for an arbitrary VariableFactory.

Classes

Censored(*args, **kwargs)

Create censored random variable.

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

A class to represent a prior distribution.

Scaled(dist, factor)

Scaled distribution for numerical stability.

VariableFactory(*args, **kwargs)

Protocol for something that works like a Prior class.

Exceptions

MuAlreadyExistsError(distribution)

Error for when 'mu' is present in Prior.

UnknownTransformError

Error for when an unknown transform is used.

UnsupportedDistributionError

Error for when an unsupported distribution is used.

UnsupportedParameterizationError

The follow parameterization is not supported.

UnsupportedShapeError

Error for when the shapes from variables are not compatible.

VariableNotFound

Variable is not found.