Vexpower

MMM in Python: validate the weekly data contract

Use a small, executable Python audit to reject duplicate weeks, missing values and invalid spend before fitting a marketing mix model.

Vexpower editorial · Updated

Open in ChatGPTAsk questions about this page Open in ClaudeAsk questions about this page
Share

Validate the table before choosing the estimator

A successful import does not mean a dataset is fit for modelling. A CSV can contain duplicate weeks, unsorted dates, missing values and negative spend while still loading without an error. Make those assumptions executable before the model transforms the inputs.

MMM in Python: decision workflow
  1. Specify required columns
  2. Validate dates and values
  3. Reconcile source totals
  4. Freeze the input version

The Python MMM walkthrough teaches estimation. This article supplies a narrower preflight check that can run without a modelling library. Download the weekly data audit script and the synthetic practice CSV.

A reproducible workflow makes data checks, modelling assumptions and decision sensitivity visible. The mmm tools, code and platforms learning path connects this idea to the assumptions, practical checks and next steps.

To work through the connected decisions, read MMM data leakage: audit the path from raw data to holdout and Meridian geo data: audit the panel before fitting.

Run the audit on a known contract

Save both files in one folder and run python3 weekly-data-audit.py mmm-practice-synthetic.csv. The script expects week, revenue, search_spend, social_spend and video_spend columns. It checks ISO dates, unique ascending weeks, seven-day gaps, finite numeric values and non-negative media spend. It reports the row count and totals.

This contract is deliberately specific to the teaching dataset. A production geo panel needs a compound geography-week key, and a business may legitimately record negative net revenue after refunds. Extend the contract explicitly rather than changing the data until it passes.

Interpret a failed check as an investigation

Suppose a file has two rows for the week beginning 6 January. Summing them may be correct if they are disjoint regional totals, or wrong if they are duplicate exports. The audit cannot decide that business meaning. Stop, inspect the source and document the aggregation rule.

Similarly, a missing week is not necessarily a zero-spend week. Filling it with zeros asserts that all measured activity and outcomes were absent. Preserve the raw file and use source evidence to distinguish missing ingestion from a real observation.

Add the checks a syntax audit cannot perform

The script cannot establish whether currency, tax, refunds or week boundaries match finance. Reconcile totals to source reports and document outcome definitions. It also cannot detect every leakage path: future-derived labels and whole-history preprocessing require a separate review.

Record the file hash, extraction timestamp and data-as-of date. Split training and evaluation periods before fitting scalers or imputation rules. Keep a change log when historical data is revised. The validation guide explains the next layer of checks after the table passes.

A useful automated pipeline fails early on contract violations, produces a compact audit report and requires an explained source correction. Passing the audit means the table meets the declared structural rules. It does not establish causal identification or guarantee that the model will support a useful allocation.

Sources and further practice

This article’s worked example is illustrative, not a measured client result. Use the linked methodology to check the assumptions before applying it.

Frequently asked questions

Does passing the Python audit mean the data is ready for causal MMM?

No. It verifies a declared structural contract. You still need outcome reconciliation, causal variable selection, adequate variation and leakage-safe modelling.

Why reject unsorted weeks instead of sorting automatically?

Failing makes the data-order assumption visible. A deliberate preprocessing step can sort the source after duplicates and missing periods have been investigated.

Build the measurement skills behind this decision

Follow the mmm tools, code and platforms learning path for a worked example, an interactive tool and the checks to apply before acting.