Modifiers
This page describes each built-in modifier, the depletion forms and pools, and how the order, state and scope of modifiers set what a step computes. The API overview defines the terms used.
using ComposableRecurrences
using ComposableRecurrences: Depletion, Floor, Transform, ResumeBuilt-in modifiers
A Depletion of a population draws each step's value from the people left to infect. Over 40 steps with a gain of 2, a population of 1000 holds the total infections below 1000:
N, gain, seed = 1000.0, fill(2.0, 40), [1.0, 2.0, 3.0]
depleted = Recurrence([0.3, 0.4, 0.3]; modifiers = (Depletion(N),))
total(r) = round(sum(r(gain; history = seed)))
total(depleted)800.0Depletion(N, parts...; pools)draws each step's value from named pools, such as(S = N, V = 0.0), through its parts: draws andFlows, in the order given. A draw is a depletion form,Hazard()by default, and itsoverflowkeyword sends what a small pool cannot give to a second stratum, a queue or nowhere. AFlow(from => to, rate)moves values between pools, with0for outside the model; with flows and no draw aDepletiononly moves values. Named flows (Flow(from => to, rate; name = :out)) name the rows of its trace.Redistribute(M; leak)moves a share of each stratum's value to others through a matrix whose columns sum to one, or less withleak = true;Redistribute(K, ε)movesε[q] * K[p, q]of stratumqtop.Allocate(groups, total)rescales each group of strata to a known total, keeping each stratum's share.Transform(f, θ)maps each value through, such as Transform(+, b)orTransform(clamp, lo, hi).
Each modifier here has its own hand-written gradient. A Transform has one when f holds no numbers of its own: from f's closed-form derivative when it has one, or else from a local forward-mode derivative. The gradient of Transform(clamp, lo, hi) goes to the active bound, or to the value when neither bound is active. A plain number γ in Transform(*, γ) is fixed and has no gradient.
Movement from a kernel
A Redistribute(K, ε) holds its movement as the kernel ε of one value or a vector over origins gives one matrix, and an origins × time matrix gives an S × S × T movement. An intensity PerStratum(ε) or TimeVarying(PerStratum(E)) is read as the vector or the matrix it holds. Any other parameter is read at each step as
The diagonal of
A movement written as
Depletion forms
A depletion form sets how a pool gives up what a step asks for. Each form is a type, passed as an instance. The same run with Floor in place of the default hazard form gives a different total:
floored = Recurrence([0.3, 0.4, 0.3]; modifiers = (Depletion(N, Floor()),))
total(floored)832.0| Name | What it does |
|---|---|
Hazard(α) | draws the value as a hazard, so the pool never goes negative; the default |
Floor(α) | scales the value by the share of the pool left, with a floor |
Truncate(κ) | draws what is asked for, up to what is left; κ in [0, 1) smooths the cap |
Share() | a flow that moves a share of its pool at each step |
Split() | a flow that draws one count from several pools, in proportion to the pools the step starts from |
αinHazard(α)andFloor(α)is the heterogeneity exponent, one by default. Above one, infections slow sooner as the pool shrinks, as when the most susceptible are infected first.A flow's form is
Hazard()by default, a rate that competes with the pool's other flows. WithTruncate()orTruncate(κ)a flow is a count, capped by what remains. WithSplit()a flow from a tuple of pools,(:a, :b) => to, is one count split between them.The docstring of each form gives its formula, and A depletion form shows how to write a new one.
Pools, draws and flows
A Depletion holds pools for each group of strata, and its parts act on the step's value and the pools in the order given. Its docstring gives the formula of a draw from one pool, from several pools and into a pool.
A draw from several pools takes from their weighted sum
; when it takes from the first poolweightsnames alone.A draw into a pool (
into) fills it up to the capacity, as admissions fill beds. Its form is Truncate()orTruncate(κ), and its capacity is not negative.What a draw leaves of the value it takes,
, goes nowhere by default. Withoverflow = :stratumit goes to the group's second value, . Withoverflow = :queueit goes to a queue held after the pools in the state, and joins the draw's value at the next step.Each draw takes the value the parts before it pass on.
Flows act on the pools where they stand among the parts, as when patients leave before the step's admissions. Hazard and share flows given one after another act together, then each count flow acts in the order given. A count flow from
0brings its demand in from outside.
One pool, with draws that leave nothing over, is a pointwise step whose state is the pool. Any other Depletion is blockwise. Each group has one value, or two with overflow = :stratum, and value Depletion covers every stratum, so a stratum without a cap takes an infinite N. With overflow = :stratum group Redistribute after the Depletion, with K sending each to the shared one.
Without a draw the pools are the step's values, pool by pool, so value Recurrence with kernel [1.0] the core carries the previous step's pools forward, and the Depletion applies the step's flows,
Order
The modifiers of a Recurrence act on each step's value in tuple order, after the gain and add. With modifiers
where add, Depletion draws the capped value from its pool, the quantity it carries from step to step, while a cap applied after it only limits what is reported. With the cap
k = [0.3, 0.4, 0.3]
gain = fill(2.5, 15)
seed = [1.0, 2.0, 3.0]
pool(N) = Depletion(N; pools = (S = N - sum(seed),))
cap_first = Recurrence(k; modifiers = (Transform(clamp, 0.0, 8.0), pool(100.0)))
cap_last = Recurrence(k; modifiers = (pool(100.0), Transform(clamp, 0.0, 8.0)))
round.(cap_first(gain; history = seed)[end-2:end]; digits = 2),
round.(cap_last(gain; history = seed)[end-2:end]; digits = 2)([2.85, 2.55, 2.17], [0.94, 0.59, 0.37])Imported infections have the same choice: through add they are drawn from the pool, and through a Transform(+, b) after a Depletion they do not deplete it. A Transform(*, γ) first in the tuple scales the add input with the kernel's value, so the scale acts on both before an Allocate or a draw.
using ComposableRecurrences
CR = ComposableRecurrences
# Imports added after the depletion, so not drawn from the pool.
imports = CR.Transform(+, TimeVarying([1.0, 0.0, 2.0]))
mods = (CR.Depletion(50.0, CR.Floor()), imports)
Recurrence([1.0]; modifiers = mods)(1.0; history = [2.0], stop = 3)
# output
3-element Vector{Float64}:
3.0
2.88
4.598912State
Each modifier keeps its own state Depletion. The state starts from the call's history through the modifier's Init, or at zero. It is carried in the State a call returns, so a resumed call carries on with it.
r = Recurrence(k; modifiers = (pool(100.0),))
y, state = r(gain; history = seed, record = Resume())
state.states([10.466045960048065],)A modifier's state is one entry per stratum unless its blocks gives another count, which nstate reads. A modifier that needs no state leaves it at zero.
Scope
A modifier's step reads one stratum, one group of strata or every stratum. The trait blocks is (1, 1) for the first, the values and state entries of each group for the second, and nothing for the third. A pointwise modifier reads and writes each stratum on its own. The scope sets how much the step can do, and how well it runs under Threaded: strata run apart only when every modifier is pointwise.
| Built-in | Step reads | State holds | Place it |
|---|---|---|---|
Depletion | one stratum with its pools | each pool, then the queue with overflow = :queue | after anything that should draw from the pool |
Redistribute | every stratum | what each stratum received | before a Depletion that should deplete where values are realised |
Allocate | one group of strata, or every stratum with imports | nothing, or the import pattern after the spilled values | after what it rescales |
Transform | one stratum | nothing | where the map applies |
Pools, draws and flows gives the strata and state of a Depletion with several pools, an overflow or no draw. A Transform(+, b) after an Allocate adds to its totals, and a spill whose pattern reads the values before the Allocate goes in its spill. A Transform(+, b) goes where the addition should enter, and a Transform(clamp, lo, hi) after what it bounds.
A modifier's parameters take every form in Slots.
Allocate with imports
Allocate with imports places a share of each group's total by an import pattern, read before an optional spill. A fifth of the first group's total lands where the third stratum sends, read before the first two strata spill a tenth of their values to each other. The second group imports nothing:
using ComposableRecurrences
CR = ComposableRecurrences
P = [0.0 0.0 0.2; 0.0 0.0 0.8; 0.0 0.0 0.0]
within = CR.Redistribute([0.9 0.1 0.0; 0.1 0.9 0.0; 0.0 0.0 1.0])
split = CR.Allocate(
[1:2, 3:3], PerStratum([10.0, 2.0]);
imports = (PerStratum([0.2, 0.0]), P), spill = within
)
r = Recurrence([1.0]; modifiers = (split,))
y = r(ones(3, 1); history = [3.0; 1.0; 1.0;;])
round.(y; digits = 3)
# output
3×1 Matrix{Float64}:
6.0
4.0
2.0Its trace is its state after each step. With trace_imports = Val(true) it also holds imports, the part of each stratum's value the import term placed, and names its rows (trace_rows): spilled, pattern and imports. The imports cost a little more to record, so they are built only when asked for. The keyword trace_imports takes a Val, not a Bool, so the choice is part of the type even when the model is built inside a gradient. Named in the modifiers, it is read by name:
using ComposableRecurrences
CR = ComposableRecurrences
P = [0.0 0.0 0.2; 0.0 0.0 0.8; 0.0 0.0 0.0]
within = CR.Redistribute([0.9 0.1 0.0; 0.1 0.9 0.0; 0.0 0.0 1.0])
split = CR.Allocate(
[1:2, 3:3], PerStratum([10.0, 2.0]);
imports = (PerStratum([0.2, 0.0]), P), spill = within,
trace_imports = Val(true)
)
r = Recurrence([1.0]; modifiers = (; split))
out = r(ones(3, 1); history = [3.0; 1.0; 1.0;;], record = CR.Trace())
round.(out.trace.split.imports; digits = 3)
# output
3×1 Matrix{Float64}:
0.4
1.6
0.0A group's imports sum to
Below Allocate is meant for non-negative values: a group sum at or below zero is floored to
Choosing a modifier
To bound values,
Transform(clamp, lo, hi)cuts them, and aDepletionfilled up to a capacity keeps account of what is cut.To mix strata before the gain use a coupling, and after it a
Redistribute.To map values,
Transformtakes any function; a new modifier suits a map that keeps state or reads other strata, and composes with the built-ins in the same tuple.
Depletion in practice
A Depletion builds each model below from a draw and flows, in the order given.
| model | parts |
|---|---|
| a population with births and deaths | a TimeVarying N, Hazard(), then the births as a count Flow(0 => :S, b; form = Truncate()) and the deaths as one out of :S |
| leaky protection at relative susceptibility | Hazard(; weights = (S = 1, V = σ)), then the doses as Flow(:S => :V, d; form = Truncate()) |
| beds that a share | Flow(:O => 0, δ; form = Share()), then Truncate(; into = :O), with N the beds |
| an allowance granted | Flow(0 => :B, TimeVarying(b); form = Truncate()), then Truncate() |
| the same without carry over | as above, with Flow(:B => 0, starts; form = Share()) before the grant |
The step reads N at its own time, and a pool is not rescaled when N changes: arrivals enter it only through the flows. Here starts is a TimeVarying share of one at each period start and zero otherwise, and a lifetime allowance is a starting pool alone. A second pool gives leaky, all-or-nothing and waning protection; a delay from dose to protection is a Convolution of the doses before the call. A share ComposableRecurrences.Primary indexed TimeVarying kernel for cohorts, and bound the sum with Transform(clamp, lo, hi) or a Depletion that fills a pool.
A herd of 100 with 3 births a step: N grows by the births, and the births enter the pool as a count from outside after the draw.
using ComposableRecurrences
CR = ComposableRecurrences
births = fill(3.0, 8)
N = TimeVarying(100.0 .+ cumsum(births))
herd = CR.Depletion(
N, CR.Hazard(), CR.Flow(0 => :S, TimeVarying(births); form = CR.Truncate());
pools = (S = 90.0,)
)
r = Recurrence([0.5, 0.5]; modifiers = (herd,))
y = r(fill(1.5, 8); history = [5.0, 5.0])
round.(y; digits = 3)
# output
8-element Vector{Float64}:
6.32
6.672
7.099
6.951
6.563
5.876
5.085
4.261Leaky vaccination: 5 doses a step move susceptibles into a pool drawn at a third of the rate, and the protection wanes back at rate 0.1.
using ComposableRecurrences
CR = ComposableRecurrences
leaky = CR.Depletion(
1000.0, CR.Hazard(; weights = (S = 1, V = 0.3)),
CR.Flow(:S => :V, 5.0; form = CR.Truncate()), CR.Flow(:V => :S, 0.1);
pools = (:S, :V)
)
r = Recurrence([0.3, 0.5, 0.2]; modifiers = (leaky,))
y = r(fill(2.0, 6); history = [5.0])
round.(y; digits = 3)
# output
6-element Vector{Float64}:
2.996
6.73
8.846
12.779
18.196
25.151Ten beds, a tenth of patients leaving each step before the admissions: demand above the free beds goes to the community stratum.
using ComposableRecurrences
CR = ComposableRecurrences
beds = CR.Depletion(
10.0, CR.Flow(:O => 0, 0.1; form = CR.Share()),
CR.Truncate(; into = :O, overflow = :stratum); pools = (O = 0.0,)
)
demand = [4.0 3.0 5.0 6.0 2.0; 0.0 0.0 0.0 0.0 0.0]
r = Recurrence([0.0]; modifiers = (beds,))
y = r(; history = zeros(2, 1), add = demand)
round.(y; digits = 3)
# output
2×5 Matrix{Float64}:
4.0 3.0 4.06 1.0 1.0
0.0 0.0 0.94 5.0 1.0A budget of 5 doses a week: the 1.5 doses left in the first week carry over to the second.
using ComposableRecurrences
CR = ComposableRecurrences
grants = TimeVarying([rem(t - 1, 7) == 0 ? 5.0 : 0.0 for t in 1:14])
doses = CR.Depletion(
CR.Flow(0 => :B, grants; form = CR.Truncate()),
CR.Truncate(; overflow = :stratum); pools = (:B,)
)
demand = [fill(0.5, 1, 7) fill(2.0, 1, 7); zeros(1, 14)]
r = Recurrence([0.0]; modifiers = (doses,))
y = r(; history = zeros(2, 1), add = demand)
y[1, :]'
# output
1×14 adjoint(::Vector{Float64}) with eltype Float64:
0.5 0.5 0.5 0.5 0.5 0.5 0.5 2.0 2.0 2.0 0.5 0.0 0.0 0.0A ward without a draw: suspected patients are confirmed or ruled out, and confirmed patients are discharged, each at a hazard rate.
using ComposableRecurrences
CR = ComposableRecurrences
ward = CR.Depletion(
CR.Flow(:suspected => :confirmed, 0.3), CR.Flow(:suspected => 0, 0.4),
CR.Flow(:confirmed => 0, 0.1); pools = (:suspected, :confirmed)
)
admitted = [fill(10.0, 1, 4); zeros(1, 4)]
r = Recurrence(; modifiers = (ward,))
y = r(; add = admitted)
round.(y; digits = 3)
# output
2×4 Matrix{Float64}:
4.966 7.432 8.656 9.264
2.157 5.181 8.449 11.67