View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.math.stat.descriptive.moment;
18  
19  import java.io.Serializable;
20  
21  import org.apache.commons.math.stat.descriptive.AbstractStorelessUnivariateStatistic;
22  
23  /**
24   * Computes the skewness of the available values.
25   * <p>
26   * We use the following (unbiased) formula to define skewness:</p>
27   * <p>
28   * skewness = [n / (n -1) (n - 2)] sum[(x_i - mean)^3] / std^3 </p>
29   * <p>
30   * where n is the number of values, mean is the {@link Mean} and std is the 
31   * {@link StandardDeviation} </p>
32   * <p>
33   * <strong>Note that this implementation is not synchronized.</strong> If 
34   * multiple threads access an instance of this class concurrently, and at least
35   * one of the threads invokes the <code>increment()</code> or 
36   * <code>clear()</code> method, it must be synchronized externally. </p>
37   * 
38   * @version $Revision: 617953 $ $Date: 2008-02-02 22:54:00 -0700 (Sat, 02 Feb 2008) $
39   */
40  public class Skewness extends AbstractStorelessUnivariateStatistic implements Serializable {
41  
42      /** Serializable version identifier */
43      private static final long serialVersionUID = 7101857578996691352L;    
44      
45      /** Third moment on which this statistic is based */
46      protected ThirdMoment moment = null;
47  
48       /** 
49       * Determines whether or not this statistic can be incremented or cleared.
50       * <p>
51       * Statistics based on (constructed from) external moments cannot
52       * be incremented or cleared.</p>
53      */
54      protected boolean incMoment;
55  
56      /**
57       * Constructs a Skewness
58       */
59      public Skewness() {
60          incMoment = true;
61          moment = new ThirdMoment();
62      }
63  
64      /**
65       * Constructs a Skewness with an external moment
66       * @param m3 external moment
67       */
68      public Skewness(final ThirdMoment m3) {
69          incMoment = false;
70          this.moment = m3;
71      }
72  
73      /**
74       * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#increment(double)
75       */
76      public void increment(final double d) {
77          if (incMoment) {
78              moment.increment(d);
79          }
80      }
81  
82      /**
83       * Returns the value of the statistic based on the values that have been added.
84       * <p>
85       * See {@link Skewness} for the definition used in the computation.</p>
86       * 
87       * @return the skewness of the available values.
88       */
89      public double getResult() {
90          
91          if (moment.n < 3) {
92              return Double.NaN;
93          }
94          double variance = moment.m2 / (double) (moment.n - 1);
95          if (variance < 10E-20) {
96              return 0.0d;
97          } else {
98              double n0 = (double) moment.getN();
99              return  (n0 * moment.m3) /
100             ((n0 - 1) * (n0 -2) * Math.sqrt(variance) * variance);
101         }
102     }
103 
104     /**
105      * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#getN()
106      */
107     public long getN() {
108         return moment.getN();
109     }
110     
111     /**
112      * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#clear()
113      */
114     public void clear() {
115         if (incMoment) {
116             moment.clear();
117         }
118     }
119 
120     /**
121      * Returns the Skewness of the entries in the specifed portion of the
122      * input array.
123      * <p>
124      * See {@link Skewness} for the definition used in the computation.</p>
125      * <p>
126      * Throws <code>IllegalArgumentException</code> if the array is null.</p>
127      * 
128      * @param values the input array
129      * @param begin the index of the first array element to include
130      * @param length the number of elements to include
131      * @return the skewness of the values or Double.NaN if length is less than
132      * 3
133      * @throws IllegalArgumentException if the array is null or the array index
134      *  parameters are not valid
135      */
136     public double evaluate(final double[] values,final int begin, 
137             final int length) {
138 
139         // Initialize the skewness
140         double skew = Double.NaN;
141 
142         if (test(values, begin, length) && length > 2 ){
143             Mean mean = new Mean();
144             // Get the mean and the standard deviation
145             double m = mean.evaluate(values, begin, length);
146             
147             // Calc the std, this is implemented here instead
148             // of using the standardDeviation method eliminate
149             // a duplicate pass to get the mean
150             double accum = 0.0;
151             double accum2 = 0.0;
152             for (int i = begin; i < begin + length; i++) {
153                 accum += Math.pow((values[i] - m), 2.0);
154                 accum2 += (values[i] - m);
155             }
156             double stdDev = Math.sqrt((accum - (Math.pow(accum2, 2) / ((double) length))) /
157                     (double) (length - 1));
158             
159             double accum3 = 0.0;
160             for (int i = begin; i < begin + length; i++) {
161                 accum3 += Math.pow(values[i] - m, 3.0d);
162             }
163             accum3 /= Math.pow(stdDev, 3.0d);
164             
165             // Get N
166             double n0 = length;
167             
168             // Calculate skewness
169             skew = (n0 / ((n0 - 1) * (n0 - 2))) * accum3;
170         }
171         return skew;
172     }
173 }