How to model demand over lead time
This guide shows how to simulate demand over a random lead time using a minimal ISSM and Monte Carlo sampling.
The example uses 500 trajectories. This is a rough starting point for inspecting quantiles through P95, not a universal production count. See How to choose a Monte Carlo sample count before applying the pattern to a production decision.
Use the playground script if you want a preloaded session.
preview = show region { 1..8, 1..25 }
/// Illustration of the demand integrated over the lead time
present = date(2021, 8, 1)
keep span date = [present .. date(2021, 10, 30)]
Day.Baseline = random.uniform(0.5 into Day, 1.5) // 'theta'
alpha = 0.3
level = 1.0 // initial level
minLevel = 0.1
dispersion = 2.0
Day.Q = each Day scan date // minimal ISSM
keep level
mean = level * Day.Baseline
deviate = random.negativeBinomial(mean, dispersion)
level = alpha * deviate / Day.Baseline + (1 - alpha) * level
level = max(minLevel, level) // arbitrary, prevents "collapse" to zero
return deviate
show linechart "A sample demand trajectory" { .., 1..14 in preview } with Day.Q
L = 7 + poisson(5) // Reorder lead time + supply lead time
montecarlo 500 with
h = random.ranvar(L)
Day.Q = each Day scan date // minimal ISSM
keep level
mean = level * Day.Baseline
deviate = random.negativeBinomial(mean, dispersion)
level = alpha * deviate / Day.Baseline + (1 - alpha) * level
level = max(minLevel, level) // arbitrary, prevents "collapse" to zero
return deviate
s = sum(Day.Q) when (date - present <= h)
sample d = ranvar(s)
show scalar "Raw integrated demand over the lead time" { .., 16..18 in preview } with d
show scalar "Smoothed integrated demand over the lead time" { .., 20..22 in preview } with smooth(d)
This preview shows one precomputed simulation, with compact summaries of the raw and smoothed distributions.
The smoothed ranvar can be easier to read, but smoothing does not add observations to the tail and does not justify using a more extreme quantile.