You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2016/05/17 15:49:56 UTC

[23/25] [math] MATH-1335

MATH-1335

Use new RNG API.


Project: http://git-wip-us.apache.org/repos/asf/commons-math/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-math/commit/a9fdcd64
Tree: http://git-wip-us.apache.org/repos/asf/commons-math/tree/a9fdcd64
Diff: http://git-wip-us.apache.org/repos/asf/commons-math/diff/a9fdcd64

Branch: refs/heads/develop
Commit: a9fdcd64bf0a982901d298596151d13e56442a11
Parents: b720481
Author: Gilles <er...@apache.org>
Authored: Thu May 12 15:13:06 2016 +0200
Committer: Gilles <er...@apache.org>
Committed: Tue May 17 15:30:23 2016 +0200

----------------------------------------------------------------------
 .../math4/random/GaussianRandomGenerator.java   | 34 +++++++++++---------
 .../MultiStartMultivariateOptimizerTest.java    | 20 +++++-------
 .../CorrelatedRandomVectorGeneratorTest.java    | 16 +++------
 .../random/GaussianRandomGeneratorTest.java     |  9 ++----
 .../UncorrelatedRandomVectorGeneratorTest.java  |  8 ++---
 .../GLSMultipleLinearRegressionTest.java        | 13 ++++----
 6 files changed, 45 insertions(+), 55 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-math/blob/a9fdcd64/src/main/java/org/apache/commons/math4/random/GaussianRandomGenerator.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math4/random/GaussianRandomGenerator.java b/src/main/java/org/apache/commons/math4/random/GaussianRandomGenerator.java
index f3172c2..c70a7df 100644
--- a/src/main/java/org/apache/commons/math4/random/GaussianRandomGenerator.java
+++ b/src/main/java/org/apache/commons/math4/random/GaussianRandomGenerator.java
@@ -17,31 +17,35 @@
 
 package org.apache.commons.math4.random;
 
+import org.apache.commons.math4.rng.UniformRandomProvider;
+import org.apache.commons.math4.distribution.RealDistribution;
+import org.apache.commons.math4.distribution.NormalDistribution;
+
 /**
- * This class is a gaussian normalized random generator for scalars.
- * <p>This class is a simple wrapper around the {@link
- * RandomGenerator#nextGaussian} method.</p>
+ * Random generator that generates normally distributed samples.
+ *
  * @since 1.2
  */
-
 public class GaussianRandomGenerator implements NormalizedRandomGenerator {
+    /** Gaussian distribution sampler. */
+    private final RealDistribution.Sampler sampler;
 
-    /** Underlying generator. */
-    private final RandomGenerator generator;
-
-    /** Create a new generator.
-     * @param generator underlying random generator to use
+    /**
+     * Creates a new generator.
+     *
+     * @param generator Underlying random generator.
      */
-    public GaussianRandomGenerator(final RandomGenerator generator) {
-        this.generator = generator;
+    public GaussianRandomGenerator(final UniformRandomProvider generator) {
+        sampler = new NormalDistribution().createSampler(generator);
     }
 
-    /** Generate a random scalar with null mean and unit standard deviation.
-     * @return a random scalar with null mean and unit standard deviation
+    /**
+     * Generates a random scalar with zero mean and unit standard deviation.
+     *
+     * @return a random value sampled from a normal distribution.
      */
     @Override
     public double nextNormalizedDouble() {
-        return generator.nextGaussian();
+        return sampler.sample();
     }
-
 }

http://git-wip-us.apache.org/repos/asf/commons-math/blob/a9fdcd64/src/test/java/org/apache/commons/math4/optim/nonlinear/scalar/MultiStartMultivariateOptimizerTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/optim/nonlinear/scalar/MultiStartMultivariateOptimizerTest.java b/src/test/java/org/apache/commons/math4/optim/nonlinear/scalar/MultiStartMultivariateOptimizerTest.java
index cdaf3b9..d5b2b84 100644
--- a/src/test/java/org/apache/commons/math4/optim/nonlinear/scalar/MultiStartMultivariateOptimizerTest.java
+++ b/src/test/java/org/apache/commons/math4/optim/nonlinear/scalar/MultiStartMultivariateOptimizerTest.java
@@ -30,8 +30,9 @@ import org.apache.commons.math4.optim.nonlinear.scalar.gradient.CircleScalar;
 import org.apache.commons.math4.optim.nonlinear.scalar.gradient.NonLinearConjugateGradientOptimizer;
 import org.apache.commons.math4.optim.nonlinear.scalar.noderiv.NelderMeadSimplex;
 import org.apache.commons.math4.optim.nonlinear.scalar.noderiv.SimplexOptimizer;
+import org.apache.commons.math4.rng.UniformRandomProvider;
+import org.apache.commons.math4.rng.RandomSource;
 import org.apache.commons.math4.random.GaussianRandomGenerator;
-import org.apache.commons.math4.random.JDKRandomGenerator;
 import org.apache.commons.math4.random.RandomVectorGenerator;
 import org.apache.commons.math4.random.UncorrelatedRandomVectorGenerator;
 import org.junit.Assert;
@@ -52,8 +53,7 @@ public class MultiStartMultivariateOptimizerTest {
         GradientMultivariateOptimizer underlying
             = new NonLinearConjugateGradientOptimizer(NonLinearConjugateGradientOptimizer.Formula.POLAK_RIBIERE,
                                                       new SimpleValueChecker(1e-10, 1e-10));
-        JDKRandomGenerator g = new JDKRandomGenerator();
-        g.setSeed(753289573253l);
+        UniformRandomProvider g = RandomSource.create(RandomSource.MT_64, 753289573253l);
         RandomVectorGenerator generator
             = new UncorrelatedRandomVectorGenerator(new double[] { 50, 50 },
                                                     new double[] { 10, 10 },
@@ -73,15 +73,12 @@ public class MultiStartMultivariateOptimizerTest {
         for (PointValuePair o : optima) {
             // we check the results of all intermediate restarts here (there are 10 such results)
             Vector2D center = new Vector2D(o.getPointRef()[0], o.getPointRef()[1]);
-            Assert.assertTrue(69.9592 < circle.getRadius(center));
-            Assert.assertTrue(69.9602 > circle.getRadius(center));
-            Assert.assertTrue(96.0745 < center.getX());
-            Assert.assertTrue(96.0762 > center.getX());
-            Assert.assertTrue(48.1344 < center.getY());
-            Assert.assertTrue(48.1354 > center.getY());
+            Assert.assertEquals(69.9597, circle.getRadius(center), 1e-3);
+            Assert.assertEquals(96.07535, center.getX(), 1.4e-3);
+            Assert.assertEquals(48.1349, center.getY(), 5e-3);
         }
 
-        Assert.assertTrue(optimizer.getEvaluations() > 850);
+        Assert.assertTrue(optimizer.getEvaluations() > 800);
         Assert.assertTrue(optimizer.getEvaluations() < 900);
 
         Assert.assertEquals(3.1267527, optimum.getValue(), 1e-8);
@@ -97,8 +94,7 @@ public class MultiStartMultivariateOptimizerTest {
                 { 0.9, 1.2 } ,
                 {  3.5, -2.3 }
             });
-        JDKRandomGenerator g = new JDKRandomGenerator();
-        g.setSeed(16069223052l);
+        UniformRandomProvider g = RandomSource.create(RandomSource.MT_64, 16069223052l);
         RandomVectorGenerator generator
             = new UncorrelatedRandomVectorGenerator(2, new GaussianRandomGenerator(g));
         int nbStarts = 10;

http://git-wip-us.apache.org/repos/asf/commons-math/blob/a9fdcd64/src/test/java/org/apache/commons/math4/random/CorrelatedRandomVectorGeneratorTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/random/CorrelatedRandomVectorGeneratorTest.java b/src/test/java/org/apache/commons/math4/random/CorrelatedRandomVectorGeneratorTest.java
index 9b2dd82..18b6543 100644
--- a/src/test/java/org/apache/commons/math4/random/CorrelatedRandomVectorGeneratorTest.java
+++ b/src/test/java/org/apache/commons/math4/random/CorrelatedRandomVectorGeneratorTest.java
@@ -25,9 +25,9 @@ import org.apache.commons.math4.linear.MatrixUtils;
 import org.apache.commons.math4.linear.RealMatrix;
 import org.apache.commons.math4.random.CorrelatedRandomVectorGenerator;
 import org.apache.commons.math4.random.GaussianRandomGenerator;
-import org.apache.commons.math4.random.JDKRandomGenerator;
 import org.apache.commons.math4.random.NormalizedRandomGenerator;
-import org.apache.commons.math4.random.RandomGenerator;
+import org.apache.commons.math4.rng.UniformRandomProvider;
+import org.apache.commons.math4.rng.RandomSource;
 import org.apache.commons.math4.stat.correlation.StorelessCovariance;
 import org.apache.commons.math4.stat.descriptive.moment.VectorialCovariance;
 import org.apache.commons.math4.stat.descriptive.moment.VectorialMean;
@@ -61,9 +61,7 @@ public class CorrelatedRandomVectorGeneratorTest {
             }
         }
 
-        RandomGenerator rg = new JDKRandomGenerator();
-        rg.setSeed(17399225432l);
-        GaussianRandomGenerator rawGenerator = new GaussianRandomGenerator(rg);
+        GaussianRandomGenerator rawGenerator = new GaussianRandomGenerator(RandomSource.create(RandomSource.WELL_1024_A, 17399225432l));
         generator = new CorrelatedRandomVectorGenerator(mean,
                                                         covariance,
                                                         1.0e-12 * covariance.getNorm(),
@@ -85,9 +83,7 @@ public class CorrelatedRandomVectorGeneratorTest {
                 { 6, 2, -1, 197 }
         };
         RealMatrix covRM = MatrixUtils.createRealMatrix(cov);
-        JDKRandomGenerator jg = new JDKRandomGenerator();
-        jg.setSeed(5322145245211l);
-        NormalizedRandomGenerator rg = new GaussianRandomGenerator(jg);
+        NormalizedRandomGenerator rg = new GaussianRandomGenerator(RandomSource.create(RandomSource.WELL_1024_A, 5322145245211l));
         CorrelatedRandomVectorGenerator sg =
             new CorrelatedRandomVectorGenerator(mean, covRM, 0.00001, rg);
 
@@ -181,7 +177,7 @@ public class CorrelatedRandomVectorGeneratorTest {
                 new double[cov.length],
                 matrix,
                 small,
-                new GaussianRandomGenerator(new Well1024a(0x366a26b94e520f41l)));
+                new GaussianRandomGenerator(RandomSource.create(RandomSource.WELL_1024_A, 0x366a26b94e520f41l)));
     }
 
     private void testSampler(final double[][] covMatrix, int samples, double epsilon) {
@@ -196,7 +192,5 @@ public class CorrelatedRandomVectorGeneratorTest {
         for (int r = 0; r < covMatrix.length; ++r) {
             TestUtils.assertEquals(covMatrix[r], sampleCov[r], epsilon);
         }
-
     }
-
 }

http://git-wip-us.apache.org/repos/asf/commons-math/blob/a9fdcd64/src/test/java/org/apache/commons/math4/random/GaussianRandomGeneratorTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/random/GaussianRandomGeneratorTest.java b/src/test/java/org/apache/commons/math4/random/GaussianRandomGeneratorTest.java
index 9c46f0d..031f55d 100644
--- a/src/test/java/org/apache/commons/math4/random/GaussianRandomGeneratorTest.java
+++ b/src/test/java/org/apache/commons/math4/random/GaussianRandomGeneratorTest.java
@@ -18,8 +18,8 @@
 package org.apache.commons.math4.random;
 
 import org.apache.commons.math4.random.GaussianRandomGenerator;
-import org.apache.commons.math4.random.JDKRandomGenerator;
-import org.apache.commons.math4.random.RandomGenerator;
+import org.apache.commons.math4.rng.UniformRandomProvider;
+import org.apache.commons.math4.rng.RandomSource;
 import org.apache.commons.math4.stat.StatUtils;
 import org.junit.Assert;
 import org.junit.Test;
@@ -29,9 +29,7 @@ public class GaussianRandomGeneratorTest {
 
     @Test
     public void testMeanAndStandardDeviation() {
-        RandomGenerator rg = new JDKRandomGenerator();
-        rg.setSeed(17399225432l);
-        GaussianRandomGenerator generator = new GaussianRandomGenerator(rg);
+        GaussianRandomGenerator generator = new GaussianRandomGenerator(RandomSource.create(RandomSource.MT, 17399225432l));
         double[] sample = new double[10000];
         for (int i = 0; i < sample.length; ++i) {
             sample[i] = generator.nextNormalizedDouble();
@@ -39,5 +37,4 @@ public class GaussianRandomGeneratorTest {
         Assert.assertEquals(0.0, StatUtils.mean(sample), 0.012);
         Assert.assertEquals(1.0, StatUtils.variance(sample), 0.01);
     }
-
 }

http://git-wip-us.apache.org/repos/asf/commons-math/blob/a9fdcd64/src/test/java/org/apache/commons/math4/random/UncorrelatedRandomVectorGeneratorTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/random/UncorrelatedRandomVectorGeneratorTest.java b/src/test/java/org/apache/commons/math4/random/UncorrelatedRandomVectorGeneratorTest.java
index bc80769..2c2df9d 100644
--- a/src/test/java/org/apache/commons/math4/random/UncorrelatedRandomVectorGeneratorTest.java
+++ b/src/test/java/org/apache/commons/math4/random/UncorrelatedRandomVectorGeneratorTest.java
@@ -18,9 +18,8 @@
 package org.apache.commons.math4.random;
 
 import org.apache.commons.math4.linear.RealMatrix;
+import org.apache.commons.math4.rng.RandomSource;
 import org.apache.commons.math4.random.GaussianRandomGenerator;
-import org.apache.commons.math4.random.JDKRandomGenerator;
-import org.apache.commons.math4.random.RandomGenerator;
 import org.apache.commons.math4.random.UncorrelatedRandomVectorGenerator;
 import org.apache.commons.math4.stat.descriptive.moment.VectorialCovariance;
 import org.apache.commons.math4.stat.descriptive.moment.VectorialMean;
@@ -35,11 +34,10 @@ public class UncorrelatedRandomVectorGeneratorTest {
     public UncorrelatedRandomVectorGeneratorTest() {
         mean              = new double[] {0.0, 1.0, -3.0, 2.3};
         standardDeviation = new double[] {1.0, 2.0, 10.0, 0.1};
-        RandomGenerator rg = new JDKRandomGenerator();
-        rg.setSeed(17399225432l);
         generator =
             new UncorrelatedRandomVectorGenerator(mean, standardDeviation,
-                    new GaussianRandomGenerator(rg));
+                                                  new GaussianRandomGenerator(RandomSource.create(RandomSource.MT,
+                                                                                                  17399225432l)));
     }
 
     @Test

http://git-wip-us.apache.org/repos/asf/commons-math/blob/a9fdcd64/src/test/java/org/apache/commons/math4/stat/regression/GLSMultipleLinearRegressionTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math4/stat/regression/GLSMultipleLinearRegressionTest.java b/src/test/java/org/apache/commons/math4/stat/regression/GLSMultipleLinearRegressionTest.java
index 06fbc26..a5b61d1 100644
--- a/src/test/java/org/apache/commons/math4/stat/regression/GLSMultipleLinearRegressionTest.java
+++ b/src/test/java/org/apache/commons/math4/stat/regression/GLSMultipleLinearRegressionTest.java
@@ -27,8 +27,10 @@ import org.apache.commons.math4.linear.RealMatrix;
 import org.apache.commons.math4.linear.RealVector;
 import org.apache.commons.math4.random.CorrelatedRandomVectorGenerator;
 import org.apache.commons.math4.random.GaussianRandomGenerator;
-import org.apache.commons.math4.random.JDKRandomGenerator;
-import org.apache.commons.math4.random.RandomGenerator;
+import org.apache.commons.math4.rng.UniformRandomProvider;
+import org.apache.commons.math4.rng.RandomSource;
+import org.apache.commons.math4.distribution.RealDistribution;
+import org.apache.commons.math4.distribution.NormalDistribution;
 import org.apache.commons.math4.stat.correlation.Covariance;
 import org.apache.commons.math4.stat.descriptive.DescriptiveStatistics;
 import org.apache.commons.math4.stat.regression.GLSMultipleLinearRegression;
@@ -220,8 +222,8 @@ public class GLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs
      */
     @Test
     public void testGLSEfficiency() {
-        RandomGenerator rg = new JDKRandomGenerator();
-        rg.setSeed(200);  // Seed has been selected to generate non-trivial covariance
+        final UniformRandomProvider rg = RandomSource.create(RandomSource.MT, 123456789L);
+        final RealDistribution.Sampler gauss = new NormalDistribution().createSampler(rg);
 
         // Assume model has 16 observations (will use Longley data).  Start by generating
         // non-constant variances for the 16 error terms.
@@ -237,7 +239,7 @@ public class GLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs
         RealMatrix errorSeeds = MatrixUtils.createRealMatrix(numSeeds, nObs);
         for (int i = 0; i < numSeeds; i++) {
             for (int j = 0; j < nObs; j++) {
-                errorSeeds.setEntry(i, j, rg.nextGaussian() * sigma[j]);
+                errorSeeds.setEntry(i, j, gauss.sample() * sigma[j]);
             }
         }
 
@@ -297,5 +299,4 @@ public class GLSMultipleLinearRegressionTest extends MultipleLinearRegressionAbs
         assert(olsBetaStats.getMean() > 1.5 * glsBetaStats.getMean());
         assert(olsBetaStats.getStandardDeviation() > glsBetaStats.getStandardDeviation());
     }
-
 }