001: /*
002: * Copyright 2003-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.math.stat.descriptive.moment;
017:
018: import java.io.Serializable;
019:
020: /**
021: * Computes a statistic related to the Third Central Moment. Specifically,
022: * what is computed is the sum of cubed deviations from the sample mean.
023: * <p>
024: * The following recursive updating formula is used:
025: * <p>
026: * Let <ul>
027: * <li> dev = (current obs - previous mean) </li>
028: * <li> m2 = previous value of {@link SecondMoment} </li>
029: * <li> n = number of observations (including current obs) </li>
030: * </ul>
031: * Then
032: * <p>
033: * new value = old value - 3 * (dev/n) * m2 + (n-1) * (n -2) * (dev^3/n^2)
034: * <p>
035: * Returns <code>Double.NaN</code> if no data values have been added and
036: * returns <code>0</code> if there is just one value in the data set.
037: * <p>
038: * <strong>Note that this implementation is not synchronized.</strong> If
039: * multiple threads access an instance of this class concurrently, and at least
040: * one of the threads invokes the <code>increment()</code> or
041: * <code>clear()</code> method, it must be synchronized externally.
042: *
043: * @version $Revision: 348519 $ $Date: 2005-11-23 12:12:18 -0700 (Wed, 23 Nov 2005) $
044: */
045: public class ThirdMoment extends SecondMoment implements Serializable {
046:
047: /** Serializable version identifier */
048: private static final long serialVersionUID = -7818711964045118679L;
049:
050: /** third moment of values that have been added */
051: protected double m3;
052:
053: /**
054: * Square of deviation of most recently added value from previous first
055: * moment, normalized by previous sample size. Retained to prevent
056: * repeated computation in higher order moments. nDevSq = nDev * nDev.
057: */
058: protected double nDevSq;
059:
060: /**
061: * Create a FourthMoment instance
062: */
063: public ThirdMoment() {
064: super ();
065: m3 = Double.NaN;
066: nDevSq = Double.NaN;
067: }
068:
069: /**
070: * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#increment(double)
071: */
072: public void increment(final double d) {
073: if (n < 1) {
074: m3 = m2 = m1 = 0.0;
075: }
076:
077: double prevM2 = m2;
078: super .increment(d);
079: nDevSq = nDev * nDev;
080: double n0 = (double) n;
081: m3 = m3 - 3.0 * nDev * prevM2 + (n0 - 1) * (n0 - 2) * nDevSq
082: * dev;
083: }
084:
085: /**
086: * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#getResult()
087: */
088: public double getResult() {
089: return m3;
090: }
091:
092: /**
093: * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#clear()
094: */
095: public void clear() {
096: super.clear();
097: m3 = Double.NaN;
098: nDevSq = Double.NaN;
099: }
100:
101: }
|