commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pste...@apache.org
Subject svn commit: r1152662 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math/random/EmpiricalDistributionImpl.java test/java/org/apache/commons/math/random/EmpiricalDistributionTest.java
Date Sun, 31 Jul 2011 23:31:35 GMT
Author: psteitz
Date: Sun Jul 31 23:31:34 2011
New Revision: 1152662

URL: http://svn.apache.org/viewvc?rev=1152662&view=rev
Log:
Made RandomGenerator configurable and reseedable.  JIRA: MATH-634.

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math/random/EmpiricalDistributionImpl.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/EmpiricalDistributionTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/random/EmpiricalDistributionImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/random/EmpiricalDistributionImpl.java?rev=1152662&r1=1152661&r2=1152662&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/random/EmpiricalDistributionImpl.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/random/EmpiricalDistributionImpl.java
Sun Jul 31 23:31:34 2011
@@ -92,23 +92,35 @@ public class EmpiricalDistributionImpl i
     private double[] upperBounds = null;
 
     /** RandomData instance to use in repeated calls to getNext() */
-    private final RandomData randomData = new RandomDataImpl();
+    private final RandomDataImpl randomData;
 
     /**
      * Creates a new EmpiricalDistribution with the default bin count.
      */
     public EmpiricalDistributionImpl() {
-        binCount = 1000;
-        binStats = new ArrayList<SummaryStatistics>();
+        this(1000, null);
     }
 
     /**
-     * Creates a new EmpiricalDistribution  with the specified bin count.
+     * Creates a new EmpiricalDistribution with the specified bin count.
      *
      * @param binCount number of bins
      */
     public EmpiricalDistributionImpl(int binCount) {
+        this(binCount, null);
+    }
+    
+    /**
+     * Creates a new EmpiricalDistribution with the specified bin count using the
+     * provided {@link RandomGenerator} as the source of random data.
+     * 
+     * @param binCount number of bins
+     * @param generator random data generator (may be null, resulting in default JDK generator)
+     * @since 3.0
+     */
+    public EmpiricalDistributionImpl(int binCount, RandomGenerator generator) {
         this.binCount = binCount;
+        randomData = new RandomDataImpl(generator);
         binStats = new ArrayList<SummaryStatistics>();
     }
 
@@ -385,7 +397,7 @@ public class EmpiricalDistributionImpl i
         }
 
         // Start with a uniformly distributed random number in (0,1)
-        double x = FastMath.random();
+        double x = randomData.nextUniform(0,1);
 
         // Use this to select the bin and generate a Gaussian within the bin
         for (int i = 0; i < binCount; i++) {
@@ -485,4 +497,14 @@ public class EmpiricalDistributionImpl i
     public boolean isLoaded() {
         return loaded;
     }
+    
+    /**
+     * Reseeds the random number generator used by {@link #getNextValue()}.
+     * 
+     * @param seed random generator seed
+     * @since 3.0
+     */
+    public void reSeed(long seed) {
+        randomData.reSeed(seed);
+    }
 }

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/EmpiricalDistributionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/EmpiricalDistributionTest.java?rev=1152662&r1=1152661&r2=1152662&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/EmpiricalDistributionTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/random/EmpiricalDistributionTest.java
Sun Jul 31 23:31:34 2011
@@ -41,7 +41,7 @@ import org.junit.runner.RunWith;
 @RunWith(RetryRunner.class)
 public final class EmpiricalDistributionTest {
 
-    protected EmpiricalDistribution empiricalDistribution = null;
+    protected EmpiricalDistributionImpl empiricalDistribution = null;
     protected EmpiricalDistribution empiricalDistribution2 = null;
     protected File file = null;
     protected URL url = null;
@@ -214,6 +214,37 @@ public final class EmpiricalDistribution
         TestUtils.assertEquals(expectedBinUpperBounds, dist.getUpperBounds(), tol);
         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 EmpiricalDistributionImpl(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 EmpiricalDistributionImpl(5, 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);
+        }
+    }
 
     private void verifySame(EmpiricalDistribution d1, EmpiricalDistribution d2) {
         Assert.assertEquals(d1.isLoaded(), d2.isLoaded());



Mime
View raw message