Manual
Introduction
In this package we consider the following state-space model
where
Data structures
StateSpaceModels.StateSpaceDimensions
— TypeStateSpaceDimensions
StateSpaceModel dimensions, following the notation of on the book "Time Series Analysis by State Space Methods" (2012) by J. Durbin and S. J. Koopman.
n
is the number of observationsp
is the dimension of the observation vector $y_t$m
is the dimension of the state vector $\alpha_t$r
is the dimension of the state covariance matrix $Q$
StateSpaceModels.StateSpaceModel
— TypeStateSpaceModel{Typ <: Real}
Following the notation of on the book "Time Series Analysis by State Space Methods" (2012) by J. Durbin and S. J. Koopman.
y
A $n \times p$ matrix containing observationsZ
A $p \times m \times n$ matrixT
A $m \times m$ matrixR
A $m \times r$ matrixd
A $n \times p$ matrixc
A $n \times m$ matrixH
A $p \times p$ matrixQ
A $r \times r$ matrix
StateSpaceModels.SmoothedState
— TypeSmoothedState{T <: Real}
Following the notation of on the book "Time Series Analysis by State Space Methods" (2012) by J. Durbin and S. J. Koopman.
alpha
Expected value of the smoothed state $E(\alpha_t|y_1, \dots , y_n)$V
Error covariance matrix of smoothed state $Var(\alpha_t|y_1, \dots , y_n)$
StateSpaceModels.FilterOutput
— TypeFilterOutput{T <: Real}
Following the notation of on the book "Time Series Analysis by State Space Methods" (2012) by J. Durbin and S. J. Koopman.
a
Predictive state $E(\alpha_t|y_{t-1}, \dots , y_1)$att
Filtered state $E(\alpha_t|y_{t}, \dots , y_1)$v
Prediction error $v_t = y_t − Z_t a_t$P
Covariance matrix of predictive state $P = Var(\alpha_t|y_{t−1}, \dots , y_1)$Ptt
Covariance matrix of filtered state $P = Var(\alpha_t|y_{t}, \dots , y_1)$F
Variance of prediction error $Var(v_{t})$steadystate
Boolean to indicate if steady state was attainedtsteady
Instant when steady state was attained; in case it was not,tsteady = n+1
StateSpaceModels.StateSpace
— TypeStateSpace{T <: Real}
A state-space structure containing the model, filter output, smoother output, covariance matrices, filter type and optimization method.
Predefined models
The package provides constructors for some classic state-space models.
The local level model is defined by
StateSpaceModels.local_level
— Functionlocal_level(y::VecOrMat{Typ}) where Typ <: Real
Build state-space system for a local level model with observations y.
If y
is provided as an Array{Typ, 1}
it will be converted to an Array{Typ, 2}
inside the StateSpaceModel
.
The linear trend model is defined by
StateSpaceModels.linear_trend
— Functionlinear_trend(y::VecOrMat{Typ}) where Typ <: Real
Build state-space system for a linear trend model with observations y.
If y
is provided as an Array{Typ, 1}
it will be converted to an Array{Typ, 2}
inside the StateSpaceModel
.
The structural model is defined by
StateSpaceModels.structural
— Functionstructural(y::VecOrMat{Typ}, s::Int; X::VecOrMat{Typ} = Matrix{Typ}(undef, 0, 0)) where Typ <: Real
Build state-space system for a given structural model with observations y
, seasonality s
, and, optionally, exogenous variables X
.
If y
is provided as an Array{Typ, 1}
it will be converted to an Array{Typ, 2}
inside the StateSpaceModel
. The same will happen to X, if an Array{Typ, 1}
it will be converted to an Array{Typ, 2}
inside the StateSpaceModel
.
A regression can also be defined in terms of state-space model. We consider the simple model $y_t = X_t \beta_t + \varepsilon_t$ where $\varepsilon_{t} \sim \mathcal{N}(0, H_t)$. This can be written in the state-space form by doing $Z_t = X_t, T_t = I, R = 0$ and $Q = 0$
StateSpaceModels.regression
— Functionregression(y::VecOrMat{Typ}, X::VecOrMat{Typ}) where Typ <: Real
Build state-space system for estimating a regression model $y_t = X_t\beta_t + \varepsilon_t$. Once the model is estimated the user can recover the parameter $\hat{\beta}$ by the querying the smoothed states of the model.
Custom models
You can also build your own custom model by passing all or some of the matrices that compose the state-space model.
Currently there is a list of constructors that allow you to define a new model. When building the StateSpaceModel
the parameters to be estimated will be indicated with a NaN
.
StateSpaceModel(y::Matrix{Typ}, Z::Array{Typ, 3}, T::Matrix{Typ}, R::Matrix{Typ}, H::Matrix{Typ}, Q::Matrix{Typ}) where Typ <: Real
StateSpaceModel(y::Matrix{Typ}, Z::Matrix{Typ}, T::Matrix{Typ}, R::Matrix{Typ}, H::Matrix{Typ}, Q::Matrix{Typ}) where Typ <: Real
StateSpaceModel(y::Matrix{Typ}, Z::Array{Typ, 3}, T::Matrix{Typ}, R::Matrix{Typ}) where Typ <: Real
StateSpaceModel(y::Matrix{Typ}, Z::Matrix{Typ}, T::Matrix{Typ}, R::Matrix{Typ}) where Typ <: Real
In case H
and Q
are not provided, they are automatically filled with NaN
and set to be estimated.
Estimation
The model estimation is made using the function statespace(model; filter_type = KalmanFilter, optimization_method = RandomSeedsLBFGS(), verbose = 1)
. It receives as argument the pre-specified StateSpaceModel
object model
. Optionally, the user can define the Kalman filter variant to be used, the optimization method and the verbosity level.
StateSpaceModels.statespace
— Functionstatespace(model; filter_type = KalmanFilter, opt_method = LBFGS(), verbose = 1)
Estimate the pre-specified state-space model. The function will only estimate the entries that are declared NaN
. If there are no NaNs in the model
it will only perform the filter and smoother computations.
Forecasting
Forecasting is conducted with the function forecast
. It receives as argument a StateSpace
object and the number of steps ahead N
.
StateSpaceModels.forecast
— Functionforecast(ss::StateSpace{Typ}, N::Int) where Typ
Obtain the minimum mean square error forecasts N steps ahead. Returns the forecasts and the predictive distributions at each time period.
Simulation
Simulation is made using the function simulate
. It receives as argument a StateSpace
object, the number of steps ahead N
and the number of scenarios to simulate S
.
StateSpaceModels.simulate
— Functionsimulate(ss::StateSpace{Typ}, N::Int, S::Int) where Typ
Simulate S future scenarios up to N steps ahead. Returns a p x N x S matrix where the dimensions represent, respectively, the number of series in the model, the number of steps ahead, and the number of scenarios.
Filters
Kalman Filter
The implementation of the Kalman Filter follows the recursion
where $K_{t} = T P_{t} Z^{\top}_{t} F_{t}^{-1}$. The terms $a_{t+1}$ and $P_{t+1}$ can be simplifed to
In case of missing observation the mean of inovations $v_{t}$ and variance of inovations $F_{t}$ become NaN
and the recursion becomes
StateSpaceModels.kalman_filter
— Functionkalman_filter(model::StateSpaceModel{Typ}; tol::Typ = Typ(1e-5)) where Typ
Kalman filter with big Kappa initialization, i.e., initializing state variances as 1e6.
The implementation of the smoother follows the recursion
where $L_{t} = T - K_{t} Z_{t}$.
In case of missing observation then $r_{t-1}$ and $N_{t-1}$ become
StateSpaceModels.smoother
— Functionsmoother(model::StateSpaceModel{Typ}, kfilter::KalmanFilter{Typ}) where Typ
Smoother for state-space model.
Square Root Kalman Filter
StateSpaceModels.sqrt_kalman_filter
— Functionsqrt_kalman_filter(model::StateSpaceModel{Typ}; tol::Typ = Typ(1e-5)) where Typ
Square-root Kalman filter with big Kappa initialization.
StateSpaceModels.sqrt_smoother
— Functionsqrt_smoother(model::StateSpaceModel, sqrt_filter::SquareRootFilter) where Typ
Square-root smoother for state space model.
StateSpaceModels.filtered_state
— Functionfiltered_state(model::StateSpaceModel{Typ}, sqrt_filter::SquareRootFilter{Typ}) where Typ
Obtain the filtered state estimates and their covariance matrices.
Filter interface
StateSpaceModels has an interface that allows users to define their own versions of the Kalman filter.
StateSpaceModels.AbstractFilter
— TypeAbstractFilter
Abstract type used to implement an interface for generic filters.
StateSpaceModels.AbstractSmoother
— TypeAbstractSmoother
Abstract type used to implement an interface for generic smoothers.
StateSpaceModels.kfas
— Functionkfas(model::StateSpaceModel{T}, filter_type::DataType) where T
Perform Kalman filter and smoother according to the chosen filter_type
.
Every filter must provide its own version of the statespace_loglik
function
StateSpaceModels.statespace_loglik
— Functionstatespace_loglik(psitilde::Vector{T}, model::StateSpaceModel, unknowns::Unknowns,
valid_insts::Vector{Int}, filter_type::DataType) where T
Compute log-likelihood concerning hyperparameter vector psitilde ($\psi$)
Evaluate $\ell(\psi;y_n)= -\frac{np}{2}\log2\pi - \frac{1}{2} \sum_{t=1}^n \log |F_t| - \frac{1}{2} \sum_{t=1}^n v_t^{\top} F_t^{-1} v_t$
Optimization methods
StateSpaceModels has an interface that allows users to define their own optimization methods. It is easily integrated with Optim.jl.
LBFGS
StateSpaceModels.LBFGS
— TypeLBFGS(model::StateSpaceModel{T}, args...; kwargs...)
If an Int
is provided the method will sample random seeds and use them as initial points for Optim LBFGS method. If a Vector{Vector{T}}
is provided it will use them as initial points for Optim LBFGS method.
BFGS
StateSpaceModels.BFGS
— TypeBFGS(model::StateSpaceModel{T}, args...; kwargs...)
If an Int
is provided the method will sample random seeds and use them as initial points for Optim BFGS method. If a Vector{Vector{T}}
is provided it will use them as initial points for Optim BFGS method.
Optimization methods interface
StateSpaceModels.AbstractOptimizationMethod
— TypeAbstractOptimizationMethod{T}
Abstract type used to implement an interface for generic optimization methods.
StateSpaceModels.estimate_statespace
— Functionestimate_statespace(model::StateSpaceModel{T}, filter_type::DataType,
optimization_method::AbstractOptimizationMethod; verbose::Int = 1) where T
Estimate parameters of the StateSpaceModel
according to its filter_type
and optimization_method
.
Diagnostics
After the estimation is completed, diagnostics can be run over the residuals. The implemented diagnostics include the Jarque-Bera normality test, the Ljung-Box independence test, and a homoscedasticity test.
StateSpaceModels.diagnostics
— Functiondiagnostics(ss::StateSpace{T}) where T
Run diagnostics and print results
StateSpaceModels.residuals
— Functionresiduals(ss::StateSpace{T}) where T
Obtain standardized residuals from a state-space model
StateSpaceModels.jarquebera
— Functionjarquebera(e::Matrix{T}) where T
Run Jarque-Bera normality test and return p-values
StateSpaceModels.ljungbox
— Functionljungbox(e::Matrix{T}; maxlag::Int = 20) where T
Run Ljung-Box independence test and return p-values
StateSpaceModels.homoscedast
— Functionhomoscedast(e::Matrix{T}) where T
Run homoscedasticity test and return p-values