You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ps...@apache.org on 2004/06/27 01:33:27 UTC

cvs commit: jakarta-commons/math/src/java/org/apache/commons/math/stat/univariate/moment Variance.java

psteitz     2004/06/26 16:33:27

  Modified:    math/src/java/org/apache/commons/math/stat/univariate/moment
                        Variance.java
  Log:
  Improved javadoc, simplified implementation.
  
  Revision  Changes    Path
  1.21      +20 -38    jakarta-commons/math/src/java/org/apache/commons/math/stat/univariate/moment/Variance.java
  
  Index: Variance.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/math/src/java/org/apache/commons/math/stat/univariate/moment/Variance.java,v
  retrieving revision 1.20
  retrieving revision 1.21
  diff -u -r1.20 -r1.21
  --- Variance.java	23 Jun 2004 16:26:15 -0000	1.20
  +++ Variance.java	26 Jun 2004 23:33:27 -0000	1.21
  @@ -20,9 +20,17 @@
   import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic;
   
   /**
  - * Updating forumulas use West's algorithm as described in
  - * <a href="http://doi.acm.org/10.1145/359146.359152">Chan, T. F. and
  - * J. G. Lewis 1979, <i>Communications of the ACM</i>,
  + * Computes the (unbiased) sample variance.  Uses the definitional formula: 
  + * <p>
  + * variance = sum((x_i - mean)^2) / (n - 1)
  + * <p>
  + * where mean is the {@link Mean} and <code>n</code> is the number
  + * of sample observations.  
  + * <p>
  + * The definitional formula does not have good numerical properties, so
  + * this implementation uses updating formulas based on West's algorithm
  + *  as described in <a href="http://doi.acm.org/10.1145/359146.359152">
  + * Chan, T. F. andJ. G. Lewis 1979, <i>Communications of the ACM</i>,
    * vol. 22 no. 9, pp. 526-531.</a>.
    *
    * @version $Revision$ $Date$
  @@ -43,20 +51,6 @@
       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.
        */
       public Variance() {
  @@ -64,7 +58,7 @@
       }
   
       /**
  -     * 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 @@
        * @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 @@
           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 @@
        * 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)) {
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org