Skip to content

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 ​

julia
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 holds the mean cases of each type in generation . The coupling is the mean offspring matrix. Its entry is the mean number of type cases that one type case causes. The kernel is   and the gain is  . Growth per generation converges to the spectral radius of , the reproduction number.

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

After 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 to four decimal places. The ratio of type 1 to type 2 cases is then 2.53.

Isolation as a thinned kernel ​

Isolating a share of cases blocks a share of their later transmission. The mean offspring at lag and the reproduction number are then

where is the generation interval on lag , gi in the code, and the reproduction number without isolation. The probability that a case is isolated by lag is , F_D in the code. The thinned kernel     is gi_isolated. Here  ,   and  . This assumes the generation interval is independent of the time to isolation.

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

Isolation 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 is the mean number of type cases on day and   the gain. The coupling is the mean offspring matrix, K_house in the code. The weight is type 's thinned kernel on lag , row of W_house. The next-generation matrix is ngm_house.

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

The reproduction number, the spectral radius of , is 0.773. From 10 seed cases the mean number of cases of both types falls towards zero, to 0.014 in total on day 60.

Contact tracing as two types ​

Traced and untraced cases are two types. A share of each case's contacts is traced, and traced cases use the thinned kernel. A 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 is the Pairwise weight on lag from infector type to infectee type . The full kernel is gi and the thinned one is gi_isolated. The gain is  . This is an approximation: tracing correlates an infector's and infectee's isolation times.

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

On 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 with probability . Offspring are negative binomial (Lloyd-Smith et al. 2005), with mean and dispersion :

Here is the offspring probability generating function. The recurrence has the unit kernel [1.0] and no gain, so the value entering the modifiers is . Its one modifier is a Transform that applies with parameters  . Here  , and the history is  .

is written as a callable struct, so it can carry its partial derivatives in and in as a local rule: a pullback! method in the Local role. With    , they are

A gradient's reverse pass uses these. Without them it would evaluate on dual numbers at every step, which costs more than the closed form. Each partial derivative contains itself, so the rule takes the step's output   as an argument. The reverse pass passes the value from the forward pass instead of recomputing the power.

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

Smaller concentrates offspring in fewer cases, so chains die out more often. By generation 30 the probability of extinction is 0.93 with   and 0.46 with  .

The derivative of with respect to comes from the same call, with ForwardDiff:

julia
dq30_dκ = ForwardDiff.derivative(κ -> last(extinction((; R = 1.5, κ), generations)), 0.5)

At   it is -0.28, so a larger dispersion parameter lowers the probability of extinction.

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.

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