# Good criteria to sort state-space $\beta_{t}$ according to Kalman filter output

Let the usual state-space linear model (without constant term for the sake of simplicity):

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

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

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

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

Sys.setenv(TZ = 'UTC')

# Contents:

# 1. Installing packages
# 4. Kalman filtering of linear regression Beta

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

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

# *********************************
# *********************************

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

# *********************************
# *********************************

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 you to 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 having available$P_{t}$and$v_{t}$, i.e. how to penalize a linear relationship because of too high variance and prediction errors. 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. - I am not exactly clear what your question is, do you mind rephrasing it a little or expanding on it? Thanks – Matt Wolf Aug 14 '13 at 2:03 @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. – Lisa Ann Aug 14 '13 at 6:08 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 – Matt Wolf Aug 14 '13 at 6:16 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. –  Lisa Ann Aug 14 '13 at 6:33
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...) –  Matt Wolf Aug 14 '13 at 6:43