Spatial models
Introduction
A model can run several strata side by side, one per place, mixed within each step. Strata can also be types, as for the case types of a branching process.
Packages used
using ComposableRecurrences
using ComposableRecurrences: Depletion, Redistribute, Allocate
using CairoMakie, AlgebraOfGraphics, DataFramesMeta
using LinearAlgebra
using SparseArrays
CairoMakie.activate!(type = "png", px_per_unit = 2)A gravity coupling
The coupling is the three-town example's gravity coupling, with the same towns as patches and 5% mixing rather than 0.2%:
where
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"))
endMost weight sits on the diagonal. A and B, the closest pair, share the most, and C is the most isolated.
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)]
models = [
"Fixed coupling" => Recurrence(gi; coupling = K, modifiers = (depletion,)),
"Per-pair intervals" => Recurrence(Pairwise(A); modifiers = (depletion,)),
"Travel cut on day 30" => Recurrence(gi; coupling = Kt, modifiers = (depletion,)),
]
long(y) = @chain DataFrame(permutedims(y), patches) begin
@transform(:day = 1:size(y, 2))
stack(Not(:day); variable_name = :patch, value_name = :count)
end
@chain models begin
map(_) do (name, r)
@transform(long(r(1.6; history = seed, stop = T)), :model = name)
end
reduce(vcat, _)
data(_) * mapping(:day, :count, color = :model, layout = :patch) *
visual(Lines, linewidth = 2)
draw(_; axis = (xlabel = "Day", ylabel = "Infections"))
endAll three patches take off together, because even 5% mixing gives B more than one infection a day by day 3 and C by day 4. C, the smallest patch, peaks first, on day 36 with the fixed coupling, and A last, on day 40. Longer intervals between patches delay the peaks in B and C by one day. Cutting travel on day 30 lowers the peaks in A, B and C by 6%, 17% and 19%, because B and C depend most on A.
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]
patch = Recurrence(gi; modifiers = (Redistribute(K, ε), depletion))
infections, tr = patch(1.6; history = seed, stop = T, record = (:trace,))The Redistribute records the arrivals tr.modifiers[1], one column per day:
arrivals = tr.modifiers[1]
@chain long(arrivals) begin
@transform(:series = "Imported infections")
data(_) * mapping(:day, :count, color = :series, layout = :patch) *
visual(Lines, linewidth = 2)
draw(_; axis = (xlabel = "Day", ylabel = "Imported infections"))
endB receives the most, 106 infections over the 120 days, from its large neighbour A. A receives the least, 19 infections.
Sharing a total fixed elsewhere
When each patch's infections are known, from the model above or an earlier fit, Allocate splits them among its districts by the districts' renewals:
where
districts = [1:3, 4:5, 6:7]
totals = models[1].second(1.6; history = seed, stop = T)
district_seed = repeat([6.0, 3.0, 1.0, 2.0, 1.0, 1.0, 1.0], 1, 6)
share = Allocate(districts, TimeVarying(PerStratum(totals)))
R_district = repeat([1.5, 1.6, 1.8, 1.6, 1.6, 1.4, 1.8], 1, T)
by_district = Recurrence(gi; modifiers = (share,))(R_district; history = district_seed)
@chain DataFrame(permutedims(by_district), ["A1", "A2", "A3", "B1", "B2", "C1", "C2"]) begin
@transform(:day = 1:T)
stack(Not(:day); variable_name = :district, value_name = :count)
@transform(:patch = first.(:district, 1))
data(_) * mapping(:day, :count, color = :district, layout = :patch) *
visual(Lines, linewidth = 2)
draw(_; axis = (xlabel = "Day", ylabel = "Infections"))
endB1 and B2 share a reproduction number, so B1 keeps twice B2's infections throughout. A district with a higher reproduction number takes a growing share of its patch. A3 overtakes A1 on day 26 despite starting with a sixth of its seed. C2 leads C1 from the first day. The districts still sum to their patch's infections:
@assert reduce(vcat, [sum(by_district[zs, :]; dims = 1) for zs in districts]) ≈ totalsCommunity and funeral transmission routes
Some diseases spread by more than one route, each with its own timing and contacts. Ebola spreads in the community while a case is ill and at the funeral after death. Pairwise(K => k, ...) pairs each route's kernel with its own coupling:
where Convolution from the infection-to-death delay I. They 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)
ebola = Recurrence(routes; modifiers = (depletion,))
ebola_cases = ebola(0.9; history = seed, stop = 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.
function route_force(K_r, g_r)
y = hcat(seed, ebola_cases)
return 0.9 .* (K_r * Convolution(vcat(0.0, g_r))(y)[:, (size(seed, 2) + 1):end])
end
@chain [("Community", K, gi), ("Funeral", K_funeral, gi_funeral)] begin
map(_) do (name, K_r, g_r)
@transform(long(route_force(K_r, g_r)), :route = name)
end
reduce(vcat, _)
data(_) * mapping(:day, :count, color = :route, layout = :patch) *
visual(Lines, linewidth = 2)
draw(_; axis = (xlabel = "Day", ylabel = "Force of infection"))
endNeither route alone sustains spread: their reproduction numbers are 0.9 and 0.45. Together they do, and 40% to 57% of each patch is infected. The funeral pressure peaks five days after the community pressure in each patch. Its mean lag is 7.7 days, against 2.9 days for the community route.
Performance
Each bar times five strata with a dense route, a sparse route and floored depletion over 200 days with a 20-day kernel, against a preallocated hand-written loop.
performance_plot("spatial strata")Learning more
API overview: a map of every operator, coupling and modifier.
Public API: every public name with its docstring.