pymc-labs / pymc-marketing

Bayesian marketing toolbox in PyMC. Media Mix (MMM), customer lifetime value (CLV), buy-till-you-die (BTYD) models and more.
https://www.pymc-marketing.io/
Apache License 2.0
667 stars 184 forks source link

Hierarchical Model Implementation Proposal #768

Open wd60622 opened 3 months ago

wd60622 commented 3 months ago

Following the hierarchical configurations and media transformation interface (example here), I think hierarchical models can be supported by similar to current MMM class but with a few modifications. Namely,

For example, the user can just toggle out hierarchical_lam and independent_lam for a hierarchical vs non-hierarchical model.

from pymc_markerting.prior import Prior
from pymc_marketing.mmm import GeometricAdstock

independent_lam = Prior("Beta", alpha=1, beta=3, dims=("channel", "geo"))
hierarchical_lam = Prior(
    "Beta",
    alpha=Prior("HalfNormal", dims="channel"), 
    beta=Prior("HalfNormal", dims="channel"), 
    dims=("channel", "geo"),
)

priors = {
  "lam": hierarchical_lam,
}
adstock = GeometricAdstock(priors=priors, l_max=...)

with pm.Model(coords=...) as geo_model: 
    # (date, channel, geo) data suggests (channel, geo) parameters
    dims = get_paramter_dims_from_data(data)
    # (date, channel, geo)
    adstocked_data = adstock.apply(data, dims=dims)
    # similar with saturation
    # (date, channel, geo)
    contributions = saturation.apply(adstocked_data, dims=dims)

It would be up to us to pull out the parameters shapes from data shape, do some validation, combine all into model, etc.

wd60622 commented 3 months ago

Some questions that I still have are:

Note: The way I am building out #679 will work with additional dims. The dimension of the output is just based on the dimensions of the priors which the use would specify