Reporting delays and generation intervals that change over time
Introduction
This tutorial is for a user whose delays or generation intervals change over the course of an outbreak. A TimeVarying kernel has one column of weights per day. The tutorial uses it for the following.
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.
It assumes you have read Symptom onsets, hospital admissions and deaths as a chain of delays.
Packages used
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...))
endA delay that changes over time
Each column of P is a discretised delay, lag 0 first. Column
on lag means in the code, rises to day 40 and falls after it. The figure gives the mean delay of each column,
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 P. The two indexings give
where Secondary(), column Primary(), column
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
Indexed by infection day, each infection is reported once. Indexed by report day, the weights that one infection receives come from several columns and need not sum to one.
The sums of reports under each indexing
Summing the reports over all days, while the window is long enough to hold every delay, gives
where
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 long_gi and short_gi below, with means of 3.7 and 2.1 days. The reproduction number is
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
where long_gi, and delay_iso. The period kernel applies the same thinning to every case from day
where
How the two kernels are laid out
With Primary() column start = 7 after six seed values, as a Primary() kernel has a column only for days from 1.
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
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.
Why the kernel is not built entry by entry
The operator computes the weights inside its call and its reverse pass, so the differentiated code does not build the kernel entry by entry.
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.
performance_plot("time-varying delays")Learning more
Wrappers states the two indexings of a
TimeVaryingkernel.Nowcasting builds a delay from a hazard with an effect of the reporting day.