@Test public void testHLLNoBiasDisabledHalfDistinct() { Random rand = new Random(size); HyperLogLog hll = HyperLogLog.builder().enableNoBias(false).build(); int unique = size / 2; Set<Long> hashset = new HashSet<>(); for (int i = 0; i < size; i++) { long val = rand.nextInt(unique); hashset.add(val); hll.addLong(val); } double threshold = size > 40000 ? biasedlongRangeTolerance : shortRangeTolerance; double delta = threshold * hashset.size() / 100; assertEquals((double) hashset.size(), (double) hll.count(), delta); }
@Test public void testHLLNoBiasDisabled() { Random rand = new Random(size); HyperLogLog hll = HyperLogLog.builder().enableNoBias(false).build(); int size = 100; for (int i = 0; i < size; i++) { hll.addLong(rand.nextLong()); } double threshold = size > 40000 ? biasedlongRangeTolerance : shortRangeTolerance; double delta = threshold * size / 100; assertEquals((double) size, (double) hll.count(), delta); }
/** * Reduces the accuracy of the HLL provided to a smaller size * @param p0 * - new p size for the new HyperLogLog (smaller or no change) * @return reduced (or same) HyperLogLog instance */ public HyperLogLog squash(final int p0) { if (p0 > p) { throw new IllegalArgumentException( "HyperLogLog cannot be be squashed to be bigger. Current: " + toString() + " Provided: " + p0); } if (p0 == p) { return this; } final HyperLogLog hll = new HyperLogLogBuilder() .setNumRegisterIndexBits(p0).setEncoding(EncodingType.DENSE) .enableNoBias(noBias).build(); final HLLDenseRegister result = hll.denseRegister; if (encoding == EncodingType.SPARSE) { sparseRegister.extractLowBitsTo(result); } else if (encoding == EncodingType.DENSE) { denseRegister.extractLowBitsTo(result); } return hll; }
/** * Reduces the accuracy of the HLL provided to a smaller size * @param p0 * - new p size for the new HyperLogLog (smaller or no change) * @return reduced (or same) HyperLogLog instance */ public HyperLogLog squash(final int p0) { if (p0 > p) { throw new IllegalArgumentException( "HyperLogLog cannot be be squashed to be bigger. Current: " + toString() + " Provided: " + p0); } if (p0 == p) { return this; } final HyperLogLog hll = new HyperLogLogBuilder() .setNumRegisterIndexBits(p0).setEncoding(EncodingType.DENSE) .enableNoBias(noBias).build(); final HLLDenseRegister result = hll.denseRegister; if (encoding == EncodingType.SPARSE) { sparseRegister.extractLowBitsTo(result); } else if (encoding == EncodingType.DENSE) { denseRegister.extractLowBitsTo(result); } return hll; }