Time-varying delays and kernels
Introduction
Reporting delays and generation intervals often change as testing or behaviour changes. A TimeVarying kernel models this with one column per day.
Packages used
using ComposableRecurrences
using ComposableRecurrences: Primary
using CairoMakie, AlgebraOfGraphics, DataFramesMeta
CairoMakie.activate!(type = "png", px_per_unit = 2)A delay that changes over time
Each column of P is a discretised delay, lag 0 first. Column
on lag means in the code, rises up to day 40 and falls as reporting recovers. The plot shows the mean delay of each column,
T, L_d = 80, 15
function delay_pmf(mean)
w = [(d + 1)^(mean - 1) * exp(-d) for d in 0:(L_d - 1)]
return w ./ sum(w)
end
means = [2 + 4 * exp(-((t - 40) / 15)^2) for t in 1:T]
P = hcat(delay_pmf.(means)...)
realised = vec(sum((0:(L_d - 1)) .* P; dims = 1))
@chain DataFrame(day = 1:T, mean_delay = realised) begin
data(_) * mapping(:day, :mean_delay) * visual(Lines, linewidth = 2)
draw(_; axis = (xlabel = "Day", ylabel = "Mean delay (days)"))
endThe mean delay is 1.2 days at the start and end, and peaks at 5.0 days on day 40.
Reporting by report day or infection day
Let P. The two indexings give
where Secondary(), column Primary(), column
infections = [100 * exp(-((t - 35) / 12)^2) for t in 1:T]
reports = @chain DataFrame(
"day" => 1:T,
"Infections" => infections,
"Constant delay" => Convolution(P[:, 1])(infections),
"By report day" => Convolution(TimeVarying(P))(infections),
"By infection day" => Convolution(TimeVarying(P, Primary()))(infections)
) begin
stack(Not(:day); variable_name = :series, value_name = :count)
end
@chain reports begin
data(_) * mapping(:day, :count, color = :series) * visual(Lines, linewidth = 2)
draw(_; axis = (xlabel = "Day", ylabel = "Count"))
endBoth time-varying delays report later than the constant one while the delay is long. Indexed by report day, the curve moves later but keeps most of its height, with a peak of 96 on day 40. By infection day, it is also flatter, with a peak of 85 on day 41. The infections at the peak have the longest delays.
Which indexing conserves cases
Summing the reports over all days, while the window is long enough to hold every delay, gives
where
cumulative = @chain reports begin
@groupby(:series)
@transform(:cumulative = cumsum(:count))
end
@chain cumulative begin
data(_) * mapping(:day, :cumulative, color = :series) * visual(Lines, linewidth = 2)
draw(_; axis = (xlabel = "Day", ylabel = "Cumulative count"))
endIndexed by infection day, the cumulative reports reach the cumulative infections. By day 80 both reach 2127. Indexed by report day, they reach 2165, an overshoot of 1.8%. The delay shortens after day 40, so some infections are counted on more than one day.
A generation interval that shortens
A recurrence kernel can change over time too, one generation interval per day:
Here long_gi and short_gi below, with means of 3.7 and 2.1 days. Isolation shortens the interval from day 30, so offspring come sooner. The reproduction number is held at
long_gi = [0.05, 0.15, 0.25, 0.25, 0.2, 0.1]
short_gi = [0.3, 0.4, 0.2, 0.1]
fixed = Recurrence(long_gi)(1.2; history = fill(10.0, 6), stop = T)
G = TimeVarying([1 => long_gi, 30 => short_gi])
shortened = Recurrence(G)(1.2; history = fill(10.0, 6), stop = T)
@chain DataFrame(
"day" => 1:T, "Fixed interval" => fixed, "Interval shortens on day 30" => shortened
) 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 = "Infections"))
endWith the shorter interval the same reproduction number grows faster. By day 80 there are 4310 infections a day, against 586 with the fixed interval.
An isolation policy that starts on a set day
A recurrence kernel read by infection day gives each case the generation interval of the day it was infected, a cohort effect. The default, output-day kernel is a period effect, the same for every case on a given day.
Isolation thins the generation interval. Here a case infected on day
where long_gi, and delay_iso. The calendar-day kernel instead starts the same thinning on day
where Primary() column start = 7 after six seed values, and vcat joins the seed in front of the run.
p_iso, b_iso, t0 = 0.7, 0.9, 30
delay_iso = [0.2, 0.3, 0.3, 0.2] # P(D = 0, 1, 2, 3)
P_iso(l, from) = sum(delay_iso[d + 1] for d in max(from, 0):min(l, 3); init = 0.0)
cohort = [long_gi[l] * (1 - p_iso * b_iso * P_iso(l, t0 - c)) for l in 1:6, c in 1:T]
period = [long_gi[l] * (1 - (t >= t0 ? p_iso * b_iso * P_iso(l, 0) : 0.0)) for l in 1:6, t in 1:T]
R_iso = fill(1.8, T)
seed_iso = fill(10.0, 6)
function seeded_iso(kernel)
y = Recurrence(kernel)(R_iso; history = seed_iso, start = 7)
return vcat(seed_iso, y)
end
@chain DataFrame(
"day" => 1:T,
"No isolation" => seeded_iso(long_gi),
"By infection day" => seeded_iso(TimeVarying(cohort, Primary())),
"By calendar day" => seeded_iso(TimeVarying(period)),
) 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 = "Infections (log scale)", yscale = log10))
endWithout isolation the outbreak keeps growing. Both isolation curves turn down near day 30. Read by calendar day, every case infectious on day 30 is thinned at once, as if isolated from infection. Infections fall from 589 on day 29 to 291 on day 30. Read by infection day, cases infected shortly before day 30 keep most of their transmission, because their isolation would fall before the policy starts. Infections then peak on day 31, at 642. The extra cases seed later generations, so infections stay 1.86 times as high.
A kernel from a hazard and a survival
Some cohort kernels are products of a profile over the lags and a cohort's survival against a hazard over calendar days. Here patients admitted on day
Survival is the product, against TimeVarying(h), which a cohort's lag from = 1. Arithmetic with it builds a Derived, so the kernel is written as the formula. The operator computes the weights it reads inside its own call and reverse pass, so the kernel is never built entry by entry in the code being differentiated.
using ComposableRecurrences: Survival
stay = [exp(-l / 8) for l in 0:(L_d - 1)]
testing = [0.05 + 0.25 / (1 + exp(-(t - 35) / 5)) for t in 1:T]
in_care = Convolution(stay)
confirmed = Convolution(TimeVarying(stay * (1 - Survival(TimeVarying(testing); from = 1)), Primary()))
admissions = [20 * exp(-((t - 40) / 15)^2) for t in 1:T]
@chain DataFrame(
"day" => 1:T, "In care" => in_care(admissions), "Confirmed in care" => confirmed(admissions),
) 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 = "Patients"))
endFew patients in care are confirmed while testing is scarce; once it scales up after day 35, most patients are confirmed within a few days of admission.
Performance
Each bar times a 15-day delay indexed by infection day over 200 days, against a preallocated hand-written loop.
performance_plot("time-varying delays")Learning more
API overview: a map of every operator, coupling and modifier.
Public API: every public name with its docstring.