model_config#
Model configuration utilities.
Example configuration for a scalar parameter:
scalar_parameter = {
"dist": "Normal",
"kwargs": {
"mu": 0,
"sigma": 1,
},
}
Example configuration of a 1D parameter:
vector_parameter = {
"dist": "Normal",
"kwargs": {
"mu": 0,
"sigma": 1,
},
# dims need to be specified now!
"dims": "channel",
}
Example configuration of 1D parameter with a hierarchical distribution for only the mu:
hierarchical_parameter = {
"dist": "Normal",
"kwargs": {
# Replace the scalar parameter values with additional distributions
"mu": {
"dist": "Normal",
"kwargs": {
"mu": 0,
"sigma": 1,
},
},
# Common sigma for all channels
"sigma": 1,
},
"dims": "channel",
}
Example parameter configuration with a hierarchical non-centered distribution:
hierarchical_non_centered_parameter = {
"dist": "Normal",
"kwargs": {
"mu": {"dist": "HalfNormal", "kwargs": {"sigma": 2},},
"sigma": {"dist": "HalfNormal", "kwargs": {"sigma": 1},},
},
"dims": ("channel"),
"centered": False,
}
Example configuration of a 2D parameter:
matrix_parameter = {
"dist": "Normal",
"kwargs": {
"mu": 0,
"sigma": 1,
},
# dims need to be specified now!
"dims": ("channel", "geo"),
}
Model configuration with all of these variables:
model_config = {
"alpha": scalar_parameter,
"beta": vector_parameter,
"gamma": hierarchical_parameter,
"delta": matrix_parameter,
}
Creating variables from the configuration:
import pymc as pm
from pymc_marketing.model_config import create_distribution_from_config
coords = {
"channel": ["A", "B", "C"],
"geo": ["Region1", "Region2"],
}
with pm.Model(coords=coords) as model:
alpha = create_distribution_from_config("alpha", model_config)
beta = create_distribution_from_config("beta", model_config)
gamma = create_distribution_from_config("gamma", model_config)
delta = create_distribution_from_config("delta", model_config)
Functions
Check if the parameter configuration contains a deeper nested distribution. |
|
|
Create a function that maps variable shapes to the desired dims. |
|
Create a PyMC distribution with the specified parameters. |
|
Wrapper around create_distribution that uses a configuration dictionary. |
|
Create a hierarchical non-centered distribution. |
|
Create observed variable for the model. |
|
Retrieve a PyMC distribution class by name. |
|
Broadcast a 1D variable to the desired dims. |
|
Broadcast a 2D variable to the desired dims. |
|
Handle a nested distribution configuration. |
|
Handle the parameter configuration for a variable. |
|
Loop over the parameter configurations and handle them. |
|
Broadcast a scalar to the desired dims. |
Exceptions
|
Error for invalid model configuration. |
|
Error for when 'mu' is present in the nested 'kwargs'. |
|
Error for when a nested distribution is detected where it is not allowed. |
|
Error for when an invalid distribution is used for non-centered hierarchical distribution. |
|
Error for when an unsupported distribution is used. |
|
Error for when the shape of the hierarchical variable is not supported. |