Mastering Bayesian Media Mix Modeling (MMM) With Pymc-marketing
Ditch the black-box attribution. Our guide to PyMC-Marketing helps you build a transparent Bayesian MMM to master adstock and saturation.

Introduction: Why Media Mix Modeling (MMM) Matters Now?
In today’s data-driven landscape, one question keeps marketing teams awake: What is the true return on every dollar spent?
As privacy regulations tighten and consumer journeys become more fragmented, traditional "last-click" attribution is falling short. This is where Media Mix Modeling (MMM) shines. It uses historical, aggregated data to estimate the contribution of marketing activities to your KPIs (like sales or conversions).
Unlike simplistic methods, a robust MMM accounts for three critical real-world dynamics:
- Adstock (Carryover Effects): The impact of an ad often lingers days or weeks after the initial exposure.
- Saturation (Diminishing Returns): There is a "ceiling" to effectiveness; eventually, spending more yields less marginal gain.
- Baseline Effects: Factoring in non-media drivers like seasonality, holidays, and organic brand strength.
The Power of the Bayesian Approach
PyMC-Marketing is a leading open-source library that brings the power of Bayesian statistics to marketing analytics. By moving beyond simple point estimates, Bayesian modeling allows you to:
- Incorporate Prior Knowledge: Use historical data or business constraints to inform the model.
- Quantify Uncertainty: Instead of a single ROI number, you get a range of probable outcomes, making decision-making far more realistic.
- Gain Full Transparency: No "black boxes"—you have complete control over the model's assumptions and structure.
In a previous blog post, we explored MMM using Meridian from Google. In this post, we focus on another open source library PyMC-Marketing, highlighting its flexibility and transparency through a hands-on example. (For more info about Meridian, visit the official website)
The remainder of this post walks through an end-to-end MMM analysis implemented in a Jupyter Notebook, covering data loading, model specification, inference, and budget optimization. All details are available in this repository.
- Visit this website, for more information about PyMC-Marketin
- This article explains Bayesian MMM modeling
Setting Up Your Environment
To get started with this hands-on implementation, follow these steps to set up your local Python environment:
Next, we import the core libraries for data manipulation, visualization, and the specialized Bayesian tools from PyMC-Marketing:
Data Preparation: Building the Foundation
A high-quality MMM requires structured time-series data, usually at a weekly or daily granularity. Your dataset should include your KPI (sales), Media Spend per channel, and Control Variables (such as seasonality, or promotions).
For this notebook, we have generated a synthetic dataset, details can be found in the jupyter notebook present in the github repository. For simplicity we directly load the generated csv.
Exploratory Data Analysis (EDA)
Before modeling, you must understand your data's "heartbeat." Visualizing sales over time helps identify trends and recurring "valleys," such as the common dip in late summer before the Q4 recovery.

To better understand sales trends we can also look at the rolling average of sales over time. The rolling average (the blue line chart below) is a "smoothing" technique used to filter out the weekly "noise" and reveal the underlying direction of the business.

Analyzing Spend and Correlation
While correlation between spend and sales is a good starting point, it can be misleading due to confounding factors like seasonality. In this example, we observe that while Channel 2 acts as a "sales trigger" with quick spikes, Channels 1 and 3 function as brand builders.

Building the Media Mix Model
We respect the temporal nature of marketing by using a time-series split, reserving the last 10% of data as a "test set" to evaluate how the model predicts future performance .

The plot above represents the evolution of sales over time, with the dataframe being split into training period (blue) and test period (orange). We can now proceed to define training and test sets for modeling.
Defining Priors and Model Specifications
Bayesian modeling allows us to set "priors"—initial beliefs about how our marketing works. For instance, we use a HalfNormal distribution for the intercept to ensure the baseline sales contribution is never negative.
In the following code, we use spend shares per channels to scale certain priors:
We are ready to use these priors for the saturation coefficients in the model configuration.
The model uses Markov Chain Monte Carlo (MCMC) sampling to estimate the values of unknown parameters. By setting time_varying_intercept=False and using Fourier terms, we allow the model to learn smooth, recurring annual seasonality patterns.
The MMM object is equipped with a set of methods (or functions) that enable the MCMC sampling algorithm.
Model Fitting and Diagnostics
Once configured, we run the MCMC sampler. This is where the model "learns" from your data.
Health Check: R-hat and Trace Plots
To trust your model, you must verify convergence.
- R-hat Statistic: Ideally, this should be 1.00. Anything above 1.1 suggests the model is still "wandering" and hasn't found a stable answer.
- Trace Plots: Look for the "fuzzy caterpillar" pattern. This indicates that the sampling process was stable and consistent across multiple runs.
R-hat Statistic
R-hat is a mathematical measure of how well MCMC chains have converged to a stable posterior distribution. It compares the variance between different chains to the variance within each chain. In simpler words, it tells you whether the MCMC algorithm actually found the right answers or if it's still wandering around lost.
Ideally, R-hat should be less than 1.05 and if it is above 1.1, then you need to fit the model again by trying different target_accept or re-parametrizing.
Also, we can look at R-hat with the following code:
Showing R-hat statistics are all 1.
Trace Plots
Trace plot is a visual history of the learning process your model went through during its estimation phase. While R-hat gives you a single number to check for health, a trace plot shows you the "heartbeat" of your model.
A trace plot typically consists of two parts for every parameter, first part is the KDE (Kernel Density Estimate), it is a distribution curve showing where the model thinks the value most likely sits. The second part is path plot, which is a time-series graph that tracks the journey of the model’s parameters during the fitting process.

The "fuzzy caterpillar" patterns in the right-hand trace plots indicate that the sampling process was stable, while the overlapping density curves in the left-hand plots show that the model reached a consistent consensus across multiple independent runs.
To get more clarity on trace plots, look at an example of what a trace plot shouldn’t look like.
Deep Dive: Extracting Actionable Insights
The "why" of MMM is to inform better spending. We can now decompose our KPI to see exactly what drove sales.
1. Contribution Breakdown
In this scenario, the Baseline (Intercept) accounts for 40% of sales, indicating strong organic brand establishment. Among media, Channel 3 is the top contributor (30%), followed by Channel 1 (24%).


2. Saturation and Response Curves
"Hilltop" curves visualize the relationship between spend and return. All channels here show a concave shape, meaning that as you spend more, each additional dollar generates a smaller incremental return .

These Direct Response Curves (also known as saturation or hilltop curves) visualize the relationship between marketing spend and the resulting contribution for three different channels. Read more about the Diminishing Return in this post.
3. ROI with Credible Intervals
Unlike traditional tools that give you a single "ROI number," PyMC-Marketing provides a posterior distribution. For example, Channel 3 is a clear winner because even its "worst-case scenario" ROI is often better than the mean ROI of other channels.

4. Share of Spend vs. Share of Revenue
Next we can plot spend and revenue for each channel with ROAS, which shows how much each channel is contributing to the revenue (or sales) relative to its share within the marketing mix.

5. Return on Ad Spend (ROAS)
We can also look at ROAS with error bars (credible intervals) as follows.

Predictions on test set
Let us now take a look at how the model performs on the test data, PyMC-Marketing generates prediction from a fitted model as follows:
Next, one can visualize training set’s fit and testing set’s prediction together as follows:

The vertical line splits the time frame into training and testing periods. The model’s fit looks good. Let us look at the test period more closely.

Overall the performance of the model looks good on the test set, it captures the trend really well with MAPE on the test set as 0.12.
Conclusion
PyMC-Marketing transforms historical data into a strategic roadmap. By moving beyond simple regressions, you can:
- Model Adstock and Saturation with realistic complexity.
- Make uncertainty-aware decisions that reflect the real world.
- Drive forward-looking growth through automated budget optimization.
Ready to start? You can explore the full code and example notebooks on GitHub.
Scale Your Marketing with Custom MMM Solutions
Ready to unlock the true potential of your marketing spend? At ELIYA, we help brands navigate complexity by developing custom, tailored MMM solutions designed to maximize ROI and optimize every dollar of your budget.
FAQs
1. What is Media Mix Modeling (MMM) and why is it essential for modern brands?
Media Mix Modeling (MMM) is a high-level statistical analysis used for comprehensive marketing measurement. By evaluating historical aggregate data, MMM quantifies the effectiveness of various marketing channels on sales. It is essential today because it provides a privacy-safe way to achieve accurate marketing impact measurement without relying on individual user tracking or cookies.
2. How does Bayesian modeling improve marketing spend optimization?
Unlike traditional "frequentist" statistics, Bayesian media mix modeling allows businesses to incorporate "priors" or existing business knowledge into the model. This results in more stable and realistic marketing spend optimization, as the model can better navigate data volatility and provide a range of probable outcomes rather than a single, potentially misleading number.
3. What is the role of saturation in marketing budget optimization?
In any marketing budget optimization strategy, understanding saturation is key. Every channel eventually hits a point of diminishing returns where spending more doesn't lead to a proportional increase in sales. PyMC-Marketing models these "Hilltop" curves, allowing you to identify exactly when to stop spending on one channel and shift resources to another for better marketing allocation optimization.
4. Can Media Mix Modeling provide accurate marketing impact measurement for offline channels?
Yes. One of the greatest strengths of media mix modeling is its ability to measure the impact of both digital and offline channels (like TV, Radio, or OOH) simultaneously. By analyzing spend and results over time, it provides a holistic view of marketing measurement, ensuring that offline contributions are not ignored in your overall growth strategy.
5. How often should we run a marketing allocation optimization analysis?
While traditional MMM was an annual event, modern tools like PyMC-Marketing allow for more frequent updates. We recommend performing marketing allocation optimization on a quarterly or even monthly basis. Regular marketing measurement ensures your strategy remains agile, allowing you to adjust to market shifts and competitor activity in real-time.
6. Why should I choose ELIYA for my marketing measurement solutions?
At ELIYA, we specialize in moving beyond "off-the-shelf" software. We develop custom, tailored media mix modeling solutions that adapt to your specific and complexity of the real business nuances. By focusing on precise marketing impact measurement and data-driven marketing budget optimization, we empower your brand to spend smarter and grow faster.
For more information, please visit our Marketing Analytics service page
7. What is PyMC-Marketing?
PyMC-Marketing is an open-source library built on top of PyMC, designed specifically for marketing analytics tasks such as Media Mix Modeling (MMM), adstock modeling, saturation modeling, and budget optimization using Bayesian methods.
PyMC-Marketing is a library created by using the core PyMC library.
8. What kind of models can I build with PyMC-Marketing?
PyMC-Marketing supports a range of marketing models including:
- Bayesian Media Mix Models
- Adstock and Saturation response curves
- Hierarchical MMM (multi-market or multi-brand)
- Seasonality and control variables
- Budget optimization based on posterior estimates
For more information, we refer the readers to PyMC-Marketing's GitHub repository
9. Can I use PyMC-Marketing for budget optimization?
Yes. PyMC-Marketing includes functionality to perform budget allocation optimization using the posterior distribution of channel effects. This helps identify the mix of spend that maximizes ROI under realistic constraints.
10. What are the key differences between Robyn, Meridian, and PyMC-Marketing?
While all three are leading open-source frameworks for media mix modeling, they serve different technical needs and business goals:
- Robyn (Meta): Developed by Meta, Robyn is primarily R-based and utilizes machine learning (Ridge Regression) rather than a purely Bayesian approach. It is highly automated and semi-autonomous, making it a popular choice for digital-first teams looking for a fast, accessible entry into marketing impact measurement.
- Meridian (Google): Google’s newest Bayesian framework is designed to integrate deeply with the Google ecosystem, incorporating unique data points like Google Search Volume and YouTube reach/frequency. It is an excellent tool for marketing spend optimization, particularly for brands with high investment in Google’s advertising platforms. For a comparison between Meridian and Robyn, see this post.
- PyMC-Marketing: As a Python-native library, PyMC-Marketing offers the highest level of flexibility and transparency. It allows data scientists to build highly customized models with bespoke priors and time-varying parameters. This granular control makes it the "gold standard" for advanced marketing budget optimization and long-term marketing allocation optimization where business-specific nuances are critical to the model's success.



