@Test public void testWeightedProbabilitySampling() { logger.info("weightedProbabilitySampling"); AssociativeArray2D strataFrequencyTable = new AssociativeArray2D(); strataFrequencyTable.put2d("strata1", "1", 10); strataFrequencyTable.put2d("strata1", "2", 20); strataFrequencyTable.put2d("strata1", "3", 30); strataFrequencyTable.put2d("strata1", "4", 40); strataFrequencyTable.put2d("strata2", "1", 100); strataFrequencyTable.put2d("strata2", "2", 200); strataFrequencyTable.put2d("strata2", "3", 300); strataFrequencyTable.put2d("strata2", "4", 400); strataFrequencyTable.put2d("strata2", "5", 500); strataFrequencyTable.put2d("strata2", "6", 600); strataFrequencyTable.put2d("strata2", "7", 800); AssociativeArray nh = generateNh(); boolean withReplacement = true; double expResult = Descriptives.sum(nh.toFlatDataCollection()); TransposeDataCollection sampledIds = StratifiedSampling.weightedProbabilitySampling(strataFrequencyTable, nh, withReplacement); double result = 0; for(Object stata : sampledIds.keySet()) { result+= sampledIds.get(stata).size(); } assertEquals(expResult, result, Constants.DOUBLE_ACCURACY_HIGH); }
public static TransposeDataCollection weightedProbabilitySampling(AssociativeArray2D strataFrequencyTable, AssociativeArray nh, boolean withReplacement) { TransposeDataCollection sampledIds = new TransposeDataCollection(); for(Map.Entry<Object, AssociativeArray> entry : strataFrequencyTable.entrySet()) { Object strata = entry.getKey(); Number sampleN = ((Number)nh.get(strata)); if(sampleN==null) { continue; } sampledIds.put(strata, SimpleRandomSampling.weightedSampling(entry.getValue(), sampleN.intValue(), withReplacement)); } return sampledIds; }
StratifiedSampling { public static TransposeDataCollection weightedProbabilitySampling(AssociativeArray2D strataFrequencyTable, AssociativeArray nh, boolean withReplacement) { TransposeDataCollection sampledIds = new TransposeDataCollection(); for(Map.Entry<Object, AssociativeArray> entry : strataFrequencyTable.entrySet()) { Object strata = entry.getKey(); Number sampleN = ((Number)nh.get(strata)); if(sampleN==null) { continue; } sampledIds.put(strata, SimpleRandomSampling.weightedSampling(entry.getValue(), sampleN.intValue(), withReplacement)); } return sampledIds; } }
StratifiedSampling { public static TransposeDataCollection weightedProbabilitySampling(AssociativeArray2D strataFrequencyTable, AssociativeArray nh, boolean withReplacement) { TransposeDataCollection sampledIds = new TransposeDataCollection(); for(Map.Entry<Object, AssociativeArray> entry : strataFrequencyTable.entrySet()) { Object strata = entry.getKey(); Number sampleN = ((Number)nh.get(strata)); if(sampleN==null) { continue; } sampledIds.put(strata, SimpleRandomSampling.weightedSampling(entry.getValue(), sampleN.intValue(), withReplacement)); } return sampledIds; }  }
StratifiedSampling { public static TransposeDataCollection weightedProbabilitySampling(AssociativeArray2D strataFrequencyTable, AssociativeArray nh, boolean withReplacement) { TransposeDataCollection sampledIds = new TransposeDataCollection(); for(Map.Entry<Object, AssociativeArray> entry : strataFrequencyTable.entrySet()) { Object strata = entry.getKey(); Number sampleN = ((Number)nh.get(strata)); if(sampleN==null) { continue; } sampledIds.put(strata, SimpleRandomSampling.weightedSampling(entry.getValue(), sampleN.intValue(), withReplacement)); } return sampledIds; }  static TransposeDataCollection weightedProbabilitySampling(AssociativeArray2D strataFrequencyTable, AssociativeArray nh, boolean withReplacement); static TransposeDataCollection randomSampling(TransposeDataList strataIdList, AssociativeArray nh, boolean withReplacement); static double mean(TransposeDataCollection sampleDataCollection, AssociativeArray populationNh); static double variance(TransposeDataCollection sampleDataCollection, AssociativeArray populationNh); static double std(TransposeDataCollection sampleDataCollection, AssociativeArray populationNh); static double xbarVariance(TransposeDataCollection sampleDataCollection, AssociativeArray nh, AssociativeArray populationNh); static double xbarStd(TransposeDataCollection sampleDataCollection, AssociativeArray nh, AssociativeArray populationNh); static AssociativeArray optimumSampleSize(int n, AssociativeArray populationNh, AssociativeArray populationStdh); }
StratifiedSampling { public static TransposeDataCollection weightedProbabilitySampling(AssociativeArray2D strataFrequencyTable, AssociativeArray nh, boolean withReplacement) { TransposeDataCollection sampledIds = new TransposeDataCollection(); for(Map.Entry<Object, AssociativeArray> entry : strataFrequencyTable.entrySet()) { Object strata = entry.getKey(); Number sampleN = ((Number)nh.get(strata)); if(sampleN==null) { continue; } sampledIds.put(strata, SimpleRandomSampling.weightedSampling(entry.getValue(), sampleN.intValue(), withReplacement)); } return sampledIds; }  static TransposeDataCollection weightedProbabilitySampling(AssociativeArray2D strataFrequencyTable, AssociativeArray nh, boolean withReplacement); static TransposeDataCollection randomSampling(TransposeDataList strataIdList, AssociativeArray nh, boolean withReplacement); static double mean(TransposeDataCollection sampleDataCollection, AssociativeArray populationNh); static double variance(TransposeDataCollection sampleDataCollection, AssociativeArray populationNh); static double std(TransposeDataCollection sampleDataCollection, AssociativeArray populationNh); static double xbarVariance(TransposeDataCollection sampleDataCollection, AssociativeArray nh, AssociativeArray populationNh); static double xbarStd(TransposeDataCollection sampleDataCollection, AssociativeArray nh, AssociativeArray populationNh); static AssociativeArray optimumSampleSize(int n, AssociativeArray populationNh, AssociativeArray populationStdh);  }