User Basis Regression

Summary

This demo illustrates how the JMSL Library can be used to easily create a Java™ application that uses User Basis Regression to analyze data sets. It uses a basis function and specified number of terms to approximate a designated function.

Usage

Select the function to approximate and the Basis Function to use from the pull down menus. Set the number of coefficients to use with the slider bar. The analytical and basis functions are displayed in the text area. Coefficients and the derived function may be viewed upon selection of the appropriate button.

JMSL Library Math/Stat Classes

com.imsl.stat.UserBasisRegression - The update() method is used to add observations to the UserRegressionBasis object as they are calculated from the selected analytical function. The getCoefficients() method is then used to extract the calculated coefficients (number determined by the user) from the object. The coefficients are then used with the basis function to generate an approximate solution.

com.imsl.math.JMath - the pure Java implementation of the standard java.lang.Math class used to calculate the sin and cos functions.

com.imsl.math.Hyperbolic - a pure Java implementation of the hyperbolic functions and their inverses used to calculate sinh and cosh functions.

com.imsl.math.Physical - the value of pi is used to create the harmonic functions: final double pi = Physical.constant("PI").doubleValue();

com.imsl.stat.Random - this class is used to create the random noise added to a sin curve as the fourth function.

JMSL Library Charting Classes

This application utilizes the com.imsl.chart.Data object to generate a basic line chart. Several methods associated with this class, are used to customize the chart.

Java Code

The following code is the implementation of a RegressionBasis interface (user supplied basis function).

/** Sine basis function. */
class BasisSin implements RegressionBasis {
   public double basis(int index, double x) {
      return JMath.sin((index + 1) * x);
   }
}

In this code, the appropriate basis function along with the selected number of coefficients is used to instantiate the UserBasisRegression object. The computeFunc() method then calculates the function values and adds these observations to the UserBasisRegression object. The computeUser() method then computes the approximate solution using the calculated coefficients.

drawGraph()

case 0: /* Sin */
   ubr = new UserBasisRegression(new BasisSin(), ncoef, false);
   textUser = "sin(kx)";
   break;

computeFunc()

case 0: /* function 1 */
   for (int i = 0; i < npoints; i++) {
      ff[i] = JMath.sin(xx[i]) + 7.0 * JMath.sin(3.0 * xx[i]);
      ubr.update(xx[i], ff[i], 1.0);
   }
   textFunc = "sin(x) + 7sin(3x)";
   break;

computeUser()

coef = ubr.getCoefficients();
...
for (int i = 0; i < npoints; i++) {
   double sum = 0.0;
   double tmp = 0.0;
   for (int j = 0; j < ncoef; j++) {
      switch (val) {
         case 0: /* Sin */
            tmp = JMath.sin((double) (j + 1) * x[i]);
         break;
         ...
      }
      sum = sum + coef[j] * tmp;
   }
   yy[i] = sum;
}
return yy;

Links to Source Code

Basis.java This is the main class for this demo. It extends JFrameChart to include a JButton, JScrollBar, two JComboBoxes, and the appropriate listeners. In addition, all of the methods that calculate the analytical function and approximate basis regression solution are contained within this class.

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.