{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "(mmm_quickstart)=\n", "\n", "# MMM Quickstart Guide\n", "\n", "Welcome to **PyMC-Marketing**! This library provides powerful Bayesian modeling tools for marketing analytics.\n", "PyMC-Marketing is built on top of [PyMC](https://www.pymc.io/), a probabilistic programming library that enables Bayesian inferenc. In this quickstart, we'll walk through fitting a basic Media Mix Model (MMM) in PyMC-Marketing.\n", "\n", "## What is Media Mix Modeling?\n", "\n", "Media Mix Modeling (MMM) helps marketers understand how different advertising channels contribute to business outcomes (like sales or conversions). MMM answers key questions:\n", "\n", "- **Which channels drive the most sales?**\n", "- **What is the Return on Ad Spend (ROAS) for each channel?**\n", "- **How should I allocate my marketing budget?**\n", "\n", "### Key Concepts\n", "\n", "MMM accounts for two important phenomena in advertising:\n", "\n", "1. **Adstock (Carry-over effect)**: The impact of advertising doesn't happen instantaneously—it builds up over time and gradually decays.\n", "2. **Saturation**: Returns diminish as you increase spend—the first dollar spent is more effective than the millionth dollar.\n", "\n", "Let's see how we can fit a basic MMM model to understand these effects and measure channel contributions.\n", "\n", "```{note}\n", "The focus of PyMC-Marketing is to provide tooling for real application. Typically, we need to think about the causal structure, lift test calibration and advanced budget optimization. This example should be seen as a first step to a more complex and rich tool-box to drive marketing decisions of the order of millions of dollars. In our example gallery, you will find extensive resources to help you and guide you through the MMM modeling iterative process.\n", "```\n", "\n", "```{tip}\n", "- For an extended version of this example, see {ref}`mmm_example`. Here we delve deeper into the data generating process and model diagnostics. We also include ROAS estimation and out of sample predictions.\n", "- If you want to see a complete end-to-end analysis, see {ref}`mmm_case_study`. Here we take a \"real\" world dataset and go through the entire process of model specification, fitting, optimization and scenario planning.\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Prepare Notebook\n", "\n", "Let's import the necessary libraries:\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import arviz as az\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import pandas as pd\n", "from pymc_extras.prior import Prior\n", "\n", "from pymc_marketing.mmm import MMM, GeometricAdstock, LogisticSaturation\n", "\n", "az.style.use(\"arviz-darkgrid\")\n", "plt.rcParams[\"figure.figsize\"] = [12, 7]\n", "plt.rcParams[\"figure.dpi\"] = 100\n", "\n", "%config InlineBackend.figure_format = \"retina\"\n", "\n", "%load_ext autoreload\n", "%autoreload 2" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# Set random seed for reproducibility\n", "seed = sum(map(ord, \"mmm\"))\n", "rng = np.random.default_rng(seed=seed)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load Data\n", "\n", "We'll use a synthetic dataset that simulates weekly sales data along with spend on two marketing channels (`x1` and `x2`), plus some control variables for special events.\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Data shape: (179, 8)\n" ] }, { "data": { "text/html": [ "
\n", " | date_week | \n", "y | \n", "x1 | \n", "x2 | \n", "event_1 | \n", "event_2 | \n", "dayofyear | \n", "t | \n", "
---|---|---|---|---|---|---|---|---|
0 | \n", "2018-04-02 | \n", "3984.662237 | \n", "0.318580 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "92 | \n", "0 | \n", "
1 | \n", "2018-04-09 | \n", "3762.871794 | \n", "0.112388 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "99 | \n", "1 | \n", "
2 | \n", "2018-04-16 | \n", "4466.967388 | \n", "0.292400 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "106 | \n", "2 | \n", "
3 | \n", "2018-04-23 | \n", "3864.219373 | \n", "0.071399 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "113 | \n", "3 | \n", "
4 | \n", "2018-04-30 | \n", "4441.625278 | \n", "0.386745 | \n", "0.0 | \n", "0.0 | \n", "0.0 | \n", "120 | \n", "4 | \n", "