@Test
public void testErrorBound()
throws Exception
{
int trials = 20;
BinomialDistribution binomial = new BinomialDistribution(trials, 0.5);
int successes = 0;
Random rand = new Random(0);
for (int i = 0; i < trials; i++) {
int sum = 1_000;
List<TupleInfo> tupleInfos = ImmutableList.of(TupleInfo.SINGLE_LONG, TupleInfo.SINGLE_LONG);
PageBuilder builder = new PageBuilder(tupleInfos);
for (int j = 0; j < sum; j++) {
if (rand.nextDouble() < 0.5) {
builder.getBlockBuilder(0).append(1);
builder.getBlockBuilder(1).append(2);
}
}
BootstrappedAggregation function = new BootstrappedAggregation(LONG_SUM);
successes += approximateAggregationWithinErrorBound(function, 1, 0.5, sum, 0, builder.build()) ? 1 : 0;
}
// Since we used a confidence of 0.5, successes should have a binomial distribution B(n=20, p=0.5)
assertTrue(binomial.inverseCumulativeProbability(0.01) < successes && successes < binomial.inverseCumulativeProbability(0.99));
}