commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject [1/3] [math] Quadratic mean
Date Fri, 23 Jan 2015 16:07:43 GMT
Repository: commons-math
Updated Branches:
  refs/heads/master 6383def7f -> 75efbfd6b


Quadratic mean

MATH-1199


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

Branch: refs/heads/master
Commit: 6a8ba1fecb47e5ffd5387654d0cdb7c372ccdc9d
Parents: 4e19582
Author: Gilles <erans@apache.org>
Authored: Fri Jan 23 16:46:48 2015 +0100
Committer: Gilles <erans@apache.org>
Committed: Fri Jan 23 16:46:48 2015 +0100

----------------------------------------------------------------------
 .../stat/descriptive/DescriptiveStatistics.java   | 12 ++++++++++++
 .../math3/stat/descriptive/SummaryStatistics.java | 12 ++++++++++++
 .../descriptive/DescriptiveStatisticsTest.java    | 18 ++++++++++++++++--
 .../stat/descriptive/SummaryStatisticsTest.java   | 17 +++++++++++++++++
 4 files changed, 57 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-math/blob/6a8ba1fe/src/main/java/org/apache/commons/math3/stat/descriptive/DescriptiveStatistics.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math3/stat/descriptive/DescriptiveStatistics.java
b/src/main/java/org/apache/commons/math3/stat/descriptive/DescriptiveStatistics.java
index b03e9f5..31acd24 100644
--- a/src/main/java/org/apache/commons/math3/stat/descriptive/DescriptiveStatistics.java
+++ b/src/main/java/org/apache/commons/math3/stat/descriptive/DescriptiveStatistics.java
@@ -259,6 +259,18 @@ public class DescriptiveStatistics implements StatisticalSummary, Serializable
{
     }
 
     /**
+     * Returns the quadratic mean, a.k.a.
+     * <a href="http://mathworld.wolfram.com/Root-Mean-Square.html">
+     * root-mean-square</a> of the available values
+     * @return The quadratic mean or {@code Double.NaN} if no values
+     * have been added.
+     */
+    public double getQuadraticMean() {
+        final long n = getN();
+        return n > 0 ? FastMath.sqrt(getSumsq() / n) : Double.NaN;
+    }
+
+    /**
      * Returns the skewness of the available values. Skewness is a
      * measure of the asymmetry of a given distribution.
      * @return The skewness, Double.NaN if no values have been added

http://git-wip-us.apache.org/repos/asf/commons-math/blob/6a8ba1fe/src/main/java/org/apache/commons/math3/stat/descriptive/SummaryStatistics.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/math3/stat/descriptive/SummaryStatistics.java
b/src/main/java/org/apache/commons/math3/stat/descriptive/SummaryStatistics.java
index 2fe8599..e10291c 100644
--- a/src/main/java/org/apache/commons/math3/stat/descriptive/SummaryStatistics.java
+++ b/src/main/java/org/apache/commons/math3/stat/descriptive/SummaryStatistics.java
@@ -225,6 +225,18 @@ public class SummaryStatistics implements StatisticalSummary, Serializable
{
     }
 
     /**
+     * Returns the quadratic mean, a.k.a.
+     * <a href="http://mathworld.wolfram.com/Root-Mean-Square.html">
+     * root-mean-square</a> of the available values
+     * @return The quadratic mean or {@code Double.NaN} if no values
+     * have been added.
+     */
+    public double getQuadraticMean() {
+        final long n = getN();
+        return n > 0 ? FastMath.sqrt(getSumsq() / n) : Double.NaN;
+    }
+
+    /**
      * Returns the (sample) variance of the available values.
      *
      * <p>This method returns the bias-corrected sample variance (using {@code n -
1} in

http://git-wip-us.apache.org/repos/asf/commons-math/blob/6a8ba1fe/src/test/java/org/apache/commons/math3/stat/descriptive/DescriptiveStatisticsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math3/stat/descriptive/DescriptiveStatisticsTest.java
b/src/test/java/org/apache/commons/math3/stat/descriptive/DescriptiveStatisticsTest.java
index 7562399..462a739 100644
--- a/src/test/java/org/apache/commons/math3/stat/descriptive/DescriptiveStatisticsTest.java
+++ b/src/test/java/org/apache/commons/math3/stat/descriptive/DescriptiveStatisticsTest.java
@@ -31,8 +31,6 @@ import org.junit.Test;
 
 /**
  * Test cases for the DescriptiveStatistics class.
- *
- *          2007) $
  */
 public class DescriptiveStatisticsTest {
 
@@ -107,6 +105,22 @@ public class DescriptiveStatisticsTest {
     }
 
     @Test
+    public void testQuadraticMean() {
+        final double[] values = { 1.2, 3.4, 5.6, 7.89 };
+        final DescriptiveStatistics stats = new DescriptiveStatistics(values);
+
+        final int len = values.length;
+        double expected = 0;
+        for (int i = 0; i < len; i++) {
+            final double v = values[i];
+            expected += v * v / len;
+        }
+        expected = Math.sqrt(expected);
+
+        Assert.assertEquals(expected, stats.getQuadraticMean(), Math.ulp(expected));
+    }
+
+    @Test
     public void testToString() {
         DescriptiveStatistics stats = createDescriptiveStatistics();
         stats.addValue(1);

http://git-wip-us.apache.org/repos/asf/commons-math/blob/6a8ba1fe/src/test/java/org/apache/commons/math3/stat/descriptive/SummaryStatisticsTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/math3/stat/descriptive/SummaryStatisticsTest.java
b/src/test/java/org/apache/commons/math3/stat/descriptive/SummaryStatisticsTest.java
index f104d65..40ddf28 100644
--- a/src/test/java/org/apache/commons/math3/stat/descriptive/SummaryStatisticsTest.java
+++ b/src/test/java/org/apache/commons/math3/stat/descriptive/SummaryStatisticsTest.java
@@ -299,6 +299,23 @@ public class SummaryStatisticsTest {
         }
     }
     
+    @Test
+    public void testQuadraticMean() {
+        final double[] values = { 1.2, 3.4, 5.6, 7.89 };
+        final SummaryStatistics stats = createSummaryStatistics();
+
+        final int len = values.length;
+        double expected = 0;
+        for (int i = 0; i < len; i++) {
+            final double v = values[i];
+            expected += v * v / len;
+
+            stats.addValue(v);
+        }
+        expected = Math.sqrt(expected);
+
+        Assert.assertEquals(expected, stats.getQuadraticMean(), Math.ulp(expected));
+    }
     
     /**
      * JIRA: MATH-691


Mime
View raw message