Skip to content

Modifiers ​

Modifiers change each step's value of a recurrence, in order, each with its own state; the API overview shows where they fit.

julia
using ComposableRecurrences
using ComposableRecurrences: Depletion, Floor, Transform

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

julia
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.0
  • Depletion(N, parts...; pools) draws each step's value from named pools, such as (S = N, V = 0.0), through its parts: draws and Flows, in the order given. A draw is a depletion form, Hazard() by default, and its overflow keyword sends what a small pool cannot give to a second stratum, a queue or nowhere. A Flow(from => to, rate) moves values between pools, with 0 for outside the model; with flows and no draw a Depletion only 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 with leak = true; Redistribute(K, ε) moves ε[q] * K[p, q] of stratum q to p.

  • 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) or Transform(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 local rule when it has one, or else from a local forward-mode derivative.

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:

julia
floored = Recurrence([0.3, 0.4, 0.3]; modifiers = (Depletion(N, Floor()),))
total(floored)
832.0
NameWhat 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
  • α in Hazard(α) and Floor(α) 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. With Truncate() or Truncate(κ) a flow is a count, capped by what remains. With Split() 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.

Order ​

The modifiers of a Recurrence act on each step's value in tuple order, after the gain and add. With modifiers , each with its own state , a step computes

where is the value after the gain and add, the value modifier passes on, and the output. Each sees the value the one before it passed on, so the order belongs to the model. A cap applied before a 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   and the hazard form of a pool of , the two orders give

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

julia
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.598912

State ​

Each modifier keeps its own state from step to step, such as the remaining pool of a 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.

julia
r = Recurrence(k; modifiers = (pool(100.0),))
y, state = r(gain; history = seed, record = (:state,))
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-inStep readsState holdsPlace it
Depletionone stratum with its poolseach pool, then the queue with overflow = :queueafter anything that should draw from the pool
Redistributeevery stratumwhat each stratum receivedbefore a Depletion that should deplete where values are realised
Allocateone group of strata, or every stratum with importsnothing, or the import pattern after the spilled valuesafter what it rescales
Transformone stratumnothingwhere the map applies

With overflow = :stratum a Depletion also reads a second stratum. Without a draw it reads one group of pools held in the stratum, and its state holds what flowed into each pool. It then sits on a kernel of [1.0], which carries the pools forward. 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:

julia
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.0

Its 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. Named in the modifiers, it is read by name:

julia
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 = (:split,))
round.(out.trace.modifiers.split.imports; digits = 3)

# output

3×1 Matrix{Float64}:
 0.4
 1.6
 0.0

A group's imports sum to . The pattern places of them and, while the pattern sums to less than , the spilled values place the rest:

Choosing a modifier ​

  • To bound values, Transform(clamp, lo, hi) cuts them, and a Depletion filled 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, Transform takes 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.

modelparts
a population with births and deathsa 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 leaves each stepFlow(:O => 0, δ; form = Share()), then Truncate(; into = :O), with N the beds
an allowance granted at each period startFlow(0 => :B, TimeVarying(b); form = Truncate()), then Truncate()
the same without carry overas 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 leaving the beds is a geometric length of stay, with survival   after steps. For another stay, convolve the admissions with its survival, as a 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.

julia
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.261

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

julia
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.151

Ten beds, a tenth of patients leaving each step before the admissions: demand above the free beds goes to the community stratum.

julia
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.0

A budget of 5 doses a week: the 1.5 doses left in the first week carry over to the second.

julia
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.0

A ward without a draw: suspected patients are confirmed or ruled out, and confirmed patients are discharged, each at a hazard rate.

julia
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([1.0]; modifiers = (ward,))
y = r(; history = zeros(2, 1), 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