Skip to content

Commit

Permalink
EfraimidisSampling can have weights 0.0 and 1.0 (gstamatelat#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
gstamatelat committed Jan 27, 2018
1 parent a6144a7 commit 78198af
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/gr/james/sampling/EfraimidisSampling.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public EfraimidisSampling<T> feed(T item, double weight) {

// Calculate item weight
final Weighted<T> newItem = new Weighted<>(item, Math.pow(r, 1 / weight));
assert newItem.weight > 0.0 && newItem.weight < 1.0;
assert newItem.weight >= 0.0 && newItem.weight <= 1.0; // weight can also be 0.0 or 1.0 because of double precision

// Add item to reservoir
if (pq.size() < sampleSize) {
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/gr/james/sampling/WeightedRandomSamplingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,15 @@ public void stream() {
}
}

/**
* Pass very small and very large weights to check if algorithms fail.
*/
@Test
public void cornerWeights() {
final WeightedRandomSampling<Integer> alg = impl.get();
alg.feed(0, Double.MIN_VALUE);
alg.feed(1, Double.MAX_VALUE);
alg.sample();
}

}

0 comments on commit 78198af

Please sign in to comment.