Rishabh5825/Probabilistic-Revenue-Forecasting
π Probabilistic Revenue Forecasting for E-Commerce Marketing
   
A production-grade, end-to-end Bayesian forecasting system that ingests omni-channel marketing data (Google Ads, Meta Ads, Microsoft Ads), generates probabilistic revenue predictions with calibrated uncertainty intervals (P10/P50/P90), and delivers LLM-powered causal summaries β all through an interactive Streamlit dashboard.
π― The Problem
Marketing teams pour budgets across Google, Meta, and Microsoft campaigns, but have no reliable way to answer: "If I spend X next month, what revenue range should I actually expect?" Point-estimate forecasts hide uncertainty. This system replaces guesswork with mathematically honest confidence intervals so executives know the best case, expected case, and worst case before committing a single dollar.
ποΈ Architecture & Engineering Principles
This project was built following Bayesian-first and Mathematical Honesty principles.
- Probabilistic, Not Point-Estimate: Every forecast is a full posterior distribution via
BayesianRidge, not a single number. We draw 5,000 Monte Carlo samples per segment and report P10/P50/P90 revenue intervals so stakeholders see the true uncertainty envelope. - Hierarchical Roll-Up: Forecasts are generated at the Channel Γ Campaign Type level (e.g., Google β PMAX, Meta β Retargeting), then statistically aggregated upward to channel-level and account-level totals β preserving correlation structure across segments.
- Recursive Multi-Step Forecasting: Instead of a single jump prediction, the system forecasts one week at a time, feeding predicted values back as lag features for the next week. This prevents the classic "flat-line" failure of naΓ―ve multi-step models.
- Zero-Inflation Handling: Sparse segments (e.g., Microsoft Shopping) with >50% zero-revenue weeks get a dampened zero-injection layer on the Monte Carlo samples, preventing the model from hallucinating revenue where none historically existed.
- AI-Powered Causal Summaries: A 9-node LangGraph state machine analyzes the numerical forecast outputs, detects anomalies, assesses confidence, and uses Groq (Qwen-2.5-32b) to generate executive-ready causal narratives and budget reallocation opportunities.
β¨ Key Features
- Bayesian Revenue Forecasting: Predicts future revenue across 3 channels and 12+ campaign types using 20+ engineered features including ad-stock lags, seasonality harmonics, holiday flags, YoY growth, and rolling efficiency metrics.
- Dynamic Budget Simulator: Adjust total budget with a slider and instantly see projected P10/P50/P90 revenue outcomes using fitted OLS response curves with diminishing-returns elasticity (default Ξ± = 0.75).
- AI Executive Insights: LangGraph orchestrates sequential LLM calls to produce a structured JSON report: executive summary, key revenue drivers, identified risks, statistical anomalies, and concrete budget reallocation opportunities.
- Interactive Streamlit Dashboard: 5-page clinical-grade UI combining Plotly visualizations with real-time forecasting β Account Overview, Channel Drilldown, Budget Simulator, AI Insights, and Historical Data Explorer.
- Hackathon-Ready Pipeline: Single-command execution via
run.shwith pre-trained pickle artifacts, pinned dependencies, and reproducible seeds.
π οΈ Technology Stack
π How to Run the Project (Step-by-Step)
To run this entire system end-to-end on your local machine, follow these instructions.
Step 1: Environment Setup
Clone the repository and install the locked dependencies to guarantee reproducibility.
git clone https://github.com/your-username/Probabilistic-Revenue-Forecasting.git
cd Probabilistic-Revenue-Forecasting
# Create and activate a virtual environment
python -m venv .venv
.venv\Scripts\activate # Windows
# source .venv/bin/activate # Mac/Linux
# Install all dependencies
pip install -r requirements.txtStep 2: Configure Environment Variables
Create a .env file in the root directory and add your Groq API keys:
GROQ_API_KEY_1=your_groq_api_key_here
GROQ_API_KEY_2=your_second_groq_api_key_hereNote: The AI Insights feature requires valid Groq API keys. All other features work without them.
Step 3: Train the Bayesian Pipeline (One-Time Setup)
This step ingests the raw CSV data, applies schema normalization, engineers 20+ features, fits the BayesianRidge models per segment, and serializes everything to a pickle artifact.
python Forecasting/train.pyYou should see "Models successfully saved to pickle/model.pkl" in the terminal.
Step 4: Generate the Baseline Forecast
Load the pre-trained model and produce probabilistic predictions. You can specify a forecast window of 30, 60, or 90 days:
python Forecasting/main.py --data_dir ./data --model_path ./pickle/model.pkl --output_path ./output/predictions.csvOr simply run the hackathon entry point:
bash run.shStep 5: Launch the Dashboard
streamlit run ui_backend/streamlit_app/app.pyA browser window will automatically open at `http://localhost:8501`. You can now view account-level metrics, drill into channels, simulate budget scenarios, and read AI-generated causal insights!
π Project Structure
βββ run.sh # Single-command entry point (hackathon pipeline)
βββ requirements.txt # Pinned Python dependencies
βββ app.py # Hugging Face Spaces entry point
βββ data/ # Raw marketing CSV datasets (overwritten at test time)
β βββ google_ads_campaign_stats.csv
β βββ meta_ads_campaign_stats.csv
β βββ bing_campaign_stats.csv
βββ pickle/ # Pre-trained model artifacts
β βββ model.pkl # Serialized BayesianRidge models + ResponseCurves
βββ Forecasting/ # Core ML pipeline
β βββ train.py # Phase 1: Train & serialize models
β βββ main.py # Phase 2: Load model β predict β export CSV
β βββ pipeline.py # Schema normalization & weekly aggregation
β βββ feature_engineering.py # 20+ feature transformations (lags, seasonality, etc.)
β βββ models.py # BayesianForecaster & SeasonalNaiveForecaster classes
β βββ budget_simulator.py # OLS response curve with diminishing-returns elasticity
βββ llm_Integration/ # LangGraph AI processing layer
β βββ graph.py # 9-node state machine for causal analysis
β βββ config.py # Groq API & model configuration
β βββ nodes/ # Individual LangGraph node implementations
β βββ prompts/ # LLM prompt templates
βββ ui_backend/ # Frontend application
β βββ streamlit_app/ # Streamlit dashboard
β βββ app.py # Main app entry with sidebar navigation
β βββ pages/ # 5 dashboard pages
β β βββ 1_Dashboard.py # Account-level metrics & channel breakdown
β β βββ 2_Channels.py # Campaign-type drilldown with efficiency matrix
β β βββ 3_Budget_Sim.py # Interactive budget simulator
β β βββ 4_AI_Insights.py # LangGraph causal summaries
β β βββ 5_Data_Explorer.py # Historical trend visualization
β βββ styles/ # Custom CSS
βββ README.mdπ¬ Model Details & Methodology
β οΈ Deployment Notes
- Hugging Face Spaces: The dashboard runs as a standalone Streamlit app reading directly from pre-generated JSON artifacts. No backend server required.
- Hackathon Pipeline: The automated testing system runs
./run.sh ./data ./pickle/model.pkl ./output/predictions.csvβ this loads the pickle, generates features from the test data, and writes predictions to CSV. - LLM Dependency: The AI Insights page requires valid Groq API keys. All forecasting and simulation features work fully offline with no network calls.
- Reproducibility: Random seed is set to
42vianp.random.seed(42)before all prediction runs. All dependency versions are pinned inrequirements.txt.
