Kalman filter and smoother
The Kalman filter and smoother included in this package use symmetric matrices (via LinearAlgebra
) and the Joseph's stabilised form. This is particularly beneficial for the stability and speed of estimation algorithms (e.g., the EM algorithm in Shumway and Stoffer, 1982), and to handle high-dimensional forecasting problems.
Types
The Kalman filter and smoother are controlled by two custom types: KalmanSettings
and KalmanStatus
. KalmanSettings
contains the data, model parameters and a few auxiliary variables useful to speed-up the filtering and smoothing routines. KalmanStatus
is an abstract supertype denoting a container for the filter and low-level smoother output. This is specified into the following three structures:
OnlineKalmanStatus
: lightweight and specialised for high-dimensional online filtering problems;DynamicKalmanStatus
: specialised for filtering and smoothing problems in which the total number of observed time periods can be dynamically changed;SizedKalmanStatus
: specialised for filtering and smoothing problems in which the total number of observed time periods is fixed to $T$.
Kalman filter and smoother input
KalmanSettings
can be constructed field by field or through a series of constructors that require a significantly smaller amount of variables.
MessyTimeSeries.KalmanSettings
— TypeKalmanSettings(...)
Define an immutable structure that includes all the Kalman filter and smoother inputs.
Model
The state space model used below is,
$Y_{t} = B*X_{t} + e_{t}$
$X_{t} = C*X_{t-1} + D*U_{t}$
where $e_{t} \sim N(0_{nx1}, R)$ and $U_{t} \sim N(0_{mx1}, Q)$.
Fields
Y
: Observed measurements (nxT
)B
: Measurement equations' coefficientsR
: Covariance matrix of the measurement equations' error termsC
: Transition equations' coefficientsD
: Transition equations' coefficients associated to the error termsQ
: Covariance matrix of the transition equations' error termsX0
: Mean vector for the states at time $t=0$P0
: Covariance matrix for the states at time $t=0$DQD
: Covariance matrix of $D*U_{t}$ (i.e., $D*Q*D'$)m
: Number of latent statescompute_loglik
: Boolean (true
for computing the loglikelihood in the Kalman filter)store_history
: Boolean (true
to store the history of the filter and smoother)
MessyTimeSeries.KalmanSettings
— MethodKalmanSettings(Y::Union{FloatMatrix, JMatrix{Float64}}, B::FloatMatrix, R::Union{UniformScaling{Float64}, SymMatrix}, C::FloatMatrix, Q::SymMatrix; kwargs...)
KalmanSettings
constructor.
Arguments
Y
: Observed measurements (nxT
)B
: Measurement equations' coefficientsR
: Covariance matrix of the measurement equations' error termsC
: Transition equations' coefficientsQ
: Covariance matrix of the transition equations' error terms
Keyword arguments
compute_loglik
: Boolean (true
for computing the loglikelihood in the Kalman filter - default:true
)store_history
: Boolean (true
to store the history of the filter and smoother - default:true
)
Notes
This particular constructor sets D
to be an identity matrix, X0
to be a vector of zeros and computes P0
via solve_discrete_lyapunov(C, Q)
.
MessyTimeSeries.KalmanSettings
— MethodKalmanSettings(Y::Union{FloatMatrix, JMatrix{Float64}}, B::FloatMatrix, R::Union{UniformScaling{Float64}, SymMatrix}, C::FloatMatrix, D::FloatMatrix, Q::SymMatrix; kwargs...)
KalmanSettings
constructor.
Arguments
Y
: Observed measurements (nxT
)B
: Measurement equations' coefficientsR
: Covariance matrix of the measurement equations' error termsC
: Transition equations' coefficientsD
: Transition equations' coefficients associated to the error termsQ
: Covariance matrix of the transition equations' error terms
Keyword arguments
compute_loglik
: Boolean (true
for computing the loglikelihood in the Kalman filter - default:true
)store_history
: Boolean (true
to store the history of the filter and smoother - default:true
)
Notes
This particular constructor sets X0
to be a vector of zeros and computes P0
via solve_discrete_lyapunov(C, Q)
.
MessyTimeSeries.KalmanSettings
— MethodKalmanSettings(Y::Union{FloatMatrix, JMatrix{Float64}}, B::FloatMatrix, R::Union{UniformScaling{Float64}, SymMatrix}, C::FloatMatrix, D::FloatMatrix, Q::SymMatrix, X0::FloatVector, P0::SymMatrix; kwargs...)
KalmanSettings
constructor.
Arguments
Y
: Observed measurements (nxT
)B
: Measurement equations' coefficientsR
: Covariance matrix of the measurement equations' error termsC
: Transition equations' coefficientsD
: Transition equations' coefficients associated to the error termsQ
: Covariance matrix of the transition equations' error termsX0
: Mean vector for the states at time $t=0$P0
: Covariance matrix for the states at time $t=0$
Keyword arguments
compute_loglik
: Boolean (true
for computing the loglikelihood in the Kalman filter - default:true
)store_history
: Boolean (true
to store the history of the filter and smoother - default:true
)
Kalman filter and low-level smoother output
Each subtype of KalmanStatus
can be either initialised manually or through a simplified method. The low-level smoother output are convienent buffer arrays used for low-level matrix operations.
OnlineKalmanStatus
MessyTimeSeries.OnlineKalmanStatus
— TypeOnlineKalmanStatus(...)
Define a mutable structure to handle minimal Kalman filter output.
The Kalman filter history is not stored. This makes it ideal for online filtering problems. However, it is incompatible with the kalman smoother implementation.
Arguments
t
: Current point in timeloglik
: LoglikelihoodX_prior
: Latest a-priori XX_post
: Latest a-posteriori XP_prior
: Latest a-priori PP_post
: Latest a-posteriori Pe
: Forecast errorinv_F
: Inverse of the forecast error covarianceL
: Convenient shortcut for the filter and smootherbuffer_J1
,buffer_J2
,buffer_m_m
,buffer_m_n_obs
: Buffer arrays used for low-level matrix operations
MessyTimeSeries.OnlineKalmanStatus
— MethodOnlineKalmanStatus()
Return an initialised OnlineKalmanStatus
.
DynamicKalmanStatus
MessyTimeSeries.DynamicKalmanStatus
— TypeDynamicKalmanStatus(...)
Define a mutable structure to handle any type of Kalman filter output.
The Kalman filter history is stored when store_history
is set to true in the filter settings.
Arguments
t
: Current point in timeloglik
: LoglikelihoodX_prior
: Latest a-priori XX_post
: Latest a-posteriori XP_prior
: Latest a-priori PP_post
: Latest a-posteriori Pe
: Forecast errorinv_F
: Inverse of the forecast error covarianceL
: Convenient shortcut for the filter and smootherbuffer_J1
,buffer_J2
,buffer_m_m
,buffer_m_n_obs
: Buffer arrays used for low-level matrix operationshistory_X_prior
: History of a-priori Xhistory_X_post
: History of a-posteriori Xhistory_P_prior
: History of a-priori Phistory_P_post
: History of a-posteriori Phistory_e
: History of the forecast errorhistory_inv_F
: History of the inverse of the forecast error covariancehistory_L
: History of the shortcut L
MessyTimeSeries.DynamicKalmanStatus
— MethodDynamicKalmanStatus()
Return an initialised DynamicKalmanStatus
.
SizedKalmanStatus
MessyTimeSeries.SizedKalmanStatus
— TypeSizedKalmanStatus(...)
Define an immutable structure that always store the filter history up to time $T$.
Arguments
online_status
:OnlineKalmanStatus
structhistory_length
: History lengthhistory_X_prior
: History of a-priori Xhistory_X_post
: History of a-posteriori Xhistory_P_prior
: History of a-priori Phistory_P_post
: History of a-posteriori Phistory_e
: History of the forecast errorhistory_inv_F
: History of the inverse of the forecast error covariancehistory_L
: History of the shortcut L
Missing docstring for SizedKalmanStatus(T::Int64)
. Check Documenter's build log for details.
Functions
Kalman filter
The a-priori prediction and a-posteriori update can be computed for a single point in time via kfilter!
or up to $T$ with kfilter_full_sample
and kfilter_full_sample!
.
MessyTimeSeries.kfilter!
— Functionkfilter!(settings::KalmanSettings, status::KalmanStatus)
Kalman filter: a-priori prediction and a-posteriori update.
Arguments
settings
: KalmanSettings structstatus
: KalmanStatus struct
MessyTimeSeries.kfilter_full_sample
— Functionkfilter_full_sample(settings::KalmanSettings, status::KalmanStatus=DynamicKalmanStatus())
Run Kalman filter for $t=1, \ldots, T$ and return status
.
MessyTimeSeries.kfilter_full_sample!
— Functionkfilter_full_sample!(settings::KalmanSettings, status::SizedKalmanStatus)
Run Kalman filter from t=1
to history_length
and update status
in-place.
MessyTimeSeries.kforecast
— Functionkforecast(settings::KalmanSettings, X::Union{FloatVector, Nothing}, h::Int64)
Forecast X
up to h
steps ahead.
Arguments
settings
: KalmanSettings structX
: State vectorh
: Forecast horizonkforecast(settings::KalmanSettings, X::Union{FloatVector, Nothing}, P::Union{SymMatrix, Nothing}, h::Int64)
Forecast X
and P
up to h
steps ahead.
Arguments
settings
: KalmanSettings structX
: State vectorP
: Covariance matrix of the statesh
: Forecast horizon
Kalman smoother
MessyTimeSeries.ksmoother
— Functionksmoother(settings::KalmanSettings, status::KalmanStatus, t_stop::Int64=1)
Kalman smoother: RTS smoother from the last evaluated time period in status
up to $t==0$.
The smoother is implemented following the approach proposed in Durbin and Koopman (2012).
Arguments
settings
: KalmanSettings structstatus
: KalmanStatus structt_stop
: Optional argument that can be used to define the last smoothing period (default: 1)
Index
MessyTimeSeries.DynamicKalmanStatus
MessyTimeSeries.DynamicKalmanStatus
MessyTimeSeries.KalmanSettings
MessyTimeSeries.KalmanSettings
MessyTimeSeries.KalmanSettings
MessyTimeSeries.KalmanSettings
MessyTimeSeries.OnlineKalmanStatus
MessyTimeSeries.OnlineKalmanStatus
MessyTimeSeries.SizedKalmanStatus
MessyTimeSeries.kfilter!
MessyTimeSeries.kfilter_full_sample
MessyTimeSeries.kfilter_full_sample!
MessyTimeSeries.kforecast
MessyTimeSeries.ksmoother