Sample Size

Summary

This demo illustrates how the JMSL Library can be used to easily create a Java™ application that answers the difficult question "How many samples do I need to make sure my estimate is valid?" Sample too many, and you waste a lot of money; sample too few, and you don't have a reliable estimate. This detailed example will provide more insight into what one can accomplish with this application.

Usage

Sample Size - Set values for alpha (or inversely the confidence interval), delta (the confidence interval width), and the population standard deviation. Move the cursor along the blue Tolerance line until the Probability label below the chart reaches the desired value. Then read off the required Sample Size.

Power Curve - For this case, an experiment is being designed to find, at a given alpha level, the probability of making a Type II error. As before, set the values for alpha and the standard deviation. Here, delta is the size of the effect that should not be missed. Move the cursor along the green Power/Operating Characteristic Curve to the probability and read off the Sample Size.

Press the "Reset" button to set the sliders back to their default values.

JMSL Library Math/Stat Classes

com.imsl.stat.Cdf - the chi(), inverseChi() and inverseStudentsT() methods of this class are used in computing the probability, tolerance and power curves. The normal() and beta() methods are used in tncCdf.java.

com.imsl.math.JMath - the sqrt() method of this class is used instead of java.lang.Math.sqrt(). JMath.PI, pow(), exp() and log() are used in the tncCdf class.

com.imsl.math.Sfun - the gamma() and logGamma() methods are used in the tncCdf class to compute the non-central t-distribution.

JMSL Library Charting Classes

The chart is a standard line graph containing three lines and a legend. The chart is updated in real time as the sliders are moved. The x axis range is set to be the point where the Power/OC curve drops below a threshold of 0.0005. It was empirically determined that setting the maximum of the x axis at this value would keep the interesting parts of the figure in view without requiring the user to zoom in or out. Resolving that value and implementing the range is straightforward:

int flat = -1;
// Compute the probabilities data.

for
(int k = 0; k < x.length; k++) {
    y
[0][k] = powerR(x[k], alpha, delta, sigma);
    y
[1][k] = probSuccess(x[k], alpha, delta/sigma);
    y
[2][k] = tolerProb(x[k], alpha, delta, sigma);
    if (flat == -1 && y[0][k] < 0.0005) {
        flat
= k;
    }
}

if
(flat == -1) {
    axis.getAxisX().setWindow(0.00, x[x.length-1]);
}
else {
    axis.getAxisX().setWindow(0.00, x[flat]);
}

Java Code

This example incorporates a class to compute the cumulative probability at T of the non-central t-distribution given the (possibly fractional) degrees of freedom and a non-centrality parameter. This distribution is not part of com.imsl.stat.Cdf at this time and was translated from the FORTRAN in Lenth, R. V. (1989), "Algorithm AS243: Cumulative Distribution Function of the Non-central t Distribution," Applied Statistics, 38, 185-189. Consult the source code for tncCdf.java (link below) for more information on the algorithm and how to use the class.

Link to Source Code

SampleSize.java This is the main class for this application. It extends JFrameChart and all of the charting and computation is done here.
tncCdf.java This class computes the non-central t-distribution using methods of com.imsl.stat.Cdf.

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.