πŸ“ analysis.contributionΒΆ

The ContributionAnalyzer class within the analysis submodule of the MMM package provides tools for analyzing the contribution of different marketing channels or tactics to the overall model results. This is useful for understanding the impact of each channel on the target metric and for optimizing marketing strategies.

OverviewΒΆ

The module exposes a contribution analysis class that:

  • Accepts model results and contribution parameters

  • Computes the contribution of each channel to the overall results using a β€œzero-out” approach

  • Provides methods for total, average, and per-row contributions

  • Returns structured output for downstream analysis or reporting

Class ReferenceΒΆ

class mmm.analysis.contribution.ContributionAnalyzer(df, model, feature_cols)ΒΆ

Analyzes the contribution of different marketing channels or tactics to the overall model results.

Parameters:
  • df (pandas.DataFrame) – Input dataframe containing model results.

  • model (Any) – Model object with a fit and predict method.

  • feature_cols (list[str]) – List of column names representing the features/channels to analyze.

feature_contribution(df, feature)ΒΆ

Computes the per-row contribution of a feature by zeroing it out and measuring the prediction difference.

Parameters:
  • df (pandas.DataFrame) – DataFrame to use for contribution calculation.

  • feature (str) – Feature/column name to analyze.

Returns:

Numpy array of per-row contributions.

Return type:

np.ndarray

total_contribution(df, feature)ΒΆ

Computes the total contribution of a feature across all rows.

Parameters:
  • df (pandas.DataFrame) – DataFrame to use for contribution calculation.

  • feature (str) – Feature/column name to analyze.

Returns:

Total contribution as a float.

Return type:

float

average_contribution(df, feature)ΒΆ

Computes the average contribution of a feature across all rows.

Parameters:
  • df (pandas.DataFrame) – DataFrame to use for contribution calculation.

  • feature (str) – Feature/column name to analyze.

Returns:

Average contribution as a float.

Return type:

float

summary(df, feature)ΒΆ

Returns a summary dictionary with per-row, total, and average contributions for a feature.

Parameters:
  • df (pandas.DataFrame) – DataFrame to use for contribution calculation.

  • feature (str) – Feature/column name to analyze.

Returns:

Dictionary with keys contribution (list), total_contribution (float), and average_contribution (float).

Return type:

dict

Example Use CaseΒΆ

from mmm.analysis.contribution import ContributionAnalyzer
import pandas as pd
from sklearn.ensemble import RandomForestRegressor

class Model:
     def fit(self, X, y):
         raise NotImplementedError

     def predict(self, X):
         raise NotImplementedError

# Sample data and model setup
df = pd.DataFrame({
    'channel_1': [100, 150, 200],
    'channel_2': [50, 75, 100],
    'target': [200, 300, 400]
})
X = df[['channel_1', 'channel_2']]
y = df['target']
model = RandomForestRegressor().fit(X, y)

# Initialize the analyzer
analyzer = ContributionAnalyzer(df=X, model=model, feature_cols=['channel_1', 'channel_2'])

# Analyze contribution for channel_1
contribution_summary = analyzer.summary(df=X, feature='channel_1')
print(contribution_summary)

Result Example

{
  "contribution": [0.1, 0.2, ..., 0.05],
  "total_contribution": 15.0,
  "average_contribution": 0.15
}

ReferencesΒΆ

Back to Home