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
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))
endA 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
The other symbols
The term
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"))
endThe 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
where gi_between below. The coupling
A
Pairwisekernel weights each interval byK, so its coupling staysI.A
TimeVaryingcoupling gives. It changes once, so it is given by its change points. A
Depletionper patch is the only modifier.
Each model starts from ten infections a day in patch A on the six days before day 1.
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
where I here, so the patches mix only through the move. The first modifier is Redistribute(K, ε), which reads Depletion, so each pool is depleted by what its patch receives.
ε = [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 moved.trace, with one column per day.
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
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.
patch_sums = [sum(by_district[zs, :]; dims = 1) for zs in districts]
reduce(vcat, patch_sums) ≈ totalstrueCommunity 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 Convolution from the infection-to-death delay I.
Why two routes and not one pairwise kernel
The routes equal a Pairwise kernel
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 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.
# 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.
performance_plot("spatial strata")Learning more
Where the coupling acts states which stratum each coefficient of a step belongs to.
Coupling or pairwise kernel compares the two ways of mixing strata.
Branching processes uses strata as types of case.