Skip to content

How do I add a first-order information delay to a forecast?

A planner's forecast follows demand but with a lag, because it averages what has been seen. DELAY1 is that lag. The script raises demand at week 5, lets production follow a forecast that trails by 4 weeks, and shows the stock drain.

from nagasim import build as nb

# Demand jumps from 100 to 150 a week at week 5. Production follows a forecast that catches up with
# demand through a first-order delay with a 4-week time constant, so stock drains while it does.
m = nb.Model(time_limit=40, time_unit="weeks", sd={"dt": 0.25, "method": "euler"})
m.add_auxiliary(name="Demand", expr="100 + STEP(50, 5)")
m.add_auxiliary(name="Forecast", expr='DELAY1(AUX("Demand"), 4)')
m.add_level(name="Stock", initial=400)
m.add_flow(name="production", to="Stock", rate='AUX("Forecast")')
m.add_flow(name="sales", **{"from": "Stock"}, rate='AUX("Demand")')

series = m.run(seed=1)["timeseries"].pivot(index="Time", columns="Series", values="Value")
view = series[["aux/Demand", "aux/Forecast", "level/Stock"]]
view.columns = ["Demand", "Forecast", "Stock"]
print(view.reindex([4, 8, 12, 20, 40], method="nearest").round(1).rename_axis("Week").to_string())

Save it as recipe-forecast-lag.py and run it with python. The script seeds itself, so the numbers below are the same on your machine.

      Demand  Forecast  Stock
Week                         
4      100.0     100.0  400.0
8      150.0     127.0  292.2
12     150.0     141.8  232.8
20     150.0     149.0  204.2
40     150.0     150.0  200.0

How it works

  1. DELAY1(x, tau) moves its output towards x at a rate proportional to the gap, with time constant tau; it needs simulation.sd.
  2. The forecast closes 63% of the gap in 4 weeks and 99% in 20, so production lags demand and the stock falls from 400 to 200.
  3. The level and the two flows give the model a step grid to run on, and the auxiliaries are read from the timeseries frame.

Elements used

Back to all recipes.