Skip to content

Commit

Permalink
Implement LiLSkipFunction
Browse files Browse the repository at this point in the history
  • Loading branch information
gstamatelat committed Jul 10, 2018
1 parent 003ca7f commit 0c0f556
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/main/java/gr/james/sampling/LiLSampling.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,31 @@ long skipLength(long streamSize, int sampleSize, Random random) {
W = W * Math.pow(random2, 1.0 / sampleSize);
return skip;
}

@Deprecated
private static class LiLSkipFunction implements SkipFunction {
private final int sampleSize;
private final Random random;
private double W;

public LiLSkipFunction(int sampleSize, Random random) {
this.sampleSize = sampleSize;
this.random = random;
this.W = Math.pow(RandomSamplingUtils.randomExclusive(random), 1.0 / sampleSize);
}

@Override
public long skip() throws StreamOverflowException {
final double random1 = RandomSamplingUtils.randomExclusive(random);
final double random2 = RandomSamplingUtils.randomExclusive(random);
long skip = (long) (Math.log(random1) / Math.log(1 - W));
assert skip >= 0 || skip == Long.MIN_VALUE;
if (skip == Long.MIN_VALUE) { // Sometimes when W is very small, 1 - W = 1 and Math.log(1) = +0 instead of -0
skip = Long.MAX_VALUE; // This results in negative infinity skip
}
// W = W * Math.exp(Math.log(random2) / sampleSize);
W = W * Math.pow(random2, 1.0 / sampleSize);
return skip;
}
}
}

0 comments on commit 0c0f556

Please sign in to comment.