DelayedAdstock#
- class pymc_marketing.mmm.components.adstock.DelayedAdstock(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 delayed adstock function.
Calls
pymc_marketing.mmm.transformers.delayed_adstock()with the wrapper’sl_max,normalizeandmodesettings.The width of the response around the peak 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 \(\ell\) is \(\alpha^{(\ell-\theta)^2}\), a half-life \(h\) corresponds to\[\alpha = 2^{-1 / h^{2}}, \qquad w_\ell = 2^{-\left(\frac{\ell-\theta}{h}\right)^{2}},\]which maps any positive half-life into \((0, 1)\) and holds exactly for every
l_maxand either setting ofnormalize. Under the half-life parametrisation the trace will containadstock_halflifeinstead ofadstock_alpha.Unlike
GeometricAdstock, where the half-life is a decay time measured from lag zero, here it is a half-width either side of the peak attheta: the effect takeshalflifeperiods to rise to its peak as well as to fall back from it.The two defaults are matched, implying a median
alphaof 0.207 against 0.206. The priors imply a median half-life of 0.66 periods. A custom half-life prior must have positive support and should keep its mass away from zero, where the likelihood goes numerically flat. The adstock functions guide covers both points.- Parameters:
- alpha
tensor Retention rate of the ad effect; must be between 0 and 1. Default prior:
Prior("Beta", alpha=1, beta=3). Only used whenparametrization="alpha".- halflife
tensor Number of time periods between peak effectiveness and half of it; must be positive. Because the kernel is symmetric about
theta, this is both the time over which effectiveness rises from half to its peak and the time over which it decays from the peak back to half. Default prior:Prior("InverseGamma", alpha=9, beta=5.75). Only used whenparametrization="halflife".- theta
tensor Delay from exposure to peak effectiveness; must be between 0 and
l_max - 1. Default prior:Prior("HalfNormal", sigma=1). Used by both parametrisations, and passed tofunctionby keyword only, so that a positional call cannot bind it tohalflifeby mistake.- parametrization
str 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 aValueError.
- alpha
Examples
Parametrise the width of the response by its half-life rather than by the retention rate. For weekly data the priors below say that the effect peaks a couple of weeks after exposure and takes about three weeks to move between half and peak effectiveness:
import numpy as np from pymc_extras.prior import Prior from pymc_marketing.mmm import DelayedAdstock adstock = DelayedAdstock( l_max=12, priors={ "halflife": Prior("LogNormal", mu=np.log(3), sigma=0.3), "theta": Prior("HalfNormal", sigma=2), }, )
(
Source code,png,hires.png,pdf)
Methods
DelayedAdstock.__init__([l_max, normalize, ...])DelayedAdstock.apply(x, *[, core_dim, idx])Call within a model context.
DelayedAdstock.from_dict(data)Reconstruct an adstock transformation from a dict.
DelayedAdstock.function(x[, alpha, halflife])Delayed adstock function.
DelayedAdstock.plot_curve(curve[, ...])Plot curve HDI and samples.
DelayedAdstock.plot_curve_hdi(curve[, ...])Plot the HDI of the curve.
DelayedAdstock.plot_curve_samples(curve[, ...])Plot samples from the curve.
DelayedAdstock.sample_curve(parameters[, amount])Sample the adstock transformation given parameters.
DelayedAdstock.sample_prior([coords])Sample the priors for the transformation.
Set the dims for all priors.
DelayedAdstock.to_dict([_orig])Convert the adstock transformation to a dictionary.
DelayedAdstock.update_priors(priors)Update the priors for a function after initialization.
Return a copy with default prior dims (dims=None) set to
dimsinstead.Return a copy with updated priors.
Attributes
combined_dimsGet the combined dims for all the parameters.
default_priorsfunction_priorsGet the priors for the function.
halflife_priorsmodel_configMapping from variable name to prior for the model.
prefixpriorsGet the priors for the function.
variable_mappingMapping from parameter name to variable name in the model.