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