Naive forecasts
Every Monday Ordito’s planning office decides how many pieces of each category to send to the stores. It needs a week-by-week sales forecast. Before any model, though, there are three forecasts that learn nothing, and that are hard to beat.
Three baseline models
The naive forecast says: next week will be like this one. The seasonal one: it will be like the same week last year. The moving average: it will be like the average of the last k weeks.
To compare them we use the WAPE, the sum of the absolute errors divided by the sum of sales. A WAPE of 0.10 means that, over everything that was sold, the forecast was wrong by 10%. It is the most widely used measure in retail, because it is not dominated either by the large categories or by the weeks in which very little is sold.
In the lab you choose a category and a method, and how many weeks ahead to forecast. The WAPE is measured on 2025.
Three experiments
Last week. One week ahead, the naive forecast is wrong by 11.5% over all categories: sales change slowly, except at the start of the sale seasons. But increase the lead time: at three months the error rises to 35%.
A year ago. The seasonal one is wrong by 16%, at any lead time. One week ahead it loses to the naive one; from two weeks upwards it wins.
The forgotten shift. In the moving average you compute the shift first, then the average. Tick “dimentica lo shift” (forget the shift): the average includes the week to be forecast, and the error drops from 0.170 to 0.118. It looks like a better model, and it is false: on Monday morning that week has not happened yet.
The same experiment in Python
import numpy as np
import pandas as pd
URL = "https://raw.githubusercontent.com/mid079/machine-learning-teoria-pratica/main/dati/"
scontrini = pd.read_parquet(URL + "scontrini.parquet", columns=["id_scontrino", "data_ora"])
righe = pd.read_parquet(URL + "righe_scontrino.parquet", columns=["id_scontrino", "id_modello", "quantita"])
articoli = pd.read_parquet(URL + "articoli.parquet", columns=["id_modello", "categoria"])
r = righe.merge(scontrini, on="id_scontrino").merge(articoli, on="id_modello")
r["settimana"] = r["data_ora"].dt.to_period("W-SUN").dt.start_time
vendite = r.pivot_table(index="settimana", columns="categoria", values="quantita", aggfunc="sum").loc["2023-01-02":"2025-12-22"]
def wape(vere, previste):
return np.abs(vere - previste).to_numpy().sum() / vere.to_numpy().sum()
nel_2025 = vendite.index.year == 2025
for nome, previste in [("naive", vendite.shift(1)), ("seasonal", vendite.shift(52)),
("mean of 4", vendite.shift(1).rolling(4).mean()),
("mean of 4, without shift", vendite.rolling(4).mean())]: # the last one cheats
print(f"{nome:24s} WAPE {wape(vendite[nel_2025], previste[nel_2025]):.3f}")
In the book
This lab accompanies chapter 14, “Time series”: trend, seasonality and noise, the baseline models, lag features, validation over time with TimeSeriesSplit, ridge and gradient boosting, and the direct and recursive strategies for forecasting several weeks ahead.
The “Inside Ordito” series
- Chapter 1 The line of the five stores
- Chapter 2 A simulated store
- Chapter 3 Where the leak hides
- Chapter 4 The valley of the five stores
- Chapter 5 The threshold
- Chapter 6 Why was it returned?
- Chapter 7 Build a tree
- Chapter 8 Boosting step by step
- Chapter 9 The margin
- Workshop A Price is place
- Chapter 10 Shapley by hand
- Chapter 11 Rotate the cloud
- Chapter 12 Groups and dense areas
- Chapter 13 Isolating a point
- Chapter 14 Naive forecasts
- Workshop B A year in production
- Chapter 15 The ink stain
- Chapter 16 Backpropagation