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  /**
22   * Computes a statistic related to the Third Central Moment.  Specifically,
23   * what is computed is the sum of cubed deviations from the sample mean.
24   * <p>
25   * The following recursive updating formula is used:</p>
26   * <p>
27   * Let <ul>
28   * <li> dev = (current obs - previous mean) </li>
29   * <li> m2 = previous value of {@link SecondMoment} </li>
30   * <li> n = number of observations (including current obs) </li>
31   * </ul>
32   * Then</p>
33   * <p>
34   * new value = old value - 3 * (dev/n) * m2 + (n-1) * (n -2) * (dev^3/n^2)</p>
35   * <p>
36   * Returns <code>Double.NaN</code> if no data values have been added and
37   * returns <code>0</code> if there is just one value in the data set.</p>
38   * <p>
39   * <strong>Note that this implementation is not synchronized.</strong> If 
40   * multiple threads access an instance of this class concurrently, and at least
41   * one of the threads invokes the <code>increment()</code> or 
42   * <code>clear()</code> method, it must be synchronized externally.</p>
43   * 
44   * @version $Revision: 617953 $ $Date: 2008-02-02 22:54:00 -0700 (Sat, 02 Feb 2008) $
45   */
46  public class ThirdMoment extends SecondMoment implements Serializable {
47  
48      /** Serializable version identifier */
49      private static final long serialVersionUID = -7818711964045118679L;  
50        
51      /** third moment of values that have been added */
52      protected double m3;
53  
54       /**
55       * Square of deviation of most recently added value from previous first 
56       * moment, normalized by previous sample size.  Retained to prevent 
57       * repeated computation in higher order moments.  nDevSq = nDev * nDev.
58       */
59      protected double nDevSq;
60  
61      /**
62       * Create a FourthMoment instance
63       */
64      public ThirdMoment() {
65          super();
66          m3 = Double.NaN;
67          nDevSq = Double.NaN;
68      }
69  
70      /**
71       * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#increment(double)
72       */
73      public void increment(final double d) {
74          if (n < 1) {
75              m3 = m2 = m1 = 0.0;
76          }  
77         
78          double prevM2 = m2;
79          super.increment(d);
80          nDevSq = nDev * nDev;
81          double n0 = (double) n;
82          m3 = m3 - 3.0 * nDev * prevM2 + (n0 - 1) * (n0 - 2) * nDevSq * dev;
83      }
84  
85      /**
86       * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#getResult()
87       */
88      public double getResult() {
89          return m3;
90      }
91  
92      /**
93       * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#clear()
94       */
95      public void clear() {
96          super.clear();
97          m3 = Double.NaN;
98          nDevSq = Double.NaN;
99      }
100 
101 }