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 Fourth Central Moment. Specifically,
022: * what is computed is the sum of
023: * <p>
024: * (x_i - xbar) ^ 4,
025: * <p>
026: * where the x_i are the
027: * sample observations and xbar is the sample mean.
028: * <p>
029: * The following recursive updating formula is used:
030: * <p>
031: * Let <ul>
032: * <li> dev = (current obs - previous mean) </li>
033: * <li> m2 = previous value of {@link SecondMoment} </li>
034: * <li> m2 = previous value of {@link ThirdMoment} </li>
035: * <li> n = number of observations (including current obs) </li>
036: * </ul>
037: * Then
038: * <p>
039: * new value = old value - 4 * (dev/n) * m3 + 6 * (dev/n)^2 * m2 + <br>
040: * [n^2 - 3 * (n-1)] * dev^4 * (n-1) / n^3
041: * <p>
042: * Returns <code>Double.NaN</code> if no data values have been added and
043: * returns <code>0</code> if there is just one value in the data set.
044: * <p>
045: * <strong>Note that this implementation is not synchronized.</strong> If
046: * multiple threads access an instance of this class concurrently, and at least
047: * one of the threads invokes the <code>increment()</code> or
048: * <code>clear()</code> method, it must be synchronized externally.
049: *
050: * @version $Revision: 348519 $ $Date: 2005-11-23 12:12:18 -0700 (Wed, 23 Nov 2005) $
051: */
052: public class FourthMoment extends ThirdMoment implements Serializable {
053:
054: /** Serializable version identifier */
055: private static final long serialVersionUID = 4763990447117157611L;
056:
057: /** fourth moment of values that have been added */
058: protected double m4;
059:
060: /**
061: * Create a FourthMoment instance
062: */
063: public FourthMoment() {
064: super ();
065: m4 = Double.NaN;
066: }
067:
068: /**
069: * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#increment(double)
070: */
071: public void increment(final double d) {
072: if (n < 1) {
073: m4 = 0.0;
074: m3 = 0.0;
075: m2 = 0.0;
076: m1 = 0.0;
077: }
078:
079: double prevM3 = m3;
080: double prevM2 = m2;
081:
082: super .increment(d);
083:
084: double n0 = (double) n;
085:
086: m4 = m4 - 4.0 * nDev * prevM3 + 6.0 * nDevSq * prevM2
087: + ((n0 * n0) - 3 * (n0 - 1))
088: * (nDevSq * nDevSq * (n0 - 1) * n0);
089: }
090:
091: /**
092: * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#getResult()
093: */
094: public double getResult() {
095: return m4;
096: }
097:
098: /**
099: * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#clear()
100: */
101: public void clear() {
102: super.clear();
103: m4 = Double.NaN;
104: }
105:
106: }
|