Skip to content

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 ​

julia
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 puts weight

on lag   , with  . The shape parameter , held in means in the code, rises up to day 40 and falls as reporting recovers. The plot shows the mean delay of each column,  .

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

The 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 be the weight on lag in column of P. The two indexings give

where is the number of infections and the reports on day . With the default Secondary(), column holds the delays of the reports on day . With Primary(), column holds the delays of the infections on day . A constant delay, the first column , is a reference.

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

Both 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 is the infection day. By infection day, each infection's delay sums to one, so each case is reported once. Indexed by report day, each day's weights sum to one. One infection is then weighted by several columns with different delays, so its weights need not sum to one and the total drifts.

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

Indexed 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 is the number of infections on day and the gain, the reproduction number. The weight on lag on day is , and is the interval's length. The intervals and are 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  , so only the timing changes. The kernel changes once, so it is given by its change points, each start day paired with the interval from that day.

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

With 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 is isolated with probability after a delay . Isolation blocks a share of its later transmission. The policy starts on day , and an isolation that would fall before it does not happen. The expected generation interval of cohort at lag is then

where is long_gi, and  ,   and  . The delay is 0, 1, 2 or 3 days, with probabilities delay_iso. The calendar-day kernel instead starts the same thinning on day for every case:

where   is one from day and zero before. Both runs use    , with  . With Primary() column is cohort 's interval, read at lag on day  , and with the default indexing column is the interval used on day . The run starts at start = 7 after six seed values, and vcat joins the seed in front of the run.

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

Without 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 stay in care for at least days with probability , and are confirmed at a daily hazard from the day after admission. Cohort 's weight on lag is the probability that it is in care and confirmed:

Survival is the product, against TimeVarying(h), which a cohort's lag reads on the day   it lands, from 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.

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

Few 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.

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