Skip to content

Reporting delays and generation intervals that change over time ​

Introduction ​

A TimeVarying kernel has one column of weights per day. This tutorial uses it for:

  • a reporting delay that changes over time, indexed by report day and by infection day;

  • a generation interval that shortens on a set day;

  • isolation that starts on a set day, as a cohort effect and as a period effect;

  • a kernel computed from a hazard that changes over time.

Packages used ​

julia
using ComposableRecurrences
using ComposableRecurrences: Primary
using CairoMakie, AlgebraOfGraphics, DataFramesMeta

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

# Lines of the named series against day, one colour a series, with `kwargs`
# passed to the axis.
function plot_series(series::Pair...; ylabel, kwargs...)
    table = stack(
        DataFrame("day" => eachindex(last(first(series))), series...),
        Not(:day); variable_name = :series
    )
    layers = data(table) * mapping(:day, :value, color = :series => "") *
        visual(Lines, linewidth = 2)
    return draw(layers; axis = (; xlabel = "Day", ylabel, kwargs...))
end

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 , means in the code, rises to day 40 and falls after it. The figure gives the mean delay of each column,  .

julia
T, L_d = 80, 15

# The weights on lags 0 to `n - 1` of a delay with shape parameter `μ`.
function delay_pmf(μ, n)
    w = [(l + 1)^(μ - 1) * exp(-l) for l in 0:(n - 1)]
    return w ./ sum(w)
end

means = [2 + 4 * exp(-((t - 40) / 15)^2) for t in 1:T]
P = reduce(hcat, delay_pmf.(means, L_d))
mean_delay = vec(sum((0:(L_d - 1)) .* P; dims = 1))

plot_series("Mean delay" => mean_delay; ylabel = "Mean delay (days)")

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 the reference.

julia
infections = [100 * exp(-((t - 35) / 12)^2) for t in 1:T]
reports = [
    "Infections" => infections,
    "Constant delay" => Convolution(P[:, 1])(infections),
    "By report day" => Convolution(TimeVarying(P))(infections),
    "By infection day" => Convolution(TimeVarying(P, Primary()))(infections),
]
plot_series(reports...; ylabel = "Count")

Infections peak at 100 on day 35. Indexed by report day, reports peak at 96 on day 40. Indexed by infection day, they peak at 85 on day 41.

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. Indexed by infection day, the delay of each infection sums to one, so each infection is reported once. Indexed by report day, the weights of each report day sum to one. The weights that one infection receives then come from several columns, and their sum   differs from one when the delay changes.

julia
cumulative = [name => cumsum(count) for (name, count) in reports]
plot_series(cumulative...; ylabel = "Cumulative count")

By day 80 the cumulative infections are 2127. Indexed by infection day the cumulative reports are also 2127. Indexed by report day they are 2165, which is 1.8% more.

A generation interval that shortens ​

The kernel of a Recurrence can change over time in the same way, with 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. The reproduction number is   throughout, so only the timing of transmission changes. The kernel changes once, so it is given by its change points, each a 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]
seed = fill(10.0, 6)
G = TimeVarying([1 => long_gi, 30 => short_gi])
fixed = Recurrence(long_gi)(1.2; history = seed, stop = T)
shortened = Recurrence(G)(1.2; history = seed, stop = T)

plot_series(
    "Fixed interval" => fixed, "Interval shortens on day 30" => shortened;
    ylabel = "Infections"
)

On day 80 there are 4310 infections with the interval that shortens and 586 with the fixed interval.

An isolation policy that starts on a set day ​

A kernel indexed by infection day gives each case the generation interval of the day on which it was infected, a cohort effect. The default indexing, by output day, is a period effect: every case that transmits on a day has the interval of that day.

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 period kernel applies the same thinning to every case from day :

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, as a Primary() kernel has a column only for days from 1.

julia
p_iso, b_iso, t0 = 0.7, 0.9, 30
delay_iso = [0.2, 0.3, 0.3, 0.2]

# The probability of isolation after a delay of `from` to `l` days, for a
# delay with probabilities `delay` on 0 days and up.
function isolated_by(delay, l, from)
    days = max(from, 0):min(l, length(delay) - 1)
    return sum(delay[d + 1] for d in days; init = 0.0)
end

thinning = p_iso * b_iso
cohort = [
    long_gi[l] * (1 - thinning * isolated_by(delay_iso, l, t0 - τ))
        for l in 1:6, τ in 1:T
]
period = [
    long_gi[l] * (1 - (t >= t0) * thinning * isolated_by(delay_iso, l, 0))
        for l in 1:6, t in 1:T
]

# The seed on days 1 to `length(seed)` followed by the run of the
# recurrence with `kernel`.
function seeded_run(kernel, R, seed)
    run = Recurrence(kernel)(R; history = seed, start = length(seed) + 1)
    return vcat(seed, run)
end

R_iso = fill(1.8, T)
plot_series(
    "No isolation" => seeded_run(long_gi, R_iso, seed),
    "By infection day" =>
        seeded_run(TimeVarying(cohort, Primary()), R_iso, seed),
    "By calendar day" => seeded_run(TimeVarying(period), R_iso, seed);
    ylabel = "Infections (log scale)", yscale = log10
)

Indexed by calendar day, the kernel of every case that transmits on day 30 is thinned on that day, and infections fall from 589 on day 29 to 291 on day 30. Indexed by infection day, a case infected shortly before day 30 keeps the transmission of the days on which its isolation would precede the policy. Infections then peak at 642 on day 31, and are 1.86 times those of the calendar-day model afterwards.

A kernel from a hazard and a survival ​

A cohort kernel can be the product of a profile over the lags and the survival of the cohort against a hazard over calendar days. 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 inside its call and its reverse pass, so the differentiated code does not build the kernel entry by entry.

julia
using ComposableRecurrences: Survival

# The convolution that gives the patients in care and confirmed, from the
# survival `stay` in care and the daily hazard `testing` of confirmation.
function confirmed_in_care(stay, testing)
    untested = Survival(TimeVarying(testing); from = 1)
    return Convolution(TimeVarying(stay * (1 - untested), Primary()))
end

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]
admissions = [20 * exp(-((t - 40) / 15)^2) for t in 1:T]
in_care = Convolution(stay)(admissions)
confirmed = confirmed_in_care(stay, testing)(admissions)

plot_series(
    "In care" => in_care, "Confirmed in care" => confirmed;
    ylabel = "Patients"
)

The daily hazard of confirmation rises from 0.05 to 0.30, with its midpoint on day 35. On day 20, 12% of the patients in care are confirmed, and on day 60, 78%.

Performance ​

The timed model is a delay of 15 lags indexed by infection day over 200 days. The figure gives its minimum time and that of a preallocated hand-written loop of the same model.

julia
performance_plot("time-varying delays")

Learning more ​

  • Wrappers states the two indexings of a TimeVarying kernel.

  • Nowcasting builds a delay from a hazard with an effect of the reporting day.