http://git-wip-us.apache.org/repos/asf/commons-math/blob/9867d9f2/src/test/java/org/apache/commons/math4/random/EmpiricalDistributionTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/math4/random/EmpiricalDistributionTest.java b/src/test/java/org/apache/commons/math4/random/EmpiricalDistributionTest.java index 4854979..3c0564f 100644 --- a/src/test/java/org/apache/commons/math4/random/EmpiricalDistributionTest.java +++ b/src/test/java/org/apache/commons/math4/random/EmpiricalDistributionTest.java @@ -36,8 +36,7 @@ import org.apache.commons.math4.distribution.UniformRealDistribution; import org.apache.commons.math4.exception.MathIllegalStateException; import org.apache.commons.math4.exception.NullArgumentException; import org.apache.commons.math4.exception.NotStrictlyPositiveException; -import org.apache.commons.math4.random.EmpiricalDistribution; -import org.apache.commons.math4.random.RandomGenerator; +import org.apache.commons.math4.rng.RandomSource; import org.apache.commons.math4.stat.descriptive.SummaryStatistics; import org.apache.commons.math4.util.FastMath; import org.junit.Assert; @@ -156,14 +155,27 @@ public final class EmpiricalDistributionTest extends RealDistributionAbstractTes } /** - * Make sure exception thrown if digest getNext is attempted - * before loading empiricalDistribution. + * Make sure exception thrown if sampling is attempted + * before loading empiricalDistribution. */ @Test - public void testNexFail() { + public void testNextFail1() { try { - empiricalDistribution.getNextValue(); - empiricalDistribution2.getNextValue(); + empiricalDistribution.createSampler(RandomSource.create(RandomSource.JDK)).sample(); + Assert.fail("Expecting MathIllegalStateException"); + } catch (MathIllegalStateException ex) { + // expected + } + } + + /** + * Make sure exception thrown if sampling is attempted + * before loading empiricalDistribution. + */ + @Test + public void testNextFail2() { + try { + empiricalDistribution2.createSampler(RandomSource.create(RandomSource.JDK)).sample(); Assert.fail("Expecting MathIllegalStateException"); } catch (MathIllegalStateException ex) { // expected @@ -245,36 +257,38 @@ public final class EmpiricalDistributionTest extends RealDistributionAbstractTes TestUtils.assertEquals(expectedGeneratorUpperBounds, dist.getGeneratorUpperBounds(), tol); } - @Test - public void testGeneratorConfig() { - double[] testData = {0, 1, 2, 3, 4}; - RandomGenerator generator = new RandomAdaptorTest.ConstantGenerator(0.5); - - EmpiricalDistribution dist = new EmpiricalDistribution(5, generator); - dist.load(testData); - for (int i = 0; i < 5; i++) { - Assert.assertEquals(2.0, dist.getNextValue(), 0d); - } - - // Verify no NPE with null generator argument - dist = new EmpiricalDistribution(5, (RandomGenerator) null); - dist.load(testData); - dist.getNextValue(); - } - - @Test - public void testReSeed() throws Exception { - empiricalDistribution.load(url); - empiricalDistribution.reSeed(100); - final double [] values = new double[10]; - for (int i = 0; i < 10; i++) { - values[i] = empiricalDistribution.getNextValue(); - } - empiricalDistribution.reSeed(100); - for (int i = 0; i < 10; i++) { - Assert.assertEquals(values[i],empiricalDistribution.getNextValue(), 0d); - } - } + // XXX REMOVE (test "embedded RNG" which is to be removed) +// @Test +// public void testGeneratorConfig() { +// double[] testData = {0, 1, 2, 3, 4}; +// RandomGenerator generator = new RandomAdaptorTest.ConstantGenerator(0.5); + +// EmpiricalDistribution dist = new EmpiricalDistribution(5, generator); +// dist.load(testData); +// for (int i = 0; i < 5; i++) { +// Assert.assertEquals(2.0, dist.getNextValue(), 0d); +// } + +// // Verify no NPE with null generator argument +// dist = new EmpiricalDistribution(5, (RandomGenerator) null); +// dist.load(testData); +// dist.getNextValue(); +// } + + // XXX REMOVE (test "embedded RNG" which is to be removed) +// @Test +// public void testReSeed() throws Exception { +// empiricalDistribution.load(url); +// empiricalDistribution.reSeed(100); +// final double [] values = new double[10]; +// for (int i = 0; i < 10; i++) { +// values[i] = empiricalDistribution.getNextValue(); +// } +// empiricalDistribution.reSeed(100); +// for (int i = 0; i < 10; i++) { +// Assert.assertEquals(values[i],empiricalDistribution.getNextValue(), 0d); +// } +// } private void verifySame(EmpiricalDistribution d1, EmpiricalDistribution d2) { Assert.assertEquals(d1.isLoaded(), d2.isLoaded()); @@ -290,10 +304,11 @@ public final class EmpiricalDistributionTest extends RealDistributionAbstractTes private void tstGen(double tolerance)throws Exception { empiricalDistribution.load(url); - empiricalDistribution.reSeed(1000); + RealDistribution.Sampler sampler + = empiricalDistribution.createSampler(RandomSource.create(RandomSource.WELL_19937_C, 1000)); SummaryStatistics stats = new SummaryStatistics(); for (int i = 1; i < 1000; i++) { - stats.addValue(empiricalDistribution.getNextValue()); + stats.addValue(sampler.sample()); } Assert.assertEquals("mean", 5.069831575018909, stats.getMean(),tolerance); Assert.assertEquals("std dev", 1.0173699343977738, stats.getStandardDeviation(),tolerance); @@ -301,10 +316,11 @@ public final class EmpiricalDistributionTest extends RealDistributionAbstractTes private void tstDoubleGen(double tolerance)throws Exception { empiricalDistribution2.load(dataArray); - empiricalDistribution2.reSeed(1000); + RealDistribution.Sampler sampler + = empiricalDistribution2.createSampler(RandomSource.create(RandomSource.WELL_19937_C, 1000)); SummaryStatistics stats = new SummaryStatistics(); for (int i = 1; i < 1000; i++) { - stats.addValue(empiricalDistribution2.getNextValue()); + stats.addValue(sampler.sample()); } Assert.assertEquals("mean", 5.069831575018909, stats.getMean(), tolerance); Assert.assertEquals("std dev", 1.0173699343977738, stats.getStandardDeviation(), tolerance); @@ -431,9 +447,10 @@ public final class EmpiricalDistributionTest extends RealDistributionAbstractTes } EmpiricalDistribution dist = new EmpiricalDistribution(10); dist.load(data); - dist.reseedRandomGenerator(1000); + RealDistribution.Sampler sampler + = dist.createSampler(RandomSource.create(RandomSource.WELL_19937_C, 1000)); for (int i = 0; i < 1000; i++) { - final double dev = dist.sample(); + final double dev = sampler.sample(); Assert.assertTrue(dev < 1); Assert.assertTrue(dev > 0); } @@ -447,9 +464,10 @@ public final class EmpiricalDistributionTest extends RealDistributionAbstractTes final double[] data = {0, 0, 1, 1}; EmpiricalDistribution dist = new EmpiricalDistribution(2); dist.load(data); - dist.reseedRandomGenerator(1000); + RealDistribution.Sampler sampler + = dist.createSampler(RandomSource.create(RandomSource.WELL_19937_C, 1000)); for (int i = 0; i < 1000; i++) { - final double dev = dist.sample(); + final double dev = sampler.sample(); Assert.assertTrue(dev == 0 || dev == 1); } Assert.assertEquals(0.5, dist.cumulativeProbability(0), Double.MIN_VALUE); @@ -489,10 +507,12 @@ public final class EmpiricalDistributionTest extends RealDistributionAbstractTes final EmpiricalDistribution dist = new ConstantKernelEmpiricalDistribution(5); final double[] data = {1d,2d,3d, 4d,5d,6d, 7d,8d,9d, 10d,11d,12d, 13d,14d,15d}; dist.load(data); + RealDistribution.Sampler sampler + = dist.createSampler(RandomSource.create(RandomSource.WELL_19937_C, 1000)); // Bin masses concentrated on 2, 5, 8, 11, 14 <- effectively discrete uniform distribution over these double[] values = {2d, 5d, 8d, 11d, 14d}; for (int i = 0; i < 20; i++) { - Assert.assertTrue(Arrays.binarySearch(values, dist.sample()) >= 0); + Assert.assertTrue(Arrays.binarySearch(values, sampler.sample()) >= 0); } final double tol = 10E-12; Assert.assertEquals(0.0, dist.cumulativeProbability(1), tol); @@ -515,11 +535,13 @@ public final class EmpiricalDistributionTest extends RealDistributionAbstractTes final EmpiricalDistribution dist = new UniformKernelEmpiricalDistribution(5); final double[] data = {1d,2d,3d, 4d,5d,6d, 7d,8d,9d, 10d,11d,12d, 13d,14d,15d}; dist.load(data); + RealDistribution.Sampler sampler + = dist.createSampler(RandomSource.create(RandomSource.WELL_19937_C, 1000)); // Kernels are uniform distributions on [1,3], [4,6], [7,9], [10,12], [13,15] final double bounds[] = {3d, 6d, 9d, 12d}; final double tol = 10E-12; for (int i = 0; i < 20; i++) { - final double v = dist.sample(); + final double v = sampler.sample(); // Make sure v is not in the excluded range between bins - that is (bounds[i], bounds[i] + 1) for (int j = 0; j < bounds.length; j++) { Assert.assertFalse(v > bounds[j] + tol && v < bounds[j] + 1 - tol); @@ -566,7 +588,7 @@ public final class EmpiricalDistributionTest extends RealDistributionAbstractTes } @Override protected RealDistribution getKernel(SummaryStatistics bStats) { - return new UniformRealDistribution(randomData.getRandomGenerator(), bStats.getMin(), bStats.getMax()); + return new UniformRealDistribution(bStats.getMin(), bStats.getMax()); } } }
http://git-wip-us.apache.org/repos/asf/commons-math/blob/9867d9f2/src/test/java/org/apache/commons/math4/stat/descriptive/rank/PSquarePercentileTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/math4/stat/descriptive/rank/PSquarePercentileTest.java b/src/test/java/org/apache/commons/math4/stat/descriptive/rank/PSquarePercentileTest.java index dfdcd4e..fdbf689 100644 --- a/src/test/java/org/apache/commons/math4/stat/descriptive/rank/PSquarePercentileTest.java +++ b/src/test/java/org/apache/commons/math4/stat/descriptive/rank/PSquarePercentileTest.java @@ -32,6 +32,7 @@ import org.apache.commons.math4.exception.NullArgumentException; import org.apache.commons.math4.exception.OutOfRangeException; import org.apache.commons.math4.random.RandomGenerator; import org.apache.commons.math4.random.Well19937c; +import org.apache.commons.math4.rng.RandomSource; import org.apache.commons.math4.stat.descriptive.StorelessUnivariateStatistic; import org.apache.commons.math4.stat.descriptive.StorelessUnivariateStatisticAbstractTest; import org.apache.commons.math4.stat.descriptive.UnivariateStatistic; @@ -709,6 +710,8 @@ public class PSquarePercentileTest extends VERY_LARGE = 10000000; private void doDistributionTest(RealDistribution distribution) { + final RealDistribution.Sampler sampler = + distribution.createSampler(RandomSource.create(RandomSource.WELL_19937_C, 1000)); double data[]; data = distribution.sample(VERY_LARGE); @@ -754,8 +757,8 @@ public class PSquarePercentileTest extends */ @Test public void testDistribution() { - doDistributionTest(new NormalDistribution(randomGenerator, 4000, 50)); - doDistributionTest(new LogNormalDistribution(randomGenerator,4000, 50)); + doDistributionTest(new NormalDistribution(4000, 50)); + doDistributionTest(new LogNormalDistribution(4000, 50)); // doDistributionTest((new ExponentialDistribution(4000)); // doDistributionTest(new GammaDistribution(5d,1d),0.1); } http://git-wip-us.apache.org/repos/asf/commons-math/blob/9867d9f2/src/test/java/org/apache/commons/math4/stat/descriptive/rank/PercentileTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/math4/stat/descriptive/rank/PercentileTest.java b/src/test/java/org/apache/commons/math4/stat/descriptive/rank/PercentileTest.java index 65a25b1..98b6f91 100644 --- a/src/test/java/org/apache/commons/math4/stat/descriptive/rank/PercentileTest.java +++ b/src/test/java/org/apache/commons/math4/stat/descriptive/rank/PercentileTest.java @@ -18,14 +18,16 @@ package org.apache.commons.math4.stat.descriptive.rank; import java.util.Arrays; +import org.apache.commons.math4.distribution.RealDistribution; +import org.apache.commons.math4.distribution.AbstractRealDistribution; import org.apache.commons.math4.distribution.NormalDistribution; import org.apache.commons.math4.exception.MathIllegalArgumentException; import org.apache.commons.math4.exception.NotANumberException; import org.apache.commons.math4.exception.NullArgumentException; import org.apache.commons.math4.exception.OutOfRangeException; -import org.apache.commons.math4.random.JDKRandomGenerator; -import org.apache.commons.math4.random.RandomGenerator; import org.apache.commons.math4.random.Well1024a; +import org.apache.commons.math4.rng.RandomSource; +import org.apache.commons.math4.rng.UniformRandomProvider; import org.apache.commons.math4.stat.descriptive.UnivariateStatistic; import org.apache.commons.math4.stat.descriptive.UnivariateStatisticAbstractTest; import org.apache.commons.math4.stat.descriptive.rank.Percentile.EstimationType; @@ -587,22 +589,23 @@ public class PercentileTest extends UnivariateStatisticAbstractTest{ @Test public void testStoredVsDirect() { - final RandomGenerator rand= new JDKRandomGenerator(); - rand.setSeed(Long.MAX_VALUE); - for (final int sampleSize:sampleSizes) { - final double[] data = new NormalDistribution(rand,4000, 50) - .sample(sampleSize); - for (final double p:new double[] {50d,95d}) { + final RealDistribution.Sampler sampler = + new NormalDistribution(4000, 50).createSampler(RandomSource.create(RandomSource.JDK, + Long.MAX_VALUE)); + + for (final int sampleSize : sampleSizes) { + final double[] data = AbstractRealDistribution.sample(sampleSize, sampler); + for (final double p : new double[] { 50d, 95d }) { for (final Percentile.EstimationType e : Percentile.EstimationType.values()) { reset(p, e); final Percentile pStoredData = getUnivariateStatistic(); pStoredData.setData(data); - final double storedDataResult=pStoredData.evaluate(); + final double storedDataResult = pStoredData.evaluate(); pStoredData.setData(null); final Percentile pDirect = getUnivariateStatistic(); - Assert.assertEquals("Sample="+sampleSize+",P="+p+" e="+e, - storedDataResult, - pDirect.evaluate(data),0d); + Assert.assertEquals("Sample=" + sampleSize + ", P=" + p + " e=" + e, + storedDataResult, + pDirect.evaluate(data), 0d); } } } http://git-wip-us.apache.org/repos/asf/commons-math/blob/9867d9f2/src/test/java/org/apache/commons/math4/stat/inference/KolmogorovSmirnovTestTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/math4/stat/inference/KolmogorovSmirnovTestTest.java b/src/test/java/org/apache/commons/math4/stat/inference/KolmogorovSmirnovTestTest.java index 5871fd3..0c9c0dd 100644 --- a/src/test/java/org/apache/commons/math4/stat/inference/KolmogorovSmirnovTestTest.java +++ b/src/test/java/org/apache/commons/math4/stat/inference/KolmogorovSmirnovTestTest.java @@ -23,8 +23,8 @@ import java.util.Arrays; import org.apache.commons.math4.TestUtils; import org.apache.commons.math4.distribution.NormalDistribution; import org.apache.commons.math4.distribution.UniformRealDistribution; -import org.apache.commons.math4.random.RandomGenerator; -import org.apache.commons.math4.random.Well19937c; +import org.apache.commons.math4.rng.RandomSource; +import org.apache.commons.math4.rng.UniformRandomProvider; import org.apache.commons.math4.util.CombinatoricsUtils; import org.apache.commons.math4.util.FastMath; import org.apache.commons.math4.util.MathArrays; @@ -318,7 +318,7 @@ public class KolmogorovSmirnovTestTest { */ @Test public void testTwoSampleMonteCarlo() { - final KolmogorovSmirnovTest test = new KolmogorovSmirnovTest(new Well19937c(1000)); + final KolmogorovSmirnovTest test = new KolmogorovSmirnovTest(RandomSource.WELL_19937_C, 1000); final int sampleSize = 14; final double tol = .001; final double[] shortUniform = new double[sampleSize]; @@ -345,7 +345,7 @@ public class KolmogorovSmirnovTestTest { @Test public void testTwoSampleMonteCarloDifferentSampleSizes() { - final KolmogorovSmirnovTest test = new KolmogorovSmirnovTest(new Well19937c(1000)); + final KolmogorovSmirnovTest test = new KolmogorovSmirnovTest(RandomSource.WELL_19937_C, 1000); final int sampleSize1 = 14; final int sampleSize2 = 7; final double d = 0.3; @@ -364,7 +364,7 @@ public class KolmogorovSmirnovTestTest { public void testTwoSampleMonteCarloPerformance() { int numIterations = 100_000; int N = (int)Math.sqrt(KolmogorovSmirnovTest.LARGE_SAMPLE_PRODUCT); - final KolmogorovSmirnovTest test = new KolmogorovSmirnovTest(new Well19937c(1000)); + final KolmogorovSmirnovTest test = new KolmogorovSmirnovTest(RandomSource.WELL_19937_C, 1000); for (int n = 2; n <= N; ++n) { long startMillis = System.currentTimeMillis(); int m = KolmogorovSmirnovTest.LARGE_SAMPLE_PRODUCT/n; @@ -500,7 +500,7 @@ public class KolmogorovSmirnovTestTest { public void testDRoundingMonteCarlo() { final double tol = 1e-2; final int iterations = 1000000; - final KolmogorovSmirnovTest test = new KolmogorovSmirnovTest(new Well19937c(1000)); + final KolmogorovSmirnovTest test = new KolmogorovSmirnovTest(RandomSource.WELL_19937_C, 1000); final double[] x = {0, 2, 3, 4, 5, 6, 7, 8, 9, 12}; final double[] y = {1, 10, 11, 13, 14, 15, 16, 17, 18}; @@ -519,14 +519,17 @@ public class KolmogorovSmirnovTestTest { } @Test - public void testFillBooleanArrayRandomlyWithFixedNumberTrueValues() { + public void testFillBooleanArrayRandomlyWithFixedNumberTrueValues() throws Exception { + Method method = KolmogorovSmirnovTest.class.getDeclaredMethod("fillBooleanArrayRandomlyWithFixedNumberTrueValues", + boolean[].class, Integer.TYPE, UniformRandomProvider.class); + method.setAccessible(true); final int[][] parameters = {{5, 1}, {5, 2}, {5, 3}, {5, 4}, {8, 1}, {8, 2}, {8, 3}, {8, 4}, {8, 5}, {8, 6}, {8, 7}}; final double alpha = 0.001; final int numIterations = 1000000; - final RandomGenerator rng = new Well19937c(0); + final UniformRandomProvider rng = RandomSource.create(RandomSource.WELL_19937_C, 0); for (final int[] parameter : parameters) { @@ -537,7 +540,7 @@ public class KolmogorovSmirnovTestTest { final long[] counts = new long[1 << arraySize]; for (int i = 0; i < numIterations; ++i) { - KolmogorovSmirnovTest.fillBooleanArrayRandomlyWithFixedNumberTrueValues(b, numberOfTrueValues, rng); + method.invoke(KolmogorovSmirnovTest.class, b, numberOfTrueValues, rng); int x = 0; for (int j = 0; j < arraySize; ++j) { x = ((x << 1) | ((b[j])?1:0)); @@ -576,7 +579,7 @@ public class KolmogorovSmirnovTestTest { public void testBootstrapSmallSamplesWithTies() { final double[] x = {0, 2, 4, 6, 8, 8, 10, 15, 22, 30, 33, 36, 38}; final double[] y = {9, 17, 20, 33, 40, 51, 60, 60, 72, 90, 101}; - final KolmogorovSmirnovTest test = new KolmogorovSmirnovTest(new Well19937c(2000)); + final KolmogorovSmirnovTest test = new KolmogorovSmirnovTest(RandomSource.WELL_19937_C, 2000); Assert.assertEquals(0.0059, test.bootstrap(x, y, 10000, false), 1E-3); } @@ -586,7 +589,7 @@ public class KolmogorovSmirnovTestTest { */ @Test public void testBootstrapLargeSamples() { - final KolmogorovSmirnovTest test = new KolmogorovSmirnovTest(new Well19937c(1000)); + final KolmogorovSmirnovTest test = new KolmogorovSmirnovTest(RandomSource.WELL_19937_C, 1000); Assert.assertEquals(0.0237, test.bootstrap(gaussian, gaussian2, 10000), 1E-2); } @@ -599,7 +602,7 @@ public class KolmogorovSmirnovTestTest { public void testBootstrapRounding() { final double[] x = {2,4,6,8,9,10,11,12,13}; final double[] y = {0,1,3,5,7}; - final KolmogorovSmirnovTest test = new KolmogorovSmirnovTest(new Well19937c(1000)); + final KolmogorovSmirnovTest test = new KolmogorovSmirnovTest(RandomSource.WELL_19937_C, 1000); Assert.assertEquals(0.06303, test.bootstrap(x, y, 10000, false), 1E-2); } @@ -629,7 +632,7 @@ public class KolmogorovSmirnovTestTest { final double[] fixedY = MathArrays.copyOf(y); checkFixTies(xP, yP); Assert.assertArrayEquals(fixedX, xP, 0); - Assert.assertArrayEquals(fixedY, yP, 0); + Assert.assertArrayEquals(fixedY, yP, 0); } @Test @@ -699,7 +702,7 @@ public class KolmogorovSmirnovTestTest { */ private static void fixTies(double[] x, double[] y) throws Exception { Method method = KolmogorovSmirnovTest.class.getDeclaredMethod("fixTies", - double[].class, double[].class); + double[].class, double[].class); method.setAccessible(true); method.invoke(KolmogorovSmirnovTest.class, x, y); } @@ -709,7 +712,7 @@ public class KolmogorovSmirnovTestTest { */ private static boolean hasTies(double[] x, double[] y) throws Exception { Method method = KolmogorovSmirnovTest.class.getDeclaredMethod("hasTies", - double[].class, double[].class); + double[].class, double[].class); method.setAccessible(true); return (boolean) method.invoke(KolmogorovSmirnovTest.class, x, y); }