Order too much stock and cash sits idle in the warehouse, or worse, expires. Order too little and customers buy elsewhere. Demand forecasting is the practice of estimating how much of each product will be needed, where and when, so purchasing, production and staffing can be planned. Spreadsheets and gut feel work up to a point. Machine learning can do better when you have many products, several locations and patterns too tangled to track by hand. This article explains what is involved, honestly, including when simpler methods are enough.
Start with a baseline, not a model
Before building anything sophisticated, measure how well simple methods do. Common baselines are:
- Naive: next week will be the same as this week.
- Seasonal naive: next week will match the same week last year.
- Moving average: the average of the last few weeks.
These are surprisingly hard to beat for stable products. A machine learning model is only worth its cost if it clearly outperforms them on your data. Without a baseline, you have no way of knowing.
The data you need
Sales history
Ideally two or more years at the granularity you plan for: daily or weekly, by product and location. A year is a bare minimum to see seasonality once. Build a clean time series from your transaction data:
SELECT p.sku,
s.store_id,
DATE_SUB(o.order_date, INTERVAL WEEKDAY(o.order_date) DAY) AS week_start,
SUM(oi.qty) AS units
FROM order_items oi
JOIN orders o ON o.id = oi.order_id
JOIN products p ON p.id = oi.product_id
JOIN stores s ON s.id = o.store_id
WHERE o.status NOT IN ('cancelled', 'test')
GROUP BY p.sku, s.store_id, week_start;
(MySQL syntax; this groups each order into the Monday-starting week.) Note that weeks with zero sales produce no row. You must fill those gaps with explicit zeros, or the model will think the product sells every week.
Sales are not demand
If a product was out of stock for three weeks, sales were zero but demand was not. Training on those zeros teaches the model to under-forecast. Where possible, record stock availability and either exclude stockout periods or adjust for them.
Drivers of demand
Machine learning becomes valuable when it can use information beyond past sales:
- Price and discounts, including competitor promotions if known.
- Marketing campaigns and their dates.
- Holidays and festivals, which in India move with the lunar calendar and shift from year to year.
- Weather, for seasonal products.
- Product attributes, which help forecast new products with no history by learning from similar ones.
Only include drivers you will actually know in advance when forecasting. A model that relies on next week's weather needs a weather forecast at prediction time.
Choosing a demand forecasting method
| Approach | Examples | Suits |
|---|---|---|
| Classical statistical | Exponential smoothing, ARIMA | Fewer series with clear trend and seasonality; easy to explain |
| Gradient-boosted trees | LightGBM, XGBoost with engineered features | Many products and locations, with promotions and other drivers |
| Deep learning and pre-trained time-series models | Neural forecasting models | Very large catalogues; worth testing, not assuming |
A common and effective pattern is a single gradient-boosted model trained across all products, using features such as recent sales averages, sales in the same week last year, price, promotion flags, holiday flags and product category. Learning across products helps items with sparse history borrow patterns from similar ones.
Measuring forecast accuracy
Choose an error measure that matches the business cost:
- MAE (mean absolute error): average size of the miss, in units.
- MAPE (mean absolute percentage error): average miss as a percentage. Easy to understand, but it misbehaves for low-volume items, since being off by 2 units on a product that sells 1 counts as a 200 percent error, and it is undefined when actual sales are zero.
- WAPE (weighted absolute percentage error): total absolute error divided by total actual sales. More stable across mixed catalogues.
- Bias: whether forecasts are consistently too high or too low. A small but persistent bias causes steady overstocking or shortages.
Evaluate with backtesting: pretend it is a past date, train only on data before it, forecast the following weeks, and compare with what actually happened. Repeat for several past dates. Never evaluate on data the model has already seen, and never let future information leak into features. Leakage is the most common reason a model looks excellent in testing and disappoints in use.
Turning forecasts into decisions
A single number ("we will sell 120 units") hides uncertainty. Better forecasts come with a range, such as "likely between 90 and 160". Planners can then set safety stock according to how costly a shortage is compared with excess stock. A high-margin item that customers will not wait for warrants a higher service level than a slow, cheap one.
Keep people in the loop. Category managers know things the data does not, like a large order a key customer has hinted at or a supplier problem. Let them adjust forecasts, record the reason, and later check whether adjustments improved accuracy.
Limits to be aware of
- Unprecedented events. Models learn from the past; sudden disruptions break historical patterns.
- Intermittent demand. Spare parts and slow movers that sell zero most weeks need specialised methods, and even then accuracy is limited.
- New products. Forecasting with no history relies on similar products and is inherently uncertain.
- Maintenance. Models drift as markets change. Monitor accuracy continuously and retrain on a schedule.
Good forecasting starts with reliable sales and stock data, which our database management team can help organise. Our AI and machine learning development team builds and backtests forecasting models against your own baselines.
Key takeaways
- Demand forecasting with machine learning pays off for many products, locations and demand drivers.
- Always compare against simple baselines using honest backtesting.
- Fill zero-sales gaps and treat stockouts carefully, since sales are not the same as demand.
- Use forecast ranges, measure bias, and let planners adjust with recorded reasons.