A local, filesystem-based proof-of-concept for an agentic Marketing Mix Modeling (MMM) and Optimization system.
This system implements a deterministic, explainable, and auditable workflow for marketing budget optimization using specialized autonomous agents.
Build an end-to-end MMX system that:
- Segments sub-categories based on sales/volume patterns
- Discovers trends, seasonality, and anomalies
- Estimates baseline sales and sets modeling priors
- Trains MMM models with adstock and saturation transformations
- Optimizes budget allocation across channels
- Generates stakeholder-ready insights and recommendations
NEW! A beautiful, interactive web interface is now available for the MMX Agent system.
- 🎨 Modern Dark Theme UI with smooth animations
- 📊 Real-time Pipeline Monitoring with live progress tracking
- 📈 Interactive Visualizations using Canvas-based charts
- ✅ Human-in-the-Loop Checkpoints for critical decisions
- 💡 AI-Generated Insights with confidence scores
- 📥 Easy Data Upload via drag-and-drop interface
- 📱 Responsive Design works on any device
# Start the web server
./start_server.sh
# Open browser to http://localhost:8080See QUICKSTART_WEB.md for detailed instructions.
| Agent | Responsibility |
|---|---|
| Segmentation Agent | Groups sub-categories using clustering |
| Trend Scanner Agent | Detects YoY trends, seasonality, anomalies, events |
| Baseline Agent | Estimates baseline sales and sets priors/constraints |
| Model Optimization Agent | Applies transformations and trains MMM |
| Budget Optimization Agent | Runs constrained optimization (scipy SLSQP) |
| Insight Agent | Generates narratives, tables, and visualizations |
- Workflow Orchestrator: Manages agent execution order
- State Manager: Persists state between stages
- Checkpoint Manager: Implements human-in-the-loop approval gates
mmx-agent-poc/
├── README.md # This file
├── requirements.txt # Python dependencies
│
├── config/ # Configuration files
│ ├── global.yaml # System-wide settings
│ ├── segmentation.yaml # Segmentation parameters
│ ├── priors.yaml # MMM priors and constraints
│ ├── optimization.yaml # Budget optimization settings
│ └── guardrails.yaml # Validation rules
│
├── data/ # Data directories
│ ├── raw/ # Input data
│ ├── processed/ # Processed artifacts
│ ├── features/ # Engineered features
│ └── scenarios/ # Scenario outputs
│
├── agents/ # Agent implementations
│ ├── segmentation_agent/
│ │ ├── agent.py # Main agent logic
│ │ ├── rules.py # Validation rules
│ │ └── tools.py # Feature engineering & clustering
│ ├── trend_scanner_agent/
│ │ ├── agent.py
│ │ └── detectors.py # Trend/anomaly detection
│ ├── baseline_agent/
│ │ ├── agent.py
│ │ ├── priors.py # Prior estimation
│ │ └── constraints.py # Constraint builder
│ ├── model_optimization_agent/
│ │ ├── agent.py
│ │ ├── transformations.py # Adstock transformations
│ │ ├── saturation.py # Hill/Weibull saturation
│ │ └── trainer.py # Model training
│ ├── budget_optimization_agent/
│ │ ├── agent.py
│ │ ├── solver.py # Scipy optimization
│ │ └── bounds.py # Bounds management
│ └── insight_agent/
│ ├── agent.py
│ ├── narratives.py # Text generation
│ └── visuals.py # Chart creation
│
├── orchestration/ # Workflow management
│ ├── workflow.py # Main orchestrator
│ ├── state_manager.py # State persistence
│ └── checkpoints.py # Checkpoint management
│
├── models/ # Model artifacts
│ ├── trained/ # Trained models
│ └── registry.json # Model registry
│
├── artifacts/ # Generated outputs
│ ├── contributions/ # Channel contributions
│ ├── curves/ # Response curves
│ ├── roi/ # ROI optimization results
│ └── decks/ # Stakeholder reports
│
├── evaluation/ # Validation utilities
│ ├── backtesting.py # Backtest framework
│ └── diagnostics.py # Model diagnostics
│
├── cli/ # Command-line interface
│ └── run_pipeline.py # CLI runner
│
└── tests/ # Unit tests
└── unit/
# Clone or navigate to the project directory
cd mmx-agent-poc
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txtCreate a CSV file with sales and marketing spend data:
date,sub_category,sales,total_sales,total_volume,tv_spend,digital_spend,print_spend,ooh_spend,radio_spend
2022-01-03,Category_A,50000,50000,1000,10000,8000,3000,2000,1000
2022-01-10,Category_A,52000,52000,1040,10500,8200,3100,2100,1100
...Required columns:
date: Weekly date (YYYY-MM-DD)salesortotal_sales: Sales valuestotal_volume: Volume sold{channel}_spend: Spend for each marketing channel (e.g.,tv_spend,digital_spend)
Place your data in data/raw/sales_data.csv.
# Basic execution
python -m cli.run_pipeline --input data/raw/sales_data.csv --auto-approve
# With event data
python -m cli.run_pipeline \
--input data/raw/sales_data.csv \
--events data/raw/events.csv \
--auto-approve
# Interactive mode with checkpoints
python -m cli.run_pipeline --input data/raw/sales_data.csv
# Verbose logging
python -m cli.run_pipeline --input data/raw/sales_data.csv --auto-approve --verboseAfter execution, find outputs in:
- Executive Summary:
artifacts/decks/executive_summary.md - Excel Report:
artifacts/decks/mmx_summary_report.xlsx - Insights:
artifacts/decks/key_insights.json - Visualizations:
artifacts/decks/*.png - Contributions:
artifacts/contributions/channel_contributions.csv - Optimization:
artifacts/roi/optimization_recommendations.csv
Agent: SegmentationAgent
Input: Raw sales data
Output: Segment mappings, statistics, visualizations
Checkpoint: User reviews segment quality before proceeding
Agent: TrendScannerAgent
Input: Time series sales data
Output: Flagged periods, event metadata, trend visualizations
Agent: BaselineAgent
Input: Sales data + event metadata
Output: Baseline estimates, priors, constraints
Checkpoint: User reviews baseline before modeling
Agent: ModelOptimizationAgent
Input: Sales data + priors + baseline
Output: Trained model, contributions, response curves
Actions:
- Applies adstock transformation (geometric decay)
- Applies saturation (Hill function)
- Trains Ridge regression model
- Calculates channel contributions
- Generates response curves
Agent: BudgetOptimizationAgent
Input: Response curves + constraints
Output: Optimized allocations for multiple scenarios
Checkpoint: User reviews model before optimization
Actions:
- Runs scipy SLSQP optimizer
- Applies channel bounds
- Generates base, aggressive, conservative scenarios
Agent: InsightAgent
Input: Contributions + optimization results + model metrics
Output: Narratives, visualizations, Excel reports
Actions:
- Generates executive narratives
- Creates contribution charts
- Creates optimization comparison charts
- Produces stakeholder-ready Excel report
data:
time_granularity: "weekly"
modeling:
granularity: "channel"
response_curve: "hill"
adstock_type: "geometric"
optimization:
solver: "scipy"
method: "SLSQP"
orchestration:
enable_checkpoints: true
checkpoint_locations:
- "after_segmentation"
- "after_baseline"
- "before_optimization"optimization:
budget:
total_budget: 1000000
channel_bounds:
TV:
min: 0.15
max: 0.40
Digital:
min: 0.20
max: 0.50
channel_constraints:
TV:
min_absolute_spend: 100000Models carryover effects of marketing spend:
effect[t] = spend[t] + decay_rate * effect[t-1]
Models diminishing returns:
response = spend^alpha / (gamma^alpha + spend^alpha)
Ridge regression with transformed features:
incremental_sales = Σ (beta_i * transformed_spend_i) + baseline
Maximize response subject to:
- Total budget constraint
- Channel min/max bounds
- Maximum change from current spend
- Minimum budget utilization
pytest tests/ -v --cov=agents --cov=orchestrationfrom evaluation.diagnostics import ModelDiagnostics
diagnostics = ModelDiagnostics()
report = diagnostics.generate_diagnostic_report(y_true, y_pred, X, feature_names)
print(report['issues'])from evaluation.backtesting import Backtester
backtester = Backtester()
results = backtester.time_series_backtest(model, X, y, n_splits=5)
print(f"Average R²: {results['avg_r2']:.3f}")| Channel | Contribution | Contribution % |
|---|---|---|
| Digital | $450,000 | 35.2% |
| TV | $380,000 | 29.7% |
| $210,000 | 16.4% | |
| OOH | $150,000 | 11.7% |
| Radio | $90,000 | 7.0% |
| Channel | Current Spend | Optimized Spend | Change % |
|---|---|---|---|
| Digital | $200,000 | $280,000 | +40.0% |
| TV | $400,000 | $320,000 | -20.0% |
| $180,000 | $160,000 | -11.1% | |
| OOH | $140,000 | $150,000 | +7.1% |
| Radio | $80,000 | $90,000 | +12.5% |
The system includes comprehensive guardrails at multiple levels:
- Missing data thresholds
- Outlier detection
- Date continuity checks
- Minimum R² thresholds
- Coefficient sign validation
- Multicollinearity detection
- ROI feasibility ranges
- Contribution validity checks
- Saturation efficiency bounds
- Maximum change constraints
- Budget preservation validation
- Bounds compliance checks
The pipeline enforces human-in-the-loop checkpoints at:
- After Segmentation: Review segment quality and mappings
- After Baseline: Review baseline estimates and priors
- Before Optimization: Review model performance before budget allocation
To bypass checkpoints:
python -m cli.run_pipeline --input data/raw/sales_data.csv --auto-approve# Ensure you're in the project root
export PYTHONPATH="${PYTHONPATH}:$(pwd)"Ensure your input CSV has:
datecolumn (datetime)salesortotal_sales{channel}_spendcolumns for each marketing channel
- Check that response curves are properly calibrated
- Ensure budget constraints are not too restrictive
- Increase
maxiterinconfig/optimization.yaml
This is a proof-of-concept implementation for demonstration purposes.
This is a POC system. For production use, consider:
- Adding Bayesian MMM (PyMC, Stan)
- Cloud deployment (AWS, GCP)
- UI dashboards (Streamlit, Dash)
- Advanced optimization (evolutionary algorithms)
- Integration with marketing platforms
- Marketing Mix Modeling: Chan, D. & Perry, M. (2017). Challenges and Opportunities in Media Mix Modeling
- Adstock & Saturation: Broadbent, S. (1979). One Way TV Advertisements Work
- Hill Function: Hill, A.V. (1910). The possible effects of the aggregation of the molecules of haemoglobin
- Optimization: Nocedal, J. & Wright, S. (2006). Numerical Optimization
Built with ❤️ for Marketing Analytics