KalmanDLL
Users Guide
Copyright © Haiku Laboratories 2010
Updated March, 2010
INTRODUCTION
The KalmanDLL is a collection of C functions that implement the Kalman filter described in the references:
The filter is a two-stage algorithm that predicts a future data value based on past data in the first stage, then adjusts the prediction in the second stage based on the current data. This prediction-correction is repeated over the entire data set. The DLL contains four callable functions:
The VBA code below demonstrates the calling procedure each of these functions.
The installation package is downloaded as Ksetup.zip, which contains the Ksetup.exe installer. Running Ksetup.exe creates a folder C:\Program Files and installs the DLL, an Excel spreadsheet and some test data to their default locations. The following table lists the files and their required locations:
|
Filename |
Location |
|
KalmanDll.dll |
C:\Program
Files\HaikuLabs\DLLs\ |
|
Kalman_Demo3.xls |
C:\Program
Files\HaikuLabs\ |
|
Test.csv |
C:\Program
Files\HaikuLabs\Data\ |
Table 1. Package files and their default locations.
The spreadsheet contains VBA code that demonstrates the calling procedures for the DLL functions and displays results. Here Test.csv refers to one or more test data files used by the spreadsheet. These csv files are in Yahoo data format. To obtain more data, go to Yahoo Finance and download 252 days of any stock. Name the file by its symbol and store it in the same folder. Alternatively, and automated downloader is avaiable from Haiku Labs.
VBA declarations
are given below. Function names are
shown in blue and function arguments in green. Argument definitions are given in the next
section. (Note that the underscore _ in
each declaration is the VBA line-continuation symbol.)
Public Declare Function init_state Lib "path\Kalmandll.dll" _
(ByVal dmn As Integer, ByRef Data As Double, ByRef sta As Double, ByRef cov As Double) As Integer
Public Declare Function xtrap_q Lib " path \Kalmandll.dll" _
(ByVal dmn As Integer, ByVal Q As Double, ByRef sta As Double, ByRef cov As Double) As Integer
Public Declare Function update Lib "path\Kalmandll.dll" _
(ByVal dmn As Integer, ByVal Inn As Double, ByVal Vr As Double, ByRef sta As Double, ByRef cov As Double, ByRef g1 As Double) As Integer
Public Declare Function stat_update Lib " path\Kalmandll.dll" _
(ByRef Er As Double, ByRef Vr As Double, ByVal R As Double, ByVal I As Integer) As Integer
The default path
is: C:\Program Files\HaikuLabs\DLLs\
and should not be modified. The first three functions each return the dimension
dmn passed to it if successful and a 0 (zero) on failure or expired.
(stat_update returns a 1 on success.)
The following is a pseudo-code summary of the VBA code used in the first reference above. It calls the four functions contained in KalmanDLL. Note that the dimension is set to 3, i.e., dmn is hard-coded to 3 for all calls (this is not adjustable). The complete code is contained in Kalman_Demo3.xls, available in the installation package.
Define Variables:
Dim Npts as Integer number of data points, must be < 300
Dim Er as Double ' residual mean
Dim En as Double ' innovation mean
Dim Vn as Double ' innovation variance
Dim Vr as Double ' residual variance
Dim Alpha as Double Alpha indicator
Dim Resid as Double residual
Dim Inn as Double innovation
Dim sta1 as Double predicted state
Dim sta2 as Double filtered state
Dim T1 as Double tracking parameter
Dim T2 as Double Q/R = 10-T1
Dim Q as Double process noise
Dim Sta(4) as Double State vector
Dim Cov(10) as Double Covariance matrix
Dim Data(300) as Double data vector
Dim I, L as Integer
Npts = 252 one trade-year
Choose Tracking Parameter:
T1 = 1 this must be adjusted for each data set
T2 = 10 ^ (-T1) ' Q/R
Read data:
For I = 1 To Npts
Data(I) = (data location)
Next I
Initialize statistics:
Er = 0
En = 0
Vn = 0
Vr = 0
L = init_state(3, Data(0), sta(0), cov(0)) initialize state for quadratic track (3)
If L = 0 Then Exit Sub KalmanDll has expired
Vr = cov(1) residual variance estimate
Q = T2 * Vr
Track_Data:
For I = 4 To Npts
L = xtrap_q(3, Q, sta(0), cov(0)) extrapolate state
sta1 = sta(1) sta1 is current predicted state
L = stat_update(En, Vn,
Alpha = (sta(1) - Data(I - 1)) / Sqr(Vn) used in Fortune calculation
L = update(3,
sta2 = sta(1) sta2 is current filtered state
Resid = Data(I) - sta2
L = stat_update(Er, Vr, Resid, I) residuals update
Q = T2 * Vr
Next I
Last_Prediction:
L = xtrap_q(3, Q, sta(0), cov(0)) extrapolate state
Alpha = (sta(1) - data(Npts)) / Sqr(Vn) used in Fortune calculation
DISCLAIMER - This product is only intended as a tool in a market-traders arsenal of software products. There are no claims made as to the ultimate performance of the market items selected using this product.