commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pste...@apache.org
Subject cvs commit: jakarta-commons/math/src/java/org/apache/commons/math/stat/univariate/moment Kurtosis.java
Date Fri, 18 Jun 2004 01:42:47 GMT
psteitz     2004/06/17 18:42:47

  Modified:    math/src/java/org/apache/commons/math/stat/univariate/moment
                        Kurtosis.java
  Log:
  Improved javadoc.
  Eliminated unecessary / unused data members.
  Made increment() and clear() throw IllegalStatException when the
  statistic is based on an external moment.
  Ensured that Double.NaN is returned for n < 4.
  
  Revision  Changes    Path
  1.20      +73 -82    jakarta-commons/math/src/java/org/apache/commons/math/stat/univariate/moment/Kurtosis.java
  
  Index: Kurtosis.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/math/src/java/org/apache/commons/math/stat/univariate/moment/Kurtosis.java,v
  retrieving revision 1.19
  retrieving revision 1.20
  diff -u -r1.19 -r1.20
  --- Kurtosis.java	27 Apr 2004 16:42:30 -0000	1.19
  +++ Kurtosis.java	18 Jun 2004 01:42:47 -0000	1.20
  @@ -27,6 +27,9 @@
    *  kurtosis = { [n(n+1) / (n -1)(n - 2)(n-3)] sum[(x_i - mean)^4] / std^4 } - [3(n-1)^2
/ (n-2)(n-3)]
    *  <p>
    *  where n is the number of values, mean is the {@link Mean} and std is the {@link StandardDeviation}
  + * <p>
  + *  Note that this statistic is undefined for n < 4.  <code>Double.Nan</code>
is returned when
  + *  there is not sufficient data to compute the statistic.
    * 
    * @version $Revision$ $Date$
    */
  @@ -35,17 +38,17 @@
       /** Serializable version identifier */
       static final long serialVersionUID = 2784465764798260919L;  
         
  -    /** */
  +    /**Fourth Moment on which this statistic is based */
       protected FourthMoment moment = null;
   
  -    /** */
  +    /** 
  +     * Determines whether or not this statistic can be incremented or cleared.
  +     * <p>
  +     * Statistics based on (contructed from) external moments cannot
  +     * be incremented or cleared.
  +    */
       protected boolean incMoment = true;
   
  -    /** */
  -    private double kurtosis = Double.NaN;
  -
  -    /** */
  -    private long n = 0;
   
       /**
        * Construct a Kurtosis
  @@ -55,7 +58,8 @@
       }
   
       /**
  -     * Construct a Kurtosis with an external moment
  +     * Construct a Kurtosis from an external moment
  +     * 
        * @param m4 external Moment
        */
       public Kurtosis(final FourthMoment m4) {
  @@ -69,6 +73,9 @@
       public void increment(final double d) {
           if (incMoment) {
               moment.increment(d);
  +        }  else  {
  +            throw new IllegalStateException
  +            ("Statistics constructed from external moments cannot be incremented");
           }
       }
   
  @@ -76,25 +83,19 @@
        * @see org.apache.commons.math.stat.univariate.StorelessUnivariateStatistic#getResult()
        */
       public double getResult() {
  -        if (n < moment.n) {
  -            if (moment.n <= 0) {
  -                kurtosis = Double.NaN;
  -            }
  -
  -            double variance =
  -                (moment.n < 1) ? 0.0 : moment.m2 / (double) (moment.n - 1);
  -
  -            if (moment.n <= 3 || variance < 10E-20) {
  -                kurtosis = 0.0;
  -            } else {
  -                kurtosis =
  -                    (moment.n0 * (moment.n0 + 1) * moment.m4 -
  -                    3 * moment.m2 * moment.m2 * moment.n1) /
  -                    (moment.n1 * moment.n2 * moment.n3 * variance * variance);
  -            }
  -            n = moment.n;
  +        double kurtosis = Double.NaN;
  +        if (moment.getN() > 3) {
  +            double variance = moment.m2 / (double) (moment.n - 1);
  +                
  +                if (moment.n <= 3 || variance < 10E-20) {
  +                    kurtosis = 0.0;
  +                } else {
  +                    kurtosis =
  +                        (moment.n0 * (moment.n0 + 1) * moment.m4 -
  +                                3 * moment.m2 * moment.m2 * moment.n1) /
  +                                (moment.n1 * moment.n2 * moment.n3 * variance * variance);
  +                }
           }
  -
           return kurtosis;
       }
   
  @@ -104,9 +105,10 @@
       public void clear() {
           if (incMoment) {
               moment.clear();
  +        } else  {
  +            throw new IllegalStateException
  +                ("Statistics constructed from external moments cannot be cleared");
           }
  -        kurtosis = Double.NaN;
  -        n = 0;
       }
   
       /**
  @@ -116,10 +118,7 @@
           return moment.getN();
       }
       
  -    /*UnvariateStatistic Approach */
  -
  -    /** */
  -    Mean mean = new Mean();
  +    /* UnvariateStatistic Approach  */
   
       /**
        * Returns the kurtosis for this collection of values.  
  @@ -129,60 +128,52 @@
        * @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 kurtosis of the values or Double.NaN if the array is empty
  +     * @return the kurtosis of the values or Double.NaN if the array is empty or has fewer
than
  +     *  four elements
        */
  -    public double evaluate(
  -        final double[] values,
  -        final int begin,
  -        final int length) {
  -
  -        // Initialize the kurtosis
  -        double kurt = Double.NaN;
  -
  -        if (test(values, begin, length)) {
  -            if (length <= 3) {
  -                kurt = 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(
  +    public double evaluate(final double[] values,final int begin, final int length) {
  +        // Initialize the kurtosis  
  +        double kurt = Double.NaN;   
  +        
  +        if (test(values, begin, length) && length > 3) {
  +            Mean mean = new Mean();          
  +            // 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), 4.0);
  -                }
  -                accum3 /= Math.pow(stdDev, 4.0d);
  -
  -                // Get N
  -                double n0 = length;
  -
  -                double coefficientOne =
  -                    (n0 * (n0 + 1)) / ((n0 - 1) * (n0 - 2) * (n0 - 3));
  -                double termTwo =
  -                    ((3 * Math.pow(n0 - 1, 2.0)) / ((n0 - 2) * (n0 - 3)));
  -
  -                // Calculate kurtosis
  -                kurt = (coefficientOne * accum3) - termTwo;
  +            
  +            // 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), 4.0);
               }
  +            accum3 /= Math.pow(stdDev, 4.0d);
  +            
  +            // Get N
  +            double n0 = length;
  +            
  +            double coefficientOne =
  +                (n0 * (n0 + 1)) / ((n0 - 1) * (n0 - 2) * (n0 - 3));
  +            double termTwo =
  +                ((3 * Math.pow(n0 - 1, 2.0)) / ((n0 - 2) * (n0 - 3)));
  +            
  +            // Calculate kurtosis
  +            kurt = (coefficientOne * accum3) - termTwo;
           }
  -
  +        
           return kurt;
       }
   
  
  
  

---------------------------------------------------------------------
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