
Senior Machine Learning Engineer at Shipt / Target
Senior Machine Learning Engineer at Shipt / Target
Senior Machine Learning Engineer at Shipt / Target
Part 1 introduced a three-metric scorecard - WMAPE, Total Bias, and Active Seller Bias- for judging whether a SKU_level forecast puts the units in the right place. Here, we turn the framework into a reusable,leakage-safe backtesting framework in Python, using the public M5 Forecasting dataset from Walmart.
M5 has ~30,400 item-store sales (3049 products, 10 stores in California, Texas, and Wisconsin, 3 categories, 7 departments) covering 1,941 days. It’s intermittent and long-tailed, with plenty of zeros and late launches, which is exactly the setting where allocation errors hide.
New if first sale in the store is < 26 weeks before the forecast origin
Differentiation at regional level or type of store group
Link to M5 Dataset : https://www.kaggle.com/competitions/m5-forecasting-accuracy/data
M5 data comes in wide format: one column per day. We keep only complete Saturday-to-Friday Walmart weeks, summing days into weeks with np.add.reduceat while the data is still wide, and then melt to a long store-item-week table.
The full file is about 30k X 1,941 integers. Pass stores=[…] to work with a subset while you develop, and run on all 10 stores for the final numbers.
For a product to be defined as “New”, it has to be defined relative to the forecast origin. Otherwise, an item launched after the origin leaks into the label. An item-store counts as “New” if its first sale occurred less than 26 weeks before the origin.
A single train/test split tells you about one six-week window. Rolling-origin backtesting repeats the exercise at several forecast origins, so you can see whether the metrics are stable or just an occurrence by chance. Each fold trains only on data up to its origin and forecasts the next six weeks. With the defaults (4 folds, 6-week step), the latest origin T is placed so its window ends at the last week of data.
For a mode trained at past origins o, the target window o+1 ….. o+6 has to end on or before the backtest origin. In the GBM below, the latest origin is origin-horizon, so its label ends exactly at the origin.
The goal is not to win M5. It’s to show that the scorecard can tell apart models with different behaviors.
By design, GroupSpread and MovingAvg_8w forecast the same total volume, so their Total Bias is identical. Any gap in Active Seller Bias between them is pure allocation. It’s a quick way to show stakeholders why Total Bias alone isn’t enough.
One function scores any cut of the data. You pass the grouping columns and get back the counts, the three core metrics, % predictions on sellers, and a 3X3 matrix.
The output has one row per store x item x fold x model, with the actual 6-week units, the prediction, and every segment attribute. Every report after this is just a groupby.
Lowest WMAPE and Active Seller Bias closest to 1. Watch for models that win on WMAPE but lose on active seller bias.
Metrics that jump between folds point to sensitivity to seasonality or events.
“New” rows usually show less seller bias: less history means more spreading.
A single department dragging the total down is a candidate for its own model or features
Stores where WMAPE looks fine but seller bias is low: forecasted volume is going to the wrong items
Largest (actual-forecast) on sellers. Good for item-level deep dives.
Run the script on the M5 files and paste the model_comparison.csv and segment tables in here. The metric definitions, thresholds, and diagnosis text stay exactly the same.
This article concludes the two-part series on the Retail Backtesting Framework and key metrics to track to save retail and similar companies that deal in forecasts millions of dollars in misallocation and lost revenue.
Evaluating SKU-Level Forecasts With WMAPE, Total Bias, and Active Seller Bias
Senior Machine Learning Engineer at Shipt / Target