src.jdd.util.math.Chi2Test Maven / Gradle / Ivy
Show all versions of jdd Show documentation
package jdd.util.math;
import jdd.util.*;
/**
* \chi^2 random distribution test.
*
* This class is used to test the distribution of series
* of numbers such hashes and (pseudo) random numbers.
*
*
*
* assume that you want to test a function f() which returns a number between 0 and N-1:
*
* Chi2Test c2t = new Chi2Test(N);
*
* while(c2t.more()) c2t.add( f() );
*
* if(c2t.isChi2Acceptable())
* System.out.println("The distribution of f() is random enough for me!");
*
*/
public class Chi2Test {
private int n, samples_needed, samples_have;
private int [] distibution;
private boolean has_chi2; //have we computed the values?
private double the_chi2, the_stddev; // when computed, the values are stored here
/**
* start a chi^2 for the input numbers 0..n-1
*
* n must be larger than 20. don't make it too large unless you
* have enough memory for it.
*
*
Also, if n is too small (say bellow 1000), then you might get
* many false answers so instead consider the majority of multiple runs.
*/
public Chi2Test(int n) {
// Test.check(n > 20, "n to small");
this.n = n;
this.samples_needed = 25 * n + 3; // Knuth said something about 5*n, but what does he knows?
this.distibution = new int[n];
reset();
}
/** reset the chi^2, start all over */
public void reset() {
samples_have = 0;
Array.set(distibution, 0);
has_chi2 = false;
}
/**
* returns true if it has enough samples to give an accurate answer.
*
*
NOTE: most other functions here cannot be called before this functions
* starts returning falsees!
*/
public boolean more() {
return samples_have " + c2t.getChi2());
System.out.println("stddev==> " + c2t.getStandardDeviation());
// check java.util.Random.nextInt()
java.util.Random rnd = new java.util.Random();
c2t.reset();
while(c2t.more()) c2t.add( rnd.nextInt(max) );
System.out.println("\nesting java.util.Random.nextInt(" + max + ")");
System.out.println("chi2 ==> " + c2t.getChi2());
System.out.println("stddev==> " + c2t.getStandardDeviation());
}
}