Metadata-Version: 2.5
Name: how-wrong-is-your-mmm
Version: 0.1.0
Summary: Collinearity diagnostics and budget phasing tools for Marketing Mix Models
Project-URL: Homepage, https://raz1470.github.io/how_wrong_is_your_mmm/overview.html
Project-URL: Repository, https://github.com/raz1470/how_wrong_is_your_mmm
Project-URL: Documentation, https://raz1470.github.io/how_wrong_is_your_mmm/api/
Project-URL: Research, https://raz1470.github.io/how_wrong_is_your_mmm/collinearity_research.html
Author-email: Ryan O'Sullivan <raz1470@hotmail.co.uk>
License: MIT
License-File: LICENSE
Keywords: bayesian,collinearity,econometrics,marketing-analytics,marketing-mix-modeling,media-planning,mmm
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Office/Business :: Financial
Classifier: Topic :: Scientific/Engineering
Requires-Python: >=3.12
Requires-Dist: numpy>=1.26
Requires-Dist: pandas>=2.0
Description-Content-Type: text/markdown

# How Wrong Is Your MMM?

[![CI](https://github.com/raz1470/how_wrong_is_your_mmm/actions/workflows/ci.yml/badge.svg)](https://github.com/raz1470/how_wrong_is_your_mmm/actions/workflows/ci.yml)

**Measure how reliable your MMM is, and phase your budget to make it more reliable.**

Marketing budgets are planned together and follow demand, so a marketing mix model (MMM) struggles to tell channels apart. This package simulates your own spend to measure the damage on three fronts (variance, bias, and whether saturation and adstock can be recovered at all), then recommends a weekly spend schedule that fixes it without changing any channel's annual budget.

Everything is measured by simulation against marginal returns you supply, not against ground truth in your data.

**[Read the overview](https://raz1470.github.io/how_wrong_is_your_mmm/overview.html)** for the full walkthrough on a worked example.

![One pipeline, three steps. Diagnose: simulate plausible histories and measure all three problems. Phase: vary each channel on its own while the annual budget stays the same. Retrain: refit on the phased data and get a tighter answer back.](https://raw.githubusercontent.com/raz1470/how_wrong_is_your_mmm/main/assets/readme-pipeline.png)

- [**Overview**](https://raz1470.github.io/how_wrong_is_your_mmm/overview.html): the problem, the fix and what it costs.
- [**Example report**](https://raz1470.github.io/how_wrong_is_your_mmm/example-report.html): the HTML report the package produces, on simulated data.
- [**API reference**](https://raz1470.github.io/how_wrong_is_your_mmm/api/): every class and function.

---

## Quick start

```bash
pip install how-wrong-is-your-mmm
```

Or from source:

```bash
git clone https://github.com/raz1470/how_wrong_is_your_mmm
cd how_wrong_is_your_mmm
uv venv --python 3.12 && uv sync
```

### Your inputs

Every step takes the same inputs: your spend, and plausible values from your MMM. The baseline inputs are used by the report only.

```python
# history: your multi-year weekly spend, one column per channel (DatetimeIndex)
# plan:    the upcoming year's weekly spend plan (DatetimeIndex, same channels)

marginal_returns = {"tv": 1.8, "paid_social": 2.4, "search": 4.1}  # revenue from the next £1
saturation = {"tv": 0.6, "paid_social": 0.75, "search": 0.9}  # curve exponent, 1.0 is linear
adstock = {"tv": 0.5, "paid_social": 0.3, "search": 0.1}  # share of the effect carried into next week
noise_std = 50_000  # weekly sales noise sd in GBP
baseline_share = 0.7  # share of sales you would get with no marketing
baseline_cv = 0.05  # how much that baseline moves week to week (sd / mean)
```

### 1. Diagnose

How much do your marginal-return estimates swing, given your own spend history?

```python
from how_wrong_is_your_mmm import CollinearityDiagnostic

diag = CollinearityDiagnostic(
    spend_df=history,
    true_marginal_returns=marginal_returns,
    saturation=saturation,
    adstock=adstock,
    revenue_noise_std=noise_std,
)
diag.fit()
diag.summary()
```

### 2. Phase

Get a weekly schedule that breaks the correlation between channels:

```python
from how_wrong_is_your_mmm import BudgetPhaser

phaser = BudgetPhaser(
    history_df=history,
    plan_df=plan,
    true_marginal_returns=marginal_returns,
    saturation=saturation,
    adstock=adstock,
    revenue_noise_std=noise_std,
)
phaser.fit()
phaser.recommended_schedule_  # 52-week DataFrame, monthly totals guaranteed to match
phaser.recommended_cost_  # spend saved and revenue given up, per channel
```

To go dark for four weeks a year and keep the saving, rather than move it to another month, pass `max_weekly_deviation_pct=Redistribute(reallocate=False)`. The plan then spends less, and `recommended_cost_` shows how much revenue that gives up.

### 3. Report

Compare the phasing strategies and package the result as one HTML report, with the winning schedule as a CSV:

```python
from how_wrong_is_your_mmm import DiscoveryReport

report = DiscoveryReport(
    history_df=history,
    plan_df=plan,
    true_marginal_returns=marginal_returns,
    saturation=saturation,
    adstock=adstock,
    revenue_noise_std=noise_std,
    baseline_share=baseline_share,
    baseline_cv=baseline_cv,
    client_name="Example Brand",
    # optional: pin a strategy instead of sweeping for one, with per-channel overrides
    # strategy_pct=60.0,
    # channel_constraints={"paid_social": 20.0},
)
report.fit()
report.to_html("reports/example_brand.html")  # self-contained HTML, open it in a browser
report.schedule_csv("reports/example_brand_schedule.csv")  # the recommended weekly schedule
```

`true_marginal_returns` and the noise level are the two inputs that matter most. Supply your own for both.

---

## Notebooks

| Notebook | What it shows |
|----------|--------------|
| [`01_scenario_walkthrough`](https://github.com/raz1470/how_wrong_is_your_mmm/blob/main/notebooks/01_scenario_walkthrough.ipynb) | The full pipeline on the example report's four-channel scenario |
| [`02_channel_scaling`](https://github.com/raz1470/how_wrong_is_your_mmm/blob/main/notebooks/02_channel_scaling.ipynb) | Whether the fix holds from 3 to 20 channels and across spend correlations |
| [`03_time_to_benefit`](https://github.com/raz1470/how_wrong_is_your_mmm/blob/main/notebooks/03_time_to_benefit.ipynb) | How many weeks of phasing it takes to cut the uncertainty |
| [`04_adstock_threat`](https://github.com/raz1470/how_wrong_is_your_mmm/blob/main/notebooks/04_adstock_threat.ipynb) | Whether the bias reduction survives carryover |
| [`05_strategy_comparison`](https://github.com/raz1470/how_wrong_is_your_mmm/blob/main/notebooks/05_strategy_comparison.ipynb) | The six default strategies compared on variance, bias, identifiability and cost |

---

## Development

```bash
uv run ruff format . && uv run ruff check . && uv run pytest
```

Python 3.12+. MIT licence.

The API reference is rebuilt and deployed with the rest of `docs/` on every push to `main`. To preview it locally:

```bash
uv run mkdocs serve
```
