Kalman filtering

Execution algorithms know about order book and traded prices but it is also useful for them to know the 'fair' price of the listing being traded. This can prevent the algorithms from trading at atypical prices. Often the VWAP price over the last few minutes is used for this purpose.

The Kalman filter 1 is an algorithm for estimating the true signal from noisy input, and has many applications in guidance systems and robotics. It can also be used on financial time series, such as prices.

Chart 1: HSBC Holdings (0005.HK) through August 29, 2014. The blue line is the last traded price at the end of each five minutes of continuous trading. The red line is the same price series after applying the Kalman filter.

Chart 1 above shows the Kalman filter applied to a sample of prices for HSBC Holdings on the HKEx during August 29, 2014. The filtered price series follows the underlying last traded price yet the price changes are dampened, making it a conservative estimate of the fair price. A nice feature of the Kalman filter is that it requires no assumptions, such as the '5 minutes' in the 5 minute VWAP fair price: it just runs with the prices given to it and iteratively adjusts its output price or forecast based on the variation it sees in the input prices.

Here an implementation of the filter in kdb+/q, adapted from the Python example on SciPy 2

\d .kalman

filter: {
    t: ([] x: `float $ x; xh: `float $ x; p: (count x) # R: var x);
    (first t), iterate[R; R]\[first t; 1 _ t] 
    }

iterate: {[Q; R; x; y]
    x[`p]+: Q;
    k: x[`p] % R + x[`p];
    `x`xh`p ! (y[`x]; x[`xh] + k * y[`x] - x[`xh]; (1 - k) * x[`p])
    }

\d .

The variable names follow the Python example. One small change from the Python implementation is that both the measurement and process variance, the Q and R arguments to the iterate function, are set to the same figure, the variance in the input prices. Below is an example of running the filter on a table with a price column:

q).kalman.filter table.price
x     xh       p
---------------------
83.55 83.55    905.6062
83.45 83.48333 603.7375
83.45 83.4625  566.0039
83.4  83.42381 560.6134
83.45 83.44    559.8293
83.45 83.44618 559.7149
83.45 83.44854 559.6983
83.45 83.44944 559.6958
83.4  83.41889 559.6955
83.35 83.37631 559.6954
83.35 83.36005 559.6954

The filter function returns a table with the original input price, x, the forecast price, xh, and the process error, p. The process error stabilises after ten prices have been filtered.

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