diff --git a/src/main/java/org/apache/commons/math/stat/descriptive/moment/Variance.java b/src/main/java/org/apache/commons/math/stat/descriptive/moment/Variance.java index e5518e31a..1de139f3e 100644 --- a/src/main/java/org/apache/commons/math/stat/descriptive/moment/Variance.java +++ b/src/main/java/org/apache/commons/math/stat/descriptive/moment/Variance.java @@ -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]; } diff --git a/src/site/xdoc/changes.xml b/src/site/xdoc/changes.xml index 1d2e38b07..e6f7bc601 100644 --- a/src/site/xdoc/changes.xml +++ b/src/site/xdoc/changes.xml @@ -52,6 +52,10 @@ The type attribute can be add,update,fix,remove. If the output is not quite correct, check for invisible trailing spaces! --> + + Fixed array indexing error in Variance evaluate method for + computing the weighted variance of an array segment. + Fixed case of unconstrained variables that still occur in the objective function in simplex solver. @@ -69,7 +73,7 @@ The type attribute can be add,update,fix,remove. 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. diff --git a/src/test/java/org/apache/commons/math/stat/descriptive/UnivariateStatisticAbstractTest.java b/src/test/java/org/apache/commons/math/stat/descriptive/UnivariateStatisticAbstractTest.java index cbb81353a..bc7069978 100644 --- a/src/test/java/org/apache/commons/math/stat/descriptive/UnivariateStatisticAbstractTest.java +++ b/src/test/java/org/apache/commons/math/stat/descriptive/UnivariateStatisticAbstractTest.java @@ -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 {