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 indimswill not be covered by the variance decomposition. Ensure all relevant covariates are included indims.- Parameters:
- r2
Prior R² prior (how much variance the model explains). Must be scalar. Typically Beta(mu=0.8, sigma=0.2).
- total_sigma
Prior Total scale of the data. Must be scalar. Typically LogNormal(mu=np.log(std(y)), sigma=0.1).
- dims
dict[str,str] Maps component name to dim name. E.g., {“control”: “control”, “fourier”: “fourier”}.
- r2
Notes
The
r2prior controls global shrinkage (how much variance the model explains).The
total_sigmaprior 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_sigmato 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
builtWhether create_variable has been called for the current model.
error_sigmaGet lazy reference to the residual standard deviation σ.
splitsAccess created splits (after create_variable).
r2total_sigmadims