package uk.ac.leeds.ccg.cluster; // Bootstrap test class for cluster methods import java.util.*; //import com.sun.java.util.collections.*; import uk.ac.leeds.ccg.geotools.*; public class BootstrapTest extends SignificanceTest implements Constants { double stat = (double) 0.0; public BootstrapTest() { System.out.println("Creating a bootstrap test"); } public final boolean isSignificant(Vector points) { MyPoint p2; double sigsum = (double) 0.0; double summean = (double) 0.0; double cases = (double) 0.0; double pop = (double) 0.0; double mean, sig2, sig, Z, prob, cutoff; int psize, k; int maxSam = parameters.getBootstrapSampleSize(); double rs[]; rs = new double[maxSam]; double thresh = parameters.getSignificanceThreshold(); // Get Excess (or deficit) boolean excess = parameters.getExcess(); psize = points.size(); for ( int i = 0; i < maxSam; i ++ ) { pop = 0.0; cases = 0.0; // sampling with replacement // System.out.println("Points:" +" "+ psize); for ( int j = 0; j < points.size(); j ++ ) { k = ( int ) ( Math.random() * psize ); p2 = ( MyPoint ) points.elementAt( k ); pop += p2.getPop(); cases += p2.getCases(); } // rs[i] = pop; // summean += pop; rs[i] = cases / pop; summean += cases / pop; } cases = 0.0; pop = 0.0; for ( int j = 0; j < points.size(); j ++ ) { p2 = ( MyPoint ) points.elementAt( j ); pop += p2.getPop(); cases += p2.getCases(); } if ( !isWorthTesting( pop, cases ) ) return false; // NB. bit of reordering will speed things up! // ie. test this before all the sampling! // calculate mean and standard deviation mean = summean / maxSam; for ( int i = 0; i < maxSam; i ++ ) { sigsum += ( ( rs[i] - mean ) * ( rs[i] - mean ) ); } sig2 = sigsum / (maxSam - 1); sig = Math.sqrt(sig2); // calculate Z statistic, and probability // Z = (sumC - mean) / sig; // Z = (cases - mean) / sig; mean = cases / pop; if ( excess ) { Z = ( mean - 1.0d ) / sig; } else { Z = ( 1.0d - mean ) / sig; } //System.out.println("Z " + Z + " , mean " + mean + ", stdev "+ sig); // significance level & corresponding cutoff on the Normal cutoff = 0.0; // prob = 0.50 for ( int j = 0; j < 27; j ++ ){ if ( ( thresh <= THRSH[ j ] ) && ( thresh > THRSH[ j + 1 ] ) ) cutoff = VALS[ j ]; } if ( thresh < THRSH[ 27 ] ) cutoff = 3.7; // System.out.println("thresh " + thresh +" "+ cutoff); prob = thresh; // if (Z > 2.32) // prob = 0.99 // Change andyt 11 Nov 03 if ( Math.abs( Z ) > cutoff ) { //if ( Z > cutoff ) { switch ( parameters.getStatType() ) { // case 1: stat = sumC - sumP; break; // case 2: stat = sumC/sumP; break; case 1: stat = cases - pop; break; case 2: stat = cases / pop; break; case 3: stat = ( double ) 1.0 - prob; break; } return true; } else stat=0.0; return false; } public double getStatistic(){ return (double) stat; } public String toString() {return "Bootstrap";} private int canMax; private double cons[]; }