Skip to content

Latest commit

Β 

History

1,486 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

PyMC-Marketing Logo


Test Test Notebook codecov Ruff docs

PyPI Version License

Downloads Downloads Downloads

PyMC-Marketing

Bayesian Tools for Marketing Analytics: Marketing Mix Modeling (MMM), Customer Lifetime Value (CLV), Customer Choice, Incrementality and more


Marketing Analytics Tools from PyMC Labs

Unlock the power of Marketing Mix Modeling (MMM), Customer Lifetime Value (CLV), Customer Choice (discrete choice, MaxDiff, Bayesian BLP), Bass Diffusion, and Predicted Incrementality by Experimentation (PIE) analytics with PyMC-Marketing. This open-source marketing analytics tool empowers businesses to make smarter, data-driven decisions for maximizing ROI in marketing campaigns.

This repository is supported by PyMC Labs.

For businesses looking to integrate PyMC-Marketing into their operational framework, PyMC Labs offers expert consulting and training. Our team is proficient in state-of-the-art Bayesian modeling techniques, with a focus on Marketing Mix Models (MMMs) and Customer Lifetime Value (CLV). For more information see here.

Explore these topics further by watching our video on Bayesian Marketing Mix Models: State of the Art.

Community Resources

Quick Installation Guide

PyMC-Marketing is built on top of PyMC >= 6.0 and ArviZ >= 1.2 (with arviz-plots for visualization), bringing the latest Bayesian sampling and plotting stack to marketing analytics. PyMC-Marketing requires Python >= 3.12.

Install PyMC-Marketing with pip:

pip install pymc-marketing

Some features are available as optional extras:

pip install pymc-marketing[dag]  # causal identification tooling
pip install pymc-marketing[pie]  # Predicted Incrementality by Experimentation (PIE), requires pymc-bart

For a comprehensive installation guide, refer to the installation documentation.

Docker

We provide a Dockerfile to build a Docker image for PyMC-Marketing so that is accessible from a Jupyter Notebook. See here for more details.

In-depth Bayesian Marketing Mix Modeling (MMM) in PyMC

Leverage our Bayesian MMM API to tailor your marketing strategies effectively. Leveraging on top of the research article Jin, Yuxue, et al. β€œBayesian methods for media mix modeling with carryover and shape effects.” (2017), and extending it by integrating the expertise from core PyMC developers, our API provides:

Feature Benefit
Custom Priors and Likelihoods Tailor your model to your specific business needs by including domain knowledge via prior distributions.
Adstock Transformation Optimize the carry-over effects in your marketing channels.
Saturation Effects Understand the diminishing returns in media investments.
Customize adstock and saturation functions You can select from a variety of adstock and saturation functions. You can even implement your own custom functions. See documentation guide.
Time-varying Intercept Capture time-varying baseline contributions in your model (using modern and efficient Gaussian processes approximation methods). See guide notebook.
Time-varying Media Contribution Capture time-varying media efficiency in your model (using modern and efficient Gaussian processes approximation methods). See the guide notebook.
Visualization and Model Diagnostics Get a comprehensive view of your model's performance and insights.
Causal Identification Input a business driven directed acyclic graph to identify the meaningful variables to include into the model to be able to draw causal conclusions. For a concrete example see the guide notebook.
Choose among many inference algorithms We provide the option to choose between various NUTS samplers (e.g. BlackJax, NumPyro and Nutpie). See the example notebook for more details.
GPU Support PyMC's multiple backends allow for GPU acceleration.
Out-of-sample Predictions Forecast future marketing performance with credible intervals. Use this for simulations and scenario planning.
Budget Optimization Allocate your marketing spend efficiently across various channels for maximum ROI. See the budget optimization example notebook
Experiment Calibration Fine-tune your model based on empirical experiments for a more unified view of marketing. See the lift test integration explanation for more details. Here you can find a Case Study: Unobserved Confounders, ROAS and Lift Tests.
ROAS / CAC Calibration Calibrate your model with ROAS or cost-per-acquisition estimates via add_cost_per_target_calibration. See the ROAS calibration notebook for a worked example.
Funnel Models Model upper-funnel to lower-funnel mediation (e.g., awareness driving search and conversions) via custom MuEffects. See the introductory funnel-aware MMM notebook and the advanced geo-level example.

MMM Quickstart

The following snippet of code shows how to initiate and fit a MMM model.

import pandas as pd
from pymc_marketing.mmm import (
    MMM,
    GeometricAdstock,
    LogisticSaturation,
)
from pymc_marketing.paths import data_dir

file_path = data_dir / "mmm_example.csv"
data = pd.read_csv(file_path, parse_dates=["date_week"])

mmm = MMM(
    adstock=GeometricAdstock(l_max=8),
    saturation=LogisticSaturation(),
    date_column="date_week",
    channel_columns=["x1", "x2"],
    control_columns=[
        "event_1",
        "event_2",
        "t",
    ],
    yearly_seasonality=2,
)

X = data.drop("y", axis=1)
y = data["y"]
mmm.fit(X, y)

After the model is fitted, we can explore the results and insights. For example, we can plot the components contributions:

You can compute channels efficiency and compare them with the estimated return on ad spend (ROAS).

Once the model is fitted, we can further optimize our budget allocation as we are including diminishing returns and carry-over effects in our model.

  • Explore our hands-on quickstart guide and more complete simulated example for more insights into MMM with PyMC-Marketing.
  • Get started with a complete end-to-end analysis: from model specification to budget allocation. See the guide notebook.

Long-Term Effects & Brand Metrics

Media investments do not only drive short-term sales; they also build brand equity that pays off over longer horizons. PyMC-Marketing lets you measure long-term brand effects in MMMs by coupling brand-tracking metrics (e.g., awareness, consideration) with a Bayesian VARX model. See the long-term brand effects notebook for a complete tutorial.

Essential Reading for Marketing Mix Modeling (MMM)

Explainer App: Streamlit App of MMM Concepts

Dynamic and interactive visualization of key Marketing Mix Modeling (MMM) concepts, including adstock, saturation, and the use of Bayesian priors. This app aims to help marketers, data scientists, and anyone interested in understanding MMM more deeply.

Check out the app here

Unlock Customer Lifetime Value (CLV) with PyMC

Understand and optimize your customer's value with our CLV models. Our API supports various types of CLV models, catering to both contractual and non-contractual settings, as well as continuous and discrete transaction modes.

Examples

Non-contractual Contractual
Continuous online purchases ad conversion time
Discrete concerts & sports events recurring subscriptions

CLV Quickstart

import matplotlib.pyplot as plt
import pandas as pd
from pymc_marketing import clv
from pymc_marketing.paths import data_dir

file_path = data_dir / "clv_quickstart.csv"
data = pd.read_csv(file_path)
data["customer_id"] = data.index

beta_geo_model = clv.BetaGeoModel()

beta_geo_model.fit(data=data)

Once fitted, we can use the model to predict the number of future purchases for known customers, the probability that they are still alive, and get various visualizations plotted.

See the Examples section for more on this.

Customer Choice Analysis with PyMC-Marketing

Understand customer choice behavior with a full suite of models: Multivariate Interrupted Time Series (MVITS) for product launch impact, discrete choice models (multinomial, nested, and mixed logit), MaxDiff (best-worst scaling), and Bayesian BLP for structural demand estimation on aggregate market shares.

Product Launch Impact with MVITS

Analyze the impact of new product launches with our MVITS models. Our API supports analysis in both saturated and unsaturated markets to help you:

Feature Benefit
Market Share Analysis Understand how new products affect existing product market shares
Causal Impact Assessment Measure the true causal effect of product launches on sales
Saturated Market Analysis Model scenarios where total market size remains constant
Unsaturated Market Analysis Handle cases where new products grow the total market size
Visualization Tools Plot market shares, causal impacts, and counterfactuals
Bayesian Inference Get uncertainty estimates around all predictions

Customer Choice Quickstart

import pandas as pd
from pymc_marketing.customer_choice import MVITS, plot_product

# Define existing products
existing_products = ["competitor", "own"]

# Create MVITS model
mvits = MVITS(
    existing_sales=existing_products,
    saturated_market=True, # Set False for unsaturated markets
)

# Fit model
mvits.fit(X, y)

# Plot causal impact on market share
mvits.plot_causal_impact_market_share()

# Plot counterfactuals
mvits.plot_counterfactual()

See our example notebooks for saturated markets and unsaturated markets to learn more about customer choice modeling with PyMC-Marketing.

Discrete Choice Models

Discrete choice models come in various forms, but each aims to show how choosing between a set of alternatives can be understood as a function of the observable attributes of the alternatives at hand. This type of modelling drives insight into the "must-have" features of a product, and can be used to assess the success or failure of product launches or re-launches. The PyMC-Marketing implementation offers a formula based model specification, for estimating the relative utility of each good in a market and identifying their most important features.

Explore the full family of choice and preference models:

Bass Diffusion Model

The Bass Diffusion Model is a popular model for predicting the adoption of new products. It is a type of product life cycle model that describes the market penetration of a new product as a function of time. PyMC-Marketing provides a flexible implementation of the Bass Diffusion Model, allowing you to customize the model parameters and fit the model to your specific data (many products).

See the Bass Diffusion Model example notebook for a worked example.

Predicted Incrementality by Experimentation (PIE)

Predict the incremental effect of ad campaigns that never ran an experiment with PIE (alpha). Randomized experiments β€” geo tests and ghost-ad holdouts β€” are the gold standard for measuring campaign incrementality, but they are costly and slow. PIE fits a Bayesian BART model on the corpus of campaigns that did run an experiment, learning the map from campaign features to measured incrementality, then predicts a full posterior of incrementality for the campaigns that never did. The approach follows Gordon, Moakler & Zettelmeyer (2026).

The pymc_marketing.pie module is in alpha: the API and defaults may change between releases. It requires the pie extra (pip install pymc-marketing[pie]). See the PIE example notebook for a worked example, including where predictions beat last-click attribution.

Why PyMC-Marketing vs other solutions?

PyMC-Marketing is and will always be free for commercial use, licensed under Apache 2.0. Developed by core developers behind the popular PyMC package and marketing experts, it provides state-of-the-art measurements and analytics for marketing teams.

Due to its open-source nature and active contributor base, new features are constantly added. Are you missing a feature or want to contribute? Fork our repository and submit a pull request. If you have any questions, feel free to open an issue.

Thanks to our contributors!

https://github.com/pymc-labs/pymc-marketing/graphs/contributors

Marketing AI Assistant: MMM-GPT with PyMC-Marketing

Not sure how to start or have questions? MMM-GPT is an AI that answers questions and provides expert advice on marketing analytics using PyMC-Marketing.

Try MMM-GPT here.

πŸ“ž Schedule a Free Consultation for MMM & CLV Strategy

Maximize your marketing ROI with a free 30-minute strategy session with our PyMC-Marketing experts. Learn how Bayesian Marketing Mix Modeling and Customer Lifetime Value analytics can boost your organization by making smarter, data-driven decisions.

We provide the following professional services:

  • Custom Models: We tailor niche marketing analytics models to fit your organization's unique needs.
  • Build Within PyMC-Marketing: Our team members are experts leveraging the capabilities of PyMC-Marketing to create robust marketing models for precise insights.
  • SLA & Coaching: Get guaranteed support levels and personalized coaching to ensure your team is well-equipped and confident in using our tools and approaches.
  • SaaS Solutions: Harness the power of our state-of-the-art software solutions to streamline your data-driven marketing initiatives.

About

Bayesian marketing toolbox in PyMC. Media Mix (MMM), customer lifetime value (CLV), buy-till-you-die (BTYD) models and more.

Topics

Resources

Contributing

Stars

1.2k stars

Watchers

34 watching

Forks

Releases

Used by

Contributors

Languages