Sales and Stock Forecasting with Machine Learning
Sales forecasting helps you plan stock, production, and staff rosters. Start with tidy data and simple methods, and test honestly before reaching for a more complex model.
Many business owners judge stock needs by feel: last month was busy, so buy more this month. Sometimes it works, and sometimes goods pile up in the storeroom or run out just as customers arrive. Sales forecasting is a more structured way to answer the question "how much will we sell next week?".
When sales forecasting helps
Forecasting is most useful when a decision must be made before the sale happens, and a wrong guess has a real cost. For example:
- Stock and raw materials. Perishable goods or limited capital make overstocking expensive.
- Production schedules. You can prepare capacity before an order surge, not after it.
- Staff rosters. Busy days and hours get enough people, without paying overtime on quiet days.
If sales are still small and stock can be adjusted daily at little cost, a regular spreadsheet log is often enough.
The data you need
Forecast quality depends more on the data than on the model. Make sure you have:
- A long enough sales history. To capture yearly seasonality, ideally at least two years. For weekly patterns, a few months can already give a picture.
- Tidy, consistent data. One row per transaction or per day, correct dates, consistent product names, and no days missing without explanation.
- Records of promotions and discounts. A promo spike must not be read as normal demand.
- Holidays and seasons. Ramadan, Eid, school holidays, year end, or harvest season can push sales far from normal.
- Special events. Stock-outs, closed days, or delivery problems should be flagged, because zero sales during a stock-out doesn't mean zero demand.
Start with simple methods
First, make a forecast you can calculate in a spreadsheet. It becomes your baseline: a more complex model is only worth using if it beats it.
- Last week as the guess. Next Monday's sales are expected to match last Monday's. It sounds too simple, but it is often hard to beat.
- Moving average. Average the last few weeks to smooth out random ups and downs.
- Seasonal pattern. Work out how much sales usually rise in a given month or on a given day, then apply that to the latest average.
Machine learning starts to make sense when you have many products, many interacting factors, and enough data to learn from. Models such as gradient boosting or LSTM can capture more complex patterns, but they are also easier to fool with messy data.
How to measure accuracy honestly
The most common mistake is testing a model on data it already saw while learning. It looks good on paper, then misses in real use. A more honest approach:
- Hold out the latest period. For example, train the model on data up to three months ago, then test it on the last three months it has never seen.
- Test on a rolling basis. Repeat with a shifting time window, so the result doesn't hinge on one conveniently easy period.
- Never use future information. Data that only becomes known after a sales day must not be used to forecast that day.
- Compare against the simple method. Measure the average gap between forecast and actual sales, then compare it with the moving average. If it isn't better, use the simple one.
Our example: an LSTM prototype
To learn how to test forecasts properly, we built a personal prototype: an LSTM model with attention that forecasts stock prices 1 to 30 days ahead. To be clear, the data is stock prices, not sales data, and the project is still a prototype that no client uses. The code is in a public repository.
What carries over to sales forecasting is the testing approach. The model is checked with walk-forward validation: it is trained on one window of data, tested on the following period, then the window shifts and the step repeats. Indicators are lagged by one day, and data scaling is learned from the training data only. Stock prices are among the hardest data to forecast, so we treat this prototype as practice in method, not a tool for investment decisions.
The same testing applies to demand, stock, or sales in your business. The results can also feed a dashboard, like the ones we use at Numekara, which say "not available" when data is missing instead of showing a fake zero.
Limits you should know
- Extraordinary events can't be predicted from the past. A pandemic, a disaster, or a competitor's product suddenly going viral will throw a forecast off.
- Little data means large uncertainty. A new product or a business only a few months old has no pattern to learn yet.
- A forecast is a range, not an exact number. Saying "between 80 and 110 units" is more useful than one confident-looking figure.
How to start
- Tidy your sales history into one table, one row per day per product.
- Add columns for promotions, holidays, and stock-out days.
- Calculate a simple moving-average forecast, and note how far off it is.
- If the misses still cost you and you have enough data, only then test a machine learning model on held-out data.
- Use the results as input for discussion, and keep the final decision in your hands.
A good forecast isn't the most complex one. It's the one that has been tested honestly and beats a simple guess.