Example: Random Number Generation
In this example, a discrete normal random sample of size 1000 is generated via Random.nextGaussian
. Random.setSeed
is first used to set the seed. After the ChiSquaredTest
constructor is called, the random observations are added to the test one at a time to simulate streaming data. The Chi-squared test is performed using Cdf.normal
as the cumulative distribution function object to see how well the random numbers fit the normal distribution.
import com.imsl.stat.*;
import com.imsl.math.*;
public class RandomEx1 implements CdfFunction {
public double cdf(double x) {
return Cdf.normal(x);
}
public static void main(String args[]) throws
InverseCdf.DidNotConvergeException {
int i,j;
double tmp[][];
int nObservations = 1000;
Random r = new Random(123457L);
ChiSquaredTest test =
new ChiSquaredTest(new RandomEx1(), 10, 0);
for (int k = 0; k < nObservations; k++) {
test.update(r.nextNormal(), 1.0);
}
double p = test.getP();
System.out.println("The P-value is "+p);
}
}
Output
The P-value is 0.5518855965158243
Link to Java source.