Expected cases and the probability of extinction of a branching process
Introduction
A branching process follows the offspring of each case. Its expectations are recurrences, and this tutorial computes four of them without simulating chains:
the mean cases of each type by generation, and the reproduction number as a spectral radius;
the reproduction number under isolation, with a delay to isolation that is shared or set by type;
the mean cases under contact tracing, with traced and untraced cases as two types;
the probability of extinction by generation, with its derivative.
Packages used
using ComposableRecurrences
using CairoMakie, AlgebraOfGraphics, DataFramesMeta
using ForwardDiff
using LinearAlgebra
CairoMakie.activate!(type = "png", px_per_unit = 2)
# Lines of the named series against `x`, one colour a series, on a log
# scale, leaving out values that are not positive.
function plot_mean_cases(x, series::Pair...; xlabel, legend = "")
table = stack(
DataFrame(xlabel => collect(x), series...), Not(xlabel);
variable_name = :series
)
table = table[table.value .> 0, :]
layers = data(table) * mapping(xlabel, :value, color = :series => legend) *
visual(Lines, linewidth = 2)
return draw(
layers; axis = (ylabel = "Mean cases (log scale)", yscale = log10)
)
end
gi = [0.1, 0.3, 0.3, 0.2, 0.1]A multi-type process
With a kernel of one weight of 1 on lag 1, each step of a Recurrence is one generation:
where
K = [1.2 0.4; 0.3 0.6]
first_case = [1.0; 0.0;;]
branching = Recurrence([1.0]; coupling = K)
by_generation = branching(1.0; history = first_case, stop = 30)
growth = norm(by_generation[:, end]) / norm(by_generation[:, end - 1])
radius = maximum(abs, eigvals(K))
round.((growth, radius); digits = 4)(1.3583, 1.3583)In generation 30 the growth per generation equals the spectral radius of
plot_mean_cases(
0:30, "Type 1" => [1.0; by_generation[1, :]],
"Type 2" => [0.0; by_generation[2, :]]; xlabel = "Generation"
)Isolation as a thinned kernel
Isolating a share
where gi in the code, and F_D in the code. The thinned kernel gi_isolated. Here
# The generation interval `gi` thinned by isolating a share `p` of cases,
# which blocks a share `b` of transmission after isolation, where `F` is
# the probability of isolation by each lag.
thinned(gi, p, b, F) = gi .* (1 .- p * b .* F)
p, b = 0.8, 0.9
F_D = [0.1, 0.4, 0.7, 0.9, 1.0]
gi_isolated = thinned(gi, p, b, F_D)
R0 = 1.6
R_iso = R0 * sum(gi_isolated)
kernels = stack(
DataFrame(
"lag" => eachindex(gi), "No isolation" => gi,
"Isolation" => gi_isolated
),
Not(:lag); variable_name = :kernel
)
draw(
data(kernels) *
mapping(:lag, :value, color = :kernel => "", dodge = :kernel) *
visual(BarPlot);
axis = (xlabel = "Lag (days)", ylabel = "Generation interval weight")
)The reproduction number falls from 1.6 to 0.886. The weight on lag 5 falls by 72% and that on lag 1 by 7%. The mean offspring with and without isolation agree with branching-process simulations from EpiBranch.jl, which the isolation use case of the tests checks.
Isolation delay by type
In this model the delay to isolation depends on the type of the case. Type 1 is the first case of a household and type 2 a later case, which is isolated sooner. Each infector type has its own thinned kernel, the rows of a PerStratum kernel. The next-generation matrix scales each column of the coupling by the sum of the kernel of its type, and its spectral radius is the reproduction number:
Here K_house in the code. The weight W_house. The next-generation matrix ngm_house.
F_fast = [0.3, 0.7, 0.9, 1.0, 1.0]
W_house = permutedims([gi_isolated thinned(gi, p, b, F_fast)])
K_house = [0.6 0.3; 0.6 0.5]
seed = [10.0; 0.0;;]
house_model = Recurrence(PerStratum(W_house); coupling = K_house)
house = house_model(R0; history = seed, stop = 60)
ngm_house = R0 * K_house * Diagonal(vec(sum(W_house; dims = 2)))
R_house = maximum(abs, eigvals(ngm_house))
plot_mean_cases(
1:60, "First case" => house[1, :], "Later case" => house[2, :];
xlabel = "Day"
)The reproduction number, the spectral radius of
Contact tracing as two types
Traced and untraced cases are two types. A share Pairwise kernel holds the interval of each pair of types, with a row for each infectee type and a column for each infector type:
where type 1 is untraced and type 2 traced. The kernel Pairwise weight on lag gi and the thinned one gi_isolated. The gain is
# Mean cases by day, summed over types, when a share `ψ` of contacts is
# traced: untraced cases transmit by `g_untraced` and traced cases by
# `g_traced`.
function traced_cases(ψ, g_untraced, g_traced, R, seed; stop)
A = zeros(2, 2, length(g_untraced))
for (infector, g) in enumerate([g_untraced, g_traced])
A[1, infector, :] = (1 - ψ) .* g
A[2, infector, :] = ψ .* g
end
cases = Recurrence(Pairwise(A))(R; history = seed, stop)
return vec(sum(cases; dims = 1))
end
traced = [
string(ψ) => traced_cases(ψ, gi, gi_isolated, R0, seed; stop = 60)
for ψ in [0.0, 0.5, 0.9]
]
plot_mean_cases(1:60, traced...; xlabel = "Day", legend = "Traced share")On day 60 the mean number of cases is 90 252 with no tracing, 590 with half of contacts traced and 2.3 with 90% traced.
Extinction by generation
A branching process that starts from one case is extinct by generation
where [1.0] and no gain, so the value that enters the modifiers is
The modifier is written as a pure step, so it can carry its partial derivatives in step_pullback. With
The reverse pass of a gradient evaluates these closed forms in place of step_pullback computes q, and step_pullback returns the cotangents of q, the state and the parameters, each a partial derivative times the cotangent
struct OffspringPGF{R, K}
R::R
κ::K
end
ComposableRecurrences.isstateless(::OffspringPGF) = true
function ComposableRecurrences.step(::OffspringPGF, θ, q, s)
return (1 + θ.R / θ.κ * (1 - q))^(-θ.κ), s
end
function ComposableRecurrences.step_pullback(::OffspringPGF, θ, q, s, ȳ, s̄)
u = 1 + θ.R / θ.κ * (1 - q)
y = u^(-θ.κ)
∂R = -(1 - q) * y / u
∂κ = y * (θ.R * (1 - q) / (θ.κ * u) - log(u))
return θ.R * y / u * ȳ, s̄, (; R = ∂R * ȳ, κ = ∂κ * ȳ)
end
# The probability of extinction by each of generations 1 to `n`, for
# offspring parameters `θ = (; R, κ)`.
function extinction(θ, n)
m = OffspringPGF(θ.R, θ.κ)
return Recurrence([1.0]; modifiers = (m,))(; history = [0.0], stop = n)
end
n_generations = 30
by_dispersion = stack(
DataFrame(
"generation" => 1:n_generations,
(
"κ = $κ" => extinction((; R = 1.5, κ), n_generations)
for κ in [0.1, 0.5, 1.0, 10.0]
)...
),
Not(:generation); variable_name = :dispersion
)
draw(
data(by_dispersion) *
mapping(:generation, :value, color = :dispersion => "") *
visual(Lines, linewidth = 2);
axis = (xlabel = "Generation", ylabel = "Probability of extinction")
)By generation 30 the probability of extinction is 0.93 with
q30(κ) = last(extinction((; R = 1.5, κ), n_generations))
dq30_dκ = ForwardDiff.derivative(q30, 0.5)
round(dq30_dκ; digits = 2)-0.28The derivative at
Performance
The timed model is extinction by generation for five types that mix, with a generation interval of 20 lags, over 200 steps. The figure gives its minimum time and that of a preallocated hand-written loop of the same model.
performance_plot("transform")References
- J. O. Lloyd-Smith, S. J. Schreiber, P. E. Kopp and W. M. Getz. Superspreading and the effect of individual variation on disease emergence. Nature 438, 355–359 (2005).
Learning more
Closed-form derivatives of maps describes the
step_pullbackthatOffspringPGFuses.Time-varying delays and kernels models isolation that starts on a set day.