The reproduction number from a run
Introduction
The reproduction number on each day can be read back from the infections a renewal run returns. A renewal's infections on a day are the reproduction number times the pressure, a weighted sum of earlier infections. A call with record = (:pressure,) returns the pressure of each day with the infections, so dividing the infections by it gives the reproduction number.
Packages used
using ComposableRecurrences
using ComposableRecurrences: Depletion
using CairoMakie, AlgebraOfGraphics, DataFramesMeta
CairoMakie.activate!(type = "png", px_per_unit = 2)The instantaneous reproduction number
The one-town renewal of Three towns is
where Recurrence. It is 1.8 up to day 50 and falls in equal steps to 0.8 on day 56. The run starts from ten infections on each of the six days before day 1. The call's trace holds trace.pressure, computed as each step computes it, so the first history. The ratio
gi = [0.05, 0.2, 0.3, 0.25, 0.12, 0.08]
T = 75
R = [1.8 - clamp((t - 50) / 6, 0, 1) for t in 1:T]
seed = fill(10.0, 6)
renewal = Recurrence(gi)
infections, trace = renewal(R; history = seed, record = (:pressure,))
R_hat = infections ./ trace.pressure
@chain DataFrame(day = 1:T, value = R, series = "Gain") begin
data(_) * mapping(:day, :value => "Reproduction number", color = :series => "") *
visual(Lines, linewidth = 2)
_ + data(DataFrame(day = 1:T, value = R_hat, series = "Infections / pressure")) *
mapping(:day, :value => "Reproduction number", color = :series => "") *
visual(Scatter, markersize = 6)
draw(_; axis = (xlabel = "Day",))
endThe ratio matches NaN with no infections and Inf otherwise. With strata, the pressure includes the coupling, so the ratio works on each stratum. For infections that did not come from a traced call, pressure recomputes the pressure from them and the same history.
The effective reproduction number
A Depletion of
where
With record = (:trace,) the call's trace also holds each modifier's Trace. For a Depletion its first row is the pool after each day, which gives
N = 60_000.0
depleted = Recurrence(gi; modifiers = (Depletion(N),))
y, tr = depleted(R; history = seed, record = (:trace,))
q = tr.pressure
R_eff = y ./ q
pool = [N; only(tr.modifiers)[1, 1:(end - 1)]]
λ = R .* q ./ N
@assert R_eff ≈ R .* pool ./ N .* -expm1.(-λ) ./ λ
@chain DataFrame(day = 1:T, Gain = R, Effective = R_eff) begin
stack(Not(:day); variable_name = :series, value_name = :R)
data(_) * mapping(:day, :R => "Reproduction number", color = :series => "") *
visual(Lines, linewidth = 2)
draw(_; axis = (xlabel = "Day",))
endThe ratio counts all of Transform(+, ι) are such values, so with them add input enters before the depletion and is drawn with the rest, so the ratio then includes its share.
Performance
Each bar times the effective reproduction number of the three towns, with depletion, over 200 days with a 20-day generation interval, against a preallocated hand-written loop.
performance_plot("reproduction number")Learning more
Vaccination: the renewal with a protected pool of the vaccinated.
Imported cases: the renewal with imports before and after depletion.
Clinical outcomes: onsets, admissions and deaths as a chain of delays.
Spatial models: the couplings the pressure mixes.