//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& // WAM = Walker's Alias Method for sampling discrete distribution // NON-STATIC, OBJECT-ORIENTED VERSION. // // To compile: javac wamdemo.java or "m wamdemo" // To run: java wamdemo //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& import java.io.*; import java.util.Random; //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& // YLL 2005/05/07 //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& class WAMsampler { // N=number of sample values, ff=bar fractions, aa=aliases Random random; int N; double ff[]; int aa[]; public WAMsampler (final int N, final double pp[]) { //pp=probability vector int i,j,jmin,jmax; double bmin,bmax,s,oon,tol = 1E-15d; double bb[] = new double[N]; random = new Random(); this.N = N; ff = new double[N]; aa = new int[N]; oon = 1.0d/N; //----- Verify that user-supplied pp[]'s sum to unity! s=0; for (i=0; i tol) {System.out.println("not norm!");Runtime.getRuntime().exit(1);} //----- Set up arrays for (i=0; ibb[j]) {bmin=bb[j]; jmin=j;} if (bmaxff[n]) n=aa[n]; return n; } } //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& // TEST DRIVE //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& public class wamdemo { public static void main (String[] args) { WAMsampler wamsampler; int N; double pp[]={.37, .04, .11, .06, .08, .19, .03, .01, .02, .09}; N = 10; int i; wamsampler = new WAMsampler(N, pp); //------- Dump internals System.out.println ("\ni \tpp[i] \tff[i] \t1-f\taa[i]"); for (i=0; i