### Question:
SimpleRandomSampling { public static FlatDataCollection weightedSampling(AssociativeArray weightedTable, int n, boolean withReplacement) { FlatDataList sampledIds = new FlatDataList(); double sumOfFrequencies = Descriptives.sum(weightedTable.toFlatDataCollection()); int populationN = weightedTable.size(); for(int i=0;i<n;++i) { if(withReplacement==false && populationN<=n) { break; } double randomFrequency = PHPMethods.mt_rand(0.0, sumOfFrequencies); double cumulativeFrequency=0; for(Map.Entry<Object, Object> entry : weightedTable.entrySet()) { Object pointID = entry.getKey(); cumulativeFrequency+= TypeInference.toDouble(entry.getValue()); if(cumulativeFrequency>=randomFrequency) { if(withReplacement==false && sampledIds.contains(pointID)) { continue; } sampledIds.add(pointID); break; } } } return sampledIds.toFlatDataCollection(); }  static FlatDataCollection weightedSampling(AssociativeArray weightedTable, int n, boolean withReplacement); static FlatDataCollection randomSampling(FlatDataList idList, int n, boolean withReplacement); static double mean(FlatDataCollection flatDataCollection); static double variance(FlatDataCollection flatDataCollection); static double std(FlatDataCollection flatDataCollection); static double xbarVariance(double variance, int sampleN); static double xbarVariance(double variance, int sampleN, int populationN); static double xbarStd(double std, int sampleN); static double xbarStd(double std, int sampleN, int populationN); static double pbarVariance(double pbar, int sampleN); static double pbarVariance(double pbar, int sampleN, int populationN); static double pbarStd(double pbar, int sampleN); static double pbarStd(double pbar, int sampleN, int populationN); static int minimumSampleSizeForMaximumXbarStd(double maximumXbarStd, double populationStd); static int minimumSampleSizeForMaximumXbarStd(double maximumXbarStd, double populationStd, int populationN); static int minimumSampleSizeForGivenDandMaximumRisk(double d, double aLevel, double populationStd); static int minimumSampleSizeForGivenDandMaximumRisk(double d, double aLevel, double populationStd, int populationN);  }### Answer:
@Test public void testWeightedProbabilitySampling() { logger.info("weightedProbabilitySampling"); AssociativeArray frequencyTable = new AssociativeArray(); frequencyTable.put(1, 0.20); frequencyTable.put(2, 0.30); frequencyTable.put(3, 0.25); frequencyTable.put(4, 0.25); int n = 100; boolean withReplacement = true; double expResult = n; FlatDataCollection sampledIds = SimpleRandomSampling.weightedSampling(frequencyTable, n, withReplacement); double result = sampledIds.size(); assertEquals(expResult, result, Constants.DOUBLE_ACCURACY_HIGH); }
@Test public void testWeightedSampling() { logger.info("weightedSampling"); AssociativeArray frequencyTable = new AssociativeArray(); frequencyTable.put(1, 10); frequencyTable.put(2, 20); frequencyTable.put(3, 30); frequencyTable.put(4, 40); int n = 100; boolean withReplacement = true; double expResult = n; FlatDataCollection sampledIds = SimpleRandomSampling.weightedSampling(frequencyTable, n, withReplacement); double result = sampledIds.size(); assertEquals(expResult, result, Constants.DOUBLE_ACCURACY_HIGH); }