@Test public void testOptimumSampleSize() { logger.info("optimumSampleSize"); int n = 0; AssociativeArray populationNh = new AssociativeArray(); populationNh.put(1,394); populationNh.put(2,461); populationNh.put(3,391); populationNh.put(4,334); populationNh.put(5,169); populationNh.put(6,113); populationNh.put(7,148); AssociativeArray populationStdh = new AssociativeArray(); populationStdh.put(1,8.3); populationStdh.put(2,13.3); populationStdh.put(3,15.1); populationStdh.put(4,19.8); populationStdh.put(5,24.5); populationStdh.put(6,26.0); populationStdh.put(7,35.0); double expResult = n; AssociativeArray sampleSizes = StratifiedSampling.optimumSampleSize(n, populationNh, populationStdh); double result = Descriptives.sum(sampleSizes.toFlatDataCollection()); assertEquals(expResult, result, Constants.DOUBLE_ACCURACY_HIGH); }
public static AssociativeArray optimumSampleSize(int n, AssociativeArray populationNh, AssociativeArray populationStdh) { AssociativeArray nh = new AssociativeArray(); double sumNhSh = 0.0; for(Map.Entry<Object, Object> entry : populationNh.entrySet()) { Object strata = entry.getKey(); Integer populationInStrata = ((Number)entry.getValue()).intValue(); Double populationStd = populationStdh.getDouble(strata); if(populationStd == null || populationInStrata <=0.0) { throw new IllegalArgumentException("Invalid strata population or strata std."); } double NhSh = populationInStrata*populationStd; sumNhSh+=NhSh; nh.put(strata, n*NhSh); } if(sumNhSh<=0) { throw new IllegalArgumentException("Invalid strata populations."); } for(Map.Entry<Object, Object> entry : nh.entrySet()) { Object strata = entry.getKey(); nh.put(strata, TypeInference.toDouble(entry.getValue())/sumNhSh); } return nh; }
StratifiedSampling { public static AssociativeArray optimumSampleSize(int n, AssociativeArray populationNh, AssociativeArray populationStdh) { AssociativeArray nh = new AssociativeArray(); double sumNhSh = 0.0; for(Map.Entry<Object, Object> entry : populationNh.entrySet()) { Object strata = entry.getKey(); Integer populationInStrata = ((Number)entry.getValue()).intValue(); Double populationStd = populationStdh.getDouble(strata); if(populationStd == null || populationInStrata <=0.0) { throw new IllegalArgumentException("Invalid strata population or strata std."); } double NhSh = populationInStrata*populationStd; sumNhSh+=NhSh; nh.put(strata, n*NhSh); } if(sumNhSh<=0) { throw new IllegalArgumentException("Invalid strata populations."); } for(Map.Entry<Object, Object> entry : nh.entrySet()) { Object strata = entry.getKey(); nh.put(strata, TypeInference.toDouble(entry.getValue())/sumNhSh); } return nh; } }
StratifiedSampling { public static AssociativeArray optimumSampleSize(int n, AssociativeArray populationNh, AssociativeArray populationStdh) { AssociativeArray nh = new AssociativeArray(); double sumNhSh = 0.0; for(Map.Entry<Object, Object> entry : populationNh.entrySet()) { Object strata = entry.getKey(); Integer populationInStrata = ((Number)entry.getValue()).intValue(); Double populationStd = populationStdh.getDouble(strata); if(populationStd == null || populationInStrata <=0.0) { throw new IllegalArgumentException("Invalid strata population or strata std."); } double NhSh = populationInStrata*populationStd; sumNhSh+=NhSh; nh.put(strata, n*NhSh); } if(sumNhSh<=0) { throw new IllegalArgumentException("Invalid strata populations."); } for(Map.Entry<Object, Object> entry : nh.entrySet()) { Object strata = entry.getKey(); nh.put(strata, TypeInference.toDouble(entry.getValue())/sumNhSh); } return nh; }  }
StratifiedSampling { public static AssociativeArray optimumSampleSize(int n, AssociativeArray populationNh, AssociativeArray populationStdh) { AssociativeArray nh = new AssociativeArray(); double sumNhSh = 0.0; for(Map.Entry<Object, Object> entry : populationNh.entrySet()) { Object strata = entry.getKey(); Integer populationInStrata = ((Number)entry.getValue()).intValue(); Double populationStd = populationStdh.getDouble(strata); if(populationStd == null || populationInStrata <=0.0) { throw new IllegalArgumentException("Invalid strata population or strata std."); } double NhSh = populationInStrata*populationStd; sumNhSh+=NhSh; nh.put(strata, n*NhSh); } if(sumNhSh<=0) { throw new IllegalArgumentException("Invalid strata populations."); } for(Map.Entry<Object, Object> entry : nh.entrySet()) { Object strata = entry.getKey(); nh.put(strata, TypeInference.toDouble(entry.getValue())/sumNhSh); } return nh; }  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 AssociativeArray optimumSampleSize(int n, AssociativeArray populationNh, AssociativeArray populationStdh) { AssociativeArray nh = new AssociativeArray(); double sumNhSh = 0.0; for(Map.Entry<Object, Object> entry : populationNh.entrySet()) { Object strata = entry.getKey(); Integer populationInStrata = ((Number)entry.getValue()).intValue(); Double populationStd = populationStdh.getDouble(strata); if(populationStd == null || populationInStrata <=0.0) { throw new IllegalArgumentException("Invalid strata population or strata std."); } double NhSh = populationInStrata*populationStd; sumNhSh+=NhSh; nh.put(strata, n*NhSh); } if(sumNhSh<=0) { throw new IllegalArgumentException("Invalid strata populations."); } for(Map.Entry<Object, Object> entry : nh.entrySet()) { Object strata = entry.getKey(); nh.put(strata, TypeInference.toDouble(entry.getValue())/sumNhSh); } return nh; }  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);  }