Renewal then delay
Introduction
A renewal process generates infections, and a reporting delay turns them into reports. The reproduction number changes over time, the seed grows from the first step and the population is finite.
Packages used
using ComposableRecurrences
using ComposableRecurrences: Depletion, Floor, exponential_history
using CairoMakie, AlgebraOfGraphics, DataFramesMeta
CairoMakie.activate!(type = "png", px_per_unit = 2)A renewal process
New infections are a weighted sum of earlier infections:
where Recurrence.
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]
infections = renewal(R; history = [5.0])
@chain DataFrame(day = 1:T, count = infections) begin
data(_) * mapping(:day, :count) * visual(Lines, linewidth = 2)
draw(_; axis = (xlabel = "Day", ylabel = "Infections"))
endSeeding on a growth path
A single seed day makes the first generations uneven. A seed over all
where exponential_history builds the seed.
r0 = 0.12
h0 = exponential_history(5.0, r0, length(gi))
seeded = vcat(h0, renewal(R; history = h0))
single = vcat(5.0, renewal(R; history = [5.0]))
# The ratio of each day's infections to the day before's, on days 1 to 25.
ratio(y) = (y[2:end] ./ y[1:(end - 1)])[(end - T + 1):(end - T + 25)]
@chain DataFrame("day" => 1:25, "Growing seed" => ratio(seeded), "Single seed day" => ratio(single)) begin
stack(Not(:day); variable_name = :seed, value_name = :ratio)
data(_) * mapping(:day, :ratio, color = :seed => "") * visual(Lines, linewidth = 2) +
mapping([exp(r0)]) * visual(HLines, color = :grey, linestyle = :dash)
draw(_; axis = (xlabel = "Day", ylabel = "Daily growth ratio", limits = (nothing, (0, 2))))
endThe dashed line is
Susceptible depletion
A finite population of Depletion draws each day's infections from the pool
where Depletion(N) takes the hazard form, and Depletion(N, Floor()) the floored form. Here
N = 2_000.0
R_high = fill(1.8, T)
run_with(d) = Recurrence(gi; modifiers = (d,))(R_high; history = [5.0])
forms = ["Hazard" => Depletion(N), "Floor" => Depletion(N, Floor())]
@chain forms begin
map(((name, d),) -> DataFrame(day = 1:T, form = name, count = run_with(d)), _)
reduce(vcat, _)
@groupby(:form)
@transform(:susceptible = N .- cumsum(:count))
stack([:count, :susceptible]; variable_name = :quantity, value_name = :value)
@transform(:quantity = ifelse.(:quantity .== "count", "Infections", "Susceptibles"))
data(_) * mapping(:day, :value, color = :form => "", row = :quantity) *
visual(Lines, linewidth = 2)
draw(_; axis = (xlabel = "Day", ylabel = ""), facet = (; linkyaxes = :none))
endBoth forms end the outbreak as the pool empties. The hazard form draws less at the peak and leaves more susceptibles, because
Reported cases
Reports are a share
where Convolution. Here
infections_depleted = run_with(Depletion(N))
delay = Convolution([0.0, 0.1, 0.25, 0.3, 0.2, 0.1, 0.05])
reports = delay(infections_depleted; gain = 0.4)
@chain DataFrame("day" => 1:T, "Infections" => infections_depleted, "Reports" => reports) begin
stack(Not(:day); variable_name = :series, value_name = :count)
data(_) * mapping(:day, :count, color = :series => "") * visual(Lines, linewidth = 2)
draw(_; axis = (xlabel = "Day", ylabel = "Count"))
endPerformance
Each bar times a seeded renewal with hazard depletion over 200 days with a 20-day generation interval, against a preallocated hand-written loop.
performance_plot("renewal")Learning more
Vaccination: this model with a protected pool of the vaccinated.
Imported cases: this model with imports before and after depletion.
Clinical outcomes: onsets, admissions and deaths as a chain of delays.
API overview: a map of every operator, coupling and modifier.
Public API: every public name with its docstring.