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