Data Fitting

Summary

This demo program illustrates how the JMSL Library can be used to develop Java™ applications that allow visual data interaction and statistical analysis. In this case, the program fits a line, curve or surface to data using several different methods.

Usage

Select one of Linear Regression, Cubic Splines, Nonlinear Model or Contour by clicking on the appropriate tab. For each case, data is loaded into the example with a fit already computed. Different data sets are available under the Data menu bar item. There is also a "Blank" item that clears all data from the graph so that points can be entered from scratch.

Data can be entered into this demo in the following ways:

  1. Use the left mouse button to add a point to the chart, and the right mouse button to remove a point from the chart.
  2. Click the "Enter Points" button to enter specific x, y pairs (or x, y, z triplets for Contour).
  3. Click the "Generate" button to generate the number of random points specified in the "# of points" box.
  4. For the Contour example, the x,y values are located as with the other examples, but the z value is set with the slider bar on the right side of the graph.

As points are added to the chart, the computed fits are updated automatically. Note that the Cubic Spline fits require three to four points to be entered before the spline is computed; similarly, the nonlinear models require one more point than there is parameters in the model.

Data can be removed from any chart by right-clicking on a data point.

JMSL Library Math/Stat Classes

com.imsl.stat.LinearRegression - this class is used in the getRegCoefficient() method to compute the slope and intercept values of the linear fit. Its getANOVA().getErrorMeanSquare() method is used to compute the mean square error that is displayed in the text area.

com.imsl.math.Spline - each of the spline classes extend this class, so it is this value() method used to retrieve computed values for the splines.

com.imsl.math.NonlinLeastSquares.Function - The nonlinear model to be fit is defined through this class, which contains a single public method f() that accepts a double array x[] of the points at which to evaluate the function and a double array f[] containing the returned values at each x value.

com.imsl.math.NonlinLeastSquares - This class does the work fitting the adjustable parameters in the NonlinLeastSquares.Function to the supplied data. The solution is obtained from its solve() method.

com.imsl.chart.ChartFunction - (in Linear Regression and Cubic Splines) this interface is used repeatedly in the drawGraph() method to plot the regression line and any of the Spline functions. Given an array of x values, it is worth pointing out how to construct a Data object for a Chart from a slope/intercept:

ChartFunction fcn = new ChartFunction() {
    public
double f(double x) {
        return
x*x1 + intercept;   // x1 is the slope
    }

}
;

line
[0] = new Data(axis, fcn, 0.0, 50.0);

or from a spline fit:

final CsAkima cs0 = new CsAkima(x, y);
ChartFunction
fcn0 = new ChartFunction() {
    public
double f(double x) {
        return
cs0.value(x);
    }

}
;
line[1] = new Data(axis, fcn0, 0.0, 50.0);

com.imsl.chart.ChartFunction - (in Nonlinear Model) this interface is used to plot the fitted solution as a line. A ChartFunction is defined for each model, which requires a single public method f() that accepts a double value for the point to evaluate the function at and returns this value as a double. This is an easy way to plot an arbitrary function - must easier than the alternative, which is to compute various y values as you loop through some range of x values manually. Additionally, one can use the com.imsl.chart.Data constructor Data(ChartNode parent, ChartFunction cf, double a, double b) to make adding the values to a chart even easier.

JMSL Library Charting Classes

com.imsl.chart.Contour - This class is used to fit gridded or scattered three-dimensional data. Ten levels are to be displayed, and since the range changes with the data set selected, the levels are computed when the Contour object is created. The number of centers used in the graph is also computed and the legend is configured for display.

double[] cLevels = new double[10];
for
(int i=0; i<cLevels.length; i++) {
    cLevels
[i] = (double)i*(zRange[1]-zRange[0])/10.0 + zRange[0];
}

int
nCenters = x.length/2;
contour
= new Contour(axis, x, y, z, cLevels, nCenters);
contour
.getContourLegend().setPaint(true);

Java Code

java.util.Vector is used in this example, although there is no reason why the collection class java.util.ArrayList could not be used in its place. In fact, such a change could be made to this source simply by replacing instances of "Vector" with "ArrayList".

Link to Source Code

FittingMain.java This is the main class, and it extends JFrame. This small class instantiates a JTabbedPane and adds each of the JPanels below, which in turn implement several listeners for the interface elements and to allow right- and left-clicking in their respective chart areas.
LRPanel.java This class contains the charting and numerics for the linear regression example.
CsPanel.java This class contains the charting and numerics for the cubic spline example.
NLPanel.java This class contains the charting and numerics for the nonlinear model example.
RBPanel.java This class contains the charting and numerics for the radial basis/contour example.

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.