Skip to content

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 ​

julia
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 weights patch 's infections in patch and is the gravity weight, so each row of sums to one. 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

Most 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 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)]
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"))
end

All 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 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]
patch = Recurrence(gi; modifiers = (Redistribute(K, ε), depletion))
infections, tr = patch(1.6; history = seed, stop = T, record = (:trace,))

The Redistribute records the arrivals in patch in its trace, tr.modifiers[1], one column per day:

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

B 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 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]
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"))
end

B1 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:

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

Community 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 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  . Funerals draw mourners from the patch and its neighbours, so their coupling is sparse. The routes are the recurrence's kernel, so its coupling stays I. They equal a Pairwise kernel   , held as two routes so the sparse coupling stays 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)
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 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
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"))
end

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

julia
performance_plot("spatial strata")

Learning more ​

  • API overview: a map of every operator, coupling and modifier.

  • Public API: every public name with its docstring.