MMM.compute_counterfactual_contributions_dataset#

MMM.compute_counterfactual_contributions_dataset()[source]#

Full-posterior counterfactual contributions as an xr.Dataset.

For each component \(j\) with value \(v_j(t)\) in the linear predictor, the per-draw contribution is:

\[\text{contribution}_j^{(d)}(t) = \text{inv}\bigl(\mu^{(d)}(t)\bigr) \cdot s - \text{inv}\bigl(\mu^{(d)}(t) - v_j^{(d)}(t)\bigr) \cdot s\]

where \(\text{inv}\) is the inverse link function, \(s\) is target_scale, and \(d\) indexes a posterior draw.

Identity link (\(\text{inv} = \text{id}\)):

\[\text{contribution}_j^{(d)}(t) = v_j^{(d)}(t) \cdot s\]

Log link (\(\text{inv} = \exp\)):

\[\text{contribution}_j^{(d)}(t) = \bigl[\exp\!\bigl(\mu^{(d)}(t)\bigr) - \exp\!\bigl(\mu^{(d)}(t) - v_j^{(d)}(t)\bigr)\bigr] \cdot s\]

The returned dataset retains the full (chain, draw) dimensions so that downstream code can compute arbitrary summaries (HDI, quantiles, etc.).

Returns:
xr.Dataset

One data variable per component (channels, controls, yearly_seasonality, intercept). Dimensions are (chain, draw, date, ...) where ... are any extra model dimensions (e.g. geo).

Raises:
ValueError

If the model has not been fitted (no idata).

See also

compute_mean_contributions_over_time

Convenience wrapper returning the posterior-mean as a pd.DataFrame.

Examples

mmm.fit(X, y)
ds = mmm.compute_counterfactual_contributions_dataset()

# Posterior mean (same as compute_mean_contributions_over_time)
ds.mean(("chain", "draw"))

# 94 % HDI per component
import arviz as az

az.hdi(ds)