Skip to content

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 ​

julia
using ComposableRecurrences
using CairoMakie, AlgebraOfGraphics, DataFramesMeta

CairoMakie.activate!(type = "png", px_per_unit = 2)

Continuing a run ​

The renewal process is

where is the number of infections on day , the reproduction number and the generation interval's weight on lag . Day reads only . So a run to day 50 continues from the last infections, on days 46 to 50, and the next day, 51. The call 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 is 1.4 to day 30, 0.9 to day 50 and 1.2 after.

julia
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"))
end

The forecast reads on days 51 to 80, as the time base counts days from 1 whatever the start. The two calls together match one call over all 80 days to rounding error.

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.

julia
performance_plot("forecasting")

Learning more ​