3
$\begingroup$

Let's assume the usual state-space linear model without constant term for simplicity:

$y_{t}=\beta_{t} X_{t}+\epsilon_{t}$

If we apply Gaussian Kalman filter to estimate $\beta_{t}$ we get $P_{t}$, which is the covariance matrices of predicted states, and $v_{t}$, which is the prediction error.

The following simple R code allows you to download pair of tickers (QQQ and XLK for instance) from Yahoo Finance and estimate $P_{t}$ and $v_{t}$ while plotting them:

# ======================================== #
# Kalman filter errors and states variance #
# ======================================== #

op <- par(no.readonly = TRUE)
Sys.setenv(TZ = 'UTC')

# Contents:

# 1. Installing packages
# 2. Loading packages
# 3. Downloading and plotting data
# 4. Kalman filtering of linear regression Beta

# *********************************
# 1. Installing packages
# *********************************

#install.packages('KFAS')
#install.packages('latticeExtra')
#install.packages('quantmod')

# *********************************
# 2. Loading packages
# *********************************

require(compiler)
require(latticeExtra)
require(KFAS)
require(quantmod)

# *********************************
# 3. Downloading and plotting data
# *********************************

Symbols <- c('QQQ', 'XLK')
getSymbols(Symbols, from = '1950-01-01')
data <- na.omit(merge(Cl(QQQ), Cl(XLK)))
colnames(data) <- Symbols
xyplot(data)

# *********************************
# 4. Kalman filtering of linear regression Beta
# *********************************

y <- na.omit(merge(ClCl(QQQ), ClCl(XLK)))[,1]
X <- na.omit(merge(ClCl(QQQ), ClCl(XLK)))[,2]
model <- regSSM(y = y, X = X, H = NA, Q = NA)
object <- fitSSM(inits = rep(0, 2), model = model)$model
KFAS <- KFS(object = object)
P <- xts(as.vector(KFAS$P)[-1], index(y))
v <- xts(t(KFAS$v), index(y))
Z <- cbind(P, v)
colnames(Z) <- c('Covariance of predicted state', 'Prediction error')
xyplot(tail(Z, 1000))

Now let's iterate this procedure over several pairs of securities to estimate their $\beta_{t}$, $P_{T}$ and $v_{t}$ and you want to sort these pairs by the stability and accuracy of $\beta_{t}$, that is, low variance and low prediction error.

I would like to know suitable criteria to make this ranking system having available $P_{t}$ and $v_{t}$, i.e. how to penalize a linear relationship because of too high variance and prediction errors?

For instance:

Replacing QQQ and XLK in my code with VXX and TLT, you will see greater $P_{t}$ and $v_{t}$, which are linearly related between VXX and TLT and are more volatile and have weaker predictive power than the one between QQQ and XLK.

This is similar to a ranking system and I would like to know how to produce some numeric criteria.

$\endgroup$
  • 1
    $\begingroup$ I am not exactly clear what your question is, do you mind rephrasing it a little or expanding on it? Thanks $\endgroup$ – Matt Aug 14 '13 at 2:03
  • $\begingroup$ @MattWolf, what's exactly that you find not clear in my question? I'll be happy to add further details. As instance: replace QQQ and XLK in my code with VXX and TLT, you will see greater $P_{t}$ and $v_{t}$, that is, the linear relationship between VXX and TLT is more volatile and has weaker predictive power than the one between QQQ and XLK. This is like a ranking system, and I would like to know how to produce a numeric criterion. $\endgroup$ – Lisa Ann Aug 14 '13 at 6:08
  • 1
    $\begingroup$ Ok, I get that but that is not related in any way to Kalman filters (other than that the prior algorithm involved a Kalman filter), correct? I was just confused because you spend 90% of your question on data acquisition and Kalman filter but your real question seems to be one of how to rank results. But maybe that is just my wrong impression $\endgroup$ – Matt Aug 14 '13 at 6:16
  • $\begingroup$ The attached code purpose is to start from a common output before answering my question. This common output is made up by $P_{t}$ and $v_{t}$ arrays. In fact, the most important part of my question lies at the end. $\endgroup$ – Lisa Ann Aug 14 '13 at 6:33
  • $\begingroup$ well, what have you tried so far in R re your real question, result ranking? (Re earlier messages, do not get me wrong please, I do like your question and +1, just wanted to clarify...) $\endgroup$ – Matt Aug 14 '13 at 6:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.