Skip to content

How do I extrapolate a growing trend?

Sales that grow steadily can be projected a few weeks ahead from their recent growth rate. TREND measures the rate and FORECAST applies it. The script grows sales by 5 a week and projects 5 weeks ahead.

from nagasim import build as nb

# Sales start at 100 and grow by 5 a week. TREND is the fractional growth rate against the 4-week
# smoothed value, and FORECAST extends the series 5 weeks ahead along that trend.
m = nb.Model(time_limit=30, time_unit="weeks", sd={"dt": 0.25, "method": "euler"})
m.add_auxiliary(name="Sales", expr="100 + RAMP(5, 0)")
m.add_auxiliary(name="Trend", expr='TREND(AUX("Sales"), 4)')
m.add_auxiliary(name="Forecast", expr='FORECAST(AUX("Sales"), 4, 5)')
m.add_level(name="Shipped", initial=0)
m.add_flow(name="shipping", to="Shipped", rate='AUX("Sales")')

series = m.run(seed=1)["timeseries"].pivot(index="Time", columns="Series", values="Value")
view = series[["aux/Sales", "aux/Trend", "aux/Forecast"]]
view.columns = ["Sales", "Trend per week", "Forecast +5"]
print(view.reindex([4, 10, 20, 30], method="nearest").round(3).rename_axis("Week").to_string())
print("actual sales 5 weeks after week 20:", 100 + 5 * 25)

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

       Sales  Trend per week  Forecast +5
Week                                     
4     120.25           0.030      138.419
10    150.25           0.035      176.632
20    199.75           0.028      227.353
30    250.00           0.022      277.161
actual sales 5 weeks after week 20: 225

How it works

  1. TREND(x, avg_time) is the fractional growth of x against its smoothed value, and FORECAST(x, avg_time, horizon) extends x along it.
  2. At week 20, sales are 199.75 and the forecast for week 25 is 227, against a true 225, so the projection is close but not exact.
  3. The averaging time sets the lag of both functions; a shorter time reacts faster and is noisier.

Elements used

Back to all recipes.