Fixed array indexing error in Variance evaluate method for

computing the weighted variance of an array segment.

JIRA: MATH-704
Reported by Leonid Ilyevsky
Patched by Thomas Niedhart



git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1208291 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Phil Steitz 2011-11-30 06:24:04 +00:00
parent d099c7c343
commit 3f645310b5
3 changed files with 35 additions and 2 deletions

View File

@ -517,7 +517,7 @@ public class Variance extends AbstractStorelessUnivariateStatistic implements Se
}
double sumWts = 0;
for (int i = 0; i < weights.length; i++) {
for (int i = begin; i < begin + length; i++) {
sumWts += weights[i];
}

View File

@ -52,6 +52,10 @@ 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-704" due-to="Thomas Niedhart">
Fixed array indexing error in Variance evaluate method for
computing the weighted variance of an array segment.
</action>
<action dev="luc" type="fix" issue="MATH-713" due-to="Thomas Neidhart">
Fixed case of unconstrained variables that still occur in the objective function
in simplex solver.
@ -69,7 +73,7 @@ The <action> type attribute can be add,update,fix,remove.
</action>
<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
geometric mean statistics not to be updated if they have been overridden
using instances of commons-math supplied implementations.
</action>
<action dev="psteitz" type="update" issue="MATH-694">

View File

@ -113,6 +113,35 @@ public abstract class UnivariateStatisticAbstractTest {
System.arraycopy(testArray, testArray.length - 5, arrayEnd, 0, 5);
Assert.assertEquals(stat.evaluate(arrayEnd), stat.evaluate(testArray, testArray.length - 5, 5), 0);
}
@Test
public void testEvaluateArraySegmentWeighted() {
// See if this statistic computes weighted statistics
// If not, skip this test
UnivariateStatistic statistic = getUnivariateStatistic();
if (!(statistic instanceof WeightedEvaluation)) {
return;
}
final WeightedEvaluation stat = (WeightedEvaluation) getUnivariateStatistic();
final double[] arrayZero = new double[5];
final double[] weightZero = new double[5];
System.arraycopy(testArray, 0, arrayZero, 0, 5);
System.arraycopy(testWeightsArray, 0, weightZero, 0, 5);
Assert.assertEquals(stat.evaluate(arrayZero, weightZero),
stat.evaluate(testArray, testWeightsArray, 0, 5), 0);
final double[] arrayOne = new double[5];
final double[] weightOne = new double[5];
System.arraycopy(testArray, 5, arrayOne, 0, 5);
System.arraycopy(testWeightsArray, 5, weightOne, 0, 5);
Assert.assertEquals(stat.evaluate(arrayOne, weightOne),
stat.evaluate(testArray, testWeightsArray, 5, 5), 0);
final double[] arrayEnd = new double[5];
final double[] weightEnd = new double[5];
System.arraycopy(testArray, testArray.length - 5, arrayEnd, 0, 5);
System.arraycopy(testWeightsArray, testArray.length - 5, weightEnd, 0, 5);
Assert.assertEquals(stat.evaluate(arrayEnd, weightEnd),
stat.evaluate(testArray, testWeightsArray, testArray.length - 5, 5), 0);
}
@Test
public void testCopy() throws Exception {