Skip to content

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 ​

julia
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 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 gain is 1. The growth per generation tends to the spectral radius of , which is the reproduction number. The history is one case of type 1 in generation 0.

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

julia
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 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  . The model assumes that the generation interval is independent of the time to isolation.

julia
# 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 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 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 , is 0.773. From 10 cases of type 1 on the day before day 1, the mean number of cases of both types is 0.014 in total on day 60.

Contact tracing as two types ​

Traced and untraced cases are two types. A share of the contacts of each case is traced, and traced cases transmit by the thinned kernel. A 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 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  . The model is an approximation, as tracing correlates the isolation times of an infector and an infectee, which two types do not represent.

julia
# 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 with probability . With a negative binomial offspring distribution of mean and dispersion [],

where is the probability generating function of the offspring distribution. The recurrence has the kernel [1.0] and no gain, so the value that enters the modifiers is . Its one modifier applies with parameters and . Here  , and the history is  .

The modifier is written as a pure step, so it can carry its partial derivatives in , and as a step_pullback. With    , they are

The reverse pass of a gradient evaluates these closed forms in place of on dual numbers. Each partial derivative contains , so the step_pullback computes once and reuses it. The step receives as q, and step_pullback returns the cotangents of q, the state and the parameters, each a partial derivative times the cotangent of the output.

julia
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   and 0.46 with  . ForwardDiff gives the derivative of with respect to from the same function:

julia
q30(κ) = last(extinction((; R = 1.5, κ), n_generations))
dq30_dκ = ForwardDiff.derivative(q30, 0.5)
round(dq30_dκ; digits = 2)
-0.28

The derivative at   is negative: the probability of extinction falls as rises.

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.

julia
performance_plot("transform")

References ​

  1. 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 ​