Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,39 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]

### Added
- Ancillary parameter dependencies, declaration side: every
`AbstractStateModel` and `AbstractObservationModel` now carries a
`depends_on` field (default `nothing`). Setting it to a `NamedTuple` of
per-trial label vectors — e.g. `obs_model.depends_on = (C = session, d =
session, R = session)` — declares that those parameters are to be estimated separately for
each group of trials, while everything else stays pooled. This is the
"stitching" setup for combining recording sessions that observe different
neurons in the same animal: shared latent dynamics, session-specific
emissions
* Keys are grouped the same way `fit_bool` groups them, since those
parameters are fit jointly as one regression — `[A b B]` is one group and
`[C d D]` another. Grouping any member groups them all, so a declaration
has to name **every** member of a group it touches: `(C = s, d = s)`, not
`(C = s,)`. `B` and `D` are zero-column when the model takes no inputs and
are then not part of the group; give the model inputs and they must be
named too. `:x0`, `:P0`, `:Q` and `:R` are groups of one. Labels may be
`Symbol`s, integers or strings. Different groups may use different label
vectors; the trial partition is their common refinement
* A malformed declaration (unknown parameter name, a group named only in
part, members of one group carrying different labels, label vectors of
unequal length) is rejected by
`validate_LDS` — so by the positional `LinearDynamicalSystem(state_model,
obs_model)` constructor, not at the first `fit!`
* Per-group values live in a new `variants` field holding one model object
per parameter-group combination, with non-varying parameters shared **by
reference** so a single M-step write covers all of them. They are read back
with the new exported `group_labels(model, name)` and
`group_parameter(model, name, label)`
* `show` reports the declared groups for a model that has any
* **Not yet honoured by fitting.** The grouped E/M-step lands in a follow-up;
until it does, `fit!`, `smooth`, `elbo`, `loglikelihood` and `rand` throw
an `ArgumentError` on a model that declares `depends_on` rather than
silently fitting one pooled parameter set
- Public allocating `elbo(model, y; ...)` for all three models (Gaussian LDS
with `ux`/`uy` keywords, Poisson LDS with Newton-smoother keywords, SLDS
with an `rng` keyword since its E-step consumes a posterior sample). Runs
Expand Down
7 changes: 7 additions & 0 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ MNPrior
x0_mean_prior
```

## Ancillary parameter dependencies

```@docs
group_labels
group_parameter
```

## Sampling

```@docs; canonical = false
Expand Down
4 changes: 4 additions & 0 deletions src/StateSpaceDynamics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ include("stats/priors.jl")
# Model definitions + inference-state containers.
include("lds/types.jl") # abstract types, model structs, SLDS
include("lds/workspaces.jl") # FilterSmooth / SufficientStatistics / workspaces
include("lds/parameter_groups.jl") # `depends_on` -> per-group parameter variants
include("utils/show.jl")
include("utils/validation.jl")

Expand Down Expand Up @@ -65,6 +66,9 @@ export GaussianStateModel, GaussianObservationModel, PoissonObservationModel
export IWPrior, MNPrior, x0_mean_prior
export CovUpdateCache

# Ancillary parameter dependencies (`depends_on`)
export group_labels, group_parameter

# Utilities
export block_tridgm
export valid_Σ, gaussian_entropy
Expand Down
4 changes: 4 additions & 0 deletions src/lds/fit_LDS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ function smooth(
ux=nothing,
uy=nothing,
) where {T<:Real,S<:GaussianStateModel{T},O<:GaussianObservationModel{T}}
_reject_unsupported_dependence(lds)
data = Data(lds, y; ux=ux, uy=uy)
tfs = _smooth_data(lds, data)
return _collect_smooth_output(tfs, y)
Expand Down Expand Up @@ -773,6 +774,7 @@ function elbo(
ux=nothing,
uy=nothing,
) where {T<:Real,S<:GaussianStateModel{T},O<:GaussianObservationModel{T}}
_reject_unsupported_dependence(lds)
data = Data(lds, y; ux=ux, uy=uy)
tfs = initialize_FilterSmooth(lds, data.tsteps)::TrialFilterSmooth{T}
npool = min(Threads.maxthreadid(), length(data.y))
Expand Down Expand Up @@ -849,6 +851,7 @@ function fit!(
ux=nothing,
uy=nothing,
) where {T<:Real,S<:GaussianStateModel{T},O<:GaussianObservationModel{T}}
_reject_unsupported_dependence(lds)
data = Data(lds, y; ux=ux, uy=uy)
return _fit_tridiag!(lds, data; max_iter=max_iter, tol=tol, progress=progress)
end
Expand Down Expand Up @@ -1098,6 +1101,7 @@ function StatsAPI.loglikelihood(
ux=nothing,
uy=nothing,
) where {T<:Real,SM<:GaussianStateModel{T},OM<:GaussianObservationModel{T}}
_reject_unsupported_dependence(lds)
data = Data(lds, y; ux=ux, uy=uy)

S_chol, K = _filter_cov_pass(lds, maximum(data.tsteps))
Expand Down
3 changes: 3 additions & 0 deletions src/lds/fit_PLDS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,7 @@ function elbo(
newton_max_iter::Int=20,
newton_tol::Float64=1e-6,
) where {T<:Real,S<:GaussianStateModel{T},O<:PoissonObservationModel{T}}
_reject_unsupported_dependence(plds)
data = Data(plds, y; ux=ux, uy=uy)
tfs = initialize_FilterSmooth(plds, data.tsteps)::TrialFilterSmooth{T}
npool = min(Threads.maxthreadid(), length(data.y))
Expand Down Expand Up @@ -806,6 +807,7 @@ function smooth(
ux=nothing,
uy=nothing,
) where {T<:Real,S<:GaussianStateModel{T},O<:PoissonObservationModel{T}}
_reject_unsupported_dependence(plds)
data = Data(plds, y; ux=ux, uy=uy)
tfs = initialize_FilterSmooth(plds, data.tsteps)::TrialFilterSmooth{T}
# Cap the pool at the trial count — workspaces beyond ntrials are never
Expand Down Expand Up @@ -862,6 +864,7 @@ function fit!(
newton_max_iter::Int=20,
newton_tol::Float64=1e-6,
) where {T<:Real,S<:GaussianStateModel{T},O<:PoissonObservationModel{T}}
_reject_unsupported_dependence(plds)
data = Data(plds, y; ux=ux, uy=uy)
T_max = maximum(data.tsteps)

Expand Down
8 changes: 7 additions & 1 deletion src/lds/fit_SLDS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ function Random.rand(
ux::Union{Nothing,AbstractMatrix{T}}=nothing,
uy::Union{Nothing,AbstractMatrix{T}}=nothing,
) where {T<:Real,S<:AbstractStateModel,O<:AbstractObservationModel}
_reject_unsupported_dependence(slds)
lds1 = slds.LDSs[1]
latent_dim = lds1.latent_dim
obs_dim = lds1.obs_dim
Expand Down Expand Up @@ -110,6 +111,7 @@ function Random.rand(
ux::Union{Nothing,AbstractVector{<:AbstractMatrix{T}}}=nothing,
uy::Union{Nothing,AbstractVector{<:AbstractMatrix{T}}}=nothing,
) where {T<:Real,S<:AbstractStateModel,O<:AbstractObservationModel}
_reject_unsupported_dependence(slds)
lds1 = slds.LDSs[1]
latent_dim = lds1.latent_dim
obs_dim = lds1.obs_dim
Expand Down Expand Up @@ -895,9 +897,11 @@ function _slds_trial_elbo(
trial_elbo = zero(T)
x_smooth_trial = fs.x_smooth

# Per-regime log-density scratch, built once rather than per regime.
ll = view(slds_ws.ll_tmp, 1:Tsteps)::AbstractVector{T}

# E_q[log p(y, x | z)], plug-in at the posterior mean, weighted by γ.
for k in 1:K
ll = view(slds_ws.ll_tmp, 1:Tsteps)::AbstractVector{T}
joint_loglikelihood!(
ll,
slds_ws,
Expand Down Expand Up @@ -1062,6 +1066,7 @@ function elbo(
uy=nothing,
rng::AbstractRNG=Random.default_rng(),
) where {T<:Real,S<:AbstractStateModel,O<:AbstractObservationModel}
_reject_unsupported_dependence(slds)
# `Data` centralizes shape validation and canonicalizes the three
# observation/input forms (regime dims are uniform, so validating against
# LDSs[1] covers all regimes). Absent ux/uy become zero-row matrices.
Expand Down Expand Up @@ -1284,6 +1289,7 @@ function fit!(
progress::Bool=true,
rng::AbstractRNG=Random.default_rng(),
) where {T<:Real,S<:AbstractStateModel,O<:AbstractObservationModel}
_reject_unsupported_dependence(slds)
#=
`Data` centralizes shape validation and canonicalizes the three
observation/input forms (regime dims are uniform, so validating against
Expand Down
Loading
Loading