Branching processes
Introduction
A branching process follows the offspring of each case, and its expectations, such as the mean cases by type or the probability of extinction, are recurrences. The operators compute these expectations; they do not simulate chains.
Packages used
using ComposableRecurrences
using ComposableRecurrences: Transform
using CairoMakie, AlgebraOfGraphics, DataFramesMeta
using ForwardDiff
using LinearAlgebra
CairoMakie.activate!(type = "png", px_per_unit = 2)
gi = [0.1, 0.3, 0.3, 0.2, 0.1]A multi-type process
With a unit kernel each step is one generation:
where
K = [1.2 0.4; 0.3 0.6]
by_generation = Recurrence([1.0]; coupling = K)(1.0; history = [1.0; 0.0;;], stop = 30)
growth = norm(by_generation[:, end]) / norm(by_generation[:, end - 1])
radius = maximum(abs, eigvals(K))
@chain DataFrame(
"generation" => 0:30, "Type 1" => [1.0; by_generation[1, :]], "Type 2" => [0.0; by_generation[2, :]]
) begin
stack(Not(:generation); variable_name = :type, value_name = :cases)
@subset(:cases .> 0)
data(_) * mapping(:generation, :cases, color = :type) * visual(Lines, linewidth = 2)
draw(_; axis = (xlabel = "Generation", ylabel = "Mean cases (log scale)", yscale = log10))
endAfter a few generations both types grow at the same rate, in a fixed ratio. By generation 30 the growth per generation is 1.3583, the spectral radius of
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
p, b = 0.8, 0.9
F_D = [0.1, 0.4, 0.7, 0.9, 1.0]
gi_isolated = gi .* (1 .- p * b .* F_D)
R0 = 1.6
R_iso = R0 * sum(gi_isolated)
@chain DataFrame("lag" => eachindex(gi), "No isolation" => gi, "Isolation" => gi_isolated) begin
stack(Not(:lag); variable_name = :kernel, value_name = :weight)
data(_) * mapping(:lag, :weight, color = :kernel, dodge = :kernel) * visual(BarPlot)
draw(_; axis = (xlabel = "Lag (days)", ylabel = "Generation interval weight"))
endIsolation thins the later lags most. It brings the reproduction number from 1.6 to 0.886, below one. The mean offspring with and without isolation match seeded branching-process simulations from EpiBranch.jl; the isolation use case checks them.
Isolation delay by type
The delay to isolation can depend on a case's state. Once a household's first case is found, later cases in it are found sooner. In expectation each infector type gets its own thinned kernel, a PerStratum kernel. Type 1 is a household's first case and type 2 a later case. The next-generation matrix scales each column of the coupling by its type's kernel sum. 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 gi .* (1 .- p * b .* F_fast)])
K_house = [0.6 0.3; 0.6 0.5]
house_model = Recurrence(PerStratum(W_house); coupling = K_house)
house = house_model(R0; history = [10.0; 0.0;;], stop = 60)
ngm_house = R0 * K_house * Diagonal(vec(sum(W_house; dims = 2)))
R_house = maximum(abs, eigvals(ngm_house))
@chain DataFrame(
"day" => 1:60, "First case" => house[1, :], "Later case" => house[2, :]
) begin
stack(Not(:day); variable_name = :type, value_name = :cases)
data(_) * mapping(:day, :cases, color = :type) * visual(Lines, linewidth = 2)
draw(_; axis = (xlabel = "Day", ylabel = "Mean cases (log scale)", yscale = log10))
endThe reproduction number, the spectral radius of
Contact tracing as two types
Traced and untraced cases are two types. A share Pairwise kernel gives each pair of types its interval. Rows are the infectee type and columns the 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
function traced_outbreak(ψ)
A_trace = zeros(2, 2, length(gi))
for (col, g) in enumerate([gi, gi_isolated])
A_trace[1, col, :] = (1 - ψ) .* g
A_trace[2, col, :] = ψ .* g
end
return Recurrence(Pairwise(A_trace))(R0; history = [10.0; 0.0;;], stop = 60)
end
@chain [0.0, 0.5, 0.9] begin
map(ψ -> DataFrame(day = 1:60, ψ = string(ψ), cases = vec(sum(traced_outbreak(ψ); dims = 1))), _)
reduce(vcat, _)
data(_) * mapping(:day, :cases, color = :ψ => "Traced share") * visual(Lines, linewidth = 2)
draw(_; axis = (xlabel = "Day", ylabel = "Cases (log scale)", yscale = log10))
endOn day 60 there are 90 252 cases with no tracing, 590 with half traced and 2.3 with 90% traced. With 90% traced the outbreak declines.
Extinction by generation
A branching process from one case dies out by generation
Here [1.0] and no gain, so the value entering the modifiers is Transform that applies
pullback! method in the Local role. With
A gradient's reverse pass uses these. Without them it would evaluate
struct OffspringPGF end
(::OffspringPGF)(s, θ) = (1 + θ.R / θ.κ * (1 - s))^(-θ.κ)
function ComposableRecurrences.pullback!(
grads, ::OffspringPGF, ::ComposableRecurrences.Local, y, s, θ
)
u = 1 + θ.R / θ.κ * (1 - s)
∂R = -(1 - s) * y / u
∂κ = y * (θ.R * (1 - s) / (θ.κ * u) - log(u))
return θ.R * y / u, (; R = ∂R, κ = ∂κ)
end
function extinction(θ, n)
m = Transform(OffspringPGF(), θ)
return Recurrence([1.0]; modifiers = (m,))(; history = [0.0], stop = n)
end
generations = 30
@chain [0.1, 0.5, 1.0, 10.0] begin
map(_) do κ
q = extinction((; R = 1.5, κ), generations)
DataFrame(generation = 1:generations, κ = "κ = $κ", q = q)
end
reduce(vcat, _)
data(_) * mapping(:generation, :q, color = :κ) * visual(Lines, linewidth = 2)
draw(_; axis = (xlabel = "Generation", ylabel = "Probability of extinction"))
endSmaller
The derivative of
dq30_dκ = ForwardDiff.derivative(κ -> last(extinction((; R = 1.5, κ), generations)), 0.5)At
Performance
Each bar times extinction by generation for five mixing types with a 20-lag generation interval over 200 steps, against a preallocated hand-written loop.
performance_plot("transform")References
- Lloyd-Smith, Schreiber, Kopp and Getz (2005), "Superspreading and the effect of individual variation on disease emergence", Nature 438, 355-359, 10.1038/nature04153
Learning more
API overview: a map of every operator, coupling and modifier.
Public API: every public name with its docstring.