Inventory Forecasting
Stop guessing reorder points - let order history tell you when to stock up
The Problem
Last month's sales are not a forecast
Most retailers manage inventory using rules of thumb: order what you sold last month, add a buffer, repeat. This approach is invisible when demand is flat and catastrophic when it is not. It does not tell you that laptop sales will spike 78% in October. It does not tell you that the 300 units you ordered in September will sell out in the first two weeks of October. It does not tell you anything about November until October has already happened.
The data to predict this already exists in the order history. Every sale is a timestamped signal. A demand forecasting model reads those signals, extracts the trend, identifies the seasonal pattern, and produces a prediction with confidence intervals: here is how many units category X will likely sell next month, and here is the reorder quantity that keeps you in stock with 95% confidence.
This playbook builds both a rolling mean baseline and a Prophet demand model on TechHeaven's 18 months of order history. The Q4 2025 spike - 78% above the monthly baseline in October - is the central test case. The rolling mean cannot predict it. Prophet learns the seasonal pattern and forecasts the next Q4 cycle 8 weeks before it happens.
The Data
What the notebook trains on
Two JSON files loaded directly from the TechHeaven public repository. No database, no local setup, no API key required - the notebook fetches the data at runtime and builds the models from scratch.
4,250 completed + processing orders used (500 cancelled and 250 pending excluded). Order date range: Jan 2025 - Jun 2026.
Laptops, Memory, Smart Home, Audio, Networking, Mobile Accessories, Monitors, Wearables, Drives, Mice, Keyboards, Cameras, Accessories, Components, Printers.
One complete Q4 seasonal cycle observed. Sufficient for annual seasonality detection with Prophet yearly_seasonality=True.
Monthly units sold per category. Category-level aggregation produces 18 data points per series - sparse categories filled with zero demand months.
October 2025: 2,354 units across all categories - 78% above the baseline average of 1,324 units/month. November 2025: 2,018 units - 53% above baseline. This two-month spike is the signal both models must handle. A rolling mean smooths over it. Prophet learns from it.
The Approaches
Rolling mean baseline vs. Prophet
Both models train on the same 18-month monthly demand series. The difference is in what each model can represent: a rolling mean averages the recent past, a Prophet model decomposes the series into trend, seasonality, and residual.
pandas rolling()Low complexityprophet (Meta open-source)Medium complexityResults
The Q4 test: where rolling mean fails
The notebook runs both models in-sample on 18 months of history, then generates a 6-month forward forecast. The Q4 spike is the central test: a model that cannot predict it is not useful for purchasing decisions. A model that can gives a buyer 8 weeks of lead time to build stock.
The buyer's timeline
A buyer using rolling mean orders normal stock in August based on the July average. October arrives, laptops sell out in two weeks, and an expedited reorder costs 15-30% more with 4-6 weeks of stockout. A buyer using Prophet sees the October forecast in August, places the larger order in time, and captures the full Q4 revenue. The forecasting cost is a one-time 30-second model fit. The stockout cost is permanent lost revenue.
Core Concepts
How each piece works
Time Series Decomposition
A time series is any sequence of measurements over time. Demand forecasting decomposes it into three components: trend (long-term direction), seasonality (repeating annual or weekly pattern), and residual (unexplained noise). A rolling mean captures none of these explicitly - it averages them together. Prophet fits each component separately, which lets it extrapolate each one independently into the future.
Prophet Seasonality (Fourier Series)
Prophet represents annual seasonality as a sum of sine and cosine terms at different frequencies (Fourier series). By default, Prophet uses 10 Fourier terms for yearly seasonality, giving it enough flexibility to model complex seasonal shapes while remaining smooth. The Fourier terms are fitted to the historical data during model training. Once fitted, they can be extrapolated forward indefinitely.
Rolling Mean as a Baseline
A 3-month rolling mean predicts next month by averaging the 3 preceding months. It is a 1-step-ahead forecast: at each time step, it uses the 3 months immediately before the target month (not including it). This is what a buyer does intuitively: "we sold 250, 270, and 290 last three months, so let's order 270." It works when demand is stable and fails when it shifts.
Confidence Intervals and Safety Stock
Prophet outputs a point estimate (yhat) and uncertainty bounds (yhat_lower, yhat_upper at 95% confidence). The uncertainty grows further into the future. The reorder table uses the upper 95% bound as the base order quantity, then adds one month of safety stock at the point estimate. Ordering to the upper bound protects against demand coming in above the forecast; safety stock covers supplier lead time.
Monthly vs. Weekly Aggregation
Weekly aggregation produces more data points (78 weeks vs 18 months) but introduces alignment noise, day-of-week effects, and sparse data for small categories. Monthly aggregation is more stable, aligns with how buyers make purchasing decisions, and works better with Prophet's yearly_seasonality at this data scale. The tradeoff: monthly forecasts cannot detect within-month demand shifts.
Architecture
Production forecasting pipeline
The notebook demonstrates forecasting in batch. In production, the pipeline runs on a schedule - typically weekly - to keep models current with the latest order data and trigger purchasing alerts before stock runs low.
- 1Fetch order history from the ecommerce database
- 2Aggregate by month and category, fill missing months with zero
- 3Fit one Prophet model per category (15-30 seconds total)
- 4Generate 6-month forward forecast per category
- 5Compute reorder quantity: upper 95% CI + safety stock
- 6Write results to a dashboard or send a purchasing report
- 1Compare current stock level against reorder quantity from the forecast
- 2Flag categories where stock < reorder quantity
- 3Generate a draft purchase order for the buyer to review
- 4Send alert via Slack, email, or ERP integration
- 5Log purchase decision and actual demand for model improvement
- 6Retrain models monthly as more seasonal cycles accumulate
Business Applications
Run the notebook
The Jupyter notebook builds both models from scratch on 18 months of TechHeaven order history. Rolling mean in-sample comparison, Prophet fit on 15 categories, decomposition charts, 6-month forward forecast, and a reorder point table with 95% confidence interval logic. No API key required.
Open in ColabEcommerce AI/ML Series
Ecommerce AI/ML Series
Resources
This playbook
Tech stack
Ecommerce AI/ML Roadmap
Completed
Products, orders, customers, policies, and FAQ