Skip to content

feature / grouped EM for the SLDS - #170

Open
harrisonritz wants to merge 8 commits into
claude/split-pr-165-incremental-t8k2s5-4from
claude/split-pr-165-incremental-t8k2s5-5
Open

harrisonritz wants to merge 8 commits into
claude/split-pr-165-incremental-t8k2s5-4from
claude/split-pr-165-incremental-t8k2s5-5

Conversation

@harrisonritz

Copy link
Copy Markdown
Collaborator

5 of 5 — splitting #165 into reviewable pieces. Stacked on #169; the diff here is only this PR's changes. With this one in, the whole of #165 has landed and the interim guard from #167 is gone.

fit!, elbo and rand now honour an SLDS's depends_on.

The one structural constraint

The trial partition is a property of the dataset, so it must be identical across regimes — only the parameter values differ per regime. Every regime must therefore declare the same labels, and a disagreement raises an ArgumentError (including the case where one regime declares nothing at all). Given that, each cell is governed by one ordinary SLDS whose sub-LDSs hold that cell's parameter arrays and which shares A and πₖ by reference, so the discrete M-step still updates the single shared chain and the existing smoother and weighted aggregator run on it unchanged.

Regime constants are refreshed once per cell rather than once per trial, so the per-cell overhead is K Cholesky sets per pass. Forward-backward stays global — the discrete chain is shared by all trials.

The M-step, and the tied initial state

The work is one γ-weighted sufficient statistic per (regime, cell) unit. From there:

  • Dynamics and emissions are per regime, so they pool across the cells that share a version within a regime.
  • x0/P0 are tied across regimes, so they pool across every (regime, cell) unit that shares a version. This is valid because Σₖ γₖ(t=1) = 1: summing the per-regime weighted init statistics reproduces the unit-weight pooled statistic the ungrouped path uses.

Units are laid out regime-major, which makes the first unit of any parameter version belong to regime 1 — the update writes there, and _broadcast_initial_state! restores the tie. It runs twice: once after the x0 update and once after P0, because the P0 scatter reads each unit's own x0, so the tie has to hold before that update, not only after it.

The prior log-density counts the per-regime terms once per distinct parameter version instead of once per regime.

Tests

  • test_grouped_slds_fit — two sessions with genuinely different emissions, K=2 regimes: finite ELBO trace, each regime keeps a separate [C d] and a positive-definite R per session, and x0/P0 stay tied across regimes.
  • test_grouped_slds_requires_matching_labels — regimes disagreeing about the partition, and a regime declaring nothing while another does, are both errors; no regime declaring anything is simply the ungrouped path.

Known gap, unchanged from #165

The single-trial smooth(slds, y, w) wrapper takes one trial plus explicit responsibilities and has no way to say which group that trial belongs to, so it still uses the base parameter arrays. It is not part of the grouped API here. Worth a follow-up if that entry point matters for grouped models — say the word and I'll add the same label-or-error treatment rand got.


Generated by Claude Code

@harrisonritz
harrisonritz force-pushed the claude/split-pr-165-incremental-t8k2s5-4 branch from e5143f7 to 3ee52cd Compare August 13, 2026 23:05
@harrisonritz
harrisonritz force-pushed the claude/split-pr-165-incremental-t8k2s5-5 branch from 93c05d9 to 48ba98e Compare August 13, 2026 23:05
@harrisonritz
harrisonritz force-pushed the claude/split-pr-165-incremental-t8k2s5-4 branch from 3ee52cd to 378f37c Compare August 13, 2026 23:26
@harrisonritz
harrisonritz force-pushed the claude/split-pr-165-incremental-t8k2s5-5 branch from 48ba98e to 9b177e4 Compare August 13, 2026 23:26
@harrisonritz
harrisonritz force-pushed the claude/split-pr-165-incremental-t8k2s5-4 branch from 378f37c to 74f1932 Compare August 13, 2026 23:33
@harrisonritz
harrisonritz force-pushed the claude/split-pr-165-incremental-t8k2s5-5 branch from 9b177e4 to 48cd373 Compare August 13, 2026 23:33
@harrisonritz
harrisonritz force-pushed the claude/split-pr-165-incremental-t8k2s5-4 branch from 74f1932 to 207c902 Compare August 14, 2026 11:15
@harrisonritz
harrisonritz force-pushed the claude/split-pr-165-incremental-t8k2s5-5 branch from 48cd373 to 41cd88c Compare August 14, 2026 11:15
Completes condition-dependent parameters (#165): `fit!`, `elbo` and `rand` now
honour an SLDS's `depends_on`, and the interim guard introduced with the
declaration API is gone.

The trial partition is a property of the dataset, so it must be identical
across regimes; only the parameter values differ per regime. Every regime must
therefore declare the same labels, and a disagreement (including one regime
declaring nothing) is an error. Given that, each cell is governed by one
ordinary SLDS whose sub-LDSs hold that cell's parameter arrays and which shares
the transition matrix and initial distribution by reference, so the existing
smoother and weighted aggregator run on it unchanged. Regime constants are
refreshed once per cell rather than once per trial, and forward-backward stays
global since the discrete chain is shared by all trials.

M-step. The work is one gamma-weighted sufficient statistic per (regime, cell).
Dynamics and emissions are per regime, so they pool across the cells that share
a version *within* a regime; x0/P0 are tied across regimes, so they pool across
every (regime, cell) unit that shares a version — which is valid because the
regime responsibilities at t=1 sum to one, so summing the per-regime weighted
init statistics reproduces the unit-weight statistic the ungrouped path uses.
Units are laid out regime-major so the first unit of any version belongs to
regime 1, where the update writes; the tie is then restored by copying regime
1's initial state into the others, before the P0 update as well as after, since
the P0 scatter reads each unit's own x0.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MgmEbyHewqUYTJLFegAoK5
@harrisonritz
harrisonritz force-pushed the claude/split-pr-165-incremental-t8k2s5-4 branch from 207c902 to f0b8efb Compare August 14, 2026 11:35
@harrisonritz
harrisonritz force-pushed the claude/split-pr-165-incremental-t8k2s5-5 branch from 41cd88c to dddd218 Compare August 14, 2026 11:35
harrisonritz and others added 2 commits August 22, 2026 17:30
Runs JuliaFormatter v1.0.62 (the version test/Project.toml pins, blue style
with always_use_return) over the tree, so the "Code Formatting" test passes.
Whitespace only, no logic changes.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014wJNTtEFZwFUUFvgKbq4F7
@codecov

codecov Bot commented Aug 22, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.40476% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 96.87%. Comparing base (58a48dd) to head (38bc606).

Files with missing lines Patch % Lines
src/lds/fit_SLDS.jl 99.40% 1 Missing ⚠️
Additional details and impacted files
@@                             Coverage Diff                              @@
##           claude/split-pr-165-incremental-t8k2s5-4     #170      +/-   ##
============================================================================
+ Coverage                                     96.79%   96.87%   +0.08%     
============================================================================
  Files                                            20       20              
  Lines                                          4084     4224     +140     
============================================================================
+ Hits                                           3953     4092     +139     
- Misses                                          131      132       +1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

harrisonritz and others added 2 commits August 23, 2026 02:01
Patch coverage here was 92.86%, and the bulk of the gap was one whole model
combination: a grouped SLDS with a Poisson emission. The SLDS dispatches its
grouped emission M-step on the observation model, and only the Gaussian branch
had a test — so the Poisson branch (the solve running once per version of
`[C d D]` over the trials of every cell sharing it) and its prior term were
both unexercised. `pd_poisson_slds` builds the counterpart of `pd_slds`, and
the test checks each regime keeps a separate emission per session while x0/P0
stay tied across regimes, as they are for an ungrouped SLDS.

`rand(rng, slds, tsteps)` draws one trial, which a grouped model cannot place
in a group on its own; the guard rejecting that had no caller. The test also
checks the escape hatch — passing `depends_on` for the single trial — actually
produces a sample.

Ancillary parameter dependencies 156 -> 173 assertions. Patch coverage of the
files this PR touches: 167/168 added executable lines. The one line left is the
`else` arm rejecting an unsupported observation model, unreachable while
Gaussian and Poisson are the only concrete subtypes.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014wJNTtEFZwFUUFvgKbq4F7
harrisonritz added a commit that referenced this pull request Aug 23, 2026
Codecov's target is the base branch's coverage, so raising #170's raised this
PR's bar to 96.80% and 96.29% no longer cleared it.

The gap worth closing was `_obs_pool_target`'s alternate-width branch, the one
`GroupedSufBuffers`' own comment describes: pooling writes into a scratch
`obs_xy` whose column count is the slot's channel count, and the preallocated
one is sized from the model's `obs_dim`, so a slot shared across cells at some
other width needs its own. Reaching it takes all three of a shared `[C d D]`,
more than one cell pooling into it, and a dataset the model was not built at —
group `:R`, leave `[C d D]` shared, and hand a 3-channel template 5-channel
data. The test pins that the pooled emission comes out at the data's width and
stays shared by reference while each cell keeps its own resized `:R`.

Stitching 117 -> 128 assertions. Patch coverage 213/216 added executable lines
(98.61%). The three left are `_obs_stats`'s no-data method (reachable only for
a slot no trial selects), `_slds_ws_for`'s `nothing` fallback (a kwarg default
no caller in the package takes), and `_seed_slot_d`'s signature line, which
Julia's instrumentation scores 0 while counting its body.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014wJNTtEFZwFUUFvgKbq4F7
@harrisonritz
harrisonritz marked this pull request as ready for review August 23, 2026 16:32
harrisonritz and others added 3 commits August 23, 2026 18:06
The SLDS shares the grouped update helpers, so its M-step passes the noise
slot vectors too and takes the GLS path on the same condition the LDS does.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants