Repository navigation
fix(Renewal): unify the constructors and give seeding a real slot - #347
Conversation
`Renewal` had two constructors that could not be combined. The keyword one discretised a continuous generation time but took no modifiers; the positional one took modifiers but required an already discretised pmf and accepted no `D_gen`/`Δd`. Both forms now take any generation time, the discretisation keywords, and modifiers, with the positional form delegating to the keyword one so there is a single construction path. `Renewal` gains a `modifiers` field, so a modifier also composes onto an inferred generation interval, where no step is baked at construction. `initialisation` is a `PriorLike` slot but is consumed as a single level: a process handed to it is drawn at length 1 and collapsed by `only`, and the resulting seeding window is deterministic geometric decay that no output exposes. The slot is now documented as the level at t₀, and a `SeedingPath` wrapper estimates the whole seeding window instead: the process is drawn at the window shape and used as the initial incidence window directly. The window is returned as `I_seed` by the renewal and by `IDModel`, drawn or decayed, so the seeding phase can be inspected. A modifier-free renewal keeps the bare force-of-infection core as its step from both constructors, as the keyword form already did. Closes #269 Closes #263 Co-Authored-By: Sam Abbott <contact@samabbott.co.uk> Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UmmqqzQPhrB74RfAv2TqBQ
|
@seabbs-review-bot |
There was a problem hiding this comment.
The PR unifies Renewal's keyword and positional constructors onto one construction path, adds a modifiers keyword, and introduces SeedingPath so the renewal's pre-window incidence can be estimated as a process instead of decayed from a single level, threading I_seed through to generated quantities. The dispatch-based design (RenewalStep vs bare core, level vs SeedingPath, fixed vs inferred generation interval) follows the ecosystem's conventions and is well tested for the main paths; the only real defect is a stray issue-number reference in a test comment, plus two minor gaps (an untested SeedingPath/inferred-generation-interval combination and unvalidated modifiers keyword input).
Automated first pass by seabbs-review-bot (Claude sonnet), triggered by: re-review requested by seabbs-bot. Not a human review. Comment @seabbs-review-bot to ask for another pass: @seabbs any time, the author's agent once it has pushed changes. Add the no-review label to opt this PR out. Ping @seabbs with any questions.
| @testitem "Renewal combines a continuous generation time with a modifier" begin | ||
| using ComposableTuringIDModels, Distributions | ||
| using ComposableTuringIDModels: RenewalStep | ||
| # The call issue #269 reported as inexpressible: a continuous generation |
There was a problem hiding this comment.
issue This comment cites "issue #269", which breaches the project standard that comments and docs carry no development history or issue/PR numbers (git blame and the PR already hold that). Reword to state the current behaviour only, e.g. "# A continuous generation time discretised by the constructor, carrying a modifier."
| # per stratum — or a whole seeding path, drawn at the incidence window's own | ||
| # shape. Both branches are decided by the slot's type, so nothing is chosen | ||
| # at run time. | ||
| if infection.initialisation isa SeedingPath |
There was a problem hiding this comment.
suggestion No test combines a SeedingPath initialisation with an inferred (AbstractPriorModel) generation_time, even though this branch explicitly supports it (the seeding shape is computed from the per-draw, lag-dropped gen_int). Add a @testitem combining the two (mirroring "Renewal composes a modifier onto an inferred generation interval") so the interaction is locked in.
| # The modifier slot takes one modifier or a collection of them; a tuple keeps | ||
| # the step's modifier types concrete. | ||
| _modifier_tuple(mod::AbstractRenewalModifier) = (mod,) | ||
| _modifier_tuple(mods) = Tuple(mods) |
There was a problem hiding this comment.
suggestion _modifier_tuple(mods) = Tuple(mods) accepts any iterable for the new modifiers keyword with no element-type check, unlike the positional constructor which is statically constrained to AbstractRenewalModifier.... A bad value (e.g. modifiers = [SomeUnrelatedObject()]) builds a Renewal fine and only fails later as an opaque MethodError at sample time. Validate at entry, e.g. _modifier_tuple(mods) = (foreach(m -> m isa AbstractRenewalModifier || throw(ArgumentError("modifiers must be AbstractRenewalModifier")), mods); Tuple(mods)).
…erred interval The keyword form has no signature constraining what `modifiers` holds, so it checks its argument and names the type it wants rather than failing later as a `MethodError` inside the step's seam. Adds a test for a `SeedingPath` with an inferred generation interval, where the window length comes from the interval drawn per iteration, and drops an issue number from a test comment. Co-Authored-By: Sam Abbott <contact@samabbott.co.uk> Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UmmqqzQPhrB74RfAv2TqBQ
|
Thanks — all three findings acted on in a50a403.
Renewal and seeding test items: 92 passed, 0 failed. Runic |
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="269-renewal")
using ComposableTuringIDModelsOption 2: Local CheckoutIf you have the repo locally: git checkout 269-renewal
julia --project=. -e "using Pkg; Pkg.instantiate()" |
`path_prior` ran in the outer keyword constructor only, so Julia's generated positional constructor bypassed it and a bare `Distribution` in the `rt` PATH slot was never widened to an `Intercept`. Adding the `modifiers` field lengthens and reorders that positional signature, so the widening moves into an inner constructor, as `TransformLatentModel` and the other widened components do. `path_prior` is idempotent, so rebuilding from stored fields is a no-op and no `ConstructionBase.constructorof` is needed. A drawn coupling operator is now named `mixing.*` whichever constructor built the renewal and whether or not the step carries modifiers: `RenewalStep`'s seam composes its core flat rather than prefixing it. This renames `core.mixing.*`, which the positional constructor used to give. The tutorial and the `MixingStep` docstring are updated. The infection role contract said the model returns `(; I_t, Z_t)`. It now says at least those two, with consumers reading by name, so a model returning more (a `Renewal` returns `I_seed`) does not make it stale. Refs #344, whose other six types are fixed in #346. Co-Authored-By: Sam Abbott <contact@samabbott.co.uk> Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UmmqqzQPhrB74RfAv2TqBQ
Closes #269
Closes #263
Also fixes
Renewal's half of #344; #346 covers the other six types.Breaking: the drawn coupling operator is renamed
A
Gravity, or anyAbstractMixingModeldrawn throughmixing, now names itsparameters
mixing.α,mixing.β,mixing.γ. Through the positionalconstructor they were
core.mixing.αand so on. Anyone indexingchain[@varname(core.mixing.α)]gets aKeyErrorafter this lands.The two constructors disagreed on this before the PR. The keyword form stored
the bare force-of-infection core and gave
mixing.α; the positional formwrapped that core in a
RenewalStep, whose seam prefixed it, and gavecore.mixing.α. Unifying the constructors forces a choice between them, andleaving it implicit would have made the name depend on whether the renewal
happens to carry modifiers.
RenewalStep's seam now composes its core flat, sothe name is
mixing.*in every case — either constructor, modifiers or none.docs/src/tutorials/patch-models.mdand theMixingStepdocstring are updatedto match.
#269 — a continuous generation time can carry a modifier
The keyword constructor discretised
generation_timebut took no modifiers; thepositional one took modifiers but required a discretised pmf and accepted no
D_gen/Δd. Both now take any generation time, the discretisation keywords andmodifiers, and the positional form delegates to the keyword one, so there is one
construction path and one discretisation of the interval:
Renewalgains amodifiersfield. It is needed because an inferred generationinterval bakes no step at construction, so the step is rebuilt per draw and the
modifiers have to be reachable there. A modifier now composes onto an inferred
interval as well as a fixed one. The keyword form has no signature to constrain
what it is given, so it checks its argument and names the type it wants.
Every existing call form still works; call sites in
src/,test/,docs/andbenchmark/were checked.#263 —
initialisationsays what it is, and a process gets a real slotNeither option in the issue works exactly as written, because rejecting a
process at construction is not implementable. A process in that slot is
legitimate and in use:
initialisation = IID(Normal(log(20), 0.3))gives oneseed per stratum for a
Dims{2}model (test/unit/infection_models/strata_renewal.jl,test/unit/inference/inference.jl). At construction there is no way to tell alevel-process from a seeding-path process, since the shape is only known when
as_turing_modelis called. Rejecting processes would break working stratifiedmodels. The reframing is written up on the issue itself.
So this takes option (a), scoped:
initialisationis documented as what it is: the LEVEL at t₀, one value orone per stratum. A draw of any other length now raises an error naming the
alternative instead of failing inside
only.A new
SeedingPath(process)wrapper changes what the slot means. The processis drawn at the seeding window's own shape —
len_gen_intfor a singleseries,
n_strata × len_gen_intfor a stratified renewal, soSeedingPath(Stratify(...))works through the existing shape contract —transformed, and used as the initial incidence window directly. Nothing is
decayed and no growth rate is implied, so the data inform the run-up:
Observability: the renewal returns
(; I_t, Z_t, I_seed)andIDModelpassesI_seedon when the infection model exposes one. It is the window the scanstarted from, whether drawn or decayed, so the deterministic seeding window is
now inspectable too. It is not part of
I_t, which keeps its length.SeedingPathoutside a renewalinitialisationslot errors rather thansilently behaving as the process it wraps.
On the name: the wrappers in
src/latent_models/modifiers/are<Verb>LatentModel-shaped because they wrap a latent and stay a latent. This isnot that — it marks how one slot is read and is a component of no role — so a
noun for the thing it produces reads better at the call site.
Seedingis theobvious alternative if you prefer it; say so and I will rename.
#344 —
Renewal's PATH slot is widened on every construction pathpath_priorwas applied in the outer keyword constructor only, so Julia'sgenerated positional constructor bypassed it and a bare
Distributioninrtwas never widened to an
Intercept. This PR takesRenewalto seven fields,which lengthens and reorders that positional signature, so the fix belongs here
rather than in a PR racing this one.
path_priormoves into an innerconstructor, following the pattern #346 uses for the other six types. It is
idempotent, so no
ConstructionBase.constructorofis needed, and although theinner signature leaves
rtunbounded, the widening runs beforenew, so thePriorLikeguard still fires.Other notes
recurrent_stepfrom both constructors; the keyword form already did this,the positional form wrapped it in a
RenewalStepwith an empty modifiertuple.
src/base/roles.jlandsrc/base/interfaces.jlstated the infection contractas
(; I_t, Z_t), which was already stale. They now say at least thosetwo, and that a model may return more and consumers read by name, so the next
field added to that tuple does not make them wrong again.
did through
IDModel.Tests
New
@testitems intest/unit/infection_models/renewal_step.jl(the call #269reported as inexpressible, as a regression test; a modifier on an inferred
generation interval; the
modifierskeyword's input check; and thertwidening on both construction paths) and a new
test/unit/infection_models/seeding_path.jlcovering the decayed window, adrawn window used as-is, the first scanned step, a stratified seeding path, a
seeding path sized to an inferred interval, composition with a modifier,
exposure through
IDModel, and both error paths.Renewal, infection, inference, compose and mixing test items pass locally, as do
base and utils. Runic
--checkexits 0,Test.detect_ambiguitieson the moduleis clean with the inner constructor in place, and a ForwardDiff gradient through
a seeded renewal is finite.
Not done
I_seedis exposed but no AD fixture was added for a seeded renewal, to keep theAD suite's runtime where it is. Say if you want one.
This was opened by a bot. Please ping @seabbs for any questions.