Example · demand forecasting · public data
How to Build a Demand Forecasting Model (Worked Example)
Know what to reorder before the shelf runs empty
Turn your order lines into one row per product per week, train a model to predict each product’s units over the next 4 weeks, and trust it only if it beats “same as last month” on weeks it never saw. Below, we do exactly that on two years of real orders from a UK online giftware shop, including the first attempt that failed.
Last updated · Published
00 · Define
Decide what you predict, and when
We predict how many units of each product will sell over the next 4 weeks, the length of a typical reorder cycle. The forecast is made at the end of each week, so every input is something the shop knew by then: what sold this week and before, at what price, to how many customers, plus the calendar weeks ahead.
Do you need a model at all?
With a handful of products that sell steadily, a reorder-point spreadsheet is enough: average daily sales times supplier lead time, plus safety stock. A trained model earns its place when you have hundreds of products, seasonal swings and wholesale orders that make last month a poor guide.
01 · Data
Start with the order lines you already have
Two years of real orders from the UCI Online Retail II dataset: a UK online shop selling all-occasion giftware, many of its customers wholesalers. 1,067,371 order lines from December 2009 to December 2011, the same shape as a QuickBooks or Shopify sales export.
| Invoice | Date | Product | Qty | Price |
|---|---|---|---|---|
| 577597 | 2011-11-21 08:19 | 85123A White Hanging Heart T-Light Holder | 12 | £2.95 |
| 577691 | 2011-11-21 11:34 | 85123A White Hanging Heart T-Light Holder | 2 | £2.95 |
| 577690 | 2011-11-21 11:34 | 85123A White Hanging Heart T-Light Holder | 2 | £2.95 |
| 577695 | 2011-11-21 11:55 | 85123A White Hanging Heart T-Light Holder | 12 | £2.95 |
| 577696 | 2011-11-21 11:57 | 85123A White Hanging Heart T-Light Holder | 1 | £2.95 |
02 · Clean and reshape
Turn orders into one row per product per week
Remove what is not demand, then group what is left by product and week. Each row gets what was known that week and, as the target, the units sold over the 4 weeks after. We kept the 1,088 products the shop stocked throughout: sold in at least half of the 104 weeks, on sale by March 2010 and still selling in November 2011.
| Removed | Lines | Why |
|---|---|---|
| Non-product codes | 6,094 | Postage, manual entries, bank charges, fees, vouchers |
| Zero-price lines | 6,116 | Stock adjustments such as “damaged” or “missing”, not sales |
| Exact duplicate rows | 34,034 | The same line recorded twice |
| Cancellations | 17,914 netted | Subtracted from their orders, so an 80,995-unit order cancelled minutes later counts as zero |
| Week | Units this week | Avg last 4 wk | Same 4 wk last year | Price | Units next 4 wk |
|---|---|---|---|---|---|
| 2011-09-05 | 449 | 477 | 4031 | £2.77 | 1925 |
| 2011-09-12 | 662 | 568.5 | 3991 | £2.76 | 1946 |
| 2011-09-19 | 697 | 549 | 3701 | £2.88 | 1482 |
| 2011-09-26 | 404 | 553 | 3934 | £2.86 | 1525 |
The white hanging heart T-light holder, the shop’s best seller, in September 2011. Sixteen inputs per row in all.
03 · Split by date
Hold back the most recent weeks
The test weeks must come after the training weeks, or the model peeks at the weeks next to the ones it is graded on. We held back the last 12 complete weeks, the run-up to Christmas 2011. Every training window ends before them, so the model never saw a single test week.
Train: 84,864 rows, 4-week windows from Feb 2010 to Sep 2011
Test: 9,792 rows, 9 windows starting 12 Sep to 7 Nov 2011
04 · First attempt
A model that only ties last month
Our first run predicted next week’s units directly. On the 12 test weeks it scored 33.8% accuracy against 33.6% for “same as last month”, and won only 6 of the 12 weeks. A tie. The cause was the training objective: squared error chases the rare 1,000-unit wholesale week, while a reorder decision cares about the typical one.
First model, 1 week ahead
33.8%
Trained on raw weekly units.
“Same as last month”
33.6%
The bar it had to clear, and didn’t.
05 · Fix the framing
Forecast 4 weeks, on a log scale
Two changes, chosen on training weeks only. Predict the 4 weeks a reorder has to cover rather than one, and train on the logarithm of units so a single huge order stops dominating. The forecast is converted back to units and scaled by 1.31, the ratio that makes training-period forecasts add up to what actually sold. One more free-tier run, 66 minutes, in time-series mode: every check trains on earlier weeks and scores later ones. The winner was a random forest stacked on a linear model.
06 · Test against last month
Beat “same as last month” or don’t use it
Accuracy is 1 minus the units missed divided by the units sold, across all 1,088 products and all 9 test windows. Lumpy wholesale demand keeps both numbers modest. What matters is the gap.
Model accuracy, 4 weeks ahead
52.8%
Won all 9 test windows and 64% of products. Off by 114 units per product per window on average.
“Same as last month”
47.0%
Off by 128 units per product per window on average.
On the public Online Retail II dataset, a model trained on EasyDeploy AI forecast each product’s next 4 weeks 52.8% accurately, against 47.0% for repeating last month, and missed 11% fewer units over the run-up to Christmas 2011. Both forecasts ran low as demand climbed, the model by 7% and last month by 14%, which is what safety stock is for.
07 · Reorder
Turn the forecast into a reorder list
The six largest forecasts for the 4 weeks from 7 November 2011, made the Sunday before, next to what actually sold. Some products land close and some do not: a wholesale order for popcorn holders doubled that line. With stock on hand from your own system, reorder anything whose stock is below the forecast for its lead time plus safety stock.
| Product | Forecast, 4 wk | Actually sold | Same as last month |
|---|---|---|---|
| Small popcorn holder | 5,123 | 12,965 | 5,928 |
| Jumbo bag red retrospot | 4,856 | 5,503 | 3,252 |
| World War 2 gliders, assorted designs | 4,598 | 3,941 | 8,007 |
| White hanging heart T-light holder | 3,673 | 3,547 | 3,068 |
| Paper chain kit, 50s Christmas | 3,478 | 6,802 | 3,881 |
| Pack of 72 retrospot cake cases | 3,430 | 1,837 | 2,516 |
Data: Chen, D. (2012). Online Retail II [Dataset]. UCI Machine Learning Repository. https://doi.org/10.24432/C5CG6D. Licensed CC BY 4.0.
Try it on your own order history
Connect QuickBooks or Shopify to your AI assistant, or bring a CSV. Train, test against last month, and only then reorder from the forecast.
