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/07/10 19:02:44 UTC

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

psteitz     2004/07/10 10:02:43

  Modified:    math/src/java/org/apache/commons/math/stat/univariate/moment
                        Skewness.java
  Log:
  Changed getN() to return long. Changed evalutate() to return NaN if sample size is less than 3 (consistent w/ getResult()).
  
  Revision  Changes    Path
  1.25      +28 -34    jakarta-commons/math/src/java/org/apache/commons/math/stat/univariate/moment/Skewness.java
  
  Index: Skewness.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/math/src/java/org/apache/commons/math/stat/univariate/moment/Skewness.java,v
  retrieving revision 1.24
  retrieving revision 1.25
  diff -u -r1.24 -r1.25
  --- Skewness.java	4 Jul 2004 09:02:36 -0000	1.24
  +++ Skewness.java	10 Jul 2004 17:02:43 -0000	1.25
  @@ -104,7 +104,7 @@
       /**
        * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getN()
        */
  -    public double getN() {
  +    public long getN() {
           return moment.getN();
       }
       
  @@ -139,41 +139,35 @@
           // Initialize the skewness
           double skew = Double.NaN;
   
  -        if (test(values, begin, length)) {
  +        if (test(values, begin, length) && length > 2 ){
               Mean mean = new Mean();
  -            if (length <= 2) {
  -                skew = 0.0;
  -            } else {
  -                // 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));
  -
  -                double accum3 = 0.0;
  -                for (int i = begin; i < begin + length; i++) {
  -                    accum3 += Math.pow(values[i] - m, 3.0d);
  -                }
  -                accum3 /= Math.pow(stdDev, 3.0d);
  -
  -                // Get N
  -                double n0 = length;
  -
  -                // Calculate skewness
  -                skew = (n0 / ((n0 - 1) * (n0 - 2))) * accum3;
  +            // 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));
  +            
  +            double accum3 = 0.0;
  +            for (int i = begin; i < begin + length; i++) {
  +                accum3 += Math.pow(values[i] - m, 3.0d);
  +            }
  +            accum3 /= Math.pow(stdDev, 3.0d);
  +            
  +            // Get N
  +            double n0 = length;
  +            
  +            // Calculate skewness
  +            skew = (n0 / ((n0 - 1) * (n0 - 2))) * accum3;
           }
  -
           return skew;
       }
  -
   }
  
  
  

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