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.

InvoiceDateProductQtyPrice
5775972011-11-21 08:1985123A White Hanging Heart T-Light Holder12£2.95
5776912011-11-21 11:3485123A White Hanging Heart T-Light Holder2£2.95
5776902011-11-21 11:3485123A White Hanging Heart T-Light Holder2£2.95
5776952011-11-21 11:5585123A White Hanging Heart T-Light Holder12£2.95
5776962011-11-21 11:5785123A White Hanging Heart T-Light Holder1£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.

RemovedLinesWhy
Non-product codes6,094Postage, manual entries, bank charges, fees, vouchers
Zero-price lines6,116Stock adjustments such as “damaged” or “missing”, not sales
Exact duplicate rows34,034The same line recorded twice
Cancellations17,914 nettedSubtracted from their orders, so an 80,995-unit order cancelled minutes later counts as zero
WeekUnits this weekAvg last 4 wkSame 4 wk last yearPriceUnits next 4 wk
2011-09-054494774031£2.771925
2011-09-12662568.53991£2.761946
2011-09-196975493701£2.881482
2011-09-264045533934£2.861525

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.

ProductForecast, 4 wkActually soldSame as last month
Small popcorn holder5,12312,9655,928
Jumbo bag red retrospot4,8565,5033,252
World War 2 gliders, assorted designs4,5983,9418,007
White hanging heart T-light holder3,6733,5473,068
Paper chain kit, 50s Christmas3,4786,8023,881
Pack of 72 retrospot cake cases3,4301,8372,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.

Free tier included. No credit card required.