This is an automated email from the ASF dual-hosted git repository. aherbert pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-rng.git
commit f43200e36f70d8ea21a7ea9aeb39ee5f37d989f3 Author: aherbert <[email protected]> AuthorDate: Thu Nov 28 17:02:39 2019 +0000 [RNG-126] PoissonSamplerCache to return a SharedStateDiscreteSampler. --- .../PoissonSamplerCachePerformance.java | 4 ++-- .../sampling/distribution/PoissonSamplerCache.java | 22 ++++++++++++++++- .../distribution/PoissonSamplerCacheTest.java | 28 +++++++++++++++------- src/changes/changes.xml | 3 +++ 4 files changed, 46 insertions(+), 11 deletions(-) diff --git a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/PoissonSamplerCachePerformance.java b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/PoissonSamplerCachePerformance.java index 80e4381..363157e 100644 --- a/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/PoissonSamplerCachePerformance.java +++ b/commons-rng-examples/examples-jmh/src/main/java/org/apache/commons/rng/examples/jmh/sampling/distribution/PoissonSamplerCachePerformance.java @@ -330,7 +330,7 @@ public class PoissonSamplerCachePerformance { final PoissonSamplerFactory factory = new PoissonSamplerFactory() { @Override public DiscreteSampler createPoissonSampler(double mean) { - return cache.createPoissonSampler(r, mean); + return cache.createSharedStateSampler(r, mean); } }; runSample(factory, range, bh); @@ -351,7 +351,7 @@ public class PoissonSamplerCachePerformance { final PoissonSamplerFactory factory = new PoissonSamplerFactory() { @Override public DiscreteSampler createPoissonSampler(double mean) { - return cache.createPoissonSampler(r, mean); + return cache.createSharedStateSampler(r, mean); } }; runSample(factory, range, bh); diff --git a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/PoissonSamplerCache.java b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/PoissonSamplerCache.java index a294783..1ee8f0f 100644 --- a/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/PoissonSamplerCache.java +++ b/commons-rng-sampling/src/main/java/org/apache/commons/rng/sampling/distribution/PoissonSamplerCache.java @@ -151,9 +151,29 @@ public class PoissonSamplerCache { * @return A Poisson sampler * @throws IllegalArgumentException if {@code mean <= 0} or * {@code mean >} {@link Integer#MAX_VALUE}. + * @deprecated Use {@link #createSharedStateSampler(UniformRandomProvider, double)}. */ + @Deprecated public DiscreteSampler createPoissonSampler(UniformRandomProvider rng, double mean) { + return createSharedStateSampler(rng, mean); + } + + /** + * Creates a new Poisson sampler. + * + * <p>The returned sampler will function exactly the + * same as {@link PoissonSampler#of(UniformRandomProvider, double)}. + * + * @param rng Generator of uniformly distributed random numbers. + * @param mean Mean. + * @return A Poisson sampler + * @throws IllegalArgumentException if {@code mean <= 0} or + * {@code mean >} {@link Integer#MAX_VALUE}. + * @since 1.4 + */ + public SharedStateDiscreteSampler createSharedStateSampler(UniformRandomProvider rng, + double mean) { // Ensure the same functionality as the PoissonSampler by // using a SmallMeanPoissonSampler under the switch point. if (mean < PoissonSampler.PIVOT) { @@ -218,7 +238,7 @@ public class PoissonSamplerCache { * {@code false}. * * <p>The cache can still be used to create a {@link PoissonSampler} using - * {@link #createPoissonSampler(UniformRandomProvider, double)}. + * {@link #createSharedStateSampler(UniformRandomProvider, double)}. * * <p>This method can be used to determine if the cache has a potential * performance benefit. diff --git a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/PoissonSamplerCacheTest.java b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/PoissonSamplerCacheTest.java index 54c6fbf..7be442d 100644 --- a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/PoissonSamplerCacheTest.java +++ b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/distribution/PoissonSamplerCacheTest.java @@ -241,28 +241,28 @@ public class PoissonSamplerCacheTest { // Edge cases for creating a Poisson sampler /** - * Test createPoissonSampler() with a bad mean. + * Test createSharedStateSampler() with a bad mean. * * <p>Note this test actually tests the SmallMeanPoissonSampler throws. */ @Test(expected = IllegalArgumentException.class) - public void testCreatePoissonSamplerThrowsWithZeroMean() { + public void testCreateSharedStateSamplerThrowsWithZeroMean() { final RestorableUniformRandomProvider rng = RandomSource.create(RandomSource.SPLIT_MIX_64); final PoissonSamplerCache cache = createPoissonSamplerCache(); - cache.createPoissonSampler(rng, 0); + cache.createSharedStateSampler(rng, 0); } /** - * Test createPoissonSampler() with a mean that is too large. + * Test createSharedStateSampler() with a mean that is too large. */ @Test(expected = IllegalArgumentException.class) - public void testCreatePoissonSamplerThrowsWithNonIntegerMean() { + public void testCreateSharedStateSamplerThrowsWithNonIntegerMean() { final RestorableUniformRandomProvider rng = RandomSource.create(RandomSource.SPLIT_MIX_64); final PoissonSamplerCache cache = createPoissonSamplerCache(); final double mean = Integer.MAX_VALUE + 1.0; - cache.createPoissonSampler(rng, mean); + cache.createSharedStateSampler(rng, mean); } // Sampling tests @@ -385,7 +385,7 @@ public class PoissonSamplerCacheTest { PoissonSamplerCache cache, double mean) { final DiscreteSampler s1 = PoissonSampler.of(rng1, mean); - final DiscreteSampler s2 = cache.createPoissonSampler(rng2, mean); + final DiscreteSampler s2 = cache.createSharedStateSampler(rng2, mean); for (int j = 0; j < 10; j++) { Assert.assertEquals(s1.sample(), s2.sample()); } @@ -468,7 +468,7 @@ public class PoissonSamplerCacheTest { // Test all means in the test range (which may be different // from the cache range). for (int i = minMean; i <= maxMean; i++) { - cache.createPoissonSampler(rng1, i); + cache.createSharedStateSampler(rng1, i); } final PoissonSamplerCache cache2 = cache.withRange(minMean2, maxMean2); @@ -486,4 +486,16 @@ public class PoissonSamplerCacheTest { testPoissonSamples(rng1, rng2, cache2, i + 0.5); } } + + /** + * Explicit test for the deprecated method createPoissonSampler(). + * All other uses of this method have been removed. + */ + @SuppressWarnings("deprecation") + @Test + public void testCreatePoissonSampler() { + final PoissonSamplerCache cache = createPoissonSamplerCache(0, 100); + final DiscreteSampler s2 = cache.createPoissonSampler(null, 42); + Assert.assertTrue(s2 instanceof LargeMeanPoissonSampler); + } } diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 26e54ed..fe0fe49 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -75,6 +75,9 @@ re-run tests that fail, and pass the build if they succeed within the allotted number of reruns (the test will be marked as 'flaky' in the report). "> + <action dev="aherbert" type="add" issue="126"> + "PoissonSamplerCache": Method to return a SharedStateDiscreteSampler. + </action> <action dev="aherbert" type="add" issue="124"> Add fixed increment versions of the PCG generators. </action>
