← Back to the lab

Simulation

Medicine supply digital twin (1/14): forecasting demand with Holt-Winters

The start of a 14-part series on a digital twin of medicine supply in a public health system. First piece, the forecast. Level, damped trend and weekly season, with every formula worked by hand.

By ·

A prescription that goes unfilled almost never fails at the pharmacy. It fails weeks earlier, when someone decided how much to order using a number that no longer matched reality. That is why I built a digital twin of medicine supply: a software copy of the whole chain, from the annual plan to the moment the patient gets the medicine, to test decisions before making them.

This series walks through that twin in 14 posts. The setting is a fictional public health system: a central warehouse, regional warehouses, and primary and secondary care units. The data is simulated; the models and the math are the ones the twin uses, and every post comes with an interactive aid to move the parameters.

#ModuleQuestion
1ForecastHow much will be used?
2Safety stockHow much buffer is needed?
3Central warehouse policyMin-max or forecast?
4AllocationIf there isn’t enough, who gets what?
5RoutesHow to deliver with fewer kilometres?
6DocksHow many docks so there is no queue?
7 and 8Cold chainHow long does a fridge last? Who notices a failure?
9 and 10Procurement and contractsWhom to buy from? What if they don’t deliver?
11Physical inventoryHow often to count?
12Last mileHow does it reach the patient?
13Plan integrityWhich data prevent manipulation?
14Control towerWhere in the chain is the failure?

Today’s piece: the forecast

Everything else depends on knowing how much will be used. Demand for one item at one unit has three parts that can be separated:

  • a level: what is used on a typical day;
  • a trend: whether that level is going up or down;
  • a season: the weekly pattern. More prescriptions are filled on Mondays than on Sundays.

The Holt-Winters method tracks all three at once. Each day it compares what it forecast with what happened and nudges each part a little. How much it nudges is set by three numbers between 0 and 1: α\alpha, β\beta and γ\gamma.

The three equations

Each one is a weighted average of what the new data says and what was believed before:

ℓt=α (yt−st−m)+(1−α) (ℓt−1+ϕ bt−1)bt=β (ℓt−ℓt−1)+(1−β) ϕ bt−1st=γ (yt−ℓt)+(1−γ) st−m\begin{aligned} \ell_t &= \alpha\,(y_t - s_{t-m}) + (1-\alpha)\,(\ell_{t-1} + \phi\, b_{t-1}) \\ b_t &= \beta\,(\ell_t - \ell_{t-1}) + (1-\beta)\,\phi\, b_{t-1} \\ s_t &= \gamma\,(y_t - \ell_t) + (1-\gamma)\,s_{t-m} \end{aligned}
  • The level takes today’s demand without the day effect (yt−st−my_t - s_{t-m}) and blends it with what it expected.
  • The trend blends the level change it just saw with the one it had.
  • The day effect compares demand with the level and updates that weekday’s value.

And the forecast puts the three together:

y^t+h=ℓt+(ϕ+ϕ2+⋯+ϕh) bt+st+h−m\hat y_{t+h} = \ell_t + (\phi + \phi^2 + \dots + \phi^h)\, b_t + s_{t+h-m}

Why damp the trend

Without damping (ϕ=1\phi = 1), a trend of one unit per day is projected forever: in 90 days the forecast would rise by 90 units. With medicines that rarely happens; increases level off. With ϕ<1\phi < 1 the trend sum converges:

ϕ+ϕ2+ϕ3+⋯=ϕ1−ϕ=0.970.03=32.3\phi + \phi^2 + \phi^3 + \cdots = \frac{\phi}{1 - \phi} = \frac{0.97}{0.03} = 32.3

So however far ahead you forecast, the current trend never adds more than about 32 units. In the first week, on the other hand, it barely shows: ∑k=170.97k=6.21\sum_{k=1}^{7} 0.97^k = 6.21 against 7 undamped.

How α, β and γ are chosen

They are not guessed: they are validated. The twin trains the model on every day except the last seven, forecasts that unseen week and measures the error with WAPE:

WAPE=∑t∣ yt−y^t ∣∑tyt\text{WAPE} = \frac{\sum_t \left|\,y_t - \hat y_t\,\right|}{\sum_t y_t}

It tries 12 combinations (α∈{0.15,0.3,0.5}\alpha \in \{0.15, 0.3, 0.5\}, β∈{0.02,0.1}\beta \in \{0.02, 0.1\}, γ∈{0.1,0.3}\gamma \in \{0.1, 0.3\}) and keeps the one with the lowest error.

The uncertainty band

A forecast without uncertainty is misleading. The twin computes the deviation σ\sigma of the fitting errors and draws an 80 % band that widens with the horizon:

y^t+h±1.28  σ 1+0.15 (h−1)\hat y_{t+h} \pm 1.28\;\sigma\,\sqrt{1 + 0.15\,(h-1)}

1.28 leaves 10 % probability above and 10 % below in a normal distribution. The factor growing with hh is a practical rule: the error of forecasting four weeks out is larger than tomorrow’s. In the aid’s series, with σ≈12.7\sigma \approx 12.7 units, the band goes from ±16\pm 16 units tomorrow to ±36\pm 36 four weeks out.

Try it: move α\alpha, β\beta and γ\gamma, watch the error on the test week (orange line) and then let the button pick by validation.

The twin’s state network on the Forecast tab (“Pronóstico”): each unit’s demand runs in simulated time. (Interface in Spanish.) · Open full screen

What this forecast doesn’t see

Holt-Winters only looks at the item’s history. It doesn’t know the respiratory season is coming, that a new service opened or that last week there was a shortage and “real” demand stayed hidden. Two details of the twin help with that: demand is counted as filled plus denied, so a shortage doesn’t look like a drop in use, and the forecast interface is the same one a machine-learning model with weather, epidemiology and calendar data would use. Swapping the engine changes nothing downstream.

What comes next

A forecast is a number with uncertainty. The next post turns that uncertainty into units: how much safety stock is needed to fill 95 % of the time, and why an unpunctual supplier weighs more than variable demand.