Return-Path: X-Original-To: apmail-accumulo-commits-archive@www.apache.org Delivered-To: apmail-accumulo-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 07F52119CE for ; Mon, 24 Mar 2014 17:27:31 +0000 (UTC) Received: (qmail 3970 invoked by uid 500); 24 Mar 2014 17:27:17 -0000 Delivered-To: apmail-accumulo-commits-archive@accumulo.apache.org Received: (qmail 3868 invoked by uid 500); 24 Mar 2014 17:27:12 -0000 Mailing-List: contact commits-help@accumulo.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@accumulo.apache.org Delivered-To: mailing list commits@accumulo.apache.org Received: (qmail 3282 invoked by uid 99); 24 Mar 2014 17:26:56 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 24 Mar 2014 17:26:56 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 84CC68B541C; Mon, 24 Mar 2014 17:26:56 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: mdrob@apache.org To: commits@accumulo.apache.org Date: Mon, 24 Mar 2014 17:26:59 -0000 Message-Id: In-Reply-To: <2e62c749742b460284126f6fd677e991@git.apache.org> References: <2e62c749742b460284126f6fd677e991@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [4/9] git commit: ACCUMULO-2494 Delegate math to commons-math 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.5.2-SNAPSHOT Commit: 0dc92ca1288f521cbbeeb8bbd266fa922c845d90 Parents: eac9585 Author: Mike Drob Authored: Tue Mar 18 15:34:44 2014 -0400 Committer: Mike Drob 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); + } +}