MMM.build_model#

MMM.build_model(X, y=None, **kwargs)[source]#

Build a probabilistic model using PyMC for marketing mix modeling.

The model incorporates channels, control variables, and Fourier components, applying adstock and saturation transformations to the channel data. The final model is constructed with multiple factors contributing to the response variable.

Parameters:
Xpd.DataFrame, xr.Dataset, or xr.DataArray

The input data for the model. When a pd.DataFrame is provided, columns should include channels, control variables (if applicable), and Fourier components (if applicable). When an xr.Dataset or xr.DataArray is provided, variables should be named "channel", "target", and optionally "control" (or the underscore-prefixed internal names "_channel", "_target", "_control").

ypd.Series, np.ndarray, xr.DataArray, or None

The target/response variable for the modeling. If omitted, X must contain a "target" (or "_target") variable.

**kwargsdict

Additional keyword arguments that might be required by underlying methods or utilities.

Notes

Sets the following attributes on the instance:

  • model: a pymc.Model containing all defined stochastic and deterministic variables.

Examples

Initialize model with custom configuration

from pymc_marketing.mmm import GeometricAdstock, LogisticSaturation
from pymc_marketing.mmm.mmm import MMM
from pymc_extras.prior import Prior

custom_config = {
    "intercept": Prior("Normal", mu=0, sigma=2),
    "saturation_beta": Prior("Gamma", mu=1, sigma=3),
    "saturation_lambda": Prior("Beta", alpha=3, beta=1),
    "adstock_alpha": Prior("Beta", alpha=1, beta=3),
    "likelihood": Prior("Normal", sigma=Prior("HalfNormal", sigma=2)),
    "gamma_control": Prior("Normal", mu=0, sigma=2, dims="control"),
    "gamma_fourier": Prior("Laplace", mu=0, b=1, dims="fourier_mode"),
}

model = MMM(
    date_column="date_week",
    channel_columns=["x1", "x2"],
    adstock=GeometricAdstock(l_max=8),
    saturation=LogisticSaturation(),
    control_columns=[
        "event_1",
        "event_2",
        "t",
    ],
    yearly_seasonality=2,
    model_config=custom_config,
)