Feeds:
Posts

## Stochastic volatility with exogenous variables via Kalman filter

The general form for a univariate state space model is:

$y_t = Z_t \alpha_t + \epsilon_t, \epsilon_t \sim N(0,H_t)$

and

$\alpha_t = T_t \alpha_{t-1} + c_t + \eta_t, \eta_t \sim N(0,Q_t)$

Now according to Sandmann/Koopman, one extends this for $k$ explanatory variables $z_t = (z^1_t,\dots,z^k_t)$ as follows.  We make $\alpha_t$ have $k+1$ components $\alpha_t = [ h_t, \gamma_t]'$.  We append an identity matrix to $T_t$ so $T_t = \diag(\phi,1,\dots,1)$.  Now $H_t = \pi^2/2$ does not change but we append zeros to $Q_t = \diag(\sigma^2_\eta-A^2,0,\dots,0)$.  Next

$c_t = [As_t, 0,\dots,0]'$

$G_t = E(\epsilon_t,\eta_t) = [ Bs_t, 0,\dots,0]'$. Here $s_t = sign(ret_t)$, and $A = 0.7979 \rho \sigma_\eta$ and $B = 1.1061 \rho \sigma_\eta$.

Let us set up this problem so we can use the Kalman filter for quasi-ML estimation of parameters using the R package ‘FKF’.  One has to translate to their notation:

fkf( a0, P0, dt, ct, Tt, Zt, HHt, GGt, yt)

Suppose that we have 3 exogenous variables, then we want to specify Zt with these.  We want dt to be also specified.  With $k=3$ we can set $a0 = [0,0,0,0]'$ and $P0 = I(4)*1e7$.

One has to be a bit careful with the formatting of the input data: the ‘fkf’ package allows time-varying or stationary inputs for several parameters such as Tt, Zt and some others.  These do not expect normal 2d matrices but 3d multidimensional array.  In our case for example we want Zt to be a 3 x 1 x N array.  Perhaps it is worthwhile spending some time on producing a wrapping that takes care of these preprocessing …

AN R FUNCTION FOR SANE LOG-LIKELIHOOD

So we are interested in a 1-dimensional observable $y_t$ and a $k+1$-dimensional latent process $\alpha_t$ where $k$ of the components handle exogenous variables.  The code to do this with stochastic volatility parameters $(\rho, \phi, \sigma_\eta)$ is the following.  In order to obtain sane log-likelihoods it is best to check and handle ‘NA’ values from st (these are signs of the returns), yt (these are log-return-squared), X (exogenous variables, such as junk bond premium (DBAA-DAAA in Fred parlance), 3-month treasury rate (DTB3), etc.  The function is just a wrapper around the ‘FKF’ filtering function.  The output includes prediction, filtering, and smoothing.

function (yt,st,X,rho,phi,sigma.eta)
{
N=dim(yt)[2]
k<-dim(X)[2]
m<-k+1
d<-1
a0<-rep(0,m)
C0<-diag(1.0,m,m)*1e7
Tt<-diag(1.0,m,m)
Tt[1,1]<-phi
A<-0.7979*rho*sigma.eta
B<- 1.1061*rho*sigma.eta
Gt<-diag(0.,m,m)
Gt[1,1]<-sigma.eta^2-A^2
Ht<-pi^2/2
HHt<-Ht %*% t(Ht)
GGt<-Gt %*% t(Gt)
Zt<-array(0., dim=c(d,m,N))
for (k in 2:m){ Zt[d,k,]<-t(X[,k-1]) }
dt<-matrix(0.,m,N)
dt[1,]<-A*st
m1<-fkf( a0, C0, dt, matrix(0), Tt, Zt, GGt,HHt,yt)
m1
}