Technical

The forecast-based method, and an honest comparison

We built a store–option forecast with LightGBM and raced it against the rule. The model was clearly more accurate in its forecasts, but not in its results. What matters is not which one won but where the difference comes from.

Warehouse to Store · 5 / 6

In the last article we built the rule-based method: the last seven days’ sales, the special-day factor and a floor. Then we raced it against Lumoda’s current method. The rule finished 8.77 points behind, and 5.45 points of that gap came from how much stock was carried.

Now we do the same job with machine learning and replay the same four months, with the same customer stream. Our question: the rule’s forecast is a rough sum, so wouldn’t a better forecast change the result?

Let me say it up front: what matters in this article is not which method wins. A better forecast did not bring a better result. What matters is where the difference comes from.

The model

We use a single global LightGBM model. In other words, there is no separate model for each store–option pair. All the pairs are in the same model.

Target variable: a store–option pair’s sales over the next seven days.

Features: weekly sales for each of the last four weeks, four- and eight-week averages, the chain’s total sales last week, line, category, store type, and the holiday and Black Friday days in the coming week. There is one more feature: how many days the pair was on the rail last week. That number tells the model whether last week’s zero sales mean “no demand” or “the rail was empty”.

Objective: Poisson. The data are counts.

Training: the model is trained once. Up to the day before the season’s first products go on the rail, with close to three years of history: 128 Mondays, 1,651,315 rows. Each row is one Monday, one pair and that pair’s sales the following week. We left out of training any pair that was not on the rail for even one day the following week, because the zero sales of an empty rail say nothing about demand. Training reads no row after this end day; a test makes sure of that.

The model learns nothing new during the four months. Each Monday only the inputs, that is the sales of the last few weeks, are refreshed; the trees are the same on the last Monday as on the first.

Hyperparameters: 31 leaves, 400 trees, a learning rate of 0.05. We picked them from a small grid in a trial on other data, and did not search again on this data.

The model’s code is in cases/warehouse-to-store/warehouse_to_store/forecast.py. The three arms are defined in policy.py, all the runs are in game.py, and the five customer streams in paths.py.

This is the standard model people build when they say “forecasting with ML”. We chose a strong rival on purpose. Comparing against a weak model teaches nobody anything.

The model is ahead on the forecast

First let us look at the model itself. We held back the last eight Mondays of the training history, trained the model on the ones before, and forecast the sales of those eight weeks. The error measure is Poisson deviance; smaller is better:

ForecastPoisson deviance
LightGBM2.978
Average of the last four weeks3.866
Last week’s sales9.108

This table measures the forecast against sales: the sales that actually happened in the validation weeks. Last week’s sales are the raw form of the rule’s forecast. The rule divides it by in-stock days and corrects it with the special-day factor; those corrections are not in this table. Even so, the distance between them is large.

The excuse you hear most often in a race like this is sparse data: on a pair that sells one or two a week there is no pattern to learn, and the difference between taking an average and building a model disappears. That excuse does not hold on this data. A pair on the rail in the validation weeks sells 12.35 units a week on average, and the model’s forecast averages 11.78.

The real test is on the decision day. The first Monday of the four months gives a clean comparison, because that morning every arm has the same history: none of them has made a single decision yet. The real demand of the 10,868 pairs on sale over the coming week is 205,714 units. Only the referee knows this number, and it includes the customers who came in while the rail was empty and left:

RuleLightGBM
Forecast155,732202,745
As a share of real demand75.7%98.6%

LightGBM’s forecast is 30.2% higher than the rule’s and much closer to real demand. The rule misses close to a quarter of demand; the model sees almost all of it.

The first set-up

We built the model, made its forecast the target stock directly and played the four months. The warehouse constraints are those of the third article: the pack rule and a loose picking cap of 25,826 units a week.

RuleLightGBM onlyDifference
Availability65.10%63.21%−1.89 points
Lost sales rate38.89%40.79%+1.90 points
Units shipped1,543,0821,473,543−69,539
Store stock at the end of the four months311,819299,711−12,108

Not a disaster. But a better forecast did not bring a better result. The model, which forecast far more stock than the rule on the first Monday, shipped less stock over the four months as a whole and finished with lower availability.

It is easy to conclude from this that “so ML is no use”. But that would be wrong. Because when we compared the two arms we had changed two things at once:

  1. Who produces the forecast: last week’s sales, or LightGBM?
  2. Is there a floor under the forecast?

The rule had a 4-unit floor; the LightGBM arm had none. In the fourth article we saw the floor having the last word in more than two-fifths of decisions. This comparison was not measuring “is the rule better, or ML” but a mixture of two questions.

A third arm

So we built a third arm. LightGBM produces the forecast. Everything else is the same as in the rule arm: the floor, the split to sizes, the warehouse constraints, the pack rule, the rationing priority. In the code the three arms differ only where they build the target:

# warehouse_to_store/policy.py (the three arms' targets; the classes' other members left out)

class RuleArm(_TargetArm):
    """target = max(rule forecast, safety stock); the special-day factors once."""

    def _target(self, view):
        forecast = self._forecast(view)
        return np.maximum(forecast, self._safety_stock(view)), forecast


class ForecastArm(_TargetArm):
    """target = the LightGBM forecast (no floor)."""

    def _target(self, view):
        forecast = self._forecast(view)
        return forecast, forecast


class ForecastFloorArm(ForecastArm):
    """target = max(LightGBM, safety stock): the same floor as the rule, another forecast."""

    def _target(self, view):
        forecast = self._forecast(view)
        return np.maximum(forecast, self._safety_stock(view)), forecast

_safety_stock is the safety stock, the floor. The second value returned is the forecast itself: the rationing priority (cover) uses it to rank the stores. So the priority rule is the same, but each arm builds the ranking with its own forecast.

The three arms’ results over the same four months:

RuleLightGBM + floorLightGBM only
Availability65.10%65.00%63.21%
Lost sales rate38.89%39.35%40.79%
Days to sale7.5 days7.5 days7.2 days
Units shipped1,543,0821,508,6901,473,543
Store stock at the end of the four months311,819331,606299,711
Picking costTRY 7,673,800TRY 7,606,220TRY 7,493,540

Put the floor back and LightGBM catches up with the rule: the gap is 0.10 points. But it cannot get ahead. And it ends the four months with 19,787 more units of stock.

Splitting the difference in two

Now we come to the real sum. Looking at a single customer stream is not enough: a gap of 0.10 points could just be chance. So we played the same four months on five different customer streams. In each stream LightGBM was retrained on that stream’s own history.

We work out two differences.

The effect of the forecast: LightGBM + floor minus the rule. Same floor, different forecast.

MeasureMedian differenceIn how many of the 5 streams
Availability−0.33 pointslower in 4
Lost sales rate+0.69 pointshigher in 4
Store stock at the end of the four months+19,866 unitshigher in 5
Picking cost−TRY 16,430lower in 4

The effect of the policy: LightGBM only minus LightGBM + floor. Same forecast, the floor removed.

MeasureMedian differenceIn how many of the 5 streams
Availability−1.79 pointslower in 5
Lost sales rate+1.44 pointshigher in 5
Store stock at the end of the four months−16,655 unitslower in 3
Picking cost−TRY 112,680lower in 5

The effect of the policy points the same way in all five streams: remove the floor and availability falls and losses rise. The effect of the forecast is small, and in one of the five streams it changes direction.

On the lost sales rate, the effect of the policy is 2.09 times that of the forecast: 1.44 points against 0.69 points at the median. On availability the ratio is bigger still, 5.42. So what makes the difference is not changing the model but what you do with the model’s output.

This is one of the main ideas of the series too: changing the forecasting engine changes little; changing the policy changes a lot. We saw the same thing from another angle in the fourth article. What brought the rule closest to Lumoda was not the forecast but the height of the floor.

Why didn’t a better forecast turn into a better result?

LightGBM is clearly more accurate than the rule at forecasting. Its floor is the same as the rule’s. Yet on availability it could not get ahead of the rule: it is behind in four of the five streams. And in all five it finishes with more stock. By the fourth article’s measure that is no gain: more stock for slightly less availability.

We cannot say why from this experiment. What we can say is this: the data are not sparse on average, and the model forecasts better than the rule. In all three arms the warehouse works at the same limit; the loose picking cap is full in 94.4% of weeks.

Pars has a short line on this:

“A forecast doesn’t walk onto the rail by itself. In between there’s the floor, the split to sizes, what’s in the warehouse, the weekly cap, the pack rule. The forecast is the first link in that chain. Improving the first link doesn’t change the end of the chain on its own.”

We did not measure separately which link the better forecast gets lost in.

One more honesty note. Lumoda is a synthetic dataset, and its demand is generated by known rules. In theory that could have helped the model: it could discover the pattern and look better than it would in real life. It did look good at forecasting, but not in the results. On a real chain’s data both sides might change. We did not measure that.

So is ML no use at all?

One row of the table is in the model’s favour: picking cost. LightGBM only is TRY 180,260 cheaper than the rule, LightGBM + floor TRY 67,580 cheaper. Across the five streams, LightGBM only is cheaper than the rule in all five and LightGBM + floor in four. On the published customer stream, the one in the table, days to sale is also shortest for LightGBM only.

So LightGBM only is not “wrong”; it just stands at a different point: it sends less stock, runs up less picking cost and loses more sales. Which one is right depends on your gross margin and your costs. The profit sum from the Transfer series needs to be done here too.

My conclusion is this: building a store–option forecast was not hard on this data, and the model clearly beat the rule at forecasting. But in this experiment, what moved the result was less the forecast’s accuracy than the policy built on top of it. If you want to build a model, build one. But fix your policy first: the floor, how much stock you will carry. On the lost sales rate, the effect there is twice as big.

What is hard in your data

In this article the validation table measured the forecast against sales; the first Monday’s table measured it against real demand. We could do the second because at Lumoda the customer who comes in while the rail is empty and leaves is written down somewhere. In your data you can only measure the forecast against sales. And sales look low while the rail is empty. A model that forecasts the sales that fall while the rail is empty will look good in the validation table and still send too little stock to the rail.

The second is the moment of decision. You train the model on historical tables, but on Monday morning you work out the features from the live system. If the two do not give the same number, the model makes its decision in a world it never saw in training. On a Monday before the game, we checked that the two paths produced exactly the same features for 66,559 physical pairs. In a real chain, transfers booked late, returns and count corrections quietly pull those two paths apart.

In the last article we look at how success should be measured. We will come back to this article’s tables there once more, because one of the measures in them is lying. That is also where we look at where the 3.32 points to Lumoda open up.

By Sabri Suyunu