Skip to main content
Version: 3.13

auto-mmm

Python

### Automatic Media-Mix Model

#You will learn how to perform attribution with the automatic media-mix model (MMM) using the `auto_mmm` function from the `ChannelAttributionPro` package.
#The model leverages copula-based methods for improved accuracy and flexibility, allowing for better media attribution across marketing channels.

### Steps:
#- Import required libraries and the dataset
#- Define a dictionary linking dependent variables to marketing channels
#- Estimate the media-mix model using `auto_mmm` with appropriate parameters

### Import libraries

from ChannelAttributionPro import *

### Set your token

token="yourtoken"

### Load data

# Load the dataset from the given URL
# This dataset should contain the timestamp, the target variable (revenue), and the dependent variables (marketing channels)
Data = pd.read_csv("https://app.channelattribution.io/data/data_mmm.csv", sep=",")
Data

### Prepare data

# Dictionary to map the dependent variables (e.g., impressions, clicks, costs) to their respective channels
D_variables = {
"revenue": "target", # The target variable is 'revenue'
"direct_searches": "direct_searches", # Direct searches attributed to direct channel
"facebook_impressions": "facebook", # Facebook impressions as a dependent variable
"facebook_cost": "facebook", # Facebook cost attributed to the Facebook channel
"google_impressions": "google", # Google impressions attributed to Google
"google_clicks": "google", # Google clicks attributed to Google
"google_cost": "google" # Google cost attributed to Google
}
D_variables

### Perform attribution with Automatic Media Mix Model

# Call the auto_mmm function to estimate the automatic media mix model (MMM)
# The function parameters are explained below:
# - Data: The dataset containing the marketing data
# - D_variables: A dictionary linking dependent variables to their respective channels
# - max_p: Maximum lagged effects to consider for each dependent variable (default = 12)
# - nsim: Number of predictions to generate from copula distributions (default = 1000)
# - seed: Random seed for reproducibility (default = 1234567)
# - verbose: Whether to show additional information during the execution (default = True)
res = auto_mmm(
Data=Data, # The dataset containing timestamp, target, and dependent variables
D_variables=D_variables, # The dictionary associating dependent variables with channels
max_p=12, # Maximum lag for each dependent variable
nsim=1000, # Number of predictions from copula distributions
seed=1234567, # Random seed to ensure reproducibility
verbose=1 # Display detailed execution information
)

### Results
#After the model estimation is complete, the `res` object will contain the attribution for each timestamp and for each channel

print(res)

#As you can see, attribution starts from a timestamp equal to max_p+1 because the algorithm uses the first max_p observations to #estimate lagged effects. If you need attribution for the first max_p timestamps, you can run auto_mmm with max_p=0. But keep in #mind that the resulting attribution could be imprecise.

res1 = auto_mmm(
Data,
D_variables,
max_p=0,
nsim=1000,
seed=1234567,
verbose=1
)

res1=res1.iloc[0:12]

print(res1)