Latent processes driving the reproduction number
Introduction
This tutorial is for a user who wants the reproduction number, or transmission itself, to follow a random or feedback process. A latent process is a series that is not observed, built from random innovations. The tutorial writes latent processes as operators and uses them in a renewal process:
a random walk, an autoregression (AR) and a moving average (MA) from one set of innovations;
an AR(1) process with a coefficient that changes over time;
an AR(1) process as the logarithm of the reproduction number;
transmission that falls with current incidence, and with the incidence of the last week.
It assumes you have read A renewal process with susceptible depletion and a reporting delay.
Packages used
using ComposableRecurrences
using ComposableRecurrences: Transform, Depletion, Flow, Hazard, Truncate,
Derived, Recent
using CairoMakie, AlgebraOfGraphics, DataFramesMeta
using ForwardDiff
using Random
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...))
endThree processes from one set of innovations
A random walk, an AR(2) process and an MA(2) process are
where Recurrence with the coefficients as its kernel and the innovations as its add input. The MA(2) process is a Convolution of the innovations with the kernel add.
T = 100
ϵ = 0.1 .* randn(Xoshiro(1), T)
# The autoregression with coefficients `φ` driven by the innovations `ϵ`,
# from a history of zeros unless one is given.
function autoregression(φ, ϵ; history = zeros(length(φ)))
return Recurrence(φ)(; history, add = ϵ)
end
# The moving average of the innovations `ϵ` with coefficients `θ`.
moving_average(θ, ϵ) = Convolution(vcat(1.0, θ))(ϵ)
rw = autoregression([1.0], ϵ)
ar = autoregression([0.6, 0.3], ϵ)
ma = moving_average([0.8, 0.4], ϵ)
plot_series("Random walk" => rw, "AR(2)" => ar, "MA(2)" => ma; ylabel = "Value")The weight of an innovation on later values is 1 at every lag for the random walk and zero after two days for the MA(2) process. For the AR(2) process it decays, as the coefficients sum to 0.9.
A time-varying AR coefficient
An AR(1) process with a coefficient that changes over time is
where TimeVarying kernel with one column per day. With one lag it can also be the gain on a kernel of one weight of 1.
Why the gain form matches
The gain multiplies the lag sum before the add input is added. With a kernel of one weight of 1 the lag sum is
φ = range(0.95, 0.5; length = T)
φ_by_day = TimeVarying(reshape(collect(φ), 1, :))
by_kernel = autoregression(φ_by_day, ϵ; history = [0.0])
by_gain = Recurrence([1.0])(φ; history = [0.0], add = ϵ)
by_kernel ≈ by_gaintrueThe two forms agree to rounding error. The figure compares the process with one whose coefficient stays at 0.95.
plot_series(
"Fixed at 0.95" => autoregression([0.95], ϵ),
"Falling from 0.95 to 0.5" => by_kernel; ylabel = "Value"
)An AR(1) process as the log reproduction number
The reproduction number of a renewal process is the exponential of an AR(1) process:
where
gi = [0.1, 0.3, 0.3, 0.2, 0.1]
draws = [0.05 .* randn(Xoshiro(100 + i), T) for i in 1:20]
# The reproduction number and the infections of a renewal process whose
# log reproduction number is an AR(1) process with coefficient `φ` and
# innovations `ϵ`.
function renewal_from_ar1(gi, φ, ϵ, history)
R = exp.(autoregression([φ], ϵ))
return (; R, infections = Recurrence(gi)(R; history))
end
paths = map(enumerate(draws)) do (i, ϵ)
run = renewal_from_ar1(gi, 0.95, ϵ, fill(10.0, 5))
DataFrame(
"day" => 1:T, "draw" => i, "Reproduction number" => run.R,
"Infections" => run.infections
)
end
table = stack(
reduce(vcat, paths), Not([:day, :draw]); variable_name = :quantity
)
draw(
data(table) *
mapping(:day, :value, group = :draw => nonnumeric, layout = :quantity) *
visual(Lines, linewidth = 1, alpha = 0.5);
axis = (xlabel = "Day", ylabel = "Value"), facet = (; linkyaxes = :none)
)Across the twenty draws
Transmission that saturates with incidence
A saturating incidence, in which transmission falls as incidence rises [], keeps daily infections below a level
where Transform, and
R = fill(1.5, T)
saturate(v, κ) = v / (1 + v / κ)
# Infections of the renewal process with generation interval `gi` after
# `modifiers`, from 5 infections on the day before day 1.
function infections(gi, R, modifiers = ())
return Recurrence(gi; modifiers)(R; history = [5.0])
end
plot_series(
"κ = 100" => infections(gi, R, (Transform(saturate, 100.0),)),
"κ = 300" => infections(gi, R, (Transform(saturate, 300.0),)),
"No saturation" => infections(gi, R);
ylabel = "Infections (log scale)", yscale = log10
)The total infections over the 100 days are a differentiable function of
where
# Total infections at `x = [κ, R]`.
function total_saturating(x)
return sum(infections(gi, fill(x[2], T), (Transform(saturate, x[1]),)))
end
line_names = ["Total infections", "Tangent from the gradient"]
# The values of `f` along each of `grids`, one parameter varied at a time
# from `x0`, with the tangent that the gradient of `f` at `x0` gives.
function tangents(f, x0, names, grids)
grad = ForwardDiff.gradient(f, x0)
frames = map(eachindex(x0)) do i
xs = collect(grids[i])
curve = [f(setindex!(copy(x0), x, i)) for x in xs]
tangent = f(x0) .+ grad[i] .* (xs .- x0[i])
DataFrame(
parameter = names[i], value = vcat(xs, xs),
total = vcat(curve, tangent),
line = repeat(line_names; inner = length(xs))
)
end
return reduce(vcat, frames)
end
# A panel a parameter of the curves and tangents of `tangents`.
function plot_tangents(table)
layers = data(table) * visual(Lines, linewidth = 2) * mapping(
:value, :total; linestyle = :line => sorter(line_names) => "",
layout = :parameter
)
return draw(
layers; facet = (; linkxaxes = :none),
axis = (xlabel = "Parameter value", ylabel = "Total infections")
)
end
plot_tangents(
tangents(
total_saturating, [100.0, 1.5], ["κ", "R"],
[60:5:140, 1.3:0.02:1.7]
)
)Each tangent meets its curve at
Feedback from recent incidence
The saturating form depends on the incidence of the current day only. In the model of this section transmission falls with the infections of the last seven days, and ring vaccination removes doses from the susceptible pool in proportion to the same sum:
where Transform. The susceptible pool after day
Recent(7) is a parameter that sums the last seven outputs of the recurrence, and Derived maps a parameter through a function. Any parameter of a modifier takes either.
N = 5_000.0
λ = 10.0
# The response of strength `λ` to the infections of the last week in a
# population `N`.
response(λ, N) = Transform(*, Derived(exp, -λ / N * Recent(7)))
# A depletion of `N` with ring vaccination of `share` of the infections
# of the last week.
function ring_vaccination(N, share)
doses = Flow(:S => 0, share * Recent(7); form = Truncate())
return Depletion(N, Hazard(), doses)
end
ring = ring_vaccination(N, 0.5)
plot_series(
"Depletion only" => infections(gi, R, (Depletion(N),)),
"Response to the last week" =>
infections(gi, R, (response(λ, N), Depletion(N))),
"Ring vaccination" => infections(gi, R, (ring,)),
"Both" => infections(gi, R, (response(λ, N), ring));
ylabel = "Infections"
)With depletion alone infections peak at 111 on day 37, and the response lowers the peak to 23, ring vaccination to 36 and both together to 16.
ForwardDiff gives the gradient of the total infections
# Total infections at `x = [λ, R]`.
function total_with_response(x)
modifiers = (response(x[1], N), Depletion(N))
return sum(infections(gi, fill(x[2], T), modifiers))
end
plot_tangents(
tangents(
total_with_response, [λ, 1.5], ["λ", "R"], [0:1:20, 1.3:0.02:1.7]
)
)The derivative of the total is negative in
Reverse-mode gradients through Recent
The rule of the recurrence propagates cotangents through the outputs that Recent reads, so reverse-mode gradients include the feedback.
Performance
The timed model is an AR(2) process over 200 days. The figure gives its minimum time and that of a preallocated hand-written loop of the same model.
performance_plot("latent processes")References
- V. Capasso and G. Serio. A generalization of the Kermack-McKendrick deterministic epidemic model. Mathematical Biosciences 42, 43–61 (1978).
Learning more
Time-series processes lists each process with its operator.
The reproduction number from a run computes the reproduction number back from the infections.