Learn R Programming

FKF.SP (version 0.3.1)

fks.SP: Fast Kalman Smoother through Sequential Processing

Description

The Kalman smoother is a backwards algorithm that is run after the Kalman filter that allows the user to refine estimates of previous states to produce "smoothed" estimates of state variables. This function performs the "Kalman smoother" algorithm using sequential processing, an approach that can substantially improve processing time over the traditional Kalman filtering/smoothing algorithms. The primary application of Kalman smoothing is in conjunction with expectation-maximization to estimate the parameters of a state space model. This function is called after running fkf.SP. fks.SP wraps the C-function fks_SP which relies upon the linear algebra subroutines of BLAS (Basic Linear Algebra Subprograms).

Usage

fks.SP(FKF.SP_obj)

Value

An S3-object of class fks.SP, which is a list with the following elements:

ahattA \(m \times n\)-matrix containing the smoothed state variables, i.e. ahatt[,t] = a_t|na(t|n)
VtA \(m \times m \times n\)-array containing the variances of ahatt, i.e. Vt[,,t] = P_t|nP(t|n)

Arguments

FKF.SP_obj

An S3-object of class "fkf.SP", returned by fkf.SP when using the argument verbose = TRUE.

Details

fks.SP is typically called after the fkf.SP function to calculate "smoothed" estimates of state variables and their corresponding variances. Smoothed estimates are used when utilizing expectation-maximization (EM) to efficiently estimate the parameters of a state space model.

Sequential Processing Kalman smoother solution:

The fks.SP function uses the solution to the Kalman smoother through sequential processing provided in the textbook of Durbin and Koopman (2001).

Given a state space model has been filtered through the sequential processing Kalman filter algorithm described in fkf.SP, the smoother can be reformulated for the univariate series:

y_t'=(y_(1,1),y_(1,2),,y_(1,p_1),y_(2,1),,y_(t,p_t))y_t'=(y_(1,1),y_(1,2),...y_(1,p_1),y_(2,1),...,y_(t,p_t))

The sequential processing Kalman smoother approach iterates backwards through both observations and time, i.e.: i=p_t, , 1i=p[t], ... ,1 and t=n,,1t=n, ... ,1, where p_tp_t is the number of observations at time tt and nn is the total number of observations.

The initialisations are:

r_(n,p_n) = 0r(n,p_n)=0 N_(n,p_n)=0N(n,p_n)=0

Then, rr and NN are recursively calculated through:

L_t,i = I_m - K_t,i Z_t,iL(t,i) = I_m - K(t,i) Z(t,i)

r_(t,i-1) = Z_t,i' F_t,i^-1 v_t,i + L_t,i' r_t,ir(t,i-1) = Z(t,i)' F(t,i)^-1 v(t,i) + L(t,i)' r(t,i)

N_t,i-1 = Z_t,i' F_t,i^-1 Z_t,i + L_t,i' N_t,i L_t,iN(t,i-1) = Z(t,i)' F(t,i)^-1 Z(t,i) + L(t,i)' N(t,i) L(t,i)

r_t-1,p_t = T_t-1' r_t,0r(t-1,p_t) = T(t-1)' r(t,0)

N_t-1,p_t = T_t-1' N_t,0 T_t-1N(t-1,p_t) = T(t-1)' N(t,0) T(t-1)

for i=p_t,,1i=p_t, ..., 1 and t=n,,1t=n, ..., 1

The equations for r_t-1,p_tr(t-1,p_t) and N_t-1,p_tN(t-1,p_t) do not apply for t=1t=1

Under this formulation, the values for r_t,0r(t,0) and N_t,0N(t,0) are the same as the values for the smoothing quantities of r_t-1r(t-1) and N_t-1N(t-1) of the standard smoothing equations, respectively.

The standard smoothing equations for a_tahat(t) and V_tV(t) are used:

a_t = a_t + P_t r_t-1ahat(t) = a(t) + P(t) r(t-1)

V_t = P_t - P_t N_t-1 P_tV(t) = P(t) - P(t) N(t-1) P(t)

Where:

a_t=a_t,1a(t) = a(t,1)

P_t = P_t,1P(t) = P(t,1)

In the equations above, r_t,ir(t,i) is an m 1m X 1 vector, I_mI_m is an m mm X m identity matrix, K_t,iK(t,i) is an m 1m X 1 column vector, Z_t,iZ(t,i) is a 1 m1 X m row vector, and both F_t,i^-1F(t,i)^-1 and v_t,iv(t,i) are scalars. The reduced dimensionality of many of the variables in this formulation compared to traditional Kalman smoothing can result in increased computational efficiency.

Finally, in the formulation described above, a_ta(t) and P_tP(t) correspond to the values of att and ptt returned from the fkf.SP function, respectively.

References

Aspinall, T. W., Harris, G., Gepp, A., Kelly, S., Southam, C., and Vanstone, B. (2022). The Estimation of Commodity Pricing Models with Applications in Capital Investments. Available Online.

Durbin, James, and Siem Jan Koopman (2001). Time series analysis by state space methods. Oxford university press.

Examples

Run this code
### Perform Kalman Filtering and Smoothing through sequential processing:
#Nile's annual flow:
yt <- Nile

# Incomplete Nile Data - two NA's are present:
yt[c(3, 10)] <- NA

dt <- ct <- matrix(0)
Zt <- Tt <- matrix(1)
a0 <- yt[1]   # Estimation of the first year flow
P0 <- matrix(100)       # Variance of 'a0'

# Parameter estimation - maximum likelihood estimation:
# Unknown parameters initial estimates:
GGt <- HHt <- var(yt, na.rm = TRUE) * .5
HHt = matrix(HHt)
GGt = matrix(GGt)
yt = rbind(yt)
# Filter through the Kalman filter - sequential processing:
Nile_filtered <- fkf.SP(HHt = matrix(HHt), GGt = matrix(GGt), a0 = a0, P0 = P0, dt = dt, ct = ct,
                  Zt = Zt, Tt = Tt, yt = rbind(yt), verbose = TRUE)
# Smooth filtered values through the Kalman smoother - sequential processing:
Smoothed_Estimates <- fks.SP(Nile_filtered)

Run the code above in your browser using DataLab