Swiching Linear Dynamical Systems
StateSpaceDynamics.SwitchingLinearDynamicalSystem
— TypeSwitching Linear Dynamical System
Struct to Encode a Hidden Markov model that switches among K distinct LinearDyanmicalSystems
Fields
A::V
: Transition matrix for mode switching.B::VL
: Vector of Linear Dynamical System models.πₖ::V
: Initial state distribution.K::Int
: Number of modes.
Base.rand
— MethodRandom.rand(rng, slds, T)
Generate synthetic data with switching LDS models
#Arguments
rng:AbstractRNG
: Random number generatorslds::SwitchingLinearDynamicalSystem
: The switching LDS modelT::Int
: Number of time steps to sample
Returns
Tuple{Array,Array, Array}
: Latent states (x), observations (y), and mode sequences (z).
StateSpaceDynamics.fit!
— Methodfit!(slds::SwitchingLinearDynamicalSystem, y::Matrix{T};
max_iter::Int=1000,
tol::Real=1e-12,
) where {T<:Real}
Fit a Switching Linear Dynamical System using the variational Expectation-Maximization (EM) algorithm with Kalman smoothing.
Arguments
slds::SwitchingLinearDynamicalSystem
: The Switching Linear Dynamical System to be fitted.y::Matrix{T}
: Observed data, size (obsdim, Tsteps).
Keyword Arguments
max_iter::Int=1000
: Maximum number of EM iterations.tol::Real=1e-12
: Convergence tolerance for log-likelihood change.
Returns
mls::Vector{T}
: Vector of log-likelihood values for each iteration.param_diff::Vector{T}
: Vector of parameter differences over each iteration.FB::ForwardBackward
: ForwardBackward structFS::FilterSmooth
: FilterSmooth struct
StateSpaceDynamics.hmm_elbo
— Methodhmm_elbo(model::AbstractHMM, FB::ForwardBackward; ϵ::Float64=1e-10)
Compute the evidence based lower bound (ELBO) from the discrete state model.
StateSpaceDynamics.initialize_slds
— Methodinitialize_slds(;K::Int=2, d::Int=2, p::Int=10, self_bias::Float64=5.0, seed::Int=42)
Initialize a Switching Linear Dynamical System with random parameters.
StateSpaceDynamics.mstep!
— Methodmstep!(slds::AbstractHMM, FS::Vector{FilterSmooth{T}}, y::AbstractMatrix{T}, FB::ForwardBackward) where {T<:Real}
Function to carry out the M-step in Expectation-Maximization algorithm for SLDS
StateSpaceDynamics.variational_expectation!
— Methodvariational_expectation!(model::SwitchingLinearDynamicalSystem, y, FB, FS) -> Float64
Compute the variational expectation (Evidence Lower Bound, ELBO) for a Switching Linear Dynamical System by executing the following operations:
Extract Responsibilities: Retrieves the responsibilities (
γ
) from the forward-backward object and computes their exponentials (hs
).Parallel Smoothing and Sufficient Statistics Calculation: For each regime
k
from1
tomodel.K
, the function:- Performs smoothing using the
smooth
function to obtain smoothed states (x_smooth
), covariances (p_smooth
), inverse off-diagonal terms, and total entropy. - Computes sufficient statistics (
E_z
,E_zz
,E_zz_prev
) from the smoothed estimates. - Calculates the ELBO contribution for the current regime and accumulates it into
ml_total
.
- Performs smoothing using the
Update Variational Distributions:
- Computes the variational distributions (
qs
) from the smoothed states, which are stored as log-likelihoods inFB
. - Executes the forward and backward passes to update the responsibilities (
γ
) based on the newqs
. - Recalculates the responsibilities (
γ
) to reflect the updated variational distributions.
- Computes the variational distributions (
Return ELBO: Returns the accumulated ELBO (
ml_total
), which quantifies the quality of the variational approximation.
StateSpaceDynamics.variational_qs!
— Methodvariational_qs!(model::AbstractVector{<:GaussianObservationModel{T, <:AbstractMatrix{T}}}, FB::ForwardBackward, y::AbstractMatrix{T}, FS::Vector{FilterSmooth{T}}) where {T<:Real}
Compute the variational distributions (qs
) and update the log-likelihoods for a set of Gaussian observation models within a Forward-Backward framework.