PyMC-Marketing is a library for the Python open-source developed by PyMC Labs, which transforms the way data scientists and marketing analysts deal with the optimization of their ad campaigns. Built on PyMC (see my article on the major update of PyMC), PyMC-Marketing provides tools for bayesian advanced Marketing Mix Modeling (MMM), the prediction of Customer Lifetime Value (CLV), the analysis of the choice of customers and much more.
In summary, PyMC-Marketing provides bayesian capabilities, as well as all its benefits, to marketing modeling. More particularly, the package allows to obtain the complete distribution of probability associated to the results, in contrast to conventional tools, which only provide point-estimate. All of this with a great flexibility and a large active community.
The 5 pillars of the Package
- Marketing Mix Modeling (MMM)
- To model the impact of each marketing channel sales, in order to optimize budget allocation
- Customer Lifetime Value (CLV)
- To quantify the lifetime value of your customers, i.e. the forecast of future income for each segment of customers
- Customer Choice Analysis (CSA)
- To analyse clients’ preferences
- Bass Diffusion Model
- To understand the adoption of your product in the market
- Predicted Incrementality by Experimentation
- To measure thecausal impact of your campaigns, thanks to theanalysis ofexperiments and A/B testing
A Practical example of MMM with PyMC-Marketing
Below is a code example for running a MMM on sample data:
import numpy as np
import pandas as pd
from pymc_marketing.mmm import ( MMM, GeometricAdstock, LogisticSaturation)
# ---------------------------------------------------------------------
# 1. Data Generation
# ----------------- ----------------------------------------------------
# Sample data was generated using:
# - date_week: the date in YYYY-MM-DD format
# - channel_1 and 2: metrics from two marketing channels
# - event_1 and 2: Boolean values indicating events related to the channels
# - dayofyear: the day of the year between 1 and 365
# - t: incremental index for each row
# ---------------------------------------------------------------------
# 2. Defining Transformations
# ---------------------------------------------------------------------
# Geometric Adstock:
# - alpha controls the persistence of the effect
# - l_max specifies the maximum number of weeks of delay
adstock = GeometricAdstock(l_max=8, normalize=True)
# LogisticSaturation:
# - models diminishing returns
# - those parameters are estimated by the model
saturation = LogisticSaturation()
# ---------------------------------------------------------------------
# 3. Marketing Mix Model definition
# ---------------------------------------------------------------------
mmm = MMM(
date_column=date_column,
channel_columns=channel_columns,
target_column=target_column,
adstock=adstock,
saturation=saturation,
# Optional :control variables
# control_columns=["price", "promotion", "holiday"],
# Optional : yearly seasonality
yearly_seasonality=2,
# Apply adstock first, then saturation
adstock_first=True,
# Sampler configuration
sampler_config={
"progressbar": True,
},
)
# ---------------------------------------------------------------------
# 4. Bayesian Inference
# ---------------------------------------------------------------------
fit_result = mmm.fit(X=X, y=y, tune=500, draws=500)
Although the code is relatively brief, the generated model is well and truly complex and would have required many lines of codes with PyMC, as shown in theimage below:
The model, once adjusted, allows to analyse the contributions of the different channels and control variables (not included in the example), an overview is provided in the chart below:

Beyond the inference, PyMC-Marketing also enables you to perform causal analysis (see this article of PyMC-Marketing)
For more information on the analyses causal, I invite you to read my article on the ladder of the causation by Judea Pearl (https://statiscau.ch/en/ljudea-pearl-causality-scale/).
