diff --git a/src/main/java/gr/james/sampling/ChaoSampling.java b/src/main/java/gr/james/sampling/ChaoSampling.java index 86ab711..a32acca 100644 --- a/src/main/java/gr/james/sampling/ChaoSampling.java +++ b/src/main/java/gr/james/sampling/ChaoSampling.java @@ -6,7 +6,8 @@ * Implementation of the algorithm by Chao in A general purpose unequal probability sampling plan. *

* According to this algorithm, the probability of an item to be in the final sample is proportional to its relative - * weight. Weights must be in the range (0,+Inf), otherwise an {@link IllegalWeightException} is thrown. + * weight. Weights must be in the range (0,+Inf), otherwise an {@link IllegalWeightException} is thrown. The default + * weight in this implementation is {@code 1.0}. *

* This implementation throws {@link StreamOverflowException} if the sum of the weights of the items feeded is * {@link Double#POSITIVE_INFINITY}. @@ -295,7 +296,7 @@ public final long streamSize() { */ @Override public boolean feed(T item) { - return WeightedRandomSampling.super.feed(item); + return this.feed(item, 1.0); } /** diff --git a/src/main/java/gr/james/sampling/EfraimidisSampling.java b/src/main/java/gr/james/sampling/EfraimidisSampling.java index f71a14d..a4455bf 100644 --- a/src/main/java/gr/james/sampling/EfraimidisSampling.java +++ b/src/main/java/gr/james/sampling/EfraimidisSampling.java @@ -7,7 +7,7 @@ *

* According to this algorithm, the relative weight determines the probability that the item is selected in each of the * explicit or implicit item selections of the sampling procedure. Weights must be in the range (0,+Inf), otherwise an - * {@link IllegalWeightException} is thrown. + * {@link IllegalWeightException} is thrown. The default weight in this implementation is {@code 1.0}. *

* This implementation never throws {@link StreamOverflowException}. *

@@ -226,7 +226,7 @@ public final long streamSize() { */ @Override public boolean feed(T item) { - return WeightedRandomSampling.super.feed(item); + return this.feed(item, 1.0); } /** diff --git a/src/main/java/gr/james/sampling/ParetoSampling.java b/src/main/java/gr/james/sampling/ParetoSampling.java index bba21be..c881ddb 100644 --- a/src/main/java/gr/james/sampling/ParetoSampling.java +++ b/src/main/java/gr/james/sampling/ParetoSampling.java @@ -7,9 +7,8 @@ *

* Weighted are not being assigned a particular meaning or have physical interpretation but the resulting inclusion * probabilities are an approximation of the exact model ({@link ChaoSampling}). Weights must be in the range (0,+Inf) - * but not the value {@code 1.0}, otherwise an {@link IllegalWeightException} is thrown. A side effect of this is that - * the signatures {@link #feed(Object)}, {@link #feed(Iterable)} and {@link #feed(Iterator)} will always throw - * {@link IllegalWeightException}. + * but not the value {@code 1.0}, otherwise an {@link IllegalWeightException} is thrown. The default weight in this + * implementation is {@code 0.5}. *

* This implementation never throws {@link StreamOverflowException}. *

@@ -141,7 +140,6 @@ public boolean feed(T item, double weight) { // Calculate item weight final Weighted newItem = new Weighted<>(item, (r * (1 - weight)) / ((1 - r) * weight)); - assert newItem.weight >= 0.0; // weight can also be 0.0 because of double precision // Add item to reservoir if (pq.size() < sampleSize) { @@ -232,7 +230,7 @@ public final long streamSize() { */ @Override public boolean feed(T item) { - return WeightedRandomSampling.super.feed(item); + return this.feed(item, 0.5); } /** diff --git a/src/main/java/gr/james/sampling/SequentialPoissonSampling.java b/src/main/java/gr/james/sampling/SequentialPoissonSampling.java index f7c8fee..d9ee257 100644 --- a/src/main/java/gr/james/sampling/SequentialPoissonSampling.java +++ b/src/main/java/gr/james/sampling/SequentialPoissonSampling.java @@ -7,7 +7,7 @@ *

* Weighted are not being assigned a particular meaning or have physical interpretation but the resulting inclusion * probabilities are an approximation of the exact model ({@link ChaoSampling}). Weights must be in the range (0,+Inf), - * otherwise an {@link IllegalWeightException} is thrown. + * otherwise an {@link IllegalWeightException} is thrown. The default weight in this implementation is {@code 1.0}. *

* This implementation never throws {@link StreamOverflowException}. *

@@ -226,7 +226,7 @@ public final long streamSize() { */ @Override public boolean feed(T item) { - return WeightedRandomSampling.super.feed(item); + return this.feed(item, 1.0); } /** diff --git a/src/main/java/gr/james/sampling/WeightedRandomSampling.java b/src/main/java/gr/james/sampling/WeightedRandomSampling.java index dd30cc3..a0f9e00 100644 --- a/src/main/java/gr/james/sampling/WeightedRandomSampling.java +++ b/src/main/java/gr/james/sampling/WeightedRandomSampling.java @@ -125,9 +125,10 @@ default boolean feed(Map items) { /** * {@inheritDoc} *

- * This method uses the value {@code 1.0} as weight and is equivalent to + * This method uses a value {@code z} set by the specific implementation as weight that guarantees legality. Hence, + * this method is equivalent to *


-     * feed(item, 1.0);
+     * feed(item, z);
      * 
* * @param item {@inheritDoc} @@ -143,7 +144,7 @@ default boolean feed(T item) { /** * {@inheritDoc} *

- * This method uses the value {@code 1.0} as weight. + * This method uses an implementation specific value as weight. * * @param items {@inheritDoc} * @return {@inheritDoc} @@ -158,7 +159,7 @@ default boolean feed(Iterator items) { /** * {@inheritDoc} *

- * This method uses the value {@code 1.0} as weight. + * This method uses an implementation specific value as weight. * * @param items {@inheritDoc} * @return {@inheritDoc} diff --git a/src/test/java/gr/james/sampling/RandomSamplingTest.java b/src/test/java/gr/james/sampling/RandomSamplingTest.java index c094591..16ed1fa 100644 --- a/src/test/java/gr/james/sampling/RandomSamplingTest.java +++ b/src/test/java/gr/james/sampling/RandomSamplingTest.java @@ -73,11 +73,7 @@ public void correctness() { final RandomSampling alg = impl.get(); for (int i = 0; i < STREAM; i++) { - if (alg instanceof ParetoSampling) { - ((ParetoSampling) alg).feed(i, 0.5); - } else { - alg.feed(i); - } + alg.feed(i); } for (int s : alg.sample()) { @@ -106,7 +102,7 @@ public void correctness() { int c = d.get(i); final double expected = (double) REPS * Math.min(SAMPLE, STREAM) / STREAM; final double actual = (double) c; - assertEquals(1, actual / expected, 1e-2); + assertEquals(String.format("Correctness failed for streamSize %d", STREAM), 1, actual / expected, 1e-2); } } }