Kalman Filter

Summary

This demo illustrates how the JMSL Library can be used to create an application that examines the uncertainty of financial data. The Kalman filter enables the user to address the question of how much of the volatility of the moment-to-moment stock price is due to the market’s pricing uncertainty and how much is due to "true" changes in a company’s value..

Usage

There are three sets of options to modify the chart. First, you can choose the amount of data to plot from one to five years. Next you can set the q/r ratio for the Kalman Filter to one of 0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1.0, 5.0 where q is the covariance of the underlying "true" stock price, and r is the covariance of the "measured" stock price that the security closed at on a stock exchange. Note that this ratio is an assumption that the user has to input into the filter. More small scale variance is included in the filter results as this ratio increases. Finally, the period of the moving average can be set as 2, 6, or 10 points.

Click in the chart area and the actual data value, the Kalman filtered value and the moving average value are displayed in the text area above the chart. Each value is marked on the chart with an asterisk.

JMSL Library Math/Stat Classes

com.imsl.stat.KalmanFilter - this class is used to compute the Kalman Filter in the computeKal() method. Note how two KalmanFilter objects must be constructed. The final filtered values are obtained from getStateVector() after filter() is called for second KalmanFilter.

JMSL Library Charting Classes

A standard line graph with a date/time axis is created. It is worth noting that all aspects of the chart creation, display, and re-display occur in separate methods: createChart(), drawGraph(), and update(), which calls removeChart() then drawGraph().

The removeChart() illustrates one programming technique necessary to learn to work efficiently with the JMSL Chart classes: to redraw an entity anew, it must first be removed from the parent node. So to redraw the lineData object, the following code is executed between the removeChart() and drawGraph() methods to remove and then recreate the Data object:

// from removeChart()
lineData
.remove();

// from drawGraph()
lineData = new Data(axis, x, y);
lineData.setLineColor(java.awt.Color.blue);
lineData.setTitle("Original Data");

Java Code

The data for this demo is stored in a csv file with two columns and read using the readFile() method. There is code that has been commented out in KFilt.java that would implement a Read File button to allow one to read in other data easily.

The selection of the length of the time series is accomplished by simply subsetting the full time series of 5 years. This is done in the subset() method, where the yearIndex array was created as the file was read in. It marks where each new year of data begins.

The Moving Average is computed "by hand" in the computeMov() method and accepts the number of points to average over as a parameter.

Links to Source Code

KFilt.java This is the main class for this demo. It extends JFrameChart, but embeds two other JPanels to contain the JTextArea and the JComboBoxes. A Label class in included to ease dealing with the JComboBoxes.

Running This Demo

Two alternatives are available to run this demo:

1) Use the source code in your development environment as any other Java code. More information is available in the How To.

2) An executable jar file containing all of the demos referenced in this guide is included in the jmsl/lib directory. On Windows, you may double-click the file to run it if files with a ".jar" extension are properly registered with javaw.exe. Alternatively, for both Windows and UNIX environments, the jar file may be executed from the command line using java -jar gallery.jar.

As list of buttons, one for each demo, is created. Demos can be subsetted as they relate to specific areas (Math, Stat, Finance, Charting) by choosing the appropriate selection on the JComboBox. To run the Additional Demos, select Quick Start in the JComboBox.