Forecasting by continuing a run from its state
Introduction
This tutorial is for readers who have a model run up to the last day with data and want to forecast past that day. It runs a renewal process to the last day with data, keeps the state of the run, and continues from that state. It then steps sample paths forward from the same state, each with its own future reproduction number.
It assumes you have read Three towns, step by step.
Packages used
using ComposableRecurrences
using ComposableRecurrences: Resume, State, advance!
using Random
using CairoMakie, AlgebraOfGraphics, DataFramesMeta
CairoMakie.activate!(type = "png", px_per_unit = 2)Model
The renewal process is
where
Day State holds both.
gi = [0.1, 0.3, 0.3, 0.2, 0.1]
T = 80
R = [t <= 30 ? 1.4 : t <= 50 ? 0.9 : 1.2 for t in 1:T]
τ = 50
renewal = Recurrence(gi)The run to the last day with data
A call with stop = τ and record = Resume() returns the infections to day
observed, state = renewal(R; history = [5.0], stop = τ, record = Resume())
round(observed[end])27.0Infections are 27 on day 50, the last day of the run.
Here
The forecast from the state
A call with state returns the infections from day
# The run of `renewal` to day `τ` and its continuation to the end of `R`.
function run_then_forecast(renewal, R, history, τ)
observed, state = renewal(R; history, stop = τ, record = Resume())
return observed, renewal(R; state)
end
observed, forecast = run_then_forecast(renewal, R, [5.0], τ)
vcat(observed, forecast) ≈ renewal(R; history = [5.0])trueThe two calls together equal one call over all 80 days to rounding error.
table = DataFrame(
day = 1:T, value = vcat(observed, forecast),
part = [t <= τ ? "Run to day 50" : "Forecast" for t in 1:T]
)
draw(
data(table) * mapping(:day, :value, color = :part => "") *
visual(Lines, linewidth = 2) +
mapping([τ + 0.5]) * visual(VLines, color = :grey, linestyle = :dash);
axis = (xlabel = "Day", ylabel = "Infections")
)The forecast rises from 34 on day 51 to 203 on day 80.
Sample paths from the state
A forecast usually carries uncertainty in the future reproduction number. Each sample path below draws its own advance! continues a state to stop in the buffer the state already holds, without copying the history. It uses up the state it is given, so each path starts from a copy, State(state, vec(state)).
# A random walk on the log scale after `τ`, so paths drift apart over time.
function future_R(rng, R, τ; sd = 0.05)
steps = sd .* randn(rng, length(R) - τ)
return vcat(R[1:τ], R[τ + 1] .* exp.(cumsum(steps)))
end
rng = Xoshiro(1)
paths = map(1:50) do path
start = State(state, vec(state))
(; y) = advance!(renewal, start, future_R(rng, R, τ); stop = T)
DataFrame(day = (τ + 1):T, value = y, path = path)
end
draw(
data(reduce(vcat, paths)) *
mapping(:day, :value, group = :path => nonnumeric) *
visual(Lines, color = (:grey, 0.3)) +
data(table) * mapping(:day, :value, color = :part => "") *
visual(Lines, linewidth = 2);
axis = (xlabel = "Day", ylabel = "Infections")
)The grey lines are the sample paths, which all continue the same run and spread out as their random walks move apart.
The same function steps a state one day at a time as new data arrive. Without stop, advance! runs one step and returns (; y, state), and the loop keeps the returned state for the next day.
Performance
The timed model is a renewal process run to day 100 and continued from its state to day 200, with a generation interval of 20 lags. The figure gives its minimum time and that of a preallocated hand-written loop of the same model.
performance_plot("forecasting")Learning more
History and state lists what a
Stateholds when the recurrence has modifiers.Particle filtering of a renewal model steps a state one day at a time with
advance!inside a filter.