Kalman filtering

Implementation

This code below is an implementation the linear Kalman filter [1] in q, the language of kdb+, adapted from the Python example in the SciPy cookbooks [2]. It is worthwhile comparing the Python and q coding.

\d .kalman

pv: 1f
mv: 1f
xh: ()

filter: {
    x: `float$ x;
    mv:: var x;
    xh:: enlist (first x; mv);
    if [1 < count x; {xh,: enlist correct [predict last xh; x]} each 1 _ x];
    xh
    }

predict: {(first x; pv + last x)}

correct: {
    k: lxh % mv + lxh: last x;
    (fxh + k * y - fxh: first x; (1 - k) * lxh)
    }

\d .

The above illustrates some features of q. Firstly, it is a highly functional language, so the predict and correct aspects of the algorithm have been coded directly as functions. Secondly, q is list based, so as an alternative to maintaining various arrays of intermediate values it keeps the prediction and estimated variance pair as a list in a list of all the iterations, xh. Thirdly, although q has traditional iteration control features such as while loops, it is often best to go with the list- and functional-language aspects of q for both clarity and speed.

q has no notion of private and public functions or variables, although the context or directory concept is useful to provide some degree of encapsulation. For example, using the above context to filter a table called prices which has a column named bid, then extract the filtered values, is straightforward:

.kalman.filter prices `bid
first each .kalman.xh
or just
first each .kalman.filter prices `bid

An example

The chart below shows the linear Kalman filter on the five minute bid prices for HSBC (HSBA.L) on December 1, 2006.

Further reading

1.Greg Welch and Gary Bishop An Introduction to the Kalman Filter, 2006
2.Andrew Straw Kalman Filtering, 2006
3.www.kx.com