Skip to content

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 ​

julia
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 is the number of infections on day , the reproduction number and the weight of the generation interval on lag , with   lags. Day   is the last day with data.

Day depends on the past only through . A run to day therefore continues from its last infections and the index of the next day,  . A State holds both.

julia
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 and the state.

julia
observed, state = renewal(R; history = [5.0], stop = τ, record = Resume())
round(observed[end])
27.0

Infections are 27 on day 50, the last day of the run.

Here is known up to day . With data, you would first estimate it up to that day. Conjugate estimation of the reproduction number does this in closed form. Particle filtering does it for a stochastic model with depletion. Either gives the and the state that the forecast below continues from.

The forecast from the state ​

A call with state returns the infections from day  . It takes the whole of , as every call reads its inputs at the day of the model, counted from 1.

julia
# 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])
true

The two calls together equal one call over all 80 days to rounding error.

julia
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 after day , as a random walk on that starts at 1.2. 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)).

julia
# 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.

julia
performance_plot("forecasting")

Learning more ​