Author: erans
Date: Thu Aug 2 22:18:08 2012
New Revision: 1368738
URL: http://svn.apache.org/viewvc?rev=1368738&view=rev
Log:
MATH-834
Replaced calls to the various sampling methods in "RandomDataImpl" by their
equivalent "sample" method in the distribution classes.
Modified:
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/fitting/PolynomialFitterTest.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/general/LevenbergMarquardtOptimizerTest.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/general/RandomCirclePointGenerator.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/general/RandomStraightLinePointGenerator.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/descriptive/AggregateSummaryStatisticsTest.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/descriptive/UnivariateStatisticAbstractTest.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/MathUtilsTest.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/ResizableDoubleArrayTest.java
Modified:
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/fitting/PolynomialFitterTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/fitting/PolynomialFitterTest.java?rev=1368738&r1=1368737&r2=1368738&view=diff
==============================================================================
---
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/fitting/PolynomialFitterTest.java
(original)
+++
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/fitting/PolynomialFitterTest.java
Thu Aug 2 22:18:08 2012
@@ -27,7 +27,8 @@ import org.apache.commons.math3.optimiza
import
org.apache.commons.math3.optimization.general.LevenbergMarquardtOptimizer;
import org.apache.commons.math3.optimization.SimpleVectorValueChecker;
import org.apache.commons.math3.util.FastMath;
-import org.apache.commons.math3.random.RandomDataImpl;
+import org.apache.commons.math3.distribution.RealDistribution;
+import org.apache.commons.math3.distribution.UniformRealDistribution;
import org.apache.commons.math3.TestUtils;
import org.junit.Test;
@@ -40,8 +41,8 @@ import org.junit.Assert;
public class PolynomialFitterTest {
@Test
public void testFit() {
- final RandomDataImpl rng = new RandomDataImpl();
- rng.reSeed(64925784252L);
+ final RealDistribution rng = new UniformRealDistribution(-100, 100);
+ rng.reseedRandomGenerator(64925784252L);
final LevenbergMarquardtOptimizer optim = new
LevenbergMarquardtOptimizer();
final PolynomialFitter fitter = new PolynomialFitter(optim);
@@ -50,7 +51,7 @@ public class PolynomialFitterTest {
// Collect data from a known polynomial.
for (int i = 0; i < 100; i++) {
- final double x = rng.nextUniform(-100, 100);
+ final double x = rng.sample();
fitter.addObservedPoint(x, f.value(x));
}
Modified:
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/general/LevenbergMarquardtOptimizerTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/general/LevenbergMarquardtOptimizerTest.java?rev=1368738&r1=1368737&r2=1368738&view=diff
==============================================================================
---
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/general/LevenbergMarquardtOptimizerTest.java
(original)
+++
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/general/LevenbergMarquardtOptimizerTest.java
Thu Aug 2 22:18:08 2012
@@ -265,10 +265,12 @@ public class LevenbergMarquardtOptimizer
final double xSigma = 10;
final double ySigma = 15;
final double radius = 111.111;
+ // The test is extremely sensitive to the seed.
+ final long seed = 59421061L;
final RandomCirclePointGenerator factory
= new RandomCirclePointGenerator(xCenter, yCenter, radius,
xSigma, ySigma,
- 59421063L);
+ seed);
final CircleProblem circle = new CircleProblem(xSigma, ySigma);
final int numPoints = 10;
Modified:
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/general/RandomCirclePointGenerator.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/general/RandomCirclePointGenerator.java?rev=1368738&r1=1368737&r2=1368738&view=diff
==============================================================================
---
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/general/RandomCirclePointGenerator.java
(original)
+++
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/general/RandomCirclePointGenerator.java
Thu Aug 2 22:18:08 2012
@@ -18,28 +18,26 @@
package org.apache.commons.math3.optimization.general;
import java.awt.geom.Point2D;
-import org.apache.commons.math3.random.RandomData;
-import org.apache.commons.math3.random.RandomDataImpl;
+import org.apache.commons.math3.random.RandomGenerator;
import org.apache.commons.math3.random.Well44497b;
import org.apache.commons.math3.util.MathUtils;
import org.apache.commons.math3.util.FastMath;
+import org.apache.commons.math3.distribution.RealDistribution;
+import org.apache.commons.math3.distribution.UniformRealDistribution;
+import org.apache.commons.math3.distribution.NormalDistribution;
/**
* Factory for generating a cloud of points that approximate a circle.
*/
public class RandomCirclePointGenerator {
- /** RNG. */
- private final RandomData random;
+ /** RNG for the x-coordinate of the center. */
+ private final RealDistribution cX;
+ /** RNG for the y-coordinate of the center. */
+ private final RealDistribution cY;
+ /** RNG for the parametric position of the point. */
+ private final RealDistribution tP;
/** Radius of the circle. */
private final double radius;
- /** x-coordinate of the circle center. */
- private final double x;
- /** y-coordinate of the circle center. */
- private final double y;
- /** Error on the x-coordinate of the center. */
- private final double xSigma;
- /** Error on the y-coordinate of the center. */
- private final double ySigma;
/**
* @param x Abscissa of the circle center.
@@ -55,12 +53,14 @@ public class RandomCirclePointGenerator
double xSigma,
double ySigma,
long seed) {
- random = new RandomDataImpl(new Well44497b((seed)));
+ final RandomGenerator rng = new Well44497b(seed);
this.radius = radius;
- this.x = x;
- this.y = y;
- this.xSigma = xSigma;
- this.ySigma = ySigma;
+ cX = new NormalDistribution(rng, x, xSigma,
+
NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
+ cY = new NormalDistribution(rng, y, ySigma,
+
NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
+ tP = new UniformRealDistribution(rng, 0, MathUtils.TWO_PI,
+
UniformRealDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
}
/**
@@ -83,12 +83,9 @@ public class RandomCirclePointGenerator
* @return a point.
*/
private Point2D.Double create() {
- final double cX = random.nextGaussian(x, xSigma);
- final double cY = random.nextGaussian(y, ySigma);
- final double t = random.nextUniform(0, MathUtils.TWO_PI);
-
- final double pX = cX + radius * FastMath.cos(t);
- final double pY = cY + radius * FastMath.sin(t);
+ final double t = tP.sample();
+ final double pX = cX.sample() + radius * FastMath.cos(t);
+ final double pY = cY.sample() + radius * FastMath.sin(t);
return new Point2D.Double(pX, pY);
}
Modified:
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/general/RandomStraightLinePointGenerator.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/general/RandomStraightLinePointGenerator.java?rev=1368738&r1=1368737&r2=1368738&view=diff
==============================================================================
---
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/general/RandomStraightLinePointGenerator.java
(original)
+++
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/general/RandomStraightLinePointGenerator.java
Thu Aug 2 22:18:08 2012
@@ -18,33 +18,31 @@
package org.apache.commons.math3.optimization.general;
import java.awt.geom.Point2D;
-import org.apache.commons.math3.random.RandomData;
-import org.apache.commons.math3.random.RandomDataImpl;
+import org.apache.commons.math3.random.RandomGenerator;
import org.apache.commons.math3.random.Well44497b;
import org.apache.commons.math3.util.MathUtils;
import org.apache.commons.math3.util.FastMath;
+import org.apache.commons.math3.distribution.RealDistribution;
+import org.apache.commons.math3.distribution.UniformRealDistribution;
+import org.apache.commons.math3.distribution.NormalDistribution;
/**
* Factory for generating a cloud of points that approximate a straight line.
*/
public class RandomStraightLinePointGenerator {
- /** RNG. */
- private final RandomData random;
/** Slope. */
private final double slope;
/** Intercept. */
private final double intercept;
- /** Error on the y-coordinate. */
- private final double sigma;
- /** Lowest value of the x-coordinate. */
- private final double lo;
- /** Highest value of the x-coordinate. */
- private final double hi;
+ /** RNG for the x-coordinate. */
+ private final RealDistribution x;
+ /** RNG for the error on the y-coordinate. */
+ private final RealDistribution error;
/**
* The generator will create a cloud of points whose x-coordinates
* will be randomly sampled between {@code xLo} and {@code xHi}, and
- * the correspoding y-coordinates will be computed as
+ * the corresponding y-coordinates will be computed as
* <pre><code>
* y = a x + b + N(0, error)
* </code></pre>
@@ -53,23 +51,24 @@ public class RandomStraightLinePointGene
*
* @param a Slope.
* @param b Intercept.
- * @param error Error on the y-coordinate of the point.
- * @param xLo Lowest value of the x-coordinate.
- * @param xHi Highest value of the x-coordinate.
+ * @param sigma Standard deviation on the y-coordinate of the point.
+ * @param lo Lowest value of the x-coordinate.
+ * @param hi Highest value of the x-coordinate.
* @param seed RNG seed.
*/
public RandomStraightLinePointGenerator(double a,
double b,
- double error,
- double xLo,
- double xHi,
+ double sigma,
+ double lo,
+ double hi,
long seed) {
- random = new RandomDataImpl(new Well44497b((seed)));
+ final RandomGenerator rng = new Well44497b(seed);
slope = a;
intercept = b;
- sigma = error;
- lo = xLo;
- hi = xHi;
+ error = new NormalDistribution(rng, 0, sigma,
+
NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
+ x = new UniformRealDistribution(rng, lo, hi,
+
UniformRealDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
}
/**
@@ -92,10 +91,10 @@ public class RandomStraightLinePointGene
* @return a point.
*/
private Point2D.Double create() {
- final double x = random.nextUniform(lo, hi);
- final double yModel = slope * x + intercept;
- final double y = yModel + random.nextGaussian(0, sigma);
+ final double abscissa = x.sample();
+ final double yModel = slope * abscissa + intercept;
+ final double ordinate = yModel + error.sample();
- return new Point2D.Double(x, y);
+ return new Point2D.Double(abscissa, ordinate);
}
}
Modified:
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/descriptive/AggregateSummaryStatisticsTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/descriptive/AggregateSummaryStatisticsTest.java?rev=1368738&r1=1368737&r2=1368738&view=diff
==============================================================================
---
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/descriptive/AggregateSummaryStatisticsTest.java
(original)
+++
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/descriptive/AggregateSummaryStatisticsTest.java
Thu Aug 2 22:18:08 2012
@@ -22,8 +22,10 @@ import java.util.Collection;
import org.apache.commons.math3.TestUtils;
-import org.apache.commons.math3.random.RandomData;
-import org.apache.commons.math3.random.RandomDataImpl;
+import org.apache.commons.math3.distribution.RealDistribution;
+import org.apache.commons.math3.distribution.UniformRealDistribution;
+import org.apache.commons.math3.distribution.IntegerDistribution;
+import org.apache.commons.math3.distribution.UniformIntegerDistribution;
import org.apache.commons.math3.util.Precision;
import org.junit.Assert;
import org.junit.Test;
@@ -246,12 +248,10 @@ public class AggregateSummaryStatisticsT
* @return array of random double values
*/
private double[] generateSample() {
- final RandomData randomData = new RandomDataImpl();
- final int sampleSize = randomData.nextInt(10,100);
- double[] out = new double[sampleSize];
- for (int i = 0; i < out.length; i++) {
- out[i] = randomData.nextUniform(-100, 100);
- }
+ final IntegerDistribution size = new UniformIntegerDistribution(10,
100);
+ final RealDistribution randomData = new UniformRealDistribution(-100,
100);
+ final int sampleSize = size.sample();
+ final double[] out = randomData.sample(sampleSize);
return out;
}
@@ -265,7 +265,6 @@ public class AggregateSummaryStatisticsT
private double[][] generatePartition(double[] sample) {
final int length = sample.length;
final double[][] out = new double[5][];
- final RandomData randomData = new RandomDataImpl();
int cur = 0;
int offset = 0;
int sampleCount = 0;
@@ -273,7 +272,8 @@ public class AggregateSummaryStatisticsT
if (cur == length || offset == length) {
break;
}
- final int next = (i == 4 || cur == length - 1) ? length - 1 :
randomData.nextInt(cur, length - 1);
+ final IntegerDistribution partitionPoint = new
UniformIntegerDistribution(cur, length - 1);
+ final int next = (i == 4 || cur == length - 1) ? length - 1 :
partitionPoint.sample();
final int subLength = next - cur + 1;
out[i] = new double[subLength];
System.arraycopy(sample, offset, out[i], 0, subLength);
Modified:
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/descriptive/UnivariateStatisticAbstractTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/descriptive/UnivariateStatisticAbstractTest.java?rev=1368738&r1=1368737&r2=1368738&view=diff
==============================================================================
---
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/descriptive/UnivariateStatisticAbstractTest.java
(original)
+++
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/stat/descriptive/UnivariateStatisticAbstractTest.java
Thu Aug 2 22:18:08 2012
@@ -21,8 +21,10 @@ import java.util.List;
import org.apache.commons.math3.TestUtils;
-import org.apache.commons.math3.random.RandomData;
-import org.apache.commons.math3.random.RandomDataImpl;
+import org.apache.commons.math3.distribution.IntegerDistribution;
+import org.apache.commons.math3.distribution.UniformIntegerDistribution;
+import org.apache.commons.math3.distribution.RealDistribution;
+import org.apache.commons.math3.distribution.NormalDistribution;
import org.apache.commons.math3.util.FastMath;
import org.junit.Assert;
import org.junit.Test;
@@ -179,21 +181,22 @@ public abstract class UnivariateStatisti
final double sigma = 5; // std dev of test data
double[] values = new double[len];
double[] weights = new double[len];
- RandomData randomData = new RandomDataImpl();
// Fill weights array with random int values between 1 and 5
int[] intWeights = new int[len];
+ final IntegerDistribution weightDist = new
UniformIntegerDistribution(1, 5);
for (int i = 0; i < len; i++) {
- intWeights[i] = randomData.nextInt(1, 5);
+ intWeights[i] = weightDist.sample();
weights[i] = intWeights[i];
}
// 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 valueDist = new NormalDistribution(mu, sigma);
List<Double> valuesList = new ArrayList<Double>();
for (int i = 0; i < len; i++) {
- double value = randomData.nextGaussian(mu, sigma);
+ double value = valueDist.sample();
values[i] = value;
for (int j = 0; j < intWeights[i]; j++) {
valuesList.add(new Double(value));
Modified:
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/MathUtilsTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/MathUtilsTest.java?rev=1368738&r1=1368737&r2=1368738&view=diff
==============================================================================
---
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/MathUtilsTest.java
(original)
+++
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/MathUtilsTest.java
Thu Aug 2 22:18:08 2012
@@ -18,6 +18,8 @@ import org.apache.commons.math3.exceptio
import org.apache.commons.math3.exception.NullArgumentException;
import org.apache.commons.math3.exception.util.LocalizedFormats;
import org.apache.commons.math3.random.RandomDataImpl;
+import org.apache.commons.math3.distribution.RealDistribution;
+import org.apache.commons.math3.distribution.UniformRealDistribution;
import org.junit.Assert;
import org.junit.Test;
@@ -80,7 +82,8 @@ public final class MathUtilsTest {
// Generate 10 distinct random values
for (int i = 0; i < 10; i++) {
- original[i] = random.nextUniform(i + 0.5, i + 0.75);
+ final RealDistribution u = new UniformRealDistribution(i + 0.5, i
+ 0.75);
+ original[i] = u.sample();
}
// Generate a random permutation, making sure it is not the identity
Modified:
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/ResizableDoubleArrayTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/ResizableDoubleArrayTest.java?rev=1368738&r1=1368737&r2=1368738&view=diff
==============================================================================
---
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/ResizableDoubleArrayTest.java
(original)
+++
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/util/ResizableDoubleArrayTest.java
Thu Aug 2 22:18:08 2012
@@ -15,8 +15,10 @@
* limitations under the License.
*/
package org.apache.commons.math3.util;
-import org.apache.commons.math3.random.RandomDataImpl;
-import org.apache.commons.math3.random.RandomData;
+
+import org.apache.commons.math3.distribution.IntegerDistribution;
+import org.apache.commons.math3.distribution.UniformIntegerDistribution;
+
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
@@ -313,8 +315,8 @@ public class ResizableDoubleArrayTest ex
ResizableDoubleArray eDA2 = new ResizableDoubleArray(2);
Assert.assertEquals("Initial number of elements should be 0", 0,
eDA2.getNumElements());
- RandomData randomData = new RandomDataImpl();
- int iterations = randomData.nextInt(100, 1000);
+ final IntegerDistribution randomData = new
UniformIntegerDistribution(100, 1000);
+ final int iterations = randomData.sample();
for( int i = 0; i < iterations; i++) {
eDA2.addElement( i );
@@ -334,8 +336,8 @@ public class ResizableDoubleArrayTest ex
ResizableDoubleArray eDA3 = new ResizableDoubleArray(3, 3.0f, 3.5f);
Assert.assertEquals("Initial number of elements should be 0", 0,
eDA3.getNumElements() );
- RandomData randomData = new RandomDataImpl();
- int iterations = randomData.nextInt(100, 3000);
+ final IntegerDistribution randomData = new
UniformIntegerDistribution(100, 3000);
+ final int iterations = randomData.sample();
for( int i = 0; i < iterations; i++) {
eDA3.addElement( i );