Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
4a43ad8
add jitter to diagional of init_yy
harrisonritz Jul 20, 2026
0bf5a3a
add jitter to all symmetric dyn
harrisonritz Jul 20, 2026
fabf6da
add ugly priors to A/pi
harrisonritz Jul 20, 2026
9ad9f2f
Merge branch 'dev' into CCN2026
harrisonritz Jul 21, 2026
519eb7d
Add infer_γ: post-fit SLDS discrete-responsibility inference (#159)
harrisonritz Jul 22, 2026
eca88b6
SLDS input addition (#158) (#160)
harrisonritz Jul 22, 2026
aff9972
add inputs to infer_\gamma
harrisonritz Jul 22, 2026
bcfd4c7
Rename infer_γ to posterior and update related tests
harrisonritz Jul 22, 2026
5061347
Refactor posterior function to return per-trial responsibility matric…
harrisonritz Jul 22, 2026
5163aa4
Refactor post-fit inference documentation for clarity and conciseness
harrisonritz Jul 22, 2026
1b51ad5
Add small constant to diagonal of sufficient statistics matrices to i…
harrisonritz Jul 22, 2026
dcc1d86
Remove small constant addition to diagonal of sufficient statistics m…
harrisonritz Jul 22, 2026
8c0d058
Remove hacky prior
harrisonritz Aug 13, 2026
3fb7c92
Update sufficient_statistics.jl
harrisonritz Aug 13, 2026
73bd17d
Update sufficient_statistics.jl
harrisonritz Aug 13, 2026
535a163
Fold `posterior` into `smooth`/`loglikelihood`, add `smoothing_iters`…
harrisonritz Aug 22, 2026
9dd52b0
Fix review findings in the SLDS smooth/ELBO path
rsenne Aug 24, 2026
57b9628
Add `num_samples` to the SLDS Monte-Carlo E-step
rsenne Aug 24, 2026
04f5bc0
Text refresh, add ability to use multiple discrete samples
rsenne Aug 24, 2026
ec768ed
Merge branch 'dev' into CCN2026
rsenne Aug 24, 2026
e5de00d
Add observatiuons alias
rsenne Aug 24, 2026
1083bc3
add trace correction
rsenne Aug 25, 2026
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
6 changes: 4 additions & 2 deletions docs/examples/SLDS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,10 @@ fb_storage = StateSpaceDynamics._make_slds_fb_storage(dl, seq_ends)
slds_ws = StateSpaceDynamics.SLDSSmoothWorkspace(Float64, learned_model, T)

# The warm-start smooth draws the first posterior sample into `x_samples`; the
# E-step then fills the mode posterior from it and re-smooths.
x_samples = [Matrix{Float64}(undef, ld.latent_dim, T)]
# E-step then fills the mode posterior from it and re-smooths. The buffer is
# `latent_dim x T x num_samples` -- one draw here; more would let the discrete
# update average over them.
x_samples = [Array{Float64,3}(undef, ld.latent_dim, T, 1)]
w_uniform = fill(1.0 / K, K, T)
StateSpaceDynamics.smooth!(
learned_model, tfs[1], y, w_uniform; ws=slds_ws, x_sample=x_samples[1], rng=rng
Expand Down
2 changes: 1 addition & 1 deletion docs/src/LinearDynamicalSystems.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ Given the latent structure of state-space models, we must rely on either the Exp
These issues affect **parameter interpretability** but not **predictive performance**; be cautious when interpreting individual entries of ``A``, ``C``, or ``Q``.

```@docs
fit!(lds::LinearDynamicalSystem{T,S,O}, y::Union{AbstractMatrix{T},AbstractArray{T,3},AbstractVector{<:AbstractMatrix{T}}}; max_iter::Int=100, tol::Float64=1e-6, progress::Bool=true) where {T<:Real,S<:GaussianStateModel{T},O<:GaussianObservationModel{T}}
fit!(lds::LinearDynamicalSystem{T,S,O}, y::StateSpaceDynamics.Observations{T}; max_iter::Int=100, tol::Float64=1e-6, progress::Bool=true) where {T<:Real,S<:GaussianStateModel{T},O<:GaussianObservationModel{T}}
```

## Inverse-Wishart Priors on Covariances (MAP)
Expand Down
27 changes: 25 additions & 2 deletions docs/src/SLDS.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,32 @@ The sampling process follows the generative model:
`StateSpaceDynamics.jl` implements a **Variational Laplace Expectation-Maximization (vLEM)** algorithm for parameter estimation in SLDS. This approach efficiently handles the challenging interaction between discrete and continuous latent variables through a structured variational approximation.

```@docs
fit!(slds::SLDS{T,S,O}, y::Union{AbstractMatrix{T},AbstractArray{T,3},AbstractVector{<:AbstractMatrix{T}}}; max_iter::Int=50, progress::Bool=true) where {T<:Real,S<:AbstractStateModel,O<:AbstractObservationModel}
fit!(slds::SLDS{T,S,O}, y::StateSpaceDynamics.Observations{T}; max_iter::Int=50, progress::Bool=true) where {T<:Real,S<:AbstractStateModel,O<:AbstractObservationModel}
```

Each E-step runs `smoothing_iters` discrete↔continuous updates before the M-step.
The discrete update averages over `num_samples` draws from ``q(x)``. Increasing
`num_samples` reduces Monte-Carlo noise; increasing `smoothing_iters` gives the
posterior more time to converge but does not average away that noise.

## Post-fit inference

[`smooth`](@ref) infers the continuous states, discrete-state responsibilities,
and ELBO with the fitted parameters held fixed. By default it runs one
forward-backward/smoother alternation. Unlike `fit!`, its discrete update uses a
second-order expansion about the smoothed mean, so repeated calls are deterministic.

The correction is ``\tfrac12 \mathrm{tr}(H\Sigma)``. It is exact for Gaussian
emissions, making the ELBO monotone in `smoothing_iters` up to roundoff. For
Poisson emissions it is approximate, so small non-monotone changes may remain.

```@docs; canonical = false
smooth(slds::SLDS{T,S,O}, y::StateSpaceDynamics.Observations{T}) where {T<:Real,S<:AbstractStateModel,O<:AbstractObservationModel}
```

[`loglikelihood`](@ref) and [`elbo`](@ref) return the same ELBO because the exact
marginal likelihood would require summing over all ``K^T`` regime sequences.

## The vLEM Algorithm

The vLEM algorithm maximizes the **Evidence Lower Bound (ELBO)** instead of the intractable marginal likelihood. The key insight is to use a structured variational approximation that factorizes as:
Expand Down Expand Up @@ -170,4 +193,4 @@ For theoretical foundations and algorithmic details:
- **"A general recurrent state space framework for modeling neural dynamics during decision-making"** by **David Zoltowski, Jonathon Pillow, and Scott Linderman** (2020)
- **"Variational Learning for Switching State-Space Models"** by **Zoubin Ghahramani and Geoffrey Hinton** (1998)
- **"Probabilistic Machine Learning: Advanced Topics, Chapter 29"** by **Kevin Murphy**
- **"A Unifying Review of Linear Gaussian Models"** by **Sam Roweis and Zoubin Ghahramani**
- **"A Unifying Review of Linear Gaussian Models"** by **Sam Roweis and Zoubin Ghahramani**
8 changes: 4 additions & 4 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ Random.rand(rng::AbstractRNG, ppca::ProbabilisticPCA, n::Int)

```@docs; canonical = false
smooth
fit!(lds::LinearDynamicalSystem{T,S,O}, y::Union{AbstractMatrix{T},AbstractArray{T,3},AbstractVector{<:AbstractMatrix{T}}}; max_iter::Int=100, tol::Float64=1e-6, progress::Bool=true) where {T<:Real,S<:GaussianStateModel{T},O<:GaussianObservationModel{T}}
fit!(slds::SLDS{T,S,O}, y::Union{AbstractMatrix{T},AbstractArray{T,3},AbstractVector{<:AbstractMatrix{T}}}; max_iter::Int=50, progress::Bool=true) where {T<:Real,S<:AbstractStateModel,O<:AbstractObservationModel}
fit!(lds::LinearDynamicalSystem{T,S,O}, y::StateSpaceDynamics.Observations{T}; max_iter::Int=100, tol::Float64=1e-6, progress::Bool=true) where {T<:Real,S<:GaussianStateModel{T},O<:GaussianObservationModel{T}}
fit!(slds::SLDS{T,S,O}, y::StateSpaceDynamics.Observations{T}; max_iter::Int=50, progress::Bool=true) where {T<:Real,S<:AbstractStateModel,O<:AbstractObservationModel}
```

```@docs
fit!(plds::LinearDynamicalSystem{T,S,O}, y::Union{AbstractMatrix{T},AbstractArray{T,3},AbstractVector{<:AbstractMatrix{T}}}) where {T<:Real,S<:GaussianStateModel{T},O<:PoissonObservationModel{T}}
fit!(plds::LinearDynamicalSystem{T,S,O}, y::StateSpaceDynamics.Observations{T}) where {T<:Real,S<:GaussianStateModel{T},O<:PoissonObservationModel{T}}
fit!(ppca::ProbabilisticPCA, X::AbstractMatrix{T}, max_iters::Int=100, tol::Float64=1e-6) where {T<:Real}
```

Expand All @@ -70,7 +70,7 @@ variants are the workspace-based internals it wraps.

```@docs
elbo
loglikelihood(lds::LinearDynamicalSystem{T,SM,OM}, y::Union{AbstractMatrix{T},AbstractArray{T,3},AbstractVector{<:AbstractMatrix{T}}}) where {T<:Real,SM<:GaussianStateModel{T},OM<:GaussianObservationModel{T}}
loglikelihood(lds::LinearDynamicalSystem{T,SM,OM}, y::StateSpaceDynamics.Observations{T}) where {T<:Real,SM<:GaussianStateModel{T},OM<:GaussianObservationModel{T}}
loglikelihood(plds::LinearDynamicalSystem{T,S,O}, y) where {T<:Real,S<:GaussianStateModel{T},O<:PoissonObservationModel{T}}
loglikelihood(slds::SLDS, y)
loglikelihood(ppca::ProbabilisticPCA, X::AbstractMatrix{T}) where {T<:Real}
Expand Down
17 changes: 4 additions & 13 deletions src/lds/fit_LDS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,7 @@ For a single-trial (matrix) `y`:
For multi-trial `y`: `Vector`s of the above, one entry per trial.
"""
function smooth(
lds::LinearDynamicalSystem{T,S,O},
y::Union{AbstractMatrix{T},AbstractArray{T,3},AbstractVector{<:AbstractMatrix{T}}};
ux=nothing,
uy=nothing,
lds::LinearDynamicalSystem{T,S,O}, y::Observations{T}; ux=nothing, uy=nothing
) where {T<:Real,S<:GaussianStateModel{T},O<:GaussianObservationModel{T}}
data = Data(lds, y; ux=ux, uy=uy)
tfs = _smooth_data(lds, data)
Expand Down Expand Up @@ -768,10 +765,7 @@ objective the M-step optimizes).
Returns a scalar.
"""
function elbo(
lds::LinearDynamicalSystem{T,S,O},
y::Union{AbstractMatrix{T},AbstractArray{T,3},AbstractVector{<:AbstractMatrix{T}}};
ux=nothing,
uy=nothing,
lds::LinearDynamicalSystem{T,S,O}, y::Observations{T}; ux=nothing, uy=nothing
) where {T<:Real,S<:GaussianStateModel{T},O<:GaussianObservationModel{T}}
data = Data(lds, y; ux=ux, uy=uy)
tfs = initialize_FilterSmooth(lds, data.tsteps)::TrialFilterSmooth{T}
Expand Down Expand Up @@ -842,7 +836,7 @@ Returns a `Vector{T}` of ELBO values, one per iteration.
"""
function fit!(
lds::LinearDynamicalSystem{T,S,O},
y::Union{AbstractMatrix{T},AbstractArray{T,3},AbstractVector{<:AbstractMatrix{T}}};
y::Observations{T};
max_iter::Int=100,
tol::Float64=1e-6,
progress::Bool=true,
Expand Down Expand Up @@ -1093,10 +1087,7 @@ Returns the **total** log-likelihood. Divide by `obs_dim * tsteps * ntrials` for
per-observation score that is comparable across configurations.
"""
function StatsAPI.loglikelihood(
lds::LinearDynamicalSystem{T,SM,OM},
y::Union{AbstractMatrix{T},AbstractArray{T,3},AbstractVector{<:AbstractMatrix{T}}};
ux=nothing,
uy=nothing,
lds::LinearDynamicalSystem{T,SM,OM}, y::Observations{T}; ux=nothing, uy=nothing
) where {T<:Real,SM<:GaussianStateModel{T},OM<:GaussianObservationModel{T}}
data = Data(lds, y; ux=ux, uy=uy)

Expand Down
9 changes: 3 additions & 6 deletions src/lds/fit_PLDS.jl
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ Returns a scalar.
"""
function elbo(
plds::LinearDynamicalSystem{T,S,O},
y::Union{AbstractMatrix{T},AbstractArray{T,3},AbstractVector{<:AbstractMatrix{T}}};
y::Observations{T};
ux=nothing,
uy=nothing,
newton_max_iter::Int=20,
Expand Down Expand Up @@ -787,10 +787,7 @@ For a single-trial (matrix) `y`:
For multi-trial `y`: `Vector`s of the above, one entry per trial.
"""
function smooth(
plds::LinearDynamicalSystem{T,S,O},
y::Union{AbstractMatrix{T},AbstractArray{T,3},AbstractVector{<:AbstractMatrix{T}}};
ux=nothing,
uy=nothing,
plds::LinearDynamicalSystem{T,S,O}, y::Observations{T}; ux=nothing, uy=nothing
) where {T<:Real,S<:GaussianStateModel{T},O<:PoissonObservationModel{T}}
data = Data(plds, y; ux=ux, uy=uy)
tfs = initialize_FilterSmooth(plds, data.tsteps)::TrialFilterSmooth{T}
Expand Down Expand Up @@ -839,7 +836,7 @@ Fit a Poisson LDS via Laplace-EM.
"""
function fit!(
plds::LinearDynamicalSystem{T,S,O},
y::Union{AbstractMatrix{T},AbstractArray{T,3},AbstractVector{<:AbstractMatrix{T}}};
y::Observations{T};
ux=nothing,
uy=nothing,
max_iter::Int=100,
Expand Down
Loading
Loading