R2D2#

class pymc_marketing.r2d2.R2D2(r2, total_sigma, dims)[source]#

R2D2 variance decomposition.

Splits total variance between model and residual, then splits model variance across components via Dirichlet.

Variables are created once via create_variable(), then referenced by split() and error_sigma.

Uses a Normal base distribution for coefficients (per Aguilar & Bürkner, 2022) for compatibility with HMC sampling.

Note

The decomposition allocates variance ONLY across the components defined in dims. Any covariates not included in dims will not be covered by the variance decomposition. Ensure all relevant covariates are included in dims.

Parameters:
r2Prior

R² prior (how much variance the model explains). Must be scalar. Typically Beta(mu=0.8, sigma=0.2).

total_sigmaPrior

Total scale of the data. Must be scalar. Typically LogNormal(mu=np.log(std(y)), sigma=0.1).

dimsdict[str, str]

Maps component name to dim name. E.g., {“control”: “control”, “fourier”: “fourier”}.

Notes

  • The r2 prior controls global shrinkage (how much variance the model explains).

  • The total_sigma prior controls the overall scale of coefficients.

  • The Dirichlet prior (currently flat, a_π=1) splits model variance across components.

  • Use split("component_name") to get a lazy reference to a component’s variance.

  • Use error_sigma to get the residual standard deviation.

  • This is single-level R2D2 (no varying effects).

Methods

R2D2.__init__(r2, total_sigma, dims)

R2D2.create_variable(name)

Create all decomposition variables ONCE inside pm.Model.

R2D2.from_dict(data)

Deserialize from dictionary.

R2D2.split(component_name)

Get lazy reference to a component's split variable.

R2D2.to_dict([_orig])

Attributes

built

Whether create_variable has been called for the current model.

error_sigma

Get lazy reference to the residual standard deviation σ.

splits

Access created splits (after create_variable).

r2

total_sigma

dims