diff --git a/src/java/org/apache/commons/math/stat/univariate/moment/Variance.java b/src/java/org/apache/commons/math/stat/univariate/moment/Variance.java index 4c9efe4da..18688777e 100644 --- a/src/java/org/apache/commons/math/stat/univariate/moment/Variance.java +++ b/src/java/org/apache/commons/math/stat/univariate/moment/Variance.java @@ -20,12 +20,20 @@ import java.io.Serializable; import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic; /** - * Updating forumulas use West's algorithm as described in - * Chan, T. F. and - * J. G. Lewis 1979, Communications of the ACM, + * Computes the (unbiased) sample variance. Uses the definitional formula: + *

+ * variance = sum((x_i - mean)^2) / (n - 1) + *

+ * where mean is the {@link Mean} and n is the number + * of sample observations. + *

+ * The definitional formula does not have good numerical properties, so + * this implementation uses updating formulas based on West's algorithm + * as described in + * Chan, T. F. andJ. G. Lewis 1979, Communications of the ACM, * vol. 22 no. 9, pp. 526-531.. * - * @version $Revision: 1.20 $ $Date: 2004/06/23 16:26:15 $ + * @version $Revision: 1.21 $ $Date: 2004/06/26 23:33:27 $ */ public class Variance extends AbstractStorelessUnivariateStatistic implements Serializable { @@ -42,20 +50,6 @@ public class Variance extends AbstractStorelessUnivariateStatistic implements Se */ protected boolean incMoment = true; - /** - * This property maintains the latest calculated - * variance for efficiency when getResult() is called - * many times between increments. - */ - protected double variance = Double.NaN; - - /** - * Maintains the current count of inrementations that have occured. - * If the external SecondMoment is used, the this is updated from - * that moments counter - */ - protected long n = 0; - /** * Constructs a Variance. */ @@ -64,7 +58,7 @@ public class Variance extends AbstractStorelessUnivariateStatistic implements Se } /** - * Constructs a Variance based on an externalized second moment. + * Constructs a Variance based on an external second moment. * @param m2 the SecondMoment (Thrid or Fourth moments work * here as well.) */ @@ -85,18 +79,13 @@ public class Variance extends AbstractStorelessUnivariateStatistic implements Se * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult() */ public double getResult() { - if (n < moment.n) { - if (moment.n <= 0) { - variance = Double.NaN; - } else if (moment.n <= 1) { - variance = 0.0; + if (moment.n == 0) { + return Double.NaN; + } else if (moment.n == 1) { + return 0d; } else { - variance = moment.m2 / (moment.n0 - 1); + return moment.m2 / (moment.n0 - 1); } - n = moment.n; - } - - return variance; } /** @@ -113,13 +102,8 @@ public class Variance extends AbstractStorelessUnivariateStatistic implements Se if (incMoment) { moment.clear(); } - variance = Double.NaN; - n = 0; } - /** Mean to be used in UnvariateStatistic evaluation approach. */ - protected Mean mean = new Mean(); - /** * Returns the variance of the available values. This uses a corrected * two pass algorithm of the following @@ -137,11 +121,9 @@ public class Variance extends AbstractStorelessUnivariateStatistic implements Se * or 0.0 for a single value set. * @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[], int, int) */ - public double evaluate( - final double[] values, - final int begin, - final int length) { + public double evaluate(final double[] values, final int begin, final int length) { + Mean mean = new Mean(); double var = Double.NaN; if (test(values, begin, length)) {