GeometricAdstock#

class pymc_marketing.mmm.components.adstock.GeometricAdstock(l_max=FieldInfo(annotation=NoneType, required=True, description='Maximum lag for the adstock transformation.', metadata=[Gt(gt=0)]), normalize=FieldInfo(annotation=NoneType, required=False, default=True, description='Whether to normalize the adstock values.'), mode=FieldInfo(annotation=NoneType, required=False, default=<ConvMode.After: 'After'>, description='Convolution mode.'), priors=FieldInfo(annotation=NoneType, required=False, default=None, description='Priors for the parameters.'), prefix=FieldInfo(annotation=NoneType, required=False, default=None, description='Prefix for the parameters.'), parametrization=FieldInfo(annotation=NoneType, required=False, default=None, description='Whether to parametrize the decay by alpha or half-life.'))[source]#

Wrapper around geometric adstock function.

Calls pymc_marketing.mmm.transformers.geometric_adstock() with the wrapper’s l_max, normalize and mode settings.

The decay can be parametrised either by the retention rate alpha (the default) or by the half-life of the ad effect. Since the weight at lag \(t\) is \(\alpha^{t}\), a half-life \(h\) corresponds to

\[\alpha = 0.5^{1 / h}\]

which maps any positive half-life into \((0, 1)\) and holds exactly for every l_max and either setting of normalize. Under the half-life parametrisation the trace will contain adstock_halflife instead of adstock_alpha.

The two defaults are matched, implying a median alpha of 0.207 against 0.206. The priors imply a median half-life of 0.44 periods. A custom half-life prior should keep its mass away from zero, where the likelihood goes numerically flat. The adstock functions guide covers both points.

Parameters:
alphatensor

Retention rate of the ad effect; must be between 0 and 1. Default prior: Prior("Beta", alpha=1, beta=3). Only used when parametrization="alpha".

halflifetensor

Number of time periods after which the ad effect has decayed by half; must be positive. Default prior: Prior("InverseGamma", alpha=2.6, beta=1). Only used when parametrization="halflife".

parametrizationstr

Either "alpha" or "halflife". When left unset it is inferred from the priors, defaulting to "alpha". Passing a prior for the parameter of the other parametrisation raises a ValueError.

Examples

Parametrise the decay by its half-life instead of the retention rate:

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

adstock = GeometricAdstock(
    l_max=10,
    priors={"halflife": Prior("InverseGamma", alpha=4, beta=2)},
)

(Source code, png, hires.png, pdf)

../../_images/pymc_marketing-mmm-components-adstock-GeometricAdstock-1.png

Methods

GeometricAdstock.__init__([l_max, ...])

GeometricAdstock.apply(x, *[, core_dim, idx])

Call within a model context.

GeometricAdstock.from_dict(data)

Reconstruct an adstock transformation from a dict.

GeometricAdstock.function(x[, alpha, halflife])

Geometric adstock function.

GeometricAdstock.plot_curve(curve[, ...])

Plot curve HDI and samples.

GeometricAdstock.plot_curve_hdi(curve[, ...])

Plot the HDI of the curve.

GeometricAdstock.plot_curve_samples(curve[, ...])

Plot samples from the curve.

GeometricAdstock.sample_curve(parameters[, ...])

Sample the adstock transformation given parameters.

GeometricAdstock.sample_prior([coords])

Sample the priors for the transformation.

GeometricAdstock.set_dims_for_all_priors(dims)

Set the dims for all priors.

GeometricAdstock.to_dict([_orig])

Convert the adstock transformation to a dictionary.

GeometricAdstock.update_priors(priors)

Update the priors for a function after initialization.

GeometricAdstock.with_default_prior_dims(dims)

Return a copy with default prior dims (dims=None) set to dims instead.

GeometricAdstock.with_updated_priors(priors)

Return a copy with updated priors.

Attributes

combined_dims

Get the combined dims for all the parameters.

default_priors

function_priors

Get the priors for the function.

halflife_priors

model_config

Mapping from variable name to prior for the model.

prefix

priors

Get the priors for the function.

variable_mapping

Mapping from parameter name to variable name in the model.