Forecasting
Introduction
A forecast continues a run past the last day with data. The run returns its state, and a second call starts from it.
Packages used
using ComposableRecurrences
using CairoMakie, AlgebraOfGraphics, DataFramesMeta
CairoMakie.activate!(type = "png", px_per_unit = 2)Continuing a run
The renewal process is
where renewal(R; history, stop = 50, record = (:state,)) runs to day 50 and also returns them as a State. Passing the state back as state continues the run from day 51, and returns the forecast. Here
gi = [0.1, 0.3, 0.3, 0.2, 0.1]
renewal = Recurrence(gi)
T = 80
R = [t <= 30 ? 1.4 : t <= 50 ? 0.9 : 1.2 for t in 1:T]
last_day = 50
to_day_50, state = renewal(R; history = [5.0], stop = last_day, record = (:state,))
forecast = renewal(R; state)
@chain DataFrame("day" => 1:T, "count" => vcat(to_day_50, forecast)) begin
@transform(:part = ifelse.(:day .<= last_day, "Run to day 50", "Forecast"))
data(_) * mapping(:day, :count, color = :part => "") * visual(Lines, linewidth = 2) +
mapping([last_day + 0.5]) * visual(VLines, color = :grey, linestyle = :dash)
draw(_; axis = (xlabel = "Day", ylabel = "Infections"))
endThe forecast reads
Performance
Each bar times this forecast, a renewal run to day 100 and continued from its state to day 200 with a 20-day generation interval, against a preallocated hand-written loop.
performance_plot("forecasting")Learning more
History and state: what a
Stateholds, modifiers included.Public API: every public name with its docstring.