Skip to content

Bodhi8/papilon

Repository files navigation

Papilon 2.0

Version Python License

The Unified Python Framework for Causal Media Modeling, Economic Systems & Complex Adaptive Systems

"Does the flap of a butterfly's wings in Brazil set off a tornado in Texas?" β€” Edward Lorenz


πŸ¦‹ What is Papilon?

Papilon (French for "butterfly") is the most comprehensive Python framework for Marketing Mix Modeling, Causal Inference, and Complex Systems Analysis. Named after the butterfly effect, it captures how small changes in marketing spend can cascade into significant business outcomes.

Why Papilon?

Feature Meridian PyMC-Marketing Robyn Papilon
Bayesian MMM βœ… βœ… ❌ βœ…
Causal Discovery ❌ ❌ ❌ βœ…
Causal Inference ❌ ❌ ❌ βœ…
Agent-Based Modeling ❌ ❌ ❌ βœ…
Economic I-O Models ❌ ❌ ❌ βœ…
Complex Systems ❌ ❌ ❌ βœ…
Autonomous Agents ❌ ❌ ❌ βœ…
Workflow Automation ❌ ❌ ❌ βœ…
Python Native βœ… βœ… ❌ (R) βœ…

πŸš€ Quick Start

Installation

pip install papilon

Marketing Mix Modeling

import papilon as pp

# Quick MMM Analysis
model = pp.MMM(
    data=df,
    target="revenue",
    media=["tv_spend", "digital_spend", "radio_spend"],
    date_col="date"
)
model.fit()

# Get ROI estimates
rois = model.get_roi()
print(rois)

# Optimize budget
optimal = model.optimize_budget(total=1_000_000)
print(optimal)

Autonomous Agent (NEW in 2.0!)

from papilon.orchestrator import MMMAgent

# Create an AI-powered agent
agent = MMMAgent(llm="claude")

# Natural language interface
results = agent.run("""
    Load my marketing data,
    fit an MMM model with TV, Digital, and Radio channels,
    optimize budget for $2M quarterly spend,
    and generate a report with visualizations.
""", data="marketing_data.csv")

# Or use quick analysis
results = agent.quick_analysis(
    data="data.csv",
    target_col="revenue",
    media_cols=["tv", "digital", "radio"],
    total_budget=2_000_000
)

Full Automation

from papilon.orchestrator import AutoMMM

# Create automated MMM system
auto = AutoMMM(
    data_source="marketing_data.csv",
    target="revenue",
    media_channels=["tv", "digital", "radio"]
)

# Configure automation
auto.set_retraining_schedule("0 9 * * 1")  # Weekly Monday 9am
auto.set_performance_threshold("mape", max_value=0.15)
auto.set_data_trigger(min_new_rows=7)

# Add notifications
from papilon.orchestrator import SlackNotifier
auto.add_notifier(SlackNotifier(webhook_url="..."))

# Start continuous automation
auto.start()

Causal Discovery & Inference

import papilon as pp

# Discover causal structure
dag = pp.discover_causal_structure(df, method="pc")

# Estimate causal effects
effect = pp.estimate_effect(
    df,
    treatment="marketing_spend",
    outcome="sales",
    method="backdoor"
)

# Synthetic control for geo experiments
sc = pp.SyntheticControl(
    treated_unit="California",
    control_units=["Texas", "Florida", "New York"],
    treatment_time="2024-01-01"
)
sc.fit(panel_data)
causal_effect = sc.estimate_effect()

πŸ“¦ Architecture

papilon/
β”œβ”€β”€ core/                    # Foundation modules
β”‚   β”œβ”€β”€ entropy.py          # Information theory
β”‚   β”œβ”€β”€ energy.py           # Statistical mechanics
β”‚   β”œβ”€β”€ states.py           # State space analysis
β”‚   β”œβ”€β”€ relationships.py    # Correlation analysis
β”‚   └── topology.py         # Network topology
β”‚
β”œβ”€β”€ mmm/                    # Marketing Mix Modeling
β”‚   β”œβ”€β”€ transforms/         # Adstock & saturation
β”‚   β”œβ”€β”€ models/             # Bayesian & frequentist
β”‚   β”œβ”€β”€ decomposition.py    # Channel attribution
β”‚   β”œβ”€β”€ response_curves.py  # Marginal response
β”‚   β”œβ”€β”€ calibration.py      # Lift test integration
β”‚   └── budget_optimizer.py # SLSQP optimization
β”‚
β”œβ”€β”€ causal/                 # Causal Inference
β”‚   β”œβ”€β”€ discovery/          # PC, FCI, GES, NOTEARS
β”‚   β”œβ”€β”€ effects/            # ATE, CATE, ITT, LATE
β”‚   β”œβ”€β”€ methods/            # DiD, synthetic control, RDD, IV
β”‚   β”œβ”€β”€ interventions.py    # do-calculus
β”‚   └── refutation.py       # Robustness checks
β”‚
β”œβ”€β”€ orchestrator/           # πŸ†• Autonomous Agents
β”‚   β”œβ”€β”€ base.py             # Agent framework
β”‚   β”œβ”€β”€ agents.py           # MMMAgent, CausalAgent
β”‚   β”œβ”€β”€ tools.py            # Tool library
β”‚   β”œβ”€β”€ workflows.py        # DAG pipelines
β”‚   └── automation.py       # AutoMMM, triggers, monitors
β”‚
β”œβ”€β”€ economics/              # Economic Modeling
β”‚   β”œβ”€β”€ io_model.py         # Leontief I-O
β”‚   β”œβ”€β”€ multipliers.py      # Economic multipliers
β”‚   └── shock_analysis.py   # Shock propagation
β”‚
β”œβ”€β”€ dynamics/               # Complex Systems
β”‚   β”œβ”€β”€ attractors.py       # Fixed points, chaos
β”‚   β”œβ”€β”€ bifurcation.py      # Parameter sensitivity
β”‚   └── stability.py        # Lyapunov exponents
β”‚
β”œβ”€β”€ agents/                 # Agent-Based Modeling
β”‚   β”œβ”€β”€ base.py             # Agent class
β”‚   β”œβ”€β”€ model.py            # ABM model
β”‚   └── space.py            # Grid, network, continuous
β”‚
β”œβ”€β”€ sensitivity/            # Sensitivity Analysis
β”‚   β”œβ”€β”€ sobol.py            # Sobol indices
β”‚   └── morris.py           # Morris screening
β”‚
└── viz/                    # Visualization
    β”œβ”€β”€ dag.py              # Causal graphs
    β”œβ”€β”€ response.py         # Response curves
    └── decomposition.py    # Waterfall charts

πŸ€– Autonomous Agent Framework

Papilon 2.0 introduces a revolutionary autonomous agent framework that can automate your entire MMM workflow:

Features

  • Natural Language Interface: Describe tasks in plain English
  • LLM Integration: Works with Claude, GPT-4, or local models
  • Tool-Based Architecture: Extensible with custom tools
  • Workflow Orchestration: DAG-based pipeline execution
  • Event-Driven Automation: Triggers, monitors, and alerts
  • Human-in-the-Loop: Approval steps when needed

Agent Types

Agent Purpose
MMMAgent Marketing Mix Modeling automation
CausalAgent Causal discovery and inference
AgentOrchestrator Multi-agent coordination

Automation Components

Component Purpose
Workflow DAG-based pipeline definition
AutoMMM Fully automated MMM system
DataTrigger Retrain on new data
PerformanceMonitor Track model metrics
SlackNotifier Send alerts to Slack

πŸ“Š Use Cases

1. Media Planning & Budget Optimization

# Optimize $10M annual budget across channels
optimal = model.optimize_budget(
    total=10_000_000,
    constraints={
        "tv": (1_000_000, 5_000_000),
        "digital": (2_000_000, 6_000_000),
    }
)

2. Incrementality Testing

# Analyze geo-lift experiment
effect = pp.difference_in_differences(
    data=geo_data,
    treatment_group="test_markets",
    control_group="holdout_markets",
    treatment_time="2024-06-01"
)

3. Scenario Planning

# What-if analysis
scenarios = [
    {"name": "Cut TV 20%", "changes": {"tv": -0.2}},
    {"name": "Double Digital", "changes": {"digital": 1.0}},
]

for scenario in scenarios:
    impact = agent.scenario(scenario["name"], scenario["changes"])
    print(f"{scenario['name']}: {impact['total_revenue_change_pct']:.1f}%")

4. Economic Impact Analysis

# Input-Output modeling
io = pp.InputOutputModel(transaction_matrix)
multipliers = io.calculate_multipliers()
shock_impact = io.shock_analysis(sector="advertising", shock=-0.1)

πŸ”§ Configuration

Environment Variables

# LLM API Keys (for agent features)
export ANTHROPIC_API_KEY="sk-..."
export OPENAI_API_KEY="sk-..."

# Optional: GPU acceleration
export JAX_PLATFORM_NAME="gpu"

Model Configuration

model = pp.BayesianMMM(
    # Adstock configuration
    adstock_type="weibull",  # geometric, weibull, delayed
    adstock_max_lag=8,
    
    # Saturation configuration
    saturation_type="hill",  # hill, logistic, tanh
    
    # Priors
    roi_prior="informative",  # informative, weakly_informative, flat
    
    # Inference
    n_samples=2000,
    n_chains=4,
    target_accept=0.9,
)

πŸ“ˆ Performance

  • GPU Acceleration: JAX/NumPyro backend for 10-100x speedup
  • Parallel Workflows: Execute independent steps concurrently
  • Incremental Updates: Update models with new data efficiently
  • Memory Efficient: Streaming for large datasets

πŸ›‘οΈ Enterprise Features

  • Audit Logging: Track all model changes and decisions
  • Role-Based Access: Control who can modify models
  • Deployment Ready: Docker, Kubernetes support
  • API Server: REST API for integration

πŸ“š Documentation


🀝 Support

  • Enterprise Support: support@vector1.ai
  • Issues: GitHub Issues (private repo)
  • Consulting: Available for custom implementations

πŸ“„ License

Proprietary License - All rights reserved.

Copyright Β© 2024-2025 Brian Curry / Vector1 Research

This software is proprietary and confidential. Unauthorized copying, distribution, or use is strictly prohibited. See LICENSE for full terms.


πŸ™ Acknowledgments

Built with insights from:

  • Google Meridian
  • PyMC-Marketing
  • DoWhy / EconML / CausalML
  • Mesa ABM Framework
  • SALib

Papilon β€” Understanding the butterfly effects in your marketing

About

Complex system and causal discovery library f

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published