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 b03e9f532..31acd24e2 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 @@ -258,6 +258,18 @@ public class DescriptiveStatistics implements StatisticalSummary, Serializable { return stdDev; } + /** + * Returns the quadratic mean, a.k.a. + * + * root-mean-square 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. 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 2fe8599a7..e10291c34 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 @@ -224,6 +224,18 @@ public class SummaryStatistics implements StatisticalSummary, Serializable { return stdDev; } + /** + * Returns the quadratic mean, a.k.a. + * + * root-mean-square 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. * diff --git a/src/main/java/org/apache/commons/math3/stat/descriptive/SynchronizedDescriptiveStatistics.java b/src/main/java/org/apache/commons/math3/stat/descriptive/SynchronizedDescriptiveStatistics.java index a24f2887f..270e4aa7b 100644 --- a/src/main/java/org/apache/commons/math3/stat/descriptive/SynchronizedDescriptiveStatistics.java +++ b/src/main/java/org/apache/commons/math3/stat/descriptive/SynchronizedDescriptiveStatistics.java @@ -114,6 +114,14 @@ public class SynchronizedDescriptiveStatistics extends DescriptiveStatistics { return super.getStandardDeviation(); } + /** + * {@inheritDoc} + */ + @Override + public synchronized double getQuadraticMean() { + return super.getQuadraticMean(); + } + /** * {@inheritDoc} */ diff --git a/src/main/java/org/apache/commons/math3/stat/descriptive/SynchronizedSummaryStatistics.java b/src/main/java/org/apache/commons/math3/stat/descriptive/SynchronizedSummaryStatistics.java index 153237ce5..7eaf9ac92 100644 --- a/src/main/java/org/apache/commons/math3/stat/descriptive/SynchronizedSummaryStatistics.java +++ b/src/main/java/org/apache/commons/math3/stat/descriptive/SynchronizedSummaryStatistics.java @@ -111,6 +111,14 @@ public class SynchronizedSummaryStatistics extends SummaryStatistics { return super.getStandardDeviation(); } + /** + * {@inheritDoc} + */ + @Override + public synchronized double getQuadraticMean() { + return super.getQuadraticMean(); + } + /** * {@inheritDoc} */ 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 756239963..462a739ea 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 { @@ -106,6 +104,22 @@ public class DescriptiveStatisticsTest { Assert.assertEquals(12.0, stats.getElement(88), 1.0e-10); } + @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(); 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 f104d6542..40ddf2879 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