Repository navigation
fix(observations): right-align a stream's data against its own expected series - #302
Conversation
Two parallel streams whose chains consume different lead-ins are given observations of equal length, as the caller has them. The branch with the shorter lead-in produces more expected values than there are observations and the error model rejects the data.
The observation-error loop right-aligned the data against the expected series only when there were more observations than expected values, and rejected the other direction. Split streams run in parallel off one expected series, so a stream whose chain consumes a shorter lead-in is left with more expected values than the caller has observations, and one series length cannot satisfy every stream at once. Score the overlap of the two series instead: a longer observation vector keeps its unscored lead-in, and a longer expected series is unobserved run-in. Callers pass each stream the observations they have rather than padding the shorter-lead-in stream with leading missings.
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="fix/285-split-lead-in")
using ComposableTuringIDModelsOption 2: Local CheckoutIf you have the repo locally: git checkout fix/285-split-lead-in
julia --project=. -e "using Pkg; Pkg.instantiate()" |
Codecov Report✅ All modified and coverable lines are covered by tests.
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
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)
|
Release notes live on the GitHub release, built on TagBot's merged-PR list, so a changelog file in the repo is not kept. The substance of these entries belongs in the pull request title and body, which is what the release notes are assembled from.
The trials vector is indexed at the expected-series step, so a per-time-point `N` given at the length of the data trips the length assertion whenever the expected series is longer. Cover both alignment directions and the partially observed carrier.
…data Scoring the overlap of the observed and expected series left the number-of-trials vector behind: it was required to match the expected series and indexed at the expected step, so a per-time-point N of data length tripped an assertion. It is now resolved against the data, and a vector of either length is accepted.
Both sides appended a new testitem to the end of test/unit/observation_models/observations.jl, so the conflict is a plain append/append and both blocks are kept. main's NEWS.md deletion is identical to this branch's, so it merges with no change. main's `_at` to `at` rename and this branch's `_scored_steps` touch different parts of the observation-error loop and merge cleanly.
This is a characterisation test, not a fix. The behaviour is already correct: the window offset generalises to a negative value on its own, so windows come off the tail of the expected series in both directions. Nothing exercised the negative direction, which scoring the overlap of the observed and expected series is what makes reachable. Cover all three cases — expected longer, equal, data longer — with a log-joint check, so windows taken off the head of the expected series would fail.
|
📖 Documentation preview is ready! View the docs for this PR at: https://EpiAware.github.io/ComposableTuringIDModels.jl/previews/PR302/ This preview will be updated automatically when you push new commits. |
There was a problem hiding this comment.
The PR generalises observation scoring so the observed and expected series can be right-aligned regardless of which is longer, replacing the old diff_t >= 0 invariant with a shared _scored_steps helper, and extends BinomialError's trials vector to the same alignment via a new _TrialDist.n_diff. The core alignment arithmetic checks out against the new tests (Aggregate, Split, BinomialError all trace correctly by hand), but CI's ad / Enzyme forward check is red, and the only Enzyme-forward scenario that exercises the touched BinomialError code path is not in the AD fixture registry's known-broken list, so this looks like an unresolved regression rather than a pre-existing flake.
Automated first pass by seabbs-review-bot (Claude sonnet), triggered by: first pass. 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.
Merge hazard with the other one of this pair — #325Verified by actually merging this branch with the other and running the result, rather than reasoning about it. #302 and #319 both rewrite the two scoring loops in Resolve by keeping #319's marginalisation body and #302's
Neither branch can carry a regression test for this today, because on each branch alone the combination is unreachable. Whichever merges second should add one. Details and the full reproduction are in #325. |
#302 and #319 both rewrite the two scoring loops in methods.jl and neither contained the other, so they conflicted in the place that matters. Issue #325 records why the obvious resolution is wrong. Resolved by keeping #319's marginalisation body and #302's _scored_steps bound. eachindex(Y_t) is only safe while diff_t >= 0, and #302 exists to make diff_t < 0 reachable, so taking #319's side alone indexes y.present at i + diff_t <= 0. Ported #302's other changes to the file explicitly rather than by conflict resolution: the two-way alignment docstring, both dropped @Assert diff_t >= 0, and _TrialDist's n_diff. Added the regression test neither branch could carry alone. A carrier with gaps against a longer expected series: that it evaluates, that no y_t reaches the chain, that the log-joint equals the observed entries against their own right-aligned expected values, and that moving the earliest observation moves it. Corrected one test inherited from #302. "BinomialError right-aligns its trials with the data" asserted 0 <= y_t[2] <= N for a gap, which is the pre-#319 behaviour where a gap is drawn. A gap is now marginalised and comes back missing. The replacement asserts that and pins the alignment with a log-joint against each observed entry's own day's trials, which the old bounds check did not do. observations.jl 96/96 locally.
Co-authored-by: Sam Abbott <contact@samabbott.co.uk> Closes #299
…ling Keep main's #272/#302 observation-error and forecast files (which superseded the branch's older right-alignment), keep the branch's new feature file requirements.jl, and consolidate the observation tests. Also fixes two pre-existing main defects exposed by the rebase's test run: - observations.jl had a missing 'end' between two @testItems, so the second never ran as its own testitem (main's own CI was failing on this). - the stale #272 NamedTuple test asserted gaps were sampled as latents; they are marginalised out, matching the implementation and the newer gap tests.
Closes #285.
Closes #306.
What is actually going on
The issue reads as
Splitbeing the odd one out. It is not: theobservation-error loop right-aligns in one direction only, and every model
shares that loop.
More observations than expected values is fine, and the head of
y_tgoesunscored (the chain's lead-in, #266). More expected values than observations is
rejected. A single stream never has to hit the second case, because the caller
picks
nand can always make the expected series come out atlength(y). ASplitruns its streams in parallel off one expected series, so onenhas toserve every stream, and a stream whose chain consumes a shorter lead-in is left
with more expected values than the caller has observations. There is no
nthatsatisfies both, hence the manual pad.
The change
Score the overlap of the two series rather than requiring one of them to be at
least as long as the other. The two series end at the same time point, so
right-aligning is the whole rule:
y_tkeeps its unscored head — the chain's lead-in, unchanged;Y_tkeeps an unobserved head — run-in the data does not cover.No observation is ever dropped by the new direction, so nothing that used to be
scored stops being scored. What used to be an assertion is now a model that runs
longer than its data, which is a legitimate setup in its own right. A
Splitneeds no per-stream arithmetic: each stream right-aligns its own data against
its own expected series, and the caller passes each stream the observations it
has.
BinomialError's number-of-trials vector is aligned the same way. It used to berequired to match the expected series exactly, which would have forced a
BinomialErrorbranch of aSplitto padNeven though its successes nolonger need padding.
Nis now right-aligned against the expected series likethe observations are, so it can be given with one entry per observation or one
per expected value; both put the same trials on the same time points and only
the unused head differs. A scalar
Nstill broadcasts. AnNtoo short tocover every scored step is rejected with a message saying how many entries are
needed.
Tests
given 13 observations each with no padding. The stream with the shorter
lead-in produces 18 expected values, and the log-joint is checked against
the sum of
logpdfover the last 13 of them, so an alignment off by thelead-in difference fails.
IDModelon a renewal process: moving theearliest observation of the shorter-lead-in stream moves the log-joint, so
nothing is dropped from its head.
a
MissingObservationscarrier.BinomialErrortrials given at data length and at expected-series lengthproduce the same log-joint, a scalar
Nstill broadcasts, and anNtooshort to cover the scored window is rejected.
Aggregate's alignment in all three directions — expected series longerthan the data, equal, and data longer — with a log-joint check, so windows
taken off the head of the expected series would fail. This one is a
characterisation test, not a fix: the behaviour is already correct,
because fix(Aggregate): right-align a delay nested inside an aggregation #307's window offset generalises to a negative value on its own.
Nothing exercised the negative direction, and scoring the overlap is what
makes that direction reachable, so it is pinned here rather than left to a
future change to break silently.
Relationship to the other open observation PRs
Checked against every one of them; none is a replacement for this.
nthe number of observations, and report what data a model needs #276 (observation lead-in). Independent, and it cherry-picks thisPR's implementation commit unchanged because three existing
Splittestsfail on that branch without it. If this merges first, rebasing Make
nthe number of observations, and report what data a model needs #276 dropsthe duplicate cleanly. The tests stay here; Make
nthe number of observations, and report what data a model needs #276 does not take them.have
Splittrim each stream's expected series by that stream's lead-in,which would have needed feat(observations): give observation chains a traversal and rewrap API #309's walk. This takes the alignment route
instead, so the two do not interact.
Both touch
ObservationErrorModels/methods.jl, but a different part of it:they change what a blank entry means, this changes how the two series are
aligned. No overlapping hunk. (fix: marginalise absent observations instead of imputing them #319 and fix(observations): keep NamedTuple observation data out of the model's hands #272 do conflict with each
other — see the note on either.)
CI
One red check,
ad / Enzyme forward, and it isCANCELLEDrather than failed —the 60-minute job cap that backend hits across the repo. Everything else is
green on
cba182f.Relationship to #276
Independent.
observation_lead_inonfix/266-observation-lead-inreports whata chain consumes; the branch was read but not touched. An alternative fix was to
have
Splittrim each stream's incoming expected series by that stream'slead-in, which needs the walk that PR adds. That is strictly more machinery for
the same result, so this takes the alignment route instead and the two do not
conflict.
observation_coveragethere assumes the surplus is always on theobservation side; once both land it is worth reporting the run-in direction too.
Interaction with the work merged since this branch was cut
mainmoved by ten PRs while this sat open, two of which touch the samealignment machinery. Both were checked directly.
Aggregate's alignment. It derivesoffset = n - length(Y_t)and reads windows asY_t[max(1, i - aggregation[i] + 1 - offset):max(0, i - offset)]. That was written for aY_tshorter thany_t, whereoffsetis non-negative, but it generalises to this change'snew direction on its own: with
Y_tlonger,offsetis negative and thelast window still ends at
length(Y_t).Aggregateon this branch isbyte-identical to
main, so no change was needed here. Running Aggregate left-aligns when the expected series is longer than the data #301's ownreproduction against the merged branch returns
[0.0, 11.0, 0.0, 15.0, 0.0, 19.0, 0.0, 23.0], the right-aligned answer that issue asks for, so thelimitation this PR originally recorded is resolved by fix(Aggregate): right-align a delay nested inside an aggregation #307 rather than by
anything here.
collapses to1:length(Y_t), exactly the oldeachindex(Y_t), wheneverdiff_t >= 0. Every pre-existing caller is in that case, soforecast'smissing`-padded horizon draws are untouched. Thechange is strictly additive: it only takes effect where the old code raised.
mainalso renamed_attoat. That sits in a different part of theobservation-error loop and merged cleanly;
_atremains as an alias.Known limitations
The two this PR originally recorded are both closed. The
BinomialErrortrialsgap (#306) is fixed here, and the
Aggregateleft-alignment (#301) was resolvedby #307 on
main.One component still assumes the two series are the same length.
ReportTriangleasserts
size(observed, 1) == length(Y_t)and indexes the expected series at thedata's reference day, so it cannot sit under a
Splitwhose branches haveunequal lead-ins. It fails loudly rather than misaligning, so nothing is silently
wrong, and the window walk is untouched here. Filed as #316.
Verification
mainwas merged into this branch (cba182f). The only conflict was anappend/append in
test/unit/observation_models/observations.jl, where bothsides had added a testitem at the end; both are kept.
main'sNEWS.mddeletion is byte-identical to this branch's, so it merged with no change.
pre-commitand the runic formatting check pass on the merge.Before the merge this PR was
CONFLICTING, which meant GitHub could not computerefs/pull/302/mergeand nopull_requestworkflow ran at all. The previoushead commit carried zero checks. The merge is what restores CI, and the full
matrix now runs on
cba182f.Reproductions run directly against the merged tree:
BinomialError's trials vector is not right-aligned with its data #306's own reproduction, both shapes:
The trials shapes agree, for a 10-value expected series scored against 6
observations:
An
Nof length 3 is rejected.Aggregate left-aligns when the expected series is longer than the data #301's own reproduction returns
[0.0, 11.0, 0.0, 15.0, 0.0, 19.0, 0.0, 23.0], the right-aligned answer, confirming fix(Aggregate): right-align a delay nested inside an aggregation #307 covers theAggregatecase.
Suite run on the merge commit in this worktree:
julia --project=test test/runtests.jl skip_quality— 1422 passed, 0failed, 16m14.4s.
The quality and AD suites were not run locally; CI covers both on
cba182f.The counts quoted in earlier revisions of this description were pre-merge and no
longer apply.
Attribution
d8039bccarries my commit message over another agent's implementation. Two ofus were working in the same worktree; I committed their uncommitted working tree
with
git add -Aunder a message of my own before they could commit itthemselves. The code in that commit, and the test in
5dbf8e0under it, aretheirs. Recorded here rather than by rewriting a pushed commit.
This was opened by a bot. Please ping @seabbs for any questions.