commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject [52/53] [math] MATH-1158
Date Fri, 25 Mar 2016 15:19:37 GMT
http://git-wip-us.apache.org/repos/asf/commons-math/blob/ae2c81ad/src/test/java/org/apache/commons/math4/distribution/ZipfDistributionTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/distribution/ZipfDistributionTest.java
b/src/test/java/org/apache/commons/math4/distribution/ZipfDistributionTest.java
index eeae81f..de23a26 100644
--- a/src/test/java/org/apache/commons/math4/distribution/ZipfDistributionTest.java
+++ b/src/test/java/org/apache/commons/math4/distribution/ZipfDistributionTest.java
@@ -20,9 +20,7 @@ package org.apache.commons.math4.distribution;
 import org.apache.commons.math4.TestUtils;
 import org.apache.commons.math4.distribution.ZipfDistribution.ZipfRejectionInversionSampler;
 import org.apache.commons.math4.exception.NotStrictlyPositiveException;
-import org.apache.commons.math4.random.AbstractRandomGenerator;
-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.util.FastMath;
 import org.junit.Assert;
 import org.junit.Ignore;
@@ -150,15 +148,18 @@ public class ZipfDistributionTest extends IntegerDistributionAbstractTest
{
                     weightSum += weights[i-1];
                 }
 
-                ZipfDistribution distribution = new ZipfDistribution(numPoints, exponent);
-                distribution.reseedRandomGenerator(6); // use fixed seed, the test is expected
to fail for more than 50% of all seeds because each test case can fail with probability 0.001,
the chance that all test cases do not fail is 0.999^(32*22) = 0.49442874426
+                // Use fixed seed, the test is expected to fail for more than 50% of all
+                // seeds because each test case can fail with probability 0.001, the chance
+                // that all test cases do not fail is 0.999^(32*22) = 0.49442874426
+                IntegerDistribution.Sampler distribution =
+                    new ZipfDistribution(numPoints, exponent).createSampler(RandomSource.create(RandomSource.WELL_19937_C,
6));
 
                 double[] expectedCounts = new double[numPoints];
                 long[] observedCounts = new long[numPoints];
                 for (int i = 0; i < numPoints; i++) {
                     expectedCounts[i] = sampleSize * (weights[i]/weightSum);
                 }
-                int[] sample = distribution.sample(sampleSize);
+                int[] sample = AbstractIntegerDistribution.sample(sampleSize, distribution);
                 for (int s : sample) {
                     observedCounts[s-1]++;
                 }
@@ -177,11 +178,11 @@ public class ZipfDistributionTest extends IntegerDistributionAbstractTest
{
         };
         for (final double testValue : testValues) {
             final double expected = FastMath.log1p(testValue);
-            TestUtils.assertRelativelyEquals(expected, ZipfRejectionInversionSampler.helper1(testValue)*testValue,
tol);
+            final double actual = ZipfRejectionInversionSampler.helper1(testValue) * testValue;
+            TestUtils.assertRelativelyEquals(expected, actual, tol);
         }
     }
 
-
     @Test
     public void testSamplerHelper1Minus1() {
         Assert.assertEquals(Double.POSITIVE_INFINITY, ZipfRejectionInversionSampler.helper1(-1d),
0d);
@@ -197,7 +198,8 @@ public class ZipfDistributionTest extends IntegerDistributionAbstractTest
{
         };
         for (double testValue : testValues) {
             final double expected = FastMath.expm1(testValue);
-            TestUtils.assertRelativelyEquals(expected, ZipfRejectionInversionSampler.helper2(testValue)*testValue,
tol);
+            final double actual = ZipfRejectionInversionSampler.helper2(testValue) * testValue;
+            TestUtils.assertRelativelyEquals(expected, actual, tol);
         }
     }
 
@@ -215,22 +217,8 @@ public class ZipfDistributionTest extends IntegerDistributionAbstractTest
{
                 long start = System.currentTimeMillis();
                 final int[] randomNumberCounter = new int[1];
 
-                RandomGenerator randomGenerator  = new AbstractRandomGenerator() {
-
-                    private final RandomGenerator r = new Well1024a(0L);
-
-                    @Override
-                    public void setSeed(long seed) {
-                    }
-
-                    @Override
-                    public double nextDouble() {
-                        randomNumberCounter[0]+=1;
-                        return r.nextDouble();
-                    }
-                };
-
-                final ZipfDistribution distribution = new ZipfDistribution(randomGenerator,
numPoints, exponent);
+                final IntegerDistribution.Sampler distribution =
+                    new ZipfDistribution(numPoints, exponent).createSampler(RandomSource.create(RandomSource.WELL_1024_A));
                 for (int i = 0; i < numGeneratedSamples; ++i) {
                     sum += distribution.sample();
                 }

http://git-wip-us.apache.org/repos/asf/commons-math/blob/ae2c81ad/src/test/java/org/apache/commons/math4/random/RandomDataGeneratorTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/random/RandomDataGeneratorTest.java b/src/test/java/org/apache/commons/math4/random/RandomDataGeneratorTest.java
index 2982efb..d7c4906 100644
--- a/src/test/java/org/apache/commons/math4/random/RandomDataGeneratorTest.java
+++ b/src/test/java/org/apache/commons/math4/random/RandomDataGeneratorTest.java
@@ -1209,18 +1209,4 @@ public class RandomDataGeneratorTest {
         }
         TestUtils.assertChiSquareAccept(densityPoints, expectedCounts, observedCounts, .001);
     }
-
-    @Test
-    /**
-     * MATH-720
-     */
-    public void testReseed() {
-        PoissonDistribution x = new PoissonDistribution(3.0);
-        x.reseedRandomGenerator(0);
-        final double u = x.sample();
-        PoissonDistribution y = new PoissonDistribution(3.0);
-        y.reseedRandomGenerator(0);
-        Assert.assertEquals(u, y.sample(), 0);
-    }
-
 }

http://git-wip-us.apache.org/repos/asf/commons-math/blob/ae2c81ad/src/test/java/org/apache/commons/math4/stat/descriptive/AggregateSummaryStatisticsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/stat/descriptive/AggregateSummaryStatisticsTest.java
b/src/test/java/org/apache/commons/math4/stat/descriptive/AggregateSummaryStatisticsTest.java
index 139a060..43ca593 100644
--- a/src/test/java/org/apache/commons/math4/stat/descriptive/AggregateSummaryStatisticsTest.java
+++ b/src/test/java/org/apache/commons/math4/stat/descriptive/AggregateSummaryStatisticsTest.java
@@ -282,7 +282,9 @@ public class AggregateSummaryStatisticsTest {
      * @return array of random double values
      */
     private double[] generateSample() {
-        final IntegerDistribution size = new UniformIntegerDistribution(10, 100);
+        final IntegerDistribution.Sampler size =
+            new UniformIntegerDistribution(10, 100).createSampler(RandomSource.create(RandomSource.WELL_512_A,
+                                                                                      327652));
         final RealDistribution.Sampler randomData
             = new UniformRealDistribution(-100, 100).createSampler(RandomSource.create(RandomSource.WELL_512_A,
                                                                                        64925784252L));;
@@ -312,7 +314,9 @@ public class AggregateSummaryStatisticsTest {
             if (i == 4 || cur == length - 1) {
                 next = length - 1;
             } else {
-                next = (new UniformIntegerDistribution(cur, length - 1)).sample();
+                final IntegerDistribution.Sampler sampler =
+                    new UniformIntegerDistribution(cur, length - 1).createSampler(RandomSource.create(RandomSource.WELL_512_A));
+                next = sampler.sample();
             }
             final int subLength = next - cur + 1;
             out[i] = new double[subLength];

http://git-wip-us.apache.org/repos/asf/commons-math/blob/ae2c81ad/src/test/java/org/apache/commons/math4/stat/descriptive/UnivariateStatisticAbstractTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/stat/descriptive/UnivariateStatisticAbstractTest.java
b/src/test/java/org/apache/commons/math4/stat/descriptive/UnivariateStatisticAbstractTest.java
index cd5be56..38e844d 100644
--- a/src/test/java/org/apache/commons/math4/stat/descriptive/UnivariateStatisticAbstractTest.java
+++ b/src/test/java/org/apache/commons/math4/stat/descriptive/UnivariateStatisticAbstractTest.java
@@ -179,7 +179,9 @@ public abstract class UnivariateStatisticAbstractTest {
 
         // Fill weights array with random int values between 1 and 5
         int[] intWeights = new int[len];
-        final IntegerDistribution weightDist = new UniformIntegerDistribution(1, 5);
+        final IntegerDistribution.Sampler weightDist =
+            new UniformIntegerDistribution(1, 5).createSampler(RandomSource.create(RandomSource.WELL_512_A,
+                                                                                   234878544L));
         for (int i = 0; i < len; i++) {
             intWeights[i] = weightDist.sample();
             weights[i] = intWeights[i];
@@ -188,9 +190,9 @@ public abstract class UnivariateStatisticAbstractTest {
         // Fill values array with random data from N(mu, sigma)
         // and fill valuesList with values from values array with
         // values[i] repeated weights[i] times, each i
-        final RealDistribution.Sampler valueDist
-            = new NormalDistribution(mu, sigma).createSampler(RandomSource.create(RandomSource.WELL_512_A,
-                                                                                  64925784252L));
+        final RealDistribution.Sampler valueDist =
+            new NormalDistribution(mu, sigma).createSampler(RandomSource.create(RandomSource.WELL_512_A,
+                                                                                64925784252L));
         List<Double> valuesList = new ArrayList<Double>();
         for (int i = 0; i < len; i++) {
             double value = valueDist.sample();

http://git-wip-us.apache.org/repos/asf/commons-math/blob/ae2c81ad/src/test/java/org/apache/commons/math4/util/MathArraysTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/util/MathArraysTest.java b/src/test/java/org/apache/commons/math4/util/MathArraysTest.java
index a6cc764..e4862fa 100644
--- a/src/test/java/org/apache/commons/math4/util/MathArraysTest.java
+++ b/src/test/java/org/apache/commons/math4/util/MathArraysTest.java
@@ -25,6 +25,7 @@ import org.apache.commons.math4.exception.NotANumberException;
 import org.apache.commons.math4.exception.NotPositiveException;
 import org.apache.commons.math4.exception.NotStrictlyPositiveException;
 import org.apache.commons.math4.exception.NullArgumentException;
+import org.apache.commons.math4.rng.RandomSource;
 import org.apache.commons.math4.random.Well1024a;
 import org.apache.commons.math4.util.FastMath;
 import org.apache.commons.math4.util.MathArrays;
@@ -1111,7 +1112,8 @@ public class MathArraysTest {
         final int[] orig = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
         final int[] list = orig.clone();
         final int start = 4;
-        MathArrays.shuffle(list, start, MathArrays.Position.TAIL, new Well1024a(7654321L));
+        MathArrays.shuffle(list, start, MathArrays.Position.TAIL,
+                           RandomSource.create(RandomSource.WELL_19937_C, 7654321L));
 
         // Ensure that all entries below index "start" did not move.
         for (int i = 0; i < start; i++) {
@@ -1134,7 +1136,8 @@ public class MathArraysTest {
         final int[] orig = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
         final int[] list = orig.clone();
         final int start = 4;
-        MathArrays.shuffle(list, start, MathArrays.Position.HEAD, new Well1024a(1234567L));
+        MathArrays.shuffle(list, start, MathArrays.Position.HEAD,
+                           RandomSource.create(RandomSource.WELL_19937_C, 1234567L));
 
         // Ensure that all entries above index "start" did not move.
         for (int i = start + 1; i < orig.length; i++) {

http://git-wip-us.apache.org/repos/asf/commons-math/blob/ae2c81ad/src/test/java/org/apache/commons/math4/util/ResizableDoubleArrayTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/util/ResizableDoubleArrayTest.java b/src/test/java/org/apache/commons/math4/util/ResizableDoubleArrayTest.java
index be897a5..bf87c3b 100644
--- a/src/test/java/org/apache/commons/math4/util/ResizableDoubleArrayTest.java
+++ b/src/test/java/org/apache/commons/math4/util/ResizableDoubleArrayTest.java
@@ -20,6 +20,7 @@ import org.apache.commons.math4.distribution.IntegerDistribution;
 import org.apache.commons.math4.distribution.UniformIntegerDistribution;
 import org.apache.commons.math4.exception.MathIllegalArgumentException;
 import org.apache.commons.math4.exception.NullArgumentException;
+import org.apache.commons.math4.rng.RandomSource;
 import org.apache.commons.math4.util.ResizableDoubleArray.ExpansionMode;
 import org.junit.After;
 import org.junit.Assert;
@@ -321,7 +322,8 @@ public class ResizableDoubleArrayTest extends DoubleArrayAbstractTest
{
         ResizableDoubleArray eDA2 = new ResizableDoubleArray(2);
         Assert.assertEquals("Initial number of elements should be 0", 0, eDA2.getNumElements());
 
-        final IntegerDistribution randomData = new UniformIntegerDistribution(100, 1000);
+        final IntegerDistribution.Sampler randomData =
+            new UniformIntegerDistribution(100, 1000).createSampler(RandomSource.create(RandomSource.WELL_19937_C));
         final int iterations = randomData.sample();
 
         for( int i = 0; i < iterations; i++) {
@@ -342,7 +344,9 @@ public class ResizableDoubleArrayTest extends DoubleArrayAbstractTest
{
         ResizableDoubleArray eDA3 = new ResizableDoubleArray(3, 3.0, 3.5);
         Assert.assertEquals("Initial number of elements should be 0", 0, eDA3.getNumElements()
);
 
-        final IntegerDistribution randomData = new UniformIntegerDistribution(100, 3000);
+        final IntegerDistribution.Sampler randomData =
+            new UniformIntegerDistribution(100, 3000).createSampler(RandomSource.create(RandomSource.WELL_19937_C));
+;
         final int iterations = randomData.sample();
 
         for( int i = 0; i < iterations; i++) {


Mime
View raw message