This commit is contained in:
Sebb 2015-01-25 00:30:26 +00:00
commit 3d9a76877b
6 changed files with 73 additions and 2 deletions

View File

@ -258,6 +258,18 @@ public class DescriptiveStatistics implements StatisticalSummary, Serializable {
return stdDev;
}
/**
* 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.

View File

@ -224,6 +224,18 @@ public class SummaryStatistics implements StatisticalSummary, Serializable {
return stdDev;
}
/**
* 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.
*

View File

@ -114,6 +114,14 @@ public class SynchronizedDescriptiveStatistics extends DescriptiveStatistics {
return super.getStandardDeviation();
}
/**
* {@inheritDoc}
*/
@Override
public synchronized double getQuadraticMean() {
return super.getQuadraticMean();
}
/**
* {@inheritDoc}
*/

View File

@ -111,6 +111,14 @@ public class SynchronizedSummaryStatistics extends SummaryStatistics {
return super.getStandardDeviation();
}
/**
* {@inheritDoc}
*/
@Override
public synchronized double getQuadraticMean() {
return super.getQuadraticMean();
}
/**
* {@inheritDoc}
*/

View File

@ -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();

View File

@ -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