commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mdigg...@apache.org
Subject cvs commit: jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/univariate/rank Median.java Max.java Min.java Percentile.java
Date Mon, 07 Jul 2003 23:06:49 GMT
mdiggory    2003/07/07 16:06:48

  Modified:    math/src/java/org/apache/commons/math/stat/univariate/moment
                        FourthMoment.java GeometricMean.java Mean.java
                        ThirdMoment.java Kurtosis.java Variance.java
                        Skewness.java StandardDeviation.java
                        SecondMoment.java
               math/src/java/org/apache/commons/math/stat/univariate/summary
                        SumOfSquares.java Product.java Sum.java
                        SumOfLogs.java
               math/src/java/org/apache/commons/math/stat/univariate/rank
                        Median.java Max.java Min.java Percentile.java
  Added:       math/src/java/org/apache/commons/math/stat/univariate/moment
                        FirstMoment.java
  Log:
  This commit adds the constructor and internals for setting and External Moment object to
base internal calculations on. It also remove author tags and adds a new class FirstMoment
to the project.
  
  Revision  Changes    Path
  1.3       +21 -19    jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/univariate/moment/FourthMoment.java
  
  Index: FourthMoment.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/univariate/moment/FourthMoment.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- FourthMoment.java	7 Jul 2003 02:15:19 -0000	1.2
  +++ FourthMoment.java	7 Jul 2003 23:06:47 -0000	1.3
  @@ -54,14 +54,21 @@
   package org.apache.commons.math.stat.univariate.moment;
   
   /**
  - * @author Mark Diggory
  + * 
    *
    */
   public class FourthMoment extends ThirdMoment {
   
       /** fourth moment of values that have been added */
       protected double m4 = Double.NaN;
  -
  +    
  +    /** temporary internal state made available for higher order moments */
  +    protected double prevM3 = 0.0;
  +
  +    /** temporary internal state made available for higher order moments */
  +    protected double n3 = 0.0;
  +            
  +            
       /**
        * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
        */
  @@ -70,30 +77,23 @@
               m4 = m3 = m2 = m1 = 0.0;
           }
   
  -        n++;
  -        double dev = d - m1;
  -        double v = dev / ((double) n);
  -        double v2 = v * v;
  -
  -        double n0 = (double) n;
  -        double n1 = (double) (n - 1);
  -        double n2 = (double) (n - 2);
  +        /* retain previous m3 */
  +        prevM3 = m3;
  +        
  +        /* increment m1, m2 and m3 (and prevM2, _n0, _n1, _n2, _v, _v2) */
  +        super.increment(d);
   
  +        n3 = (double) (n - 3);
  +        
           m4 =
               m4
  -                - (4.0 * v * m3)
  -                + (6.0 * v2 * m2)
  +                - (4.0 * v * prevM3)
  +                + (6.0 * v2 * prevM2)
                   + ((n0 * n0) - 3 * n1) * (v2 * v2 * n1 * n0);
   
  -        m3 = m3 - (3.0 * v * m2) + (n0 * n1 * n2 * v2 * v);
  -
  -        m2 = m2 + n1 * dev * v;
  -
  -        m1 = m1 + v;
  -
           return m4;
       }
  -
  +    
       /**
        * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
        */
  @@ -107,6 +107,8 @@
       public void clear() {
           super.clear();
           m4 = Double.NaN;
  +        prevM3 = 0.0;
  +        n3 = 0.0;
       }
   
   }
  
  
  
  1.5       +7 -3      jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/univariate/moment/GeometricMean.java
  
  Index: GeometricMean.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/univariate/moment/GeometricMean.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- GeometricMean.java	7 Jul 2003 16:57:33 -0000	1.4
  +++ GeometricMean.java	7 Jul 2003 23:06:47 -0000	1.5
  @@ -56,13 +56,11 @@
   import org.apache.commons.math.stat.univariate.summary.SumOfLogs;
   
   /**
  - * @author Mark Diggory
  + * 
    *
    */
   public class GeometricMean extends SumOfLogs {
    
  -    //private SumOfLogs sumLog = new SumOfLogs();
  -    
       private double geoMean = Double.NaN;
       
       private int n = 0;
  @@ -92,6 +90,12 @@
       }
       
       /**
  +     * Returns the geometric mean for this collection of values
  +     * @param values Is a double[] containing the values
  +     * @param begin processing at this point in the array
  +     * @param length processing at this point in the array
  +     * @return the geometric mean or Double.NaN if the array is empty or
  +     * any of the values are <= 0.
        * @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[],
int, int)
        */
       public double evaluate(double[] values, int begin, int length) {
  
  
  
  1.3       +41 -21    jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/univariate/moment/Mean.java
  
  Index: Mean.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/univariate/moment/Mean.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Mean.java	7 Jul 2003 02:15:19 -0000	1.2
  +++ Mean.java	7 Jul 2003 23:06:47 -0000	1.3
  @@ -53,57 +53,77 @@
    */
   package org.apache.commons.math.stat.univariate.moment;
   
  -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;
   
   /**
  - * @author Mark Diggory
  + *
    */
   public class Mean extends AbstractStorelessUnivariateStatistic {
   
  +    /** first moment of values that have been added */
  +    protected FirstMoment moment = null;
   
  -    /** count of values that have been added */
  -    protected int n = 0;
  +    protected boolean incMoment = true;
  +
  +    public Mean() {
  +        moment = new FirstMoment();
  +    }
  +
  +    public Mean(FirstMoment m1) {
  +        this.moment = m1;
  +        incMoment = false;
  +    }
   
  -    /** first moment of values that have been added */
  -    protected double m1 = Double.NaN;
  -    
  -    private Sum sum = new Sum();
  -    
       /**
        * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
        */
       public double increment(double d) {
  -        if (n < 1) {
  -             m1 = 0.0;
  +        if (incMoment) {
  +            moment.increment(d);
           }
  -         
  -        n++;
  -        m1 += (d - m1) / ((double) n);
  -        return m1;
  +
  +        return moment.m1;
       }
  -    
  +
       /**
        * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
        */
       public void clear() {
  -        m1 = Double.NaN;
  -        n = 0;
  +        if (incMoment) {
  +            moment.clear();
  +        }
       }
  -    
  +
       /**
        * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
        */
       public double getValue() {
  -        return m1;
  +        return moment.m1;
       }
   
  +    /*UnvariateStatistic Approach */
  +    Sum sum = new Sum();
  +
       /**
  +     * Returns the <a href=http://www.xycoon.com/arithmetic_mean.htm>
  +     * arithmetic mean </a> of the available values 
  +     * @param values Is a double[] containing the values
  +     * @param begin processing at this point in the array
  +     * @param length processing at this point in the array
  +     * @return the mean of the values or Double.NaN if the array is empty
        * @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[],
int, int)
        */
       public double evaluate(double[] values, int begin, int length) {
  -        if(test(values,begin,length))
  +        if (test(values, begin, length)) {
               return sum.evaluate(values, begin, length) / ((double) length);
  +        }
           return Double.NaN;
       }
   
  
  
  
  1.3       +20 -14    jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/univariate/moment/ThirdMoment.java
  
  Index: ThirdMoment.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/univariate/moment/ThirdMoment.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- ThirdMoment.java	7 Jul 2003 02:15:19 -0000	1.2
  +++ ThirdMoment.java	7 Jul 2003 23:06:47 -0000	1.3
  @@ -54,7 +54,7 @@
   package org.apache.commons.math.stat.univariate.moment;
   
   /**
  - * @author Mark Diggory
  + * 
    *
    */
   public class ThirdMoment extends SecondMoment{
  @@ -62,6 +62,15 @@
       /** third moment of values that have been added */
       protected double m3 = Double.NaN;
   
  +    /** temporary internal state made availabel for higher order moments */
  +    protected double v2 = 0.0;
  +    
  +    /** temporary internal state made availabel for higher order moments */
  +    protected double n2 = 0.0;
  +            
  +    /** temporary internal state made availabel for higher order moments */
  +    protected double prevM2 = 0.0;
  +                  
       /**
        * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
        */
  @@ -70,23 +79,17 @@
               m3 = m2 = m1 = 0.0;
           }
           
  -        n++;
  +        /* retain a reference to the last m2*/
  +        prevM2 = m2;
           
  -        double dev = d - m1;
  -        double v = dev / ((double) n);
  -        double v2 = v * v;
  -
  -        double n0 = (double) n;
  -        double n1 = (double) (n - 1);
  -        double n2 = (double) (n - 2);
  -
  +        /* increment m1 and m2 (and _n0, _n1, _v) */
  +        super.increment(d);
           
  -        m3 = m3 - (3.0 * v * m2) + (n0 * n1 * n2 * v2 * v);
  +        v2 = v * v;
  +        n2 = (double) (n - 2);
   
  -        m2 = m2 + n1 * dev * v;
  +        m3 = m3 - (3.0 * v * prevM2) + (n0 * n1 * n2 * v2 * v);
   
  -        m1 = m1 + v;
  -        
           return m3;
       }
   
  @@ -103,6 +106,9 @@
       public void clear() {
           super.clear();
           m3 = Double.NaN;
  +        v2 = 0.0;
  +        n2 = 0.0;
  +        prevM2 = 0.0;
       }
   
   }
  
  
  
  1.3       +99 -61    jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/univariate/moment/Kurtosis.java
  
  Index: Kurtosis.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/univariate/moment/Kurtosis.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Kurtosis.java	7 Jul 2003 02:15:19 -0000	1.2
  +++ Kurtosis.java	7 Jul 2003 23:06:47 -0000	1.3
  @@ -53,93 +53,131 @@
    */
   package org.apache.commons.math.stat.univariate.moment;
   
  +import org
  +    .apache
  +    .commons
  +    .math
  +    .stat
  +    .univariate
  +    .AbstractStorelessUnivariateStatistic;
  +
   /**
  - * @author Mark Diggory
  + *
    */
  -public class Kurtosis extends FourthMoment {
  +public class Kurtosis extends AbstractStorelessUnivariateStatistic {
   
       private double kurtosis = Double.NaN;
   
  +    protected FourthMoment moment = null;
  +
  +    protected boolean incMoment = true;
  +
  +    public Kurtosis() {
  +        moment = new FourthMoment();
  +    }
  +
  +    public Kurtosis(FourthMoment m4) {
  +        incMoment = false;
  +        this.moment = m4;
  +    }
  +
       /**
        * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
        */
       public double increment(double d) {
  -        super.increment(d);
  -        
  -        double variance = (n <= 1) ? 0.0 : m2 / (double) (n - 1);
  +        if (incMoment) {
  +            moment.increment(d);
  +        }
  +
  +        double variance =
  +            (moment.n < 1) ? 0.0 : moment.m2 / (double) (moment.n - 1);
   
           kurtosis =
  -            (n <= 3 || variance < 10E-20)
  +            (moment.n <= 3 || variance < 10E-20)
                   ? 0.0
  -                : ((double)n * ((double)n + 1) * m4 - 3 * m2 * m2 * (n-1))
  -                    / ((n-1) * (n-2) * (n-3) * variance * variance);
  -        
  +                : (moment.n0 * (moment.n0 + 1) * moment.m4
  +                    - 3 * moment.m2 * moment.m2 * moment.n1)
  +                    / (moment.n1 * moment.n2 * moment.n3 * variance * variance);
  +
           return kurtosis;
       }
  -    
  +
       /**
        * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
        */
       public double getValue() {
           return kurtosis;
       }
  -    
  +
       /**
        * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
        */
       public void clear() {
  -        super.clear();
  +        if (incMoment) {
  +            moment.clear();
  +        }
           kurtosis = Double.NaN;
       }
   
  +    /*UnvariateStatistic Approach */
  +
  +    Mean mean = new Mean();
  +
       /**
  -        * Returns the kurtosis for this collection of values. Kurtosis is a 
  -        * measure of the "peakedness" of a distribution.
  -        * @param values Is a double[] containing the values
  -        * @param begin processing at this point in the array
  -        * @param length processing at this point in the array
  -        * @return the kurtosis of the values or Double.NaN if the array is empty
  -        */
  -       public double evaluate(double[] values, int begin, int length) {
  -           test(values, begin, length);
  -
  -           // Initialize the kurtosis
  -           double kurt = Double.NaN;
  -
  -           // Get the mean and the standard deviation
  -           double mean = super.evaluate(values, begin, length);
  -
  -           // Calc the std, this is implemented here instead of using the 
  -           // standardDeviation method eliminate a duplicate pass to get the mean
  -           double accum = 0.0;
  -           double accum2 = 0.0;
  -           for (int i = begin; i < begin + length; i++) {
  -               accum += Math.pow((values[i] - mean), 2.0);
  -               accum2 += (values[i] - mean);
  -           }
  -        
  -           double stdDev =
  -               Math.sqrt(
  -                   (accum - (Math.pow(accum2, 2) / ((double) length)))
  -                       / (double) (length - 1));
  -
  -           // Sum the ^4 of the distance from the mean divided by the 
  -           // standard deviation
  -           double accum3 = 0.0;
  -           for (int i = begin; i < begin + length; i++) {
  -               accum3 += Math.pow((values[i] - mean) / stdDev, 4.0);
  -           }
  -
  -           // Get N
  -           double n = length;
  -
  -           double coefficientOne = (n * (n + 1)) / ((n - 1) * (n - 2) * (n - 3));
  -           double termTwo = ((3 * Math.pow(n - 1, 2.0)) / ((n - 2) * (n - 3)));
  -        
  -           // Calculate kurtosis
  -           kurt = (coefficientOne * accum3) - termTwo;
  -
  -           return kurt;
  -       }
  -       
  +    * This algorithm uses a corrected two pass algorithm of the following 
  +    * <a href="http://lib-www.lanl.gov/numerical/bookcpdf/c14-1.pdf">
  +    * corrected two pass formula (14.1.8)</a>, and also referenced in:<p/>
  +    * "Algorithms for Computing the Sample Variance: Analysis and
  +    * Recommendations", Chan, T.F., Golub, G.H., and LeVeque, R.J. 
  +    * 1983, American Statistician, vol. 37, pp. 242?247.
  +    * <p/>
  +    * Returns the kurtosis for this collection of values. Kurtosis is a 
  +    * measure of the "peakedness" of a distribution.
  +    * @param values Is a double[] containing the values
  +    * @param begin processing at this point in the array
  +    * @param length processing at this point in the array
  +    * @return the kurtosis of the values or Double.NaN if the array is empty
  +    */
  +    public double evaluate(double[] values, int begin, int length) {
  +        test(values, begin, length);
  +
  +        // Initialize the kurtosis
  +        double kurt = Double.NaN;
  +
  +        // Get the mean and the standard deviation
  +        double m = mean.evaluate(values, begin, length);
  +
  +        // Calc the std, this is implemented here instead of using the 
  +        // standardDeviation method eliminate a duplicate pass to get the mean
  +        double accum = 0.0;
  +        double accum2 = 0.0;
  +        for (int i = begin; i < begin + length; i++) {
  +            accum += Math.pow((values[i] - m), 2.0);
  +            accum2 += (values[i] - m);
  +        }
  +
  +        double stdDev =
  +            Math.sqrt(
  +                (accum - (Math.pow(accum2, 2) / ((double) length)))
  +                    / (double) (length - 1));
  +
  +        // Sum the ^4 of the distance from the mean divided by the 
  +        // standard deviation
  +        double accum3 = 0.0;
  +        for (int i = begin; i < begin + length; i++) {
  +            accum3 += Math.pow((values[i] - m) / stdDev, 4.0);
  +        }
  +
  +        // Get N
  +        double n = length;
  +
  +        double coefficientOne = (n * (n + 1)) / ((n - 1) * (n - 2) * (n - 3));
  +        double termTwo = ((3 * Math.pow(n - 1, 2.0)) / ((n - 2) * (n - 3)));
  +
  +        // Calculate kurtosis
  +        kurt = (coefficientOne * accum3) - termTwo;
  +
  +        return kurt;
  +    }
  +
   }
  
  
  
  1.3       +43 -16    jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/univariate/moment/Variance.java
  
  Index: Variance.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/univariate/moment/Variance.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Variance.java	7 Jul 2003 02:15:19 -0000	1.2
  +++ Variance.java	7 Jul 2003 23:06:47 -0000	1.3
  @@ -53,29 +53,37 @@
    */
   package org.apache.commons.math.stat.univariate.moment;
   
  -import org
  -    .apache
  -    .commons
  -    .math
  -    .stat
  -    .univariate
  -    .AbstractStorelessUnivariateStatistic;
  +import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic;
   
   /**
  - * @author Mark Diggory
  + *
    *
    */
  -public class Variance extends SecondMoment {
  +public class Variance extends AbstractStorelessUnivariateStatistic{
   
  -    private double variance = Double.NaN;
  +    protected double variance = Double.NaN;
   
  +    protected SecondMoment moment = null;
  +    
  +    protected boolean incMoment = true;
  +    
  +    public Variance(){
  +        moment = new SecondMoment();
  +    }
  +    
  +    public Variance(SecondMoment m2){
  +        incMoment = false;
  +        this.moment = m2;
  +    }
       /**
        * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
        */
       public double increment(double d) {
  -        super.increment(d);
  -
  -        variance = (n < 1) ? 0.0 : m2 / (double)(n - 1);
  +        if (incMoment) {
  +            moment.increment(d);
  +        }
  +        
  +        variance = (moment.n < 1) ? 0.0 : moment.m2 / (double)(moment.n - 1);
           
           return variance;
       }
  @@ -91,11 +99,30 @@
        * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
        */
       public void clear() {
  -        super.clear();
  +        if (incMoment) {
  +            moment.clear();
  +        }
           variance = Double.NaN;
       }
       
  -    /* (non-Javadoc)
  +    /*UnvariateStatistic Approach */
  +
  +    Mean mean = new Mean();
  +    
  +    /**
  +     * Returns the variance of the available values. This uses a corrected
  +     * two pass algorithm of the following 
  +     * <a href="http://lib-www.lanl.gov/numerical/bookcpdf/c14-1.pdf">
  +     * corrected two pass formula (14.1.8)</a>, and also referenced in:<p/>
  +     * "Algorithms for Computing the Sample Variance: Analysis and
  +     * Recommendations", Chan, T.F., Golub, G.H., and LeVeque, R.J. 
  +     * 1983, American Statistician, vol. 37, pp. 242?247.
  +     * 
  +     * @param values Is a double[] containing the values
  +     * @param begin processing at this point in the array
  +     * @param length processing at this point in the array
  +     * @return the result, Double.NaN if no values for an empty array 
  +     * or 0.0 for a single value set.  
        * @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[],
int, int)
        */
       public double evaluate(double[] values, int begin, int length) {
  @@ -103,7 +130,7 @@
           if (values.length == 1) {
               var = 0;
           } else if (values.length > 1) {
  -            double m = super.evaluate(values, begin, length);
  +            double m = mean.evaluate(values, begin, length);
               double accum = 0.0;
               double accum2 = 0.0;
               for (int i = begin; i < begin + length; i++) {
  
  
  
  1.3       +92 -55    jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/univariate/moment/Skewness.java
  
  Index: Skewness.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/univariate/moment/Skewness.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Skewness.java	7 Jul 2003 02:15:19 -0000	1.2
  +++ Skewness.java	7 Jul 2003 23:06:47 -0000	1.3
  @@ -53,28 +53,51 @@
    */
   package org.apache.commons.math.stat.univariate.moment;
   
  +import org
  +    .apache
  +    .commons
  +    .math
  +    .stat
  +    .univariate
  +    .AbstractStorelessUnivariateStatistic;
  +
   /**
  - * @author Mark Diggory
  + * 
    *
    */
  -public class Skewness extends ThirdMoment {
  +public class Skewness extends AbstractStorelessUnivariateStatistic {
   
       private double skewness = Double.NaN;
   
  +    protected ThirdMoment moment = null;
  +
  +    protected boolean incMoment = true;
  +
  +    public Skewness() {
  +        moment = new ThirdMoment();
  +    }
  +
  +    public Skewness(ThirdMoment m3) {
  +        incMoment = false;
  +        this.moment = m3;
  +    }
  +
       /**
        * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
        */
       public double increment(double d) {
  +        if (incMoment) {
  +            moment.increment(d);
  +        }
   
  -        super.increment(d);
  -
  -        double variance = (n <= 1) ? 0.0 : m2 / (double) (n - 1);
  +        double variance =
  +            (moment.n < 1) ? 0.0 : moment.m2 / (double) (moment.n - 1);
   
           skewness =
  -            (n <= 2 || variance < 10E-20)
  +            (moment.n <= 2 || variance < 10E-20)
                   ? 0.0
  -                : (((double) n) * m3)
  -                    / ((n - 1) * (n - 2) * Math.sqrt(variance) * variance);
  +                : (moment.n0 * moment.m3)
  +                    / (moment.n1 * moment.n2 * Math.sqrt(variance) * variance);
   
           return skewness;
       }
  @@ -90,55 +113,69 @@
        * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
        */
       public void clear() {
  -        super.clear();
  +        if (incMoment) {
  +            moment.clear();
  +        }
           skewness = Double.NaN;
       }
   
  +    /*UnvariateStatistic Approach */
  +
  +    Mean mean = new Mean();
  +
       /**
  -      * Returns the skewness of a collection of values.  Skewness is a 
  -      * measure of the assymetry of a given distribution. 
  -      * @param values Is a double[] containing the values
  -      * @param begin processing at this point in the array
  -      * @param length processing at this point in the array
  -      * @return the skewness of the values or Double.NaN if the array is empty
  -      */
  -     public double evaluate(double[] values, int begin, int length) {
  -
  -         test(values, begin, length);
  -
  -         // Initialize the skewness
  -         double skew = Double.NaN;
  -
  -         // Get the mean and the standard deviation
  -         double mean = super.evaluate(values, begin, length);
  -
  -         // Calc the std, this is implemented here instead of using the 
  -         // standardDeviation method eliminate a duplicate pass to get the mean
  -         double accum = 0.0;
  -         double accum2 = 0.0;
  -         for (int i = begin; i < begin + length; i++) {
  -             accum += Math.pow((values[i] - mean), 2.0);
  -             accum2 += (values[i] - mean);
  -         }
  -         double stdDev =
  -             Math.sqrt(
  -                 (accum - (Math.pow(accum2, 2) / ((double) length)))
  -                     / (double) (length - 1));
  -
  -         // Calculate the skew as the sum the cubes of the distance 
  -         // from the mean divided by the standard deviation.
  -         double accum3 = 0.0;
  -         for (int i = begin; i < begin + length; i++) {
  -             accum3 += Math.pow((values[i] - mean) / stdDev, 3.0);
  -         }
  -
  -         // Get N
  -         double n = length;
  -
  -         // Calculate skewness
  -         skew = (n / ((n - 1) * (n - 2))) * accum3;
  -
  -         return skew;
  -     }
  -     
  +     * This algorithm uses a corrected two pass algorithm of the following 
  +     * <a href="http://lib-www.lanl.gov/numerical/bookcpdf/c14-1.pdf">
  +     * corrected two pass formula (14.1.8)</a>, and also referenced in:<p/>
  +     * "Algorithms for Computing the Sample Variance: Analysis and
  +     * Recommendations", Chan, T.F., Golub, G.H., and LeVeque, R.J. 
  +     * 1983, American Statistician, vol. 37, pp. 242?247.
  +     * <p/>
  +     * Returns the skewness of a collection of values.  Skewness is a 
  +     * measure of the assymetry of a given distribution. 
  +     * @param values Is a double[] containing the values
  +     * @param begin processing at this point in the array
  +     * @param length processing at this point in the array
  +     * @return the skewness of the values or Double.NaN if the array is empty
  +     * @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[],
int, int)
  +     */
  +    public double evaluate(double[] values, int begin, int length) {
  +
  +        test(values, begin, length);
  +
  +        // Initialize the skewness
  +        double skew = Double.NaN;
  +
  +        // Get the mean and the standard deviation
  +        double m = mean.evaluate(values, begin, length);
  +
  +        // Calc the std, this is implemented here instead of using the 
  +        // standardDeviation method eliminate a duplicate pass to get the mean
  +        double accum = 0.0;
  +        double accum2 = 0.0;
  +        for (int i = begin; i < begin + length; i++) {
  +            accum += Math.pow((values[i] - m), 2.0);
  +            accum2 += (values[i] - m);
  +        }
  +        double stdDev =
  +            Math.sqrt(
  +                (accum - (Math.pow(accum2, 2) / ((double) length)))
  +                    / (double) (length - 1));
  +
  +        // Calculate the skew as the sum the cubes of the distance 
  +        // from the mean divided by the standard deviation.
  +        double accum3 = 0.0;
  +        for (int i = begin; i < begin + length; i++) {
  +            accum3 += Math.pow((values[i] - m) / stdDev, 3.0);
  +        }
  +
  +        // Get N
  +        double n = length;
  +
  +        // Calculate skewness
  +        skew = (n / ((n - 1) * (n - 2))) * accum3;
  +
  +        return skew;
  +    }
  +
   }
  
  
  
  1.3       +24 -14    jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/univariate/moment/StandardDeviation.java
  
  Index: StandardDeviation.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/univariate/moment/StandardDeviation.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- StandardDeviation.java	7 Jul 2003 02:15:19 -0000	1.2
  +++ StandardDeviation.java	7 Jul 2003 23:06:47 -0000	1.3
  @@ -53,47 +53,57 @@
    */
   package org.apache.commons.math.stat.univariate.moment;
   
  -import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic;
  -
   /**
  - * @author Mark Diggory
  + * 
    *
    */
  -public class StandardDeviation extends AbstractStorelessUnivariateStatistic {
  +public class StandardDeviation extends Variance {
   
  -    private double value = Double.NaN;
  +    private double std = Double.NaN;
       
  -    private Variance var = new Variance();
  +    public StandardDeviation(){
  +        super();
  +    }
  +    
  +    public StandardDeviation(SecondMoment m2){
  +        super(m2);
  +    }
       
       /**
        * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
        */
       public double increment(double d) {
  -        var.increment(d);
  -        value = Math.sqrt(var.getValue());
  -        return value;
  +        super.increment(d);
  +        std = (variance != 0.0) ? Math.sqrt(variance) : 0.0;
  +        return std;
       }
       
       /**
        * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
        */
       public double getValue() {
  -        return value;
  +        return std;
       }
       
       /**
        * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
        */
       public void clear() {
  -        var.clear();
  -        value = Double.NaN;
  +        super.clear();
  +        std = Double.NaN;
       }
   
  -    /* (non-Javadoc)
  +    /**
  +     * Returns the Standard Deviation on an array of values.  
  +     * @param values Is a double[] containing the values
  +     * @param begin processing at this point in the array
  +     * @param length processing at this point in the array
  +     * @return the result, Double.NaN if no values for an empty array 
  +     * or 0.0 for a single value set.  
        * @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[],
int, int)
        */
       public double evaluate(double[] values, int begin, int length) {
  -        double tmp = var.evaluate(values, begin, length);
  +        double tmp = super.evaluate(values, begin, length);
           return tmp != 0.0 ? Math.sqrt(tmp) : 0.0;
       }
   
  
  
  
  1.3       +14 -17    jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/univariate/moment/SecondMoment.java
  
  Index: SecondMoment.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/univariate/moment/SecondMoment.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- SecondMoment.java	7 Jul 2003 02:15:19 -0000	1.2
  +++ SecondMoment.java	7 Jul 2003 23:06:47 -0000	1.3
  @@ -53,23 +53,18 @@
    */
   package org.apache.commons.math.stat.univariate.moment;
   
  -import org
  -    .apache
  -    .commons
  -    .math
  -    .stat
  -    .univariate
  -    .AbstractStorelessUnivariateStatistic;
  -
   /**
  - * @author Mark Diggory
  + * 
    *
    */
  -public class SecondMoment extends Mean {
  +public class SecondMoment extends FirstMoment {
   
       /** second moment of values that have been added */
       protected double m2 = Double.NaN;
   
  +    /** temporary internal state made availabel for higher order moments */
  +    protected double n1 = 0.0;
  +    
       /**
        * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
        */
  @@ -77,16 +72,17 @@
           if (n < 1) {
               m1 = m2 = 0.0;
           }
  +        
  +        /* increment m1 and _n0, _dev,  _v) */
  +        super.increment(d);
   
  -        n++;
  -
  -        double dev = d - m1;
  -        double v = dev / ((double) n);
  -
  -        m2 += ((double)(n - 1)) * dev * v;
  -        m1 += v;
  +        n1 = n0 - 1;
  +        
  +        /* increment and return m2 */
  +        m2 += n1 * dev * v;
           
           return m2;
  +        
       }
   
       /**
  @@ -95,6 +91,7 @@
       public void clear() {
           super.clear();
           m2 = Double.NaN;
  +        n1 = 0.0;
       }
   
       /**
  
  
  
  1.1                  jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/univariate/moment/FirstMoment.java
  
  Index: FirstMoment.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowlegement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowlegement may appear in the software itself,
   *    if and wherever such third-party acknowlegements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.commons.math.stat.univariate.moment;
  
  import org.apache.commons.math.stat.univariate.AbstractStorelessUnivariateStatistic;
  
  /**
   * 
   */
  public class FirstMoment extends AbstractStorelessUnivariateStatistic {
  
      /** count of values that have been added */
      protected int n = 0;
  
      /** first moment of values that have been added */
      protected double m1 = Double.NaN;
      
      /** temporary internal state made available for higher order moments */
      protected double dev = 0.0;
      
      /** temporary internal state made available for higher order moments */
      protected double v = 0.0;
      
      /** temporary internal state made available for higher order moments */
      protected double n0 = 0.0;
      
      /**
       * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#increment(double)
       */
      public double increment(double d) {
          if (n < 1) {
               m1 = 0.0;
          }
          
          n++;
          dev = d - m1;
          n0 = (double)n;
          v = dev / n0;
  
          return m1 += v;                    
      }
      
      /**
       * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#clear()
       */
      public void clear() {
          m1 = Double.NaN;
          n = 0;
          dev = 0.0;
          v = 0.0;
          n0 = 0.0;
      }
      
      /**
       * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getValue()
       */
      public double getValue() {
          return m1;
      }
  
  }
  
  
  1.3       +5 -1      jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/univariate/summary/SumOfSquares.java
  
  Index: SumOfSquares.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/univariate/summary/SumOfSquares.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- SumOfSquares.java	7 Jul 2003 02:15:19 -0000	1.2
  +++ SumOfSquares.java	7 Jul 2003 23:06:48 -0000	1.3
  @@ -62,7 +62,6 @@
       .AbstractStorelessUnivariateStatistic;
   
   /**
  - * @author Mark Diggory
    *
    */
   public class SumOfSquares extends AbstractStorelessUnivariateStatistic {
  @@ -99,6 +98,11 @@
       }
   
       /**
  +     * Returns the sum of the squares of the available values.
  +     * @param values Is a double[] containing the values
  +     * @param begin processing at this point in the array
  +     * @param length processing at this point in the array
  +     * @return the sum of the squared values or Double.NaN if the array is empty
        * @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[],
int, int)
        */
       public double evaluate(double[] values, int begin, int length) {
  
  
  
  1.3       +6 -1      jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/univariate/summary/Product.java
  
  Index: Product.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/univariate/summary/Product.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Product.java	7 Jul 2003 02:15:19 -0000	1.2
  +++ Product.java	7 Jul 2003 23:06:48 -0000	1.3
  @@ -62,7 +62,7 @@
       .AbstractStorelessUnivariateStatistic;
   
   /**
  - * @author Mark Diggory
  + *
    */
   public class Product extends AbstractStorelessUnivariateStatistic {
   
  @@ -99,6 +99,11 @@
       }
   
       /**
  +     * Returns the product for this collection of values
  +     * @param values Is a double[] containing the values
  +     * @param begin processing at this point in the array
  +     * @param length processing at this point in the array
  +     * @return the product values or Double.NaN if the array is empty
        * @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[],
int, int)
        */
       public double evaluate(double[] values, int begin, int length) {
  
  
  
  1.3       +5 -1      jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/univariate/summary/Sum.java
  
  Index: Sum.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/univariate/summary/Sum.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Sum.java	7 Jul 2003 02:15:19 -0000	1.2
  +++ Sum.java	7 Jul 2003 23:06:48 -0000	1.3
  @@ -62,7 +62,6 @@
       .AbstractStorelessUnivariateStatistic;
   
   /**
  - * @author Mark Diggory
    *
    */
   public class Sum extends AbstractStorelessUnivariateStatistic {
  @@ -99,6 +98,11 @@
       }
   
       /**
  +     * The sum of the values that have been added to Univariate.
  +     * @param values Is a double[] containing the values
  +     * @param begin processing at this point in the array
  +     * @param length processing at this point in the array
  +     * @return the sum of the values or Double.NaN if the array is empty
        * @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[],
int, int)
        */
       public double evaluate(double[] values, int begin, int length) {
  
  
  
  1.3       +5 -1      jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/univariate/summary/SumOfLogs.java
  
  Index: SumOfLogs.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/univariate/summary/SumOfLogs.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- SumOfLogs.java	7 Jul 2003 02:15:19 -0000	1.2
  +++ SumOfLogs.java	7 Jul 2003 23:06:48 -0000	1.3
  @@ -62,7 +62,6 @@
       .AbstractStorelessUnivariateStatistic;
   
   /**
  - * @author Mark Diggory
    *
    */
   public class SumOfLogs extends AbstractStorelessUnivariateStatistic {
  @@ -100,6 +99,11 @@
       }
       
       /**
  +     * Returns the sum of the natural logs for this collection of values
  +     * @param values Is a double[] containing the values
  +     * @param begin processing at this point in the array
  +     * @param length processing at this point in the array
  +     * @return the sumLog value or Double.NaN if the array is empty
        * @see org.apache.commons.math.stat.univariate.UnivariateStatistic#evaluate(double[],
int, int)
        */
       public double evaluate(double[] values, int begin, int length) {
  
  
  
  1.2       +1 -1      jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/univariate/rank/Median.java
  
  Index: Median.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/univariate/rank/Median.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Median.java	5 Jul 2003 18:23:52 -0000	1.1
  +++ Median.java	7 Jul 2003 23:06:48 -0000	1.2
  @@ -55,7 +55,7 @@
   
   
   /**
  - * @author Mark Diggory
  + *
    */
   public class Median extends Percentile {
   
  
  
  
  1.3       +1 -1      jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/univariate/rank/Max.java
  
  Index: Max.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/univariate/rank/Max.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Max.java	7 Jul 2003 02:15:19 -0000	1.2
  +++ Max.java	7 Jul 2003 23:06:48 -0000	1.3
  @@ -62,7 +62,7 @@
       .AbstractStorelessUnivariateStatistic;
   
   /**
  - * @author Mark Diggory
  + *
    */
   public class Max extends AbstractStorelessUnivariateStatistic {
   
  
  
  
  1.3       +1 -1      jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/univariate/rank/Min.java
  
  Index: Min.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/univariate/rank/Min.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Min.java	7 Jul 2003 02:15:19 -0000	1.2
  +++ Min.java	7 Jul 2003 23:06:48 -0000	1.3
  @@ -62,7 +62,7 @@
       .AbstractStorelessUnivariateStatistic;
   
   /**
  - * @author Mark Diggory
  + *
    */
   public class Min extends AbstractStorelessUnivariateStatistic {
   
  
  
  
  1.3       +1 -3      jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/univariate/rank/Percentile.java
  
  Index: Percentile.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/math/src/java/org/apache/commons/math/stat/univariate/rank/Percentile.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Percentile.java	7 Jul 2003 02:15:19 -0000	1.2
  +++ Percentile.java	7 Jul 2003 23:06:48 -0000	1.3
  @@ -57,9 +57,7 @@
   import org.apache.commons.math.stat.univariate.AbstractUnivariateStatistic;
   
   /**
  - * @author <a href="mailto:tobrien@apache.org">Tim O'Brien</a>
  - * @author Mark Diggory
  - * @author <a href="mailto:phil@steitz.com">Phil Steitz</a>
  + *
    */
   public class Percentile extends AbstractUnivariateStatistic {
   
  
  
  

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


Mime
View raw message