Measurement and attribution

PyMC-Marketing: a practical guide to the open-source Bayesian MMM

PyMC-Marketing is an open-source Python library from PyMC Labs for Bayesian marketing mix models. It handles carryover, diminishing returns, seasonality, lift-test calibration and budget optimization, and reports every answer as a range. It's free and transparent, but it's a toolkit for an analyst who knows Bayesian modeling, not a dashboard.

A measurement vendor quotes you a five-figure monthly fee for a marketing mix model. Meanwhile, the same core math sits in a free Python library that anyone can install in a minute.

So why doesn't everyone just use the free one? Because the library is the easy part. The hard parts are your data, your assumptions and knowing when the model is lying to you.

This guide covers PyMC-Marketing: what it is, what a real project looks like, and where teams get burned.

What PyMC-Marketing is, and why owning the model matters

PyMC-Marketing is an open-source Python library for marketing analytics, maintained by PyMC Labs and built on PyMC, a widely used tool for Bayesian statistics. It's licensed under Apache 2.0, which means you can use it commercially for free.

Its main job is marketing mix modeling (MMM): estimating how much each channel adds to sales from weekly spend and sales history. If you need a refresher, start with MMM in plain English. It also includes customer lifetime value models and a few other marketing models, but MMM is why most people show up.

The business case is control. With a vendor's model, you see outputs but rarely the assumptions. With an open-source model, every choice is visible. You can check why it says Meta returns 1.7x, change what you don't believe, and rerun it. When a model is helping move a seven-figure budget, being able to audit it matters.

The catch is people. A vendor sells you a team along with the model. With PyMC-Marketing, the team is you.

Why "Bayesian" matters for a budget decision

Most of the value comes from one idea. A Bayesian model starts with what you already believe, called a prior, and updates it with your data. The result, called the posterior, is a range of plausible answers rather than a single number.

That sounds academic. It isn't. Compare two readouts:

  • "Meta ROAS is 2.1x."
  • "Meta ROAS is most likely around 2.1x, and probably somewhere between 0.9x and 3.8x."

The second one tells you the model doesn't really know yet. Nobody should move half the budget on a range that wide. The first version hides that and invites a bad decision.

Priors also let you feed in outside evidence. If a lift test showed a channel's return, you can tell the model. That helps most when two channels always moved together and the data alone can't separate them.

What's in the box

The library covers the standard parts of a modern MMM, each of which you can configure or replace:

  • Adstock for carryover, the way an ad keeps working for weeks after it runs. Geometric adstock is the common default.
  • Saturation for diminishing returns, so each extra dollar does less. Logistic saturation is one built-in option.
  • Seasonality and controls for holidays, promotions, price changes and trends.
  • Time-varying effects, so the baseline or a channel's efficiency can drift over time.
  • Lift-test calibration, to anchor the model to experiment results. The lift test calibration guide describes each test as a channel, the spend before the test, the change in spend, the change in sales, and the uncertainty of that change.
  • Budget optimization, to find the split that maximizes expected contribution under your constraints. See the budget allocation example.
  • Diagnostics and out-of-sample predictions with credible intervals, so you can check fit before trusting anything.

The basic workflow in code is short. You import the MMM class with an adstock and a saturation function, point it at your date, channel and control columns, then call fit on your data. The README has a working quickstart. Everything that matters happens before and after that line.

A worked example: from wide guesses to a budget move

Say a supplements brand spends about $50k a week across four channels. It has 104 weeks of clean weekly data: spend per channel, revenue, a promo calendar and holiday flags.

Step 1: fit the model without experiments. Here are the ROAS ranges it returns. The range is a 90% credible interval: the model thinks the true value is 90% likely to fall inside it.

ChannelWeekly spendMedian ROAS90% range
Meta$20,0002.1x0.9x to 3.8x
Google search$15,0001.9x1.5x to 2.4x
TikTok$10,0001.1x0.4x to 3.0x
YouTube$5,0001.6x0.7x to 2.9x

Google is fairly tight. Meta and TikTok are wide. The brand always raised Meta and TikTok in the same weeks, so the model can't tell them apart.

Step 2: add a lift test. The brand ran a geo test on Meta. In test regions, it raised spend from the equivalent of $20,000 a week to $30,000. Sales in those regions rose by the equivalent of $16,000 a week, with a standard error of $4,000.

In the calibration format, that's:

  • channel: Meta
  • spend before (x): $20,000
  • change in spend (delta x): $10,000
  • change in sales (delta y): $16,000
  • uncertainty (sigma): $4,000

The test says the next $10,000 on Meta returned about $16,000 ÷ $10,000 = 1.6x.

Step 3: refit with the test.

ChannelMedian ROAS90% range before90% range after
Meta1.7x0.9x to 3.8x1.4x to 2.1x
TikTok1.3x0.4x to 3.0x0.7x to 2.2x

Meta's range tightens a lot. TikTok tightens a little too, because once the model knows Meta's effect, more of the shared movement can be assigned. TikTok is still uncertain. That's the next test.

Step 4: optimize the budget. The brand keeps the total at $50k a week and caps any channel's change at 30% so the model doesn't extrapolate far beyond spend it has seen.

ChannelCurrentOptimizedChange
Meta$20,000$18,000−$2,000
Google search$15,000$17,000+$2,000
TikTok$10,000$7,000−$3,000
YouTube$5,000$8,000+$3,000

The model estimates weekly contribution from paid media rises from about $88,000 (range $80,000 to $96,000) to about $93,000 (range $83,000 to $102,000). A gain of roughly $5,000 a week, or about $260,000 a year, from moving 10% of the budget.

Notice the ranges overlap. The optimized plan is probably better, not certainly better. The sensible move is to shift in steps and watch total revenue, not to flip everything on Monday.

Where teams get burned

It's a library, not a product. There's no account manager, no onboarding and no dashboard by default. If nobody on your team is comfortable with Python and Bayesian diagnostics, the model will sit unfinished.

Bad data in, confident nonsense out. Missing promo dates, broken spend history or mixed revenue definitions produce clean-looking wrong answers. Do a readiness check and fix your data foundation first.

Priors can steer the answer. A prior that's too confident can drag the result to what you expected. Always check how much the posterior moved away from the prior. If it barely moved, the data didn't say much. The model is just repeating your belief back to you.

Skipping diagnostics. Bayesian models are fit by sampling. If sampling didn't converge, the results are noise. Check convergence and posterior predictive fit before reading any ROAS.

The optimizer extrapolates. If you've never spent more than $10k a week on YouTube, the model is guessing about $20k. Put bounds on every channel close to what you've actually spent.

Correlated channels stay blurry. No amount of modeling separates two channels that always moved together. You need deliberate variation or a test. That's what step 2 above fixed.

Time-varying everything can overfit. Letting every parameter drift weekly can make the model explain noise. Use time-varying effects when you have a reason, like a known shift in strategy.

Fitting takes time. Sampling a large model can take a while, especially with many channels or geos. Plan for it in refresh cycles.

Who should use it

PyMC-Marketing is a strong fit if:

  • You have at least one analyst who knows Python and is willing to learn Bayesian workflow properly.
  • You have a couple of years of weekly data with real variation in spend.
  • You want to own and audit the model rather than rent it.
  • You plan to run lift tests and want a model that can use them.

It's a weak fit if you need answers next week, have under a year of history, or have nobody to maintain it. Other open-source options exist too, such as Google's Meridian and Meta's Robyn. The bigger choice is whether you have the people and the data, not which library.

What to do Monday

  • Check your data first: 104-plus weeks of spend by channel, revenue, promos and holidays, all in one table with one revenue definition.
  • Look for variation: find weeks where each major channel moved without the others. If there are none, plan some.
  • Gather any past tests in the calibration format: channel, spend before, change in spend, change in sales, uncertainty.
  • Have your analyst run the quickstart on the example data before touching yours. It takes an afternoon and shows what the work looks like.
  • Write priors down with reasons. "Meta ROAS likely 1x to 3x, based on our geo test" is a prior. "Feels right" isn't.
  • Hold back the last 8 to 12 weeks to check that the model predicts data it hasn't seen.
  • Bound the optimizer to spend levels you've actually run, and move budget in steps.
  • Plan the next test for the channel with the widest range.

An AI ad agent can borrow the same discipline even without a full model: treat return estimates as ranges, prefer evidence from tests, and move budget in steps when the ranges overlap.

Free software, expensive judgment

That vendor quote wasn't for the math. The math is free, and PyMC-Marketing gives you a solid, transparent version of it.

What you're really buying, or building, is the judgment around it: clean data, honest priors, real tests and the patience to move money in steps. Get those right and a free library can steer a large budget. Get them wrong and no price tag will save the model.

Is Your Company Ready for Marketing Mix Modeling? A 7-Check Test

Is your company ready for marketing mix modeling (MMM)? Run 7 checks on your data, compare Meridian, Robyn and PyMC-Marketing, and see what to fix first.

Marketing mix modeling in plain English

Marketing mix modeling (MMM) estimates what each channel adds to sales using spend and sales history, no tracking needed. How it works, with a worked example.

Incrementality testing: would that sale have happened anyway?

Incrementality testing compares people who saw your ads with a holdout who didn't, to count the sales ads actually caused. How it works, with a worked example.

Google Meridian MMM: what it's good for, and where it isn't

Google Meridian is a free Bayesian marketing mix model with geo-level data, reach and frequency, and experiment-based ROI priors. What it does well and where it fails.

Have a correction or a question about the workflow? Contact GaaS. Read our editorial standards for sourcing and example conventions.