ACCUMULO-2494 Delegate math to commons-math

Project: http://git-wip-us.apache.org/repos/asf/accumulo/repo
Commit: http://git-wip-us.apache.org/repos/asf/accumulo/commit/0dc92ca1
Tree: http://git-wip-us.apache.org/repos/asf/accumulo/tree/0dc92ca1
Diff: http://git-wip-us.apache.org/repos/asf/accumulo/diff/0dc92ca1

Branch: refs/heads/1.6.0-SNAPSHOT
Commit: 0dc92ca1288f521cbbeeb8bbd266fa922c845d90
Parents: eac9585
Author: Mike Drob <md...@cloudera.com>
Authored: Tue Mar 18 15:34:44 2014 -0400
Committer: Mike Drob <md...@cloudera.com>
Committed: Mon Mar 24 13:12:29 2014 -0400

----------------------------------------------------------------------
 .../org/apache/accumulo/core/util/Stat.java     | 82 +++++++++++---------
 .../org/apache/accumulo/core/util/StatTest.java | 75 ++++++++++++++++++
 2 files changed, 122 insertions(+), 35 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/accumulo/blob/0dc92ca1/core/src/main/java/org/apache/accumulo/core/util/Stat.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/accumulo/core/util/Stat.java 
b/core/src/main/java/org/apache/accumulo/core/util/Stat.java
index e65265c..d2d560e 100644
--- a/core/src/main/java/org/apache/accumulo/core/util/Stat.java
+++ b/core/src/main/java/org/apache/accumulo/core/util/Stat.java
@@ -16,54 +16,66 @@
  */
 package org.apache.accumulo.core.util;
 
+import org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic;
+import org.apache.commons.math.stat.descriptive.moment.Mean;
+import org.apache.commons.math.stat.descriptive.moment.StandardDeviation;
+import org.apache.commons.math.stat.descriptive.rank.Max;
+import org.apache.commons.math.stat.descriptive.rank.Min;
+import org.apache.commons.math.stat.descriptive.summary.Sum;
+
 public class Stat {
-  
-  long max = Long.MIN_VALUE;
-  long min = Long.MAX_VALUE;
-  long sum = 0;
-  int count = 0;
-  double partialStdDev = 0;
-  
+  Min min;
+  Max max;
+  Sum sum;
+  Mean mean;
+  StandardDeviation sd;
+
+  StorelessUnivariateStatistic[] stats;
+
+  public Stat() {
+    min = new Min();
+    max = new Max();
+    sum = new Sum();
+    mean = new Mean();
+    sd = new StandardDeviation();
+
+    stats = new StorelessUnivariateStatistic[] {min, max, sum, mean, sd};
+  }
+
   public void addStat(long stat) {
-    if (stat > max)
-      max = stat;
-    if (stat < min)
-      min = stat;
-    
-    sum += stat;
-    
-    partialStdDev += stat * stat;
-    
-    count++;
+    for (StorelessUnivariateStatistic statistic : stats) {
+      statistic.increment(stat);
+    }
   }
-  
+
   public long getMin() {
-    return min;
+    return (long) min.getResult();
   }
-  
+
   public long getMax() {
-    return max;
+    return (long) max.getResult();
+  }
+
+  public long getSum() {
+    return (long) sum.getResult();
   }
-  
+
   public double getAverage() {
-    return ((double) sum) / count;
+    return mean.getResult();
   }
-  
+
   public double getStdDev() {
-    return Math.sqrt(partialStdDev / count - getAverage() * getAverage());
+    return sd.getResult();
   }
-  
+
   public String toString() {
-    return String.format("%,d %,d %,.2f %,d", getMin(), getMax(), 
getAverage(), count);
+    return String.format("%,d %,d %,.2f %,d", getMin(), getMax(), 
getAverage(), mean.getN());
   }
-  
+
   public void clear() {
-    sum = 0;
-    count = 0;
-    partialStdDev = 0;
-  }
-  
-  public long getSum() {
-    return sum;
+    for (StorelessUnivariateStatistic statistic : stats) {
+      statistic.clear();
+    }
   }
+
 }

http://git-wip-us.apache.org/repos/asf/accumulo/blob/0dc92ca1/core/src/test/java/org/apache/accumulo/core/util/StatTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/accumulo/core/util/StatTest.java 
b/core/src/test/java/org/apache/accumulo/core/util/StatTest.java
new file mode 100644
index 0000000..c32d79d
--- /dev/null
+++ b/core/src/test/java/org/apache/accumulo/core/util/StatTest.java
@@ -0,0 +1,75 @@
+package org.apache.accumulo.core.util;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Before;
+import org.junit.Test;
+
+public class StatTest {
+
+  static double delta = 0.0000001;
+
+  Stat zero;
+  Stat stat;
+
+  @Before
+  public void setUp() {
+    zero = new Stat();
+    zero.addStat(0);
+
+    stat = new Stat();
+
+    // The mean and sd for this set were checked against wolfram alpha
+    for (Long l : new long[] {9792, 5933, 4766, 5770, 3763, 3677, 5002}) {
+      stat.addStat(l);
+    }
+  }
+
+  @Test
+  public void testGetMin() {
+    assertEquals(0, zero.getMin());
+    assertEquals(3677, stat.getMin());
+  }
+
+  @Test
+  public void testGetMax() {
+    assertEquals(0, zero.getMax());
+    assertEquals(9792, stat.getMax());
+  }
+
+  @Test
+  public void testGetAverage() {
+    assertEquals(0, zero.getAverage(), delta);
+    assertEquals(5529, stat.getAverage(), delta);
+  }
+
+  @Test
+  public void testGetStdDev() {
+    assertEquals(0, zero.getStdDev(), delta);
+    assertEquals(2073.7656569632, stat.getStdDev(), delta);
+  }
+
+  @Test
+  public void testGetSum() {
+    assertEquals(0, zero.getSum());
+    assertEquals(38703, stat.getSum());
+  }
+
+  @Test
+  public void testClear() {
+    zero.clear();
+    stat.clear();
+
+    assertEquals(0, zero.getMax());
+    assertEquals(zero.getMax(), stat.getMax());
+    assertEquals(0, zero.getMin());
+    assertEquals(zero.getMin(), stat.getMin());
+    assertEquals(0, zero.getSum());
+    assertEquals(zero.getSum(), stat.getSum());
+
+    assertEquals(Double.NaN, zero.getAverage(), 0);
+    assertEquals(zero.getAverage(), stat.getAverage(), 0);
+    assertEquals(Double.NaN, zero.getStdDev(), 0);
+    assertEquals(zero.getStdDev(), stat.getStdDev(), 0);
+  }
+}

Reply via email to