Library: | kalman |
See also: | gkalarray gkallag gkalsmoother gkalresiduals |
Quantlet: | gkalfilter | |
Description: | Calculates a filtered time series for a state space model (uni- or multivariate) with time variable system matrices using the Kalman filter. Furthermore, gkalfilter gives the value of the log likelihood function. |
Usage: | {gkalfilOut,loglike} = gkalfilter(Y,mu,Sig,ca,Ta,Ra,da,Za,Ha,l) | |
Input: | ||
Y | N_max x TIME matrix of observed time series, N_max is the maximal number of variables observed at any instant t | |
mu | K x 1 vector, the mean of the initial state | |
Sig | K x K covariance matrix of the initial state | |
ca | K x 1 x TIME array with observations c_t | |
Ta | K x K x TIME array with observations T_t | |
Ra | K x K x TIME array with covariance matrices R_t | |
da | N_max x 1 x TIMES array with observations d_t | |
Za | N_max x K x TIMES array with observations Z_t | |
Ha | N_max x N_max x TIMES array with covariance matrices H_t | |
l | set to 1 if the value of the log likelihood function should be displayed. Otherwise, you can omit this variable. | |
Output: | ||
gkalfilOut | K x (K+1) x (TIME+1) array of state space series a_t~P_t. First entry is mu~Sig for t=0. | |
loglike | value of the log likelihood function evaluated with the results of the filter. |
State equation
alpha_t = c_t + T_t alpha_t-1 + e^s_t
Measurement equation
y_t = d_t + Z_t alpha_t + e^m_t
with
alpha_0 ~ (mu,Sig), e^s_t ~ (0,R_t), e^m_t ~ (0,H_t)
All parameters are known.
We denote with a_t the filtered series of the unobservable state vector alpha_t and with P_t the corresponding covariance matrix. The output of the procedure is an array with a_t and P_t for all t.
gkalfilter treats missing values in the following way:
1. missing values in the measurement matrix Y will be removed by gkalfilter.
2. there must be no missing values in the matrices belonging to the state equation if the corresponding observations in Y are non-missing.
If you are not interested in the value of the log likelihood function, you should omit l. This makes the procedure faster.
library("kalman") library("xplore") ; loads the quantlets from xplore library library("plot") ; loads the quantlets from plot library Y = read("houseprice.dat") mu = #(33,-13,1,1,1) Sig = 0.75*unit(5) ca = gkalarray(Y,#(0.1,0,0,0,0),0,0) T =(#(1.4,1)'|#(-0.4,0)')~0*matrix(2,3)|(0*matrix(3,2)~unit(3)) Ta = gkalarray(Y,T,0,0) Ra = gkalarray(Y,diag(#(0.4,0,0,0,0)),0,0) da = gkalarray(Y,#(0,0,0),0,0) Z = matrix(3)~0*matrix(3,4) IZ = 0*matrix(3,2)~(#(1:3)'|#(4:6)'|#(7:9)') XZ = read("housequality.dat") Za = gkalarray(Y,Z,IZ,XZ) Ha = gkalarray(Y,2*unit(3),0,0) {gkalfilOut,loglike} = gkalfilter(Y,mu,Sig,ca,Ta,Ra,da,Za,Ha) Time =(1:cols(Y)) T = rows(Time) state = reshape(gkalfilOut[1,1,2:(T+1)],#(1,T,1))' state = Time~state state = setmask(state,"line","blue","medium") Y1 = Time~(Y[1,]') setmaskp(Y1,0,8,5) Y2 = Time~(Y[2,]') setmaskp(Y2,0,8,5) Y3 = Time~(Y[3,]') setmaskp(Y3,0,8,5) disp = createdisplay(1,1) show(disp,1,1,state,Y1,Y2,Y3) setgopt(disp,1,1,"title","Kalman filter","xlabel","Time","ylabel","y,a","border",0) setgopt(disp,1,1,"ylabel","y,a","border",0)
The filtered common price component (i.e., the first component of the filtered state vector) is shown as a blue line; the observations of Y are shown as black dots.