Decomposition (decomp.py)

This module provides functions related to decomposing the model’s predictions into contributions from different components (baseline, seasonality, media channels, etc.).

Functions

get_baseline_breakdown_df

def get_baseline_breakdown_df(
    media_mix_model: mmm_model.DelayedSaturatedMMM,
    input_data: ind.InputData,
    degrees_seasonality: int,
) -> pd.DataFrame:

Calculates and returns a DataFrame containing the mean contributions of various model components over time. It leverages the compute_mean_contributions_over_time method (inherited from a mixin, likely ContributionMixin) of the fitted media_mix_model.

Parameters:

  • media_mix_model (mmm_model.DelayedSaturatedMMM): The fitted ABACUS MMM instance.

  • input_data (ind.InputData): The input data object. (Note: Currently unused in the function body but kept in the signature).

  • degrees_seasonality (int): The number of seasonality degrees used in the model. (Note: Currently unused in the function body but kept in the signature).

Returns:

  • pd.DataFrame: A DataFrame indexed by date, with columns representing the mean contribution of each component (e.g., ‘intercept’, ‘channel1’, ‘control_var’, ‘fourier_mode_sin_1’). Contributions are returned in the original scale of the target variable.

_dump_baseline_breakdown

def _dump_baseline_breakdown(
    results_dir: str,
    baseline_breakdown_df: pd.DataFrame,
) -> None:

A private helper function to save the baseline breakdown DataFrame to a CSV file.

Parameters:

  • results_dir (str): The path to the directory where the output file should be saved. The directory will be created if it doesn’t exist.

  • baseline_breakdown_df (pd.DataFrame): The DataFrame to save, typically the output of get_baseline_breakdown_df.

Returns:

  • None. Writes the DataFrame to os.path.join(results_dir, "all_breakdown.csv").