Fixed errors in SummaryStatistics causing overriden statistics not to be updated if the supplied impls are commons-math classes. JIRA: MATH-691.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1206666 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Phil Steitz 2011-11-27 05:20:09 +00:00
parent 92790191d0
commit 118f0cc085
3 changed files with 48 additions and 3 deletions

View File

@ -155,13 +155,13 @@ public class SummaryStatistics implements StatisticalSummary, Serializable {
secondMoment.increment(value);
// If mean, variance or geomean have been overridden,
// need to increment these
if (!(meanImpl instanceof Mean)) {
if (meanImpl != mean) {
meanImpl.increment(value);
}
if (!(varianceImpl instanceof Variance)) {
if (varianceImpl != variance) {
varianceImpl.increment(value);
}
if (!(geoMeanImpl instanceof GeometricMean)) {
if (geoMeanImpl != geoMean) {
geoMeanImpl.increment(value);
}
n++;

View File

@ -52,6 +52,11 @@ The <action> type attribute can be add,update,fix,remove.
If the output is not quite correct, check for invisible trailing spaces!
-->
<release version="3.0" date="TBD" description="TBD">
<action dev="psteitz" type="fix" issue="MATH-691">
Fixed errors in SummaryStatistics addValue causing variance, mean, or
geometric mean statistics not to be updated if they have been overriden
using instances of commons-math supplied implementations.
</action>
<action dev="psteitz" type="update" issue="MATH-694">
Removed First, Third, Fourth moments from the public API.
These internally used statistics have non-standard definitions.

View File

@ -18,7 +18,10 @@ package org.apache.commons.math.stat.descriptive;
import org.apache.commons.math.TestUtils;
import org.apache.commons.math.stat.descriptive.moment.GeometricMean;
import org.apache.commons.math.stat.descriptive.moment.Mean;
import org.apache.commons.math.stat.descriptive.moment.Variance;
import org.apache.commons.math.stat.descriptive.summary.Sum;
import org.apache.commons.math.util.FastMath;
import org.junit.Assert;
@ -305,4 +308,41 @@ public class SummaryStatisticsTest {
// expected
}
}
/**
* JIRA: MATH-691
*/
@Test
public void testOverrideVarianceWithMathClass() throws Exception {
double[] scores = {1, 2, 3, 4};
SummaryStatistics stats = new SummaryStatistics();
stats.setVarianceImpl(new Variance(false)); //use "population variance"
for(double i : scores) {
stats.addValue(i);
}
Assert.assertEquals((new Variance(false)).evaluate(scores),stats.getVariance(), 0);
}
@Test
public void testOverrideMeanWithMathClass() throws Exception {
double[] scores = {1, 2, 3, 4};
SummaryStatistics stats = new SummaryStatistics();
stats.setMeanImpl(new Mean());
for(double i : scores) {
stats.addValue(i);
}
Assert.assertEquals((new Mean()).evaluate(scores),stats.getMean(), 0);
}
@Test
public void testOverrideGeoMeanWithMathClass() throws Exception {
double[] scores = {1, 2, 3, 4};
SummaryStatistics stats = new SummaryStatistics();
stats.setGeoMeanImpl(new GeometricMean());
for(double i : scores) {
stats.addValue(i);
}
Assert.assertEquals((new GeometricMean()).evaluate(scores),stats.getGeometricMean(), 0);
}
}