From 3f3c1b46ed5a5200603fea876341173186381342 Mon Sep 17 00:00:00 2001 From: Phil Steitz Date: Fri, 2 Jul 2004 13:59:49 +0000 Subject: [PATCH] Made Sum instance local to evaluate. Improved javadoc. git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@141357 13f79535-47bb-0310-9956-ffa450edef68 --- .../math/stat/univariate/moment/Mean.java | 71 ++++++++++++------- 1 file changed, 46 insertions(+), 25 deletions(-) diff --git a/src/java/org/apache/commons/math/stat/univariate/moment/Mean.java b/src/java/org/apache/commons/math/stat/univariate/moment/Mean.java index 864598478..3c0c0c799 100644 --- a/src/java/org/apache/commons/math/stat/univariate/moment/Mean.java +++ b/src/java/org/apache/commons/math/stat/univariate/moment/Mean.java @@ -17,38 +17,60 @@ package org.apache.commons.math.stat.univariate.moment; import java.io.Serializable; -import org - .apache - .commons - .math - .stat - .univariate - .AbstractStorelessUnivariateStatistic; +import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic; import org.apache.commons.math.stat.univariate.summary.Sum; /** - * Returns the - * arithmetic mean of the available values. - * @version $Revision: 1.18 $ $Date: 2004/06/23 16:26:15 $ + * Returns the arithmetic mean of the available values. Uses the definitional + * formula: + *

+ * mean = sum(x_i) / n + *

+ * where n is the number of observations. + *

+ * The value of the statistic is computed using the following recursive + * updating algorithm: + *

+ *

    + *
  1. Initialize m = the first value
  2. + *
  3. For each additional value, update using
    + * m = m + (new value - m) / (number of observations)
  4. + *
+ *

+ * Returns Double.NaN if the dataset is empty. + *

+ * Note that this implementation is not synchronized. If + * multiple threads access an instance of this class concurrently, and at least + * one of the threads invokes the increment(), or + * clear() method, it must be synchronized externally. + * + * @version $Revision: 1.19 $ $Date: 2004/07/02 13:59:49 $ */ -public class Mean extends AbstractStorelessUnivariateStatistic implements Serializable{ +public class Mean extends AbstractStorelessUnivariateStatistic + implements Serializable { /** Serializable version identifier */ static final long serialVersionUID = -1296043746617791564L; - /** first moment of values that have been added */ + /** First moment on which this statistic is based. */ protected FirstMoment moment = null; - /** */ + /** + * Determines whether or not this statistic can be incremented or cleared. + *

+ * Statistics based on (constructed from) external moments cannot + * be incremented or cleared. + */ protected boolean incMoment = true; - /** */ + /** Constructs a Mean. */ public Mean() { moment = new FirstMoment(); } /** * Constructs a Mean with an External Moment. + * * @param m1 the moment */ public Mean(final FirstMoment m1) { @@ -87,26 +109,25 @@ public class Mean extends AbstractStorelessUnivariateStatistic implements Serial public double getN() { return moment.getN(); } - - /*UnvariateStatistic Approach */ - - /** */ - protected Sum sum = new Sum(); /** - * Returns the - * arithmetic mean of a double[] of the available values. + * Returns the arithmetic mean of the values in the input array, or + * Double.NaN if the array is empty. + *

+ * Throws IllegalArgumentException if the array is null. + *

+ * See {@link Mean} for details on the computing algorithm. + * * @param values Is a double[] containing the values * @param begin processing at this point in the array * @param length the number of elements to include * @return the mean of the values or Double.NaN if the array is empty + * @throws IllegalArgumentException if the array is null * @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) { if (test(values, begin, length)) { + Sum sum = new Sum(); return sum.evaluate(values, begin, length) / ((double) length); } return Double.NaN;