Repository navigation
Multi-strata infection processes, coupling, and arbitrary strata mappings - #245
Merged
Merged
Conversation
Contributor
Try this Pull Request!Option 1: Julia Package ManagerOpen Julia and type: import Pkg
Pkg.activate(temp=true)
Pkg.add(url="https://github.com/EpiAware/ComposableTuringIDModels.jl", rev="add-mixing")
using ComposableTuringIDModelsOption 2: Local CheckoutIf you have the repo locally: git checkout add-mixing
julia --project=. -e "using Pkg; Pkg.instantiate()" |
Codecov Report❌ Patch coverage is
Flags with carried forward coverage won't be shown. Click here to find out more.
... and 3 files with indirect coverage changes 🚀 New features to boost your workflow:
|
Contributor
Benchmark comparison vs baseMinimum time per call. Buckets are PR time as a % of base, so lower is faster (🟢 faster, ⚪ within 5%, 🔴 slower). Counts of benchmarks per bucket:
Evaluation — 9 benchmarks (by time change)
AD gradients — 7 benchmarks (by time change)
|
Contributor
|
📖 Documentation preview is ready! View the docs for this PR at: https://EpiAware.github.io/ComposableTuringIDModels.jl/previews/PR245/ This preview will be updated automatically when you push new commits. |
Add a multi-strata infection process that maps onto a multi-strata
observation process under arbitrary mappings, and coupling between the
infection strata.
Two seams carry it. The strata axis is introduced by a latent manipulator,
`Stratify(shared, across)`, returning a `strata × time` matrix, so a renewal
handed one runs a recursion per stratum with its own seed and its own `Rt`
deviation. Coupling is an operator on the incidence window applied before
`Rt`, dispatched through `renewal_pressure`, which cannot be a modifier
because `K(R ⊙ v) ≠ R ⊙ Kv`.
`n` now means the size of the value the model returns: `n::Int` is a
length-`n` path and `n::Dims{2}` is `(n_strata, n_time)`.
Every infection-to-observation cardinality goes through one weight matrix on
`Split`: one-to-one, many-to-one, many-to-many, and finer or coarser than the
infection process. The infection-stratum count is read from the observation
model and the data at build time, so nothing about the shape is stored on a
struct.
`mixing` accepts a matrix, a per-stratum generation interval, a per-pair
array via `pairwise_gen_int`, or an `AbstractMixingModel` drawn per
iteration; `Gravity` is the worked example, sharing one `gravity` function
with the fixed path so the two cannot drift. A `Hierarchy` in an
`UncertainDelay` parameter slot gives a partially pooled generation interval
with no new component.
Remove `GroupedInfections`, the grouped `IDModel` constructors,
`_grouped_combiner` and the `(n_time, n_groups)` shape. It shared the
finished `I_t` and post-multiplied by a per-stratum scalar, so the strata
axis only existed outside the recursion, which blocks per-stratum seeding and
coupling.
`IDProblem` and `forecast` now drive a matrix or a NamedTuple of streams,
neither of which was previously possible. `modifier_init_state` takes the
incidence window so a substate can match its shape.
Co-authored-by: Sam Abbott <contact@samabbott.co.uk>
Adds tests for the paths an audit found uncovered. The two that matter most
are `IDProblem`'s stratified shape resolution and `forecast` with a matrix or
NamedTuple series, neither of which had any coverage, and a real bug had
already hidden in the second.
Also covers the `Dims{2}` methods of the shape-transparent latent
manipulators, `SusceptibleDepletion` and `ImportedCases` composed on a
stratified renewal, `ExpGrowthRate` at a strata shape with a `Replicate` rt,
a mixing matrix combined with per-stratum generation intervals,
`across_shape` as a third-party extension point, and the assertions in
`_seed` and `gravity` that never fired.
`pairwise_gen_int` with `K = I` reproducing the uncoupled model exactly is
now a regression test. That identity pins the per-pair lag convention and
had only been checked by hand.
Co-authored-by: Sam Abbott <contact@samabbott.co.uk>
seabbs
reviewed
Aug 5, 2026
A prose review found the same point made in several places and a recurring habit of stacking clauses behind a colon. Deduplicated: the couplable-versus-not line now lives only in the coupled patch-models case study, with pointers from `CombineInfections` and the split-observations page. The fixed-versus-inferred gravity paths are described once, in `Gravity`'s docstring. `Split` no longer restates its own cardinality list twenty lines after giving it. Split eighteen run-on sentences into separate ones, mostly in the pooling case study and the `Renewal`, `RenewalSteps`, `Gravity` and `shapes` docstrings. Co-authored-by: Sam Abbott <contact@samabbott.co.uk>
The previous push landed while the prior commit's workflows were still running and GitHub queued no runs for the new head. This empty commit re-triggers them and can be dropped when squashing. Co-authored-by: Sam Abbott <contact@samabbott.co.uk>
seabbs
reviewed
Aug 5, 2026
This was referenced Aug 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #45, closes #175.
What this adds
A multi-strata infection process that maps onto a multi-strata observation
process under arbitrary mappings between the two strata sets, and coupling
between the infection strata.
Two seams carry it.
The strata axis is introduced by a latent manipulator, not an
infection-model wrapper.
Stratify(shared, across)returns astrata × timematrix from a shared path and a per-stratum effect. Hand it to
Renewal(; rt = ...)andRtis a matrix, so the scan window isstrata × lagsand the recursion runs per stratum, each with its own seedingand its own
Rtdeviation.Replicate(model)draws one independent path perstratum instead.
Coupling is an operator on the incidence window, applied before
Rt.renewal_pressure(mixing, gen_int, window)is the one dispatch point.mixing = I, the default, is uncoupled and covers the single-series caseunchanged. Coupling cannot be a renewal modifier, because a modifier
transforms the finished force of infection while mixing sits between the
generation-time convolution and
Rt, andK(R ⊙ v) ≠ R ⊙ Kv.nnow means the size of the value the model returns:n::Intis a length-npath,
n::Dims{2}is(n_strata, n_time), which issize(I_t).Infection strata to observation strata
Every cardinality goes through one weight matrix on
Split.map = I(n)[1.0 1.0 1.0]The infection-stratum count is read from the observation model and the data at
build time through
infection_strata, so three age strata observed as onehospitalisation stream builds a three-stratum process from a
1 × Tmatrix.Nothing about the shape is stored on a struct.
Coupling options
mixingaccepts astrata × stratamatrix, astrata × lagsgenerationinterval (one interval per stratum), a
strata × strata × lagsarray (aper-pair interval, built with
pairwise_gen_int), or anAbstractMixingModelwhose parameters are drawn per iteration.
Gravityis the worked example ofthe last; the fixed and inferred paths call the same
gravityfunction, sothey cannot drift.
Nothing about a mixing matrix is checked or normalised. A zero column
K[:, h]means stratumhinfects nobody, though its own incidence stillevolves through
K[h, h]. A zero rowK[g, :]means stratumgreceives noforce at all, so its incidence falls to zero and stays there.
Hierarchical generation intervals
An
UncertainDelay's parameters are prior slots, so giving one aHierarchypartially pools that parameter across strata. A partially pooled generation
interval therefore needs no new component; the interval is drawn at
n_strataand assembled into a
strata × lagsmatrix.Removed
GroupedInfections, theIDModel(infection, group_effect, observation)andIDModel(idmodel, group_effect)constructors,_grouped_combinerand thecombinerkeyword, and the(n_time, n_groups)shape. No deprecationhandling.
GroupedInfectionsshared the finishedI_tand post-multiplied bya per-stratum scalar, so the strata axis only ever existed outside the
recursion, which blocks per-stratum seeding and coupling. The replacement is
one line:
CombineInfectionsis unchanged. It runs one scan per stratum, so it cannot becoupled. One model over an axis is couplable; several independent models are
not.
Also changed
IDProblemandforecastnow handle a matrix or aNamedTupleof streams;neither could drive a multi-strata model before.
SusceptibleDepletionisshape-agnostic and takes a scalar or a per-stratum
pop_size. Every path modeltakes
n::Int, with a guard that tells you to wrap a path model in aStratifyrather than failing inside a submodel.modifier_init_statetakes the incidence window as a second argument, so asubstate that tracks the incidence can match its shape. Without it a scalar
pop_sizeon a stratified renewal changes type mid-scan.Verified
same model at shape
(1, n)give identical draws.K = Ireproduces the uncoupled modelexactly, which pins the lag convention.
Gravitywith fixed exponents matches the plaingravitymatrix.renewal_pressuremethod against a hand-written sum.the data alone.
Docs
A new case study, "Coupled patch models", covering independent patches, a
shared
R_twith partial pooling, exogenous and endogenous imports, a fixedmixing matrix, an inferred gravity model, and a many-to-one observation.
Gate status at time of opening
task test-fasttask test-jettask test-quality(Aqua, formatting, doctests)task test-adtask docs-fastThe docs build takes over 45 minutes here because several case studies run
MCMC, so it had not finished when this was opened. Doctests (every
@examplein a docstring) pass as part of the quality gate; what is outstanding is the
full site build of the case-study pages. CI will settle it.
This was opened by a bot. Please ping @seabbs for any questions.