You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ps...@apache.org on 2011/11/26 22:23:23 UTC
svn commit: r1206596 - in /commons/proper/math/trunk/src:
main/java/org/apache/commons/math/stat/
main/java/org/apache/commons/math/stat/descriptive/
main/java/org/apache/commons/math/stat/descriptive/moment/ site/xdoc/
test/java/org/apache/commons/mat...
Author: psteitz
Date: Sat Nov 26 21:23:22 2011
New Revision: 1206596
URL: http://svn.apache.org/viewvc?rev=1206596&view=rev
Log:
Completed implementation of sample variance support in statistical aggregates. JIRA: MATH-693.
Modified:
commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/StatUtils.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/DescriptiveStatistics.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/SummaryStatistics.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/SynchronizedSummaryStatistics.java
commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/moment/Variance.java
commons/proper/math/trunk/src/site/xdoc/changes.xml
commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/descriptive/DescriptiveStatisticsTest.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/descriptive/SummaryStatisticsTest.java
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/StatUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/StatUtils.java?rev=1206596&r1=1206595&r2=1206596&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/StatUtils.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/StatUtils.java Sat Nov 26 21:23:22 2011
@@ -302,6 +302,10 @@ public final class StatUtils {
/**
* Returns the variance of the entries in the input array, or
* <code>Double.NaN</code> if the array is empty.
+ *
+ * <p>This method returns the bias-corrected sample variance (using {@code n - 1} in
+ * the denominator). Use {@link #populationVariance()} for the non-bias-corrected
+ * population variance.</p>
* <p>
* See {@link org.apache.commons.math.stat.descriptive.moment.Variance} for
* details on the computing algorithm.</p>
@@ -322,6 +326,10 @@ public final class StatUtils {
* Returns the variance of the entries in the specified portion of
* the input array, or <code>Double.NaN</code> if the designated subarray
* is empty.
+ *
+ * <p>This method returns the bias-corrected sample variance (using {@code n - 1} in
+ * the denominator). Use {@link #populationVariance()} for the non-bias-corrected
+ * population variance.</p>
* <p>
* See {@link org.apache.commons.math.stat.descriptive.moment.Variance} for
* details on the computing algorithm.</p>
@@ -347,6 +355,10 @@ public final class StatUtils {
* Returns the variance of the entries in the specified portion of
* the input array, using the precomputed mean value. Returns
* <code>Double.NaN</code> if the designated subarray is empty.
+ *
+ * <p>This method returns the bias-corrected sample variance (using {@code n - 1} in
+ * the denominator). Use {@link #populationVariance()} for the non-bias-corrected
+ * population variance.</p>
* <p>
* See {@link org.apache.commons.math.stat.descriptive.moment.Variance} for
* details on the computing algorithm.</p>
@@ -378,6 +390,10 @@ public final class StatUtils {
* Returns the variance of the entries in the input array, using the
* precomputed mean value. Returns <code>Double.NaN</code> if the array
* is empty.
+ *
+ * <p>This method returns the bias-corrected sample variance (using {@code n - 1} in
+ * the denominator). Use {@link #populationVariance()} for the non-bias-corrected
+ * population variance.</p>
* <p>
* See {@link org.apache.commons.math.stat.descriptive.moment.Variance} for
* details on the computing algorithm.</p>
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/DescriptiveStatistics.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/DescriptiveStatistics.java?rev=1206596&r1=1206595&r2=1206596&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/DescriptiveStatistics.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/DescriptiveStatistics.java Sat Nov 26 21:23:22 2011
@@ -207,7 +207,12 @@ public class DescriptiveStatistics imple
}
/**
- * Returns the variance of the available values.
+ * Returns the (sample) variance of the available values.
+ *
+ * <p>This method returns the bias-corrected sample variance (using {@code n - 1} in
+ * the denominator). Use {@link #getPopulationVariance()} for the non-bias-corrected
+ * population variance.</p>
+ *
* @return The variance, Double.NaN if no values have been added
* or 0.0 for a single value set.
*/
@@ -216,6 +221,17 @@ public class DescriptiveStatistics imple
}
/**
+ * Returns the <a href="http://en.wikibooks.org/wiki/Statistics/Summary/Variance">
+ * population variance</a> of the available values.
+ *
+ * @return The population variance, Double.NaN if no values have been added,
+ * or 0.0 for a single value set.
+ */
+ public double getPopulationVariance() {
+ return apply(new Variance(false));
+ }
+
+ /**
* Returns the standard deviation of the available values.
* @return The standard deviation, Double.NaN if no values have been added
* or 0.0 for a single value set.
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/SummaryStatistics.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/SummaryStatistics.java?rev=1206596&r1=1206595&r2=1206596&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/SummaryStatistics.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/SummaryStatistics.java Sat Nov 26 21:23:22 2011
@@ -225,10 +225,14 @@ public class SummaryStatistics implement
}
/**
- * Returns the variance of the values that have been added.
- * <p>
- * Double.NaN is returned if no values have been added.
- * </p>
+ * Returns the (sample) variance of the available values.
+ *
+ * <p>This method returns the bias-corrected sample variance (using {@code n - 1} in
+ * the denominator). Use {@link #getPopulationVariance()} for the non-bias-corrected
+ * population variance.</p>
+ *
+ * <p>Double.NaN is returned if no values have been added.</p>
+ *
* @return the variance
*/
public double getVariance() {
@@ -236,6 +240,20 @@ public class SummaryStatistics implement
}
/**
+ * Returns the <a href="http://en.wikibooks.org/wiki/Statistics/Summary/Variance">
+ * population variance</a> of the values that have been added.
+ *
+ * <p>Double.NaN is returned if no values have been added.</p>
+ *
+ * @return the population variance
+ */
+ public double getPopulationVariance() {
+ Variance populationVariance = new Variance(secondMoment);
+ populationVariance.setBiasCorrected(false);
+ return populationVariance.getResult();
+ }
+
+ /**
* Returns the maximum of the values that have been added.
* <p>
* Double.NaN is returned if no values have been added.
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/SynchronizedSummaryStatistics.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/SynchronizedSummaryStatistics.java?rev=1206596&r1=1206595&r2=1206596&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/SynchronizedSummaryStatistics.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/SynchronizedSummaryStatistics.java Sat Nov 26 21:23:22 2011
@@ -121,6 +121,14 @@ public class SynchronizedSummaryStatisti
* {@inheritDoc}
*/
@Override
+ public synchronized double getPopulationVariance() {
+ return super.getPopulationVariance();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
public synchronized double getMax() {
return super.getMax();
}
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/moment/Variance.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/moment/Variance.java?rev=1206596&r1=1206595&r2=1206596&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/moment/Variance.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/stat/descriptive/moment/Variance.java Sat Nov 26 21:23:22 2011
@@ -159,7 +159,7 @@ public class Variance extends AbstractSt
* <code>evaluate</code> leverages the fact that is has the full
* list of values together to execute a two-pass algorithm.
* See {@link Variance}.</p>
- *
+ *
* <p>Note also that when {@link #Variance(SecondMoment)} is used to
* create a Variance, this method does nothing. In that case, the
* SecondMoment should be incremented directly.</p>
Modified: commons/proper/math/trunk/src/site/xdoc/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/site/xdoc/changes.xml?rev=1206596&r1=1206595&r2=1206596&view=diff
==============================================================================
--- commons/proper/math/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/trunk/src/site/xdoc/changes.xml Sat Nov 26 21:23:22 2011
@@ -52,6 +52,11 @@ The <action> type attribute can be add,u
If the output is not quite correct, check for invisible trailing spaces!
-->
<release version="3.0" date="TBD" description="TBD">
+ <action dev="psteitz" type="update" issue="MATH-693">
+ Added support for population variance in StatUtils, SummaryStatistics
+ and DescriptiveStatistics and clarified javadoc to make it clear that
+ 'variance' means sample variance.
+ </action>
<action dev="luc" type="fix" issue="MATH-709">
Fixed BigFraction percentage method which did not work at all.
</action>
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/descriptive/DescriptiveStatisticsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/descriptive/DescriptiveStatisticsTest.java?rev=1206596&r1=1206595&r2=1206596&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/descriptive/DescriptiveStatisticsTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/descriptive/DescriptiveStatisticsTest.java Sat Nov 26 21:23:22 2011
@@ -16,7 +16,15 @@ package org.apache.commons.math.stat.des
import java.util.Locale;
+import org.apache.commons.math.TestUtils;
import org.apache.commons.math.stat.descriptive.rank.Percentile;
+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.rank.Max;
+import org.apache.commons.math.stat.descriptive.rank.Min;
+import org.apache.commons.math.stat.descriptive.summary.Sum;
+import org.apache.commons.math.stat.descriptive.summary.SumOfSquares;
import org.apache.commons.math.util.Precision;
import org.junit.Assert;
import org.junit.Test;
@@ -203,6 +211,40 @@ public class DescriptiveStatisticsTest {
checkremoval(dstat, DescriptiveStatistics.INFINITE_WINDOW, 3.5, 2.5, 3.0);
}
+
+ @Test
+ public void testSummaryConsistency() {
+ final DescriptiveStatistics dstats = new DescriptiveStatistics();
+ final SummaryStatistics sstats = new SummaryStatistics();
+ final int windowSize = 5;
+ dstats.setWindowSize(windowSize);
+ final double tol = 1E-12;
+ for (int i = 0; i < 20; i++) {
+ dstats.addValue(i);
+ sstats.clear();
+ double[] values = dstats.getValues();
+ for (int j = 0; j < values.length; j++) {
+ sstats.addValue(values[j]);
+ }
+ TestUtils.assertEquals(dstats.getMean(), sstats.getMean(), tol);
+ TestUtils.assertEquals(new Mean().evaluate(values), dstats.getMean(), tol);
+ TestUtils.assertEquals(dstats.getMax(), sstats.getMax(), tol);
+ TestUtils.assertEquals(new Max().evaluate(values), dstats.getMax(), tol);
+ TestUtils.assertEquals(dstats.getGeometricMean(), sstats.getGeometricMean(), tol);
+ TestUtils.assertEquals(new GeometricMean().evaluate(values), dstats.getGeometricMean(), tol);
+ TestUtils.assertEquals(dstats.getMin(), sstats.getMin(), tol);
+ TestUtils.assertEquals(new Min().evaluate(values), dstats.getMin(), tol);
+ TestUtils.assertEquals(dstats.getStandardDeviation(), sstats.getStandardDeviation(), tol);
+ TestUtils.assertEquals(dstats.getVariance(), sstats.getVariance(), tol);
+ TestUtils.assertEquals(new Variance().evaluate(values), dstats.getVariance(), tol);
+ TestUtils.assertEquals(dstats.getSum(), sstats.getSum(), tol);
+ TestUtils.assertEquals(new Sum().evaluate(values), dstats.getSum(), tol);
+ TestUtils.assertEquals(dstats.getSumsq(), sstats.getSumsq(), tol);
+ TestUtils.assertEquals(new SumOfSquares().evaluate(values), dstats.getSumsq(), tol);
+ TestUtils.assertEquals(dstats.getPopulationVariance(), sstats.getPopulationVariance(), tol);
+ TestUtils.assertEquals(new Variance(false).evaluate(values), dstats.getPopulationVariance(), tol);
+ }
+ }
public void checkremoval(DescriptiveStatistics dstat, int wsize,
double mean1, double mean2, double mean3) {
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/descriptive/SummaryStatisticsTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/descriptive/SummaryStatisticsTest.java?rev=1206596&r1=1206595&r2=1206596&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/descriptive/SummaryStatisticsTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/stat/descriptive/SummaryStatisticsTest.java Sat Nov 26 21:23:22 2011
@@ -39,6 +39,7 @@ public class SummaryStatisticsTest {
private double sumSq = 18;
private double sum = 8;
private double var = 0.666666666666666666667;
+ private double popVar = 0.5;
private double std = FastMath.sqrt(var);
private double n = 4;
private double min = 1;
@@ -62,6 +63,7 @@ public class SummaryStatisticsTest {
Assert.assertEquals("sum",sum,u.getSum(),tolerance);
Assert.assertEquals("sumsq",sumSq,u.getSumsq(),tolerance);
Assert.assertEquals("var",var,u.getVariance(),tolerance);
+ Assert.assertEquals("population var",popVar,u.getPopulationVariance(),tolerance);
Assert.assertEquals("std",std,u.getStandardDeviation(),tolerance);
Assert.assertEquals("mean",mean,u.getMean(),tolerance);
Assert.assertEquals("min",min,u.getMin(),tolerance);