Statistical Examples

Summary

This demo program illustrates how the JMSL Library can be used to develop Java™ applications based on various statistical methods. Thanks to Dr. Gary McClelland, Professor of Psychology at the Univeristy Colorado at Boulder, for supplying the correlation and Cohen's D examples.

Usage

Select one of Random Distributions, Correlation Visualization, Cohen's D or Radar Plot by clicking on the appropriate tab. Each demo is straightforward to operate:

Random Distributions: Click on the different distribution names to generate a set of random numbers; the resulting values will be binned into a histogram which is plotted in a separate window. Set the number of samples to generate and the number of bins in the histogram by typing in the text areas above the list. Some summary information is printed below the list.

Correlation Visualization: Click on the slider and drag it to visualize the scatter of different values of correlation. Each time the demo is started, a new set of thirty points is generated with 0 initial correlation.

Cohen's D: Click near the vertical black bar on the x axis and drag it left or right to change the value of d. The amount of overlap is computed and displayed in the chart title; this value is computed from formulas in Cohen (1969)1.

Rader Plot: Select different cars to compare on the plot using the set of CheckBoxes on the right. Radar plots allow easy comparison of various attributes; in this demonstration those attributes include Base Sticker Price, Acceleration Time, Braking Distance, Lateral Grip, and Highway Fuel Economy.

JMSL Library Math/Stat Classes

com.imsl.stat.Random - various methods of this class are used to generate the different distributions. The typical way to use this class is to instantiate the class, then use one of the "next" methods to retrieve a random number that fits a certain distribution. For example:

Random r = new Random();
val = r
.nextCauchy();

The nextNormal() method is used in the Correlation Visualization example.

com.imsl.math.Cholesky - the output of this class is required for the nextMultivariateNormal() method of Random

com.imsl.stat.Covariances - The correlation is computed with the compute(Covariances.STDEV_CORRELATION_MATRIX) method of this class.

com.imsl.stat.Cdf - The Cohen's D example uses the normal() method of this class in computing the overlap percentage..

com.imsl.chart.ChartFunction - This interface is used to plot the standard normal density function in the Cohen's D demo:

ChartFunction norm = new ChartFunction() {
    public
double f(double z) {
        return
(1/Math.sqrt(2*Math.PI))*Math.pow((Math.E, -z*z/2);
    }

}
;

data
= new Data(axis, norm, -4, 6);

and is used again later in a similar manner to draw the offset distribution.

JMSL Library Charting Classes

The Random Distributions chart is a histogram created from com.imsl.chart.Bar. Of note is the construction of the labels for the x axis. Since the range of this axis is dynamic, labels could not be hard coded; however, informative range values for each bin was desired. The solution is to create a new String array for each chart and then construct the labels manually. The variables minv and maxv are the minimum and maximum extents of the set of random numbers.

// Setup the X axis for a labeled bar chart.
String
spacer = " : ";
String
labels[] = new String[numBins];
java
.text.DecimalFormat df = new java.text.DecimalFormat();
if
(randomK != 15) {
    df
.applyPattern("##0.00");
}
else { // special format for logNormal
    df
.applyPattern("0.0000");
}

labels[0] = String.valueOf(df.format(minv)) + spacer +
    String.valueOf(df.format(minv+wx));
for
(int i = 1; i < numBins-1; i++) {
    labels
[i] = String.valueOf(df.format(minv + i*wx)) + spacer +
        String.valueOf(df.format(minv + (i+1)*wx));
}

labels[numBins-1] = String.valueOf(df.format(maxv-wx)) + spacer +
        String.valueOf(df.format(maxv));
bar
.setLabels(labels, bar.BAR_TYPE_VERTICAL);
axis
.getAxisX().getAxisLabel().setTextAngle(270);

Java Code

The series of radio buttons in Random Distirbutions is added programmatically with the same private method to handle the ActionEvents:

for (int i=0; i<numDist ;i++) {
  
rb[i] = new javax.swing.JRadioButton(rname[i]);
   jPanel3.add(rb[i]);
   rb[i].addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
         String eventname = new String(evt.getActionCommand());
         methodActionPerformed
(eventname);
      }

   })
;

}

Link to Source Code

StatsMain.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 their own listeners for the user interfaces and interaction with chart areas.
RDPanel.java This class contains the charting and numerics for the random distributions example.
CVPanel.java This class contains the charting and numerics for the correlation visualization example.
CDPanel.java This class contains the charting and numerics for the Cohen's d example.
RAPanel.java This class contains the charting and numerics for the radar plot 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.


1Cohen, J. (1969). Statistical power analysis for the behavioral sciences. New York: Academic Press.