PyMC-Marketing

raw JSON →
0.19.3 verified Mon Apr 27 auth: no python

A Python library for marketing statistical models built on top of PyMC. It provides Bayesian approaches to marketing mix modeling (MMM), customer lifetime value (CLV), and other marketing analytics. Current version 0.19.3, requires Python >=3.12. Released under active development with frequent updates.

pip install pymc-marketing
error ModuleNotFoundError: No module named 'pymc_marketing'
cause Package not installed or installed under a different name.
fix
Run: pip install pymc-marketing
error AttributeError: module 'pymc_marketing' has no attribute 'MMM'
cause MMM is not imported directly from the top-level package; it's in a submodule.
fix
Use: from pymc_marketing.mmm import MMM
error ValueError: Date column must be datetime64[ns] dtype
cause The date column is not in datetime format.
fix
Convert the column to datetime: data['date'] = pd.to_datetime(data['date'])
breaking Version 0.19.x introduced breaking changes in the MMM API: the `adstock_max_lag` and `yearly_seasonality` parameters are now required and the default prior distributions changed.
fix Update code to include explicit adstock_max_lag and yearly_seasonality parameters; review prior specifications.
gotcha The MMM model requires a DataFrame with a date column; the column must be in datetime format. Passing strings will cause an error.
fix Ensure date column is converted to datetime: data['date'] = pd.to_datetime(data['date'])
gotcha When using the CLV module, the `CLVModel` expects a specific data shape with columns 'frequency', 'recency', 'T', and 'monetary_value'. Missing columns will raise a KeyError.
fix Verify data has the required columns before fitting.
pip install 'pymc-marketing[all]'

Fits a Bayesian Marketing Mix Model using PyMC.

import pandas as pd
from pymc_marketing.mmm import MMM

# Load some data (replace with real data)
data = pd.DataFrame({
    'date': pd.date_range('2022-01-01', periods=100),
    'spend': np.random.uniform(100, 1000, 100),
    'sales': np.random.normal(5000, 500, 100)
})
# Create model with dummy priors
mmm = MMM(
    date_column='date',
    channel_columns=['spend'],
    control_columns=[],
    adstock_max_lag=4,
    yearly_seasonality=2
)
# Sample (small draws for quick demo)
mmm.fit(data, draws=100, tune=100, chains=1)
print(mmm.summary())