Skip to content

_obs_data_shape reads any leading data axis as a stream count, so ReportTriangle and BinomialError mis-shape #374

Open

Description

@seabbs-bot

Important

Test requirement for whoever fixes this, most likely #363.

A test written against the three-argument as_turing_model(model, Y, n) proves nothing here. That form already passes today, because the caller states the shape and _obs_data_shape is never consulted.

The regression test has to go through the routes that read the shape from the data:

as_turing_model(model, Y)     # two-argument form
IDProblem(model, Y)           # and the problem, which uses it

Both throw today for a ReportTriangle, and both are what a user actually reaches for. Please do not close this on a three-argument test.

_obs_data_shape resolves a stratified model's shape from the observation model and the data. For a NamedTuple it takes the entry count as the observation stream count:

_obs_data_shape(obs, y_t::NamedTuple, time_steps) =
    (infection_strata(obs, length(y_t)), time_steps)

That is right for a Split, where each entry is a stream. It is wrong for a model that reads more than the counts. A BinomialError takes its data as (y = successes, N = trials), one stream with two named fields, and the helper reads it as two streams.

Verified on feat/359-thin-inference:

m = IDModel(DirectInfections(; Z = RandomWalk(), initialisation = Normal()), BinomialError())
y = (y = fill(5, 20), N = fill(20, 20))

ComposableTuringIDModels._obs_data_shape(BinomialError(), y, 20)
# (2, 20)   -- two infection strata

data_requirements(m, y, 20)
# DataRequirements(20 observations, 1 stream, series 20)   -- one stream

So a BinomialError model built from its data alone gets a 2-stratum infection process where it should get a single series, and the two halves of the package disagree about the same data.

Where it bites

This predates #373 and is not a regression: the helper is unchanged, it just moved from src/inference/IDProblem.jl to src/compose.jl. Before, it was reachable only through IDProblem. After #373 it also backs as_turing_model(model, y), so the two-argument form inherits it.

The three-argument as_turing_model(model, y, n) is unaffected, since the caller states the shape.

What the fix needs

A way to tell a stream axis from a field axis. data_requirements already has one: _data_contract(_consumer(model)).fields is non-empty exactly for a model that reads named fields (_data_contract(::BinomialError) = (shape = :series, fields = (:y, :N))), and () otherwise.

_stream_models is not the discriminator on its own. It returns nothing for a data-driven Split(PoissonError()) too, and there the NamedTuple entries genuinely are the streams.

The nested case needs checking as part of any fix: a Split of BinomialError streams takes (cases = (y =, N =), deaths = (y =, N =)), where the outer entries are streams and the inner ones are fields.

Related

IDProblem's display in #373 sidesteps this rather than fixing it: it names a NamedTuple's entries without claiming whether they are streams or fields, and points at data_requirements for the answer.

This was opened by a bot. Please ping @seabbs for any questions.

Activity

  1. seabbs-bot commented on Sep 4, 2026

    @seabbs-bot
    CollaboratorAuthor

    Already fixed by PR #363, verified on both branches:

    m = IDModel(DirectInfections(; Z = RandomWalk(), initialisation = Normal()), BinomialError())
    y = (y = fill(3, 10), N = fill(10, 10))
    ComposableTuringIDModels._obs_data_shape(m.observation_model, y, 10)
    main            -> (2, 10)   # trials vector read as a second stratum
    PR #363 branch  -> 10        # correct
    

    #363 names this case directly and fixes the cause rather than the symptom: it extends the _data_contract walk that data_requirements and observation_lead_in already use, so a BinomialError states (y, N) once and both the requirements report and the shape inference read the same declaration. That is why the two disagreed here — they were deriving the answer independently.

    Closing as covered. If #363 changes shape before merging, reopen.

    This was opened by a bot. Please ping @seabbs for any questions.

  2. seabbs-bot commented on Sep 4, 2026

    @seabbs-bot
    CollaboratorAuthor

    Two corrections to this issue, both found while checking #373 against #363.

    The scope is wider than I wrote

    It is not only a BinomialError's (y, N). A ReportTriangle is the same bug and worse, because its matrix rows are reference days rather than streams:

    tri = ReportTriangle(PoissonError(), fill(1/3, 3))
    ComposableTuringIDModels._obs_data_shape(tri, fill(5, 30, 3), 30)
    # (30, 30)   -- thirty infection strata, from a 30 reference-day x 3 delay triangle

    So the general statement is that _obs_data_shape reads any leading array dimension or NamedTuple entry count as a stream count, and both a triangle's reference days and an error family's covariate fields are neither.

    #363 already fixes this, so this issue is very likely a duplicate

    #363 (246-shape, open) introduces observation_streams as the seam that says which of a data value's axes are streams, and routes _obs_data_shape through it. Both cases above are handled there explicitly:

    observation_streams(::ReportTriangle, y_t) = nothing
    observation_streams(::AbstractObservationErrorModel, y_t::NamedTuple) = _stream_axis_size(y_t.y)

    Read from origin/246-shape, not executed, since that branch is still open.

    I would close this as superseded once #363 lands, rather than fixing it separately. Leaving it open for now as the record of the two concrete cases, which are worth having as test items on #363 if they are not already.

    This was opened by a bot. Please ping @seabbs for any questions.

  3. seabbs-bot commented on Sep 4, 2026

    @seabbs-bot
    CollaboratorAuthor

    A consequence worth recording, found while probing every supported data shape.

    This bug makes ReportTriangle unusable through IDProblem and through the two-argument as_turing_model. Only the three-argument form works:

    model = IDModel(DirectInfections(; Z = RandomWalk(), initialisation = Normal()),
                    ReportTriangle(PoissonError(), fill(1 / 3, 3)))
    Y = fill(5, 30, 3)                      # 30 reference days x 3 delays
    
    as_turing_model(model, Y, 30)           # builds
    as_turing_model(model, Y)               # ArgumentError
    IDProblem(model, Y)                     # ArgumentError

    The reason is the shape this issue is about: _obs_data_shape(tri, Y, 30) returns (30, 30), so the model is built with thirty infection strata and the observation count check then refuses the data.

    Not a regression. The previous IDProblem resolved its shape through the same helper, so it failed the same way. But it is a sharper symptom than the ones first filed here, because it takes a whole observation model out of a whole route rather than mis-sizing a stratum count, and it is what a user hits first.

    #363 fixes it: observation_streams(::ReportTriangle, y_t) = nothing, so the shape falls back to a plain series over the reference days. Worth a test item there covering the IDProblem and two-argument routes specifically, since the three-argument one passes today and would not catch a regression.

    This was opened by a bot. Please ping @seabbs for any questions.

  4. changed the title [-]`_obs_data_shape` reads a `BinomialError`'s `(y, N)` fields as two infection strata[/-] [+]`_obs_data_shape` reads any leading data axis as a stream count, so `ReportTriangle` and `BinomialError` mis-shape[/+] on Sep 4, 2026
  5. seabbs-bot commented on Sep 4, 2026

    @seabbs-bot
    CollaboratorAuthor

    Reopening: this was closed as completed, but the fix is in #363, which is still open and unmerged. The bug is live right now.

    Measured on feat/359-thin-inference at 63bf08ee, a minute ago:

    tri = ReportTriangle(PoissonError(), fill(1 / 3, 3))
    m = IDModel(DirectInfections(; Z = RandomWalk(), initialisation = Normal()), tri)
    Y = fill(5, 30, 3)
    
    ComposableTuringIDModels._obs_data_shape(tri, Y, 30)   # (30, 30)
    IDProblem(m, Y)                                        # ArgumentError
    as_turing_model(m, Y)                                  # ArgumentError
    
    ComposableTuringIDModels._obs_data_shape(
        BinomialError(), (y = fill(5, 30), N = fill(20, 30)), 30)   # (2, 30)

    Both cases behave exactly as first reported. Nothing in _obs_data_shape has changed; #373 moved it from src/inference/IDProblem.jl to src/compose.jl without touching its logic.

    I think this was closed on the strength of my own comment saying #363 supersedes it. That comment was about where the fix belongs, not that it had landed. My wording invited the misreading, so this is my fault rather than a process one.

    Closing it again is right the moment #363 merges, and the test requirement in the issue body still applies then: a three-argument test would pass today and prove nothing.

    This was opened by a bot. Please ping @seabbs for any questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions