Skip to content

Commit

Permalink
improved linear model type structure
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesknipp committed Oct 8, 2024
1 parent d13c80c commit 856cebb
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions examples/particle-mcmc/simple-filters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,42 +26,54 @@ end

## LINEAR GAUSSIAN STATE SPACE MODEL #######################################################

struct LinearGaussianLatentDynamics{T<:Real} <: LatentDynamics{Vector{T}}
struct LinearGaussianLatentDynamics{
T<:Real,AT<:AbstractMatrix{T},QT<:AbstractPDMat{T},ΣT<:AbstractPDMat{T}
} <: LatentDynamics{Vector{T}}
"""
Latent dynamics for a linear Gaussian state space model.
The model is defined by the following equations:
x[t] = Ax[t-1] + b + ε[t], ε[t] ∼ N(0, Q)
"""
A::Matrix{T}
A::AT
b::Vector{T}
Q::PDMat{T,Matrix{T}}
Q::QT

init::Gaussian{Vector{T},PDMat{T,Matrix{T}}}
init::Gaussian{Vector{T},ΣT}
end

# Convert covariance matrices to PDMats to avoid recomputing Cholesky factorizations
function LinearGaussianLatentDynamics(A::Matrix, b::Vector, Q::Matrix, init::Gaussian)
return LinearGaussianLatentDynamics(A, b, PSDMat(Q), init)
function LinearGaussianLatentDynamics(A::AbstractMatrix, b::Vector, Q::AbstractMatrix, init::Gaussian)
return LinearGaussianLatentDynamics(A, b, PDMat(Q), init)
end

function LinearGaussianLatentDynamics(A::AbstractMatrix, b::Vector, Q::AbstractVector, init::Gaussian)
return LinearGaussianLatentDynamics(A, b, PDiagMat(Q), init)
end

function LinearGaussianLatentDynamics(
A::Matrix{T}, Q::Matrix{T}, init::Gaussian
A::AbstractMatrix{T}, Q::AbstractArray{T}, init::Gaussian
) where {T<:Real}
return LinearGaussianLatentDynamics(A, zeros(T, size(A, 1)), PSDMat(Q), init)
return LinearGaussianLatentDynamics(A, zeros(T, size(A, 1)), Q, init)
end

struct LinearGaussianObservationProcess{T<:Real} <: ObservationProcess{Vector{T}}
struct LinearGaussianObservationProcess{
T<:Real,HT<:AbstractMatrix{T},RT<:AbstractPDMat{T}
} <: ObservationProcess{Vector{T}}
"""
Observation process for a linear Gaussian state space model.
The model is defined by the following equation:
y[t] = Hx[t] + η[t], η[t] ∼ N(0, R)
"""
H::Matrix{T}
R::PDMat{T,Matrix{T}}
H::HT
R::RT
end

function LinearGaussianObservationProcess(H::AbstractMatrix, R::AbstractMatrix)
return LinearGaussianObservationProcess(H, PDMat(R))
end

function LinearGaussianObservationProcess(H::Matrix, R::Matrix)
return LinearGaussianObservationProcess(H, PSDMat(R))
function LinearGaussianObservationProcess(H::AbstractMatrix, R::AbstractVector)
return LinearGaussianObservationProcess(H, PDiagMat(R))
end

function SSMProblems.distribution(
Expand Down

0 comments on commit 856cebb

Please sign in to comment.