Abacus Core Mixins: Contribution (contribution.py)¶
This module provides the ContributionMixin class, designed to be inherited by Marketing Mix Model (MMM) classes in Abacus. It encapsulates methods for calculating and analysing the contributions of different model components (channels, baseline features) to the predicted target variable, based on the model’s posterior distribution (fit_result).
ContributionMixin Class
This mixin assumes the inheriting class has attributes like fit_result (an ArviZ InferenceData object containing the model trace), channel_columns, potentially control_columns and yearly_seasonality, and a method get_target_transformer() that returns the scaler used for the target variable.
Methods
compute_channel_contribution_original_scale
def compute_channel_contribution_original_scale(self) -> xr.DataArray:
Computes the contribution of each marketing channel for each posterior sample and time point, inverse-transformed back to the original scale of the target variable.
It extracts the channel_contributions variable from the fit_result, flattens it, applies the inverse transformation using the target scaler, and reshapes it back to the original dimensions (chain, draw, channel, date).
Returns:
xr.DataArray: Channel contributions in the original target scale, preserving the sample dimensions.
compute_mean_contributions_over_time
def compute_mean_contributions_over_time(self, original_scale: bool = False) -> pd.DataFrame:
Calculates the mean contribution of each model component (channels, control variables, seasonality, intercept) over time, averaged across all posterior samples.
Parameters:
original_scale(bool, optional): IfTrue, the returned contributions are inverse-transformed to the original scale of the target variable. Defaults toFalse(returns contributions in the potentially transformed scale used during modelling).
Returns:
pd.DataFrame: A DataFrame indexed by date. Columns include:Each channel name from
self.channel_columns.Each control variable name from
self.control_columns(if applicable).Fourier components representing seasonality (if
self.yearly_seasonalityis applicable).intercept. The values represent the mean contribution of that component at each time point.
(Private helper methods _format_model_contributions and _get_channel_contributions_share_samples are used internally for extracting data from the fit_result and calculating contribution shares across samples, respectively.)