View Javadoc

    1   /*
    2    * Copyright 2003-2004 The Apache Software Foundation.
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *      http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  package org.apache.commons.math.stat.descriptive.moment;
    17
    18  import java.io.Serializable;
    19
    20  /***
    21   * Computes a statistic related to the Fourth Central Moment.  Specifically,
    22   * what is computed is the sum of
    23   * <p>
    24   * (x_i - xbar) ^ 4,
    25   * <p>
    26   * where the x_i are the
    27   * sample observations and xbar is the sample mean.
    28   * <p>
    29   * The following recursive updating formula is used:
    30   * <p>
    31   * Let <ul>
    32   * <li> dev = (current obs - previous mean) </li>
    33   * <li> m2 = previous value of {@link SecondMoment} </li>
    34   * <li> m2 = previous value of {@link ThirdMoment} </li>
    35   * <li> n = number of observations (including current obs) </li>
    36   * </ul>
    37   * Then
    38   * <p>
    39   * new value = old value - 4 * (dev/n) * m3 + 6 * (dev/n)^2 * m2 + <br>
    40   * [n^2 - 3 * (n-1)] * dev^4 * (n-1) / n^3
    41   * <p>
    42   * Returns <code>Double.NaN</code> if no data values have been added and
    43   * returns <code>0</code> if there is just one value in the data set.
    44   * <p>
    45   * <strong>Note that this implementation is not synchronized.</strong> If
    46   * multiple threads access an instance of this class concurrently, and at least
    47   * one of the threads invokes the <code>increment()</code> or
    48   * <code>clear()</code> method, it must be synchronized externally.
    49   *
    50   * @version $Revision: 348519 $ $Date: 2005-11-23 12:12:18 -0700 (Wed, 23 Nov 2005) $
    51   */
    52  public class FourthMoment extends ThirdMoment implements Serializable{
    53
    54      /*** Serializable version identifier */
    55      private static final long serialVersionUID = 4763990447117157611L;
    56
    57      /*** fourth moment of values that have been added */
    58      protected double m4;
    59
    60      /***
    61       * Create a FourthMoment instance
    62       */
    63      public FourthMoment() {
    64          super();
    65          m4 = Double.NaN;
    66      }
    67
    68      /***
    69       * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#increment(double)
    70       */
    71      public void increment(final double d) {
    72          if (n < 1) {
    73              m4 = 0.0;
    74              m3 = 0.0;
    75              m2 = 0.0;
    76              m1 = 0.0;
    77          }
    78
    79          double prevM3 = m3;
    80          double prevM2 = m2;
    81
    82          super.increment(d);
    83
    84          double n0 = (double) n;
    85
    86          m4 = m4 - 4.0 * nDev * prevM3 + 6.0 * nDevSq * prevM2 +
    87              ((n0 * n0) - 3 * (n0 -1)) * (nDevSq * nDevSq * (n0 - 1) * n0);
    88      }
    89
    90      /***
    91       * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#getResult()
    92       */
    93      public double getResult() {
    94          return m4;
    95      }
    96
    97      /***
    98       * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#clear()
    99       */
    100     public void clear() {
    101         super.clear();
    102         m4 = Double.NaN;
    103     }
    104
    105 }