Skip to content

Latent processes driving the reproduction number ​

Introduction ​

A latent process is a series that is not observed, built from random innovations. This 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.

Packages used ​

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

Three processes from one set of innovations ​

A random walk, an AR(2) process and an MA(2) process are

where is the value of each process on day and the innovation on that day, a normal draw with standard deviation 0.1. Here the AR coefficients are   and  , and the MA coefficients   and  . The random walk and the AR(2) process are each a 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 . With no gain the call runs over the length of add.

julia
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 is the AR coefficient on day and the innovations above. Here falls linearly from 0.95 on day 1 to 0.5 on day 100. The coefficient is a TimeVarying kernel with one column per day. With one lag it is also the gain on a kernel of one weight of 1, as the gain multiplies the lag sum before the add input is added.

julia
φ = 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_gain
true

The two forms agree to rounding error. The figure compares the process with one whose coefficient stays at 0.95.

julia
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 is the log reproduction number, an AR(1) process, the reproduction number on day and the number of infections on day . The reproduction number is the gain of the renewal, the innovations are new draws with standard deviation 0.05, and is the generation interval on lag . Here   and there are   lags. The history is 10 infections on each of the five days before day 1. An AR(1) process with   is stationary with mean zero, so reverts to 0. Twenty draws of the innovations give twenty paths of and of the infections.

julia
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 ranges from 0.68 to 1.69, and the infections on day 100 from 0.15 to 4682.

Transmission that saturates with incidence ​

A saturating incidence, in which transmission falls as incidence rises [], keeps daily infections below a level :

where is the value before the modifiers on day , the only modifier, a Transform, and the level daily infections stay below. The generation interval and the infections are as in the previous model. Here the gain is constant,   . The history is 5 infections on the day before day 1. With   infections tend to  , which is 33.3 for   and 100 for  .

julia
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 and :

where   is the number of days. ForwardDiff gives the gradient of at   and  . Each panel varies one parameter and holds the other at that point. The dashed line in each panel is the tangent the gradient gives.

julia
# 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   and   with the slope of the curve.

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 is the number of new infections before depletion and that number after the response , a Transform. The susceptible pool after day is , and the ring vaccination doses on day , half the last week's infections, are . The depletion is the second modifier , which draws from the pool by the hazard form. Here  , the response strength is   and the pool starts at  . The models without a response take  , and those without ring vaccination take  .

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.

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

The rule of the recurrence propagates cotangents through the outputs that Recent reads, so reverse-mode gradients include the feedback. ForwardDiff gives the gradient of the total infections of the response-and-depletion model at   and  .

julia
# 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 and positive in at this point.

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.

julia
performance_plot("latent processes")

References ​

  1. V. Capasso and G. Serio. A generalization of the Kermack-McKendrick deterministic epidemic model. Mathematical Biosciences 42, 43–61 (1978).

Learning more ​