Skip to content

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 ​

julia
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 is the number of infections on day and the reproduction number, the gain of the Recurrence. is the generation interval's weight on lag , and   is its length. The history is one day of 5 infections before day 1.

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

Seeding on a growth path ​

A single seed day makes the first generations uneven. A seed over all days before day 1, already growing at rate , avoids this:

where is the seed on the -th of these days and the seed on the last of them. Here   per day, close to the rate at which this renewal grows while  . exponential_history builds the seed.

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

The dashed line is . With the growing seed the daily growth ratio stays close to from day 1 until changes. With a single seed day the ratio swings for the first generations.

Susceptible depletion ​

A finite population of susceptibles limits the outbreak. A Depletion draws each day's infections from the pool left:

where is the number of new infections before depletion and  . Depletion(N) takes the hazard form, and Depletion(N, Floor()) the floored form. Here is 2000 and is 1.8 on every day.

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

Both forms end the outbreak as the pool empties. The hazard form draws less at the peak and leaves more susceptibles, because   is below .

Reported cases ​

Reports are a share of the depleted infections, recorded after a delay:

where is the number of reports on day and the delay's weight on lag . is the gain of the Convolution. Here is 0.4.

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

Performance ​

Each bar times a seeded renewal with hazard depletion over 200 days with a 20-day generation interval, against a preallocated hand-written loop.

julia
performance_plot("renewal")

Learning more ​