Skip to content

Renewal models of places that mix: couplings, movement, shared totals and routes ​

Introduction ​

This tutorial is for a user modelling places that mix, such as towns or regions. It builds renewal models of three patches, each a stratum with its own susceptible pool. The patches mix in the following ways:

  • a fixed coupling, a generation interval per pair of patches and a coupling that changes over time;

  • infections moved between patches after they occur;

  • districts that share the total of their patch, fixed by another model;

  • two transmission routes, each with its own coupling and kernel.

It assumes you have read Three towns, step by step, whose gravity coupling it reuses.

Packages used ​

julia
using ComposableRecurrences
using ComposableRecurrences: Depletion, Redistribute, Allocate, Trace
using CairoMakie, AlgebraOfGraphics, DataFramesMeta
using LinearAlgebra
using SparseArrays

CairoMakie.activate!(type = "png", px_per_unit = 2)

# The rows of `x` as named series in a long table, one row per day, with
# the column `label` set to `value` when one is given.
function long(x, names; label = nothing)
    table = DataFrame(permutedims(x), names)
    table.day = axes(x, 2)
    table = stack(table, Not(:day); variable_name = :patch)
    label === nothing || (table[!, first(label)] .= last(label))
    return table
end

# Lines of `:value` against `:day`, a panel a patch and a colour a value
# of `colour`.
function lines_by_patch(table, colour; ylabel)
    layers = data(table) *
        mapping(:day, :value, color = colour => "", layout = :patch) *
        visual(Lines, linewidth = 2)
    return draw(layers; axis = (; xlabel = "Day", ylabel))
end

A gravity coupling ​

The coupling is the gravity coupling of Three towns, step by step, with the towns as patches and 5% of the weight off the diagonal in place of 0.2%:

where weights patch 's infections in patch and is the gravity weight, so each row of sums to one.

The other symbols

The term is one when   and zero otherwise. The population of patch is , and the distance between patches and is .

julia
patches = ["A", "B", "C"]
P, T = 3, 120
pop = [60_000.0, 25_000.0, 10_000.0]
dist = [0.0 20.0 45.0; 20.0 0.0 30.0; 45.0 30.0 0.0]
gravity = [a == b ? 0.0 : pop[b] / dist[a, b]^2 for a in 1:P, b in 1:P]
K = 0.95 * I(P) + 0.05 * gravity ./ sum(gravity; dims = 2)

@chain DataFrame(K, patches) begin
    @transform(:to = patches)
    stack(Not(:to); variable_name = :from, value_name = :weight)
    data(_) * mapping(:from, :to, :weight => "Weight") * visual(Heatmap)
    draw(_; axis = (xlabel = "From patch", ylabel = "To patch"))
end

The largest weight off the diagonal is that of A in B, and the weights of C in A and in B are the smallest.

Three ways to mix ​

The three models differ in the pressure on patch , and share the draw from each patch's pool.

where is the number of infections in patch on day , the generation interval's weight on lag and patch 's susceptible pool. Each pool starts at its patch's population . The pairwise kernel is within a patch and between patches, with a longer interval, gi_between below. The coupling is with its off-diagonal entries cut by 80% from day 30, and the gain is  .

  • A Pairwise kernel weights each interval by K, so its coupling stays I.

  • A TimeVarying coupling gives . It changes once, so it is given by its change points.

  • A Depletion per patch is the only modifier.

Each model starts from ten infections a day in patch A on the six days before day 1.

julia
gi = [0.1, 0.3, 0.3, 0.2, 0.1]
gi_between = [0.0, 0.1, 0.2, 0.3, 0.2, 0.2]
A = zeros(P, P, length(gi_between))
for a in 1:P, b in 1:P
    A[a, b, :] = K[a, b] .* (a == b ? vcat(gi, 0.0) : gi_between)
end
K_travel(cut) = Diagonal(diag(K)) + (1 - cut) * (K - Diagonal(diag(K)))
Kt = TimeVarying([1 => K_travel(0.0), 30 => K_travel(0.8)])

depletion = Depletion(PerStratum(pop))
seed = [fill(10.0, 1, 6); zeros(2, 6)]
modifiers = (depletion,)
models = [
    "Fixed coupling" => Recurrence(gi; coupling = K, modifiers),
    "Per-pair intervals" => Recurrence(Pairwise(A); modifiers),
    "Travel cut on day 30" => Recurrence(gi; coupling = Kt, modifiers),
]

# Infections of `renewal` by patch and day at a gain of `γ`.
infections(renewal, γ, seed, T) = renewal(γ; history = seed, stop = T)

runs = map(models) do (name, renewal)
    long(infections(renewal, 1.6, seed, T), patches; label = :model => name)
end
lines_by_patch(reduce(vcat, runs), :model; ylabel = "Infections")

With the fixed coupling B has more than one infection a day from day 3 and C from day 4. C, the smallest patch, peaks first, on day 36, and A last, on day 40. The longer intervals between patches delay the peaks in B and C by one day. The cut in travel on day 30 lowers the peaks in A, B and C by 6%, 17% and 19%.

Moving infections between patches ​

A movement matrix moves infections between patches after they occur.

where is patch 's value before the move, its value after it and the share of patch 's infections realised in patch . The share patch sends to the others, in proportion to , is . Each column of sums to one, so no infections are lost. Here is 0.05 for A, 0.03 for B and 0.02 for C, and halves from day 40. The coupling is I here, so the patches mix only through the move. The first modifier is Redistribute(K, ε), which reads from and at each step. It sits before the Depletion, so each pool is depleted by what its patch receives.

julia
ε = [t < 40 ? e : e / 2 for e in [0.05, 0.03, 0.02], t in 1:T]
moving = Recurrence(gi; modifiers = (Redistribute(K, ε), depletion))
moved = moving(1.6; history = seed, stop = T, record = Trace())

The Redistribute records the arrivals in patch in its trace, the first entry of moved.trace, with one column per day.

julia
arrivals = long(first(moved.trace), patches; label = :series => "Arrivals")
lines_by_patch(arrivals, :series; ylabel = "Infections moved in")

Over the 120 days B receives 106 infections from the other patches and A receives 19.

Sharing a total fixed elsewhere ​

When the infections of each patch are given, by another model or a fit, Allocate splits them among the districts of the patch in proportion to the renewal of each district.

where holds the districts of patch , is the patch's known total, here from the fixed coupling model, and is district 's reproduction number. The value is district 's number of new infections before the split. A has districts A1 to A3 with reproduction numbers 1.5, 1.6 and 1.8 and seeds 6, 3 and 1 a day. B has B1 and B2, both at 1.6, with seeds 2 and 1. C has C1 at 1.4 and C2 at 1.8, each with a seed of 1.

julia
districts = [1:3, 4:5, 6:7]
district_names = ["A1", "A2", "A3", "B1", "B2", "C1", "C2"]
totals = infections(last(first(models)), 1.6, seed, T)
district_seed = repeat([6.0, 3.0, 1.0, 2.0, 1.0, 1.0, 1.0], 1, 6)
R_district = repeat([1.5, 1.6, 1.8, 1.6, 1.6, 1.4, 1.8], 1, T)

# Infections by district and day when the districts in each group of
# `districts` share the row of `totals` of their patch.
function shared_totals(gi, R, seed, districts, totals)
    share = Allocate(districts, TimeVarying(PerStratum(totals)))
    return Recurrence(gi; modifiers = (share,))(R; history = seed)
end

by_district = shared_totals(gi, R_district, district_seed, districts, totals)

table = rename(long(by_district, district_names), :patch => :district)
table.patch = first.(table.district, 1)
lines_by_patch(table, :district; ylabel = "Infections")

B1 and B2 have the same reproduction number, and B1 has twice the infections of B2 on every day, as in the seed. A3, with the highest reproduction number of its patch and a sixth of the seed of A1, has more infections than A1 from day 26. The districts of each patch sum to the total of the patch.

julia
patch_sums = [sum(by_district[zs, :]; dims = 1) for zs in districts]
reduce(vcat, patch_sums) ≈ totals
true

Community and funeral transmission routes ​

A model with two routes of transmission has a coupling and a kernel for each route. The example is transmission in the community during illness and at the funeral after death. Pairwise(K => k, ...) pairs the kernel of each route with its coupling.

where holds each patch's infections on day , is the pressure and the funeral coupling. The funeral kernel on lag is , built by a Convolution from the infection-to-death delay , with a mean of 7.3 days. The funeral's weights on the day of death and the day after are and , and is the fatality ratio. Here  ,  ,   and  . The funeral coupling links each patch with its neighbours only, so it is sparse. The routes are the recurrence's kernel, so its coupling stays I.

Why two routes and not one pairwise kernel

The routes equal a Pairwise kernel   . Holding them as two routes keeps the sparse coupling sparse.

julia
death = [0.0, 0.0, 0.0, 0.0, 0.1, 0.2, 0.3, 0.2, 0.1, 0.1]
funeral_days = [0.6, 0.4]
cfr = 0.5
gi_funeral = Convolution(funeral_days)(vcat(death, 0.0); gain = cfr)
K_funeral = sparse([0.9 0.1 0.0; 0.1 0.8 0.1; 0.0 0.1 0.9])
routes = Pairwise(K => gi, K_funeral => gi_funeral)
two_routes = Recurrence(routes; modifiers)
cases = infections(two_routes, 0.9, seed, T);

The trace does not split the pressure by route, but the infections give each route's share.

where and are route 's coupling and kernel. The kernel sum is a Convolution of the infections with the kernel, with a zero at lag 0. This is the pressure before depletion, so the routes' shares sum to more than the infections.

julia
# The pressure of one route on each patch and day, from the `cases` that
# follow `seed`, at a gain of `γ`.
function route_pressure(K_r, g_r, γ, seed, cases)
    lagged = Convolution(vcat(0.0, g_r))(hcat(seed, cases))
    return γ .* (K_r * lagged[:, (size(seed, 2) + 1):end])
end

by_route = map(
    ["Community" => (K, gi), "Funeral" => (K_funeral, gi_funeral)]
) do (name, (K_r, g_r))
    pressure = route_pressure(K_r, g_r, 0.9, seed, cases)
    long(pressure, patches; label = :route => name)
end
lines_by_patch(reduce(vcat, by_route), :route; ylabel = "Pressure")

The reproduction numbers of the two routes are 0.9 and 0.45, each below 1, and their sum is 1.35. Between 40% and 57% of each patch is infected over the 120 days. The funeral pressure peaks five days after the community pressure in each patch. The mean lag of the funeral kernel is 7.7 days and that of the community kernel 2.9 days.

Performance ​

The timed model is five strata with a dense route, a sparse route and floored depletion over 200 days, with kernels of 20 lags. The figure gives its minimum time and that of a preallocated hand-written loop of the same model.

julia
performance_plot("spatial strata")

Learning more ​