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: import org.apache.commons.math.stat.descriptive.AbstractStorelessUnivariateStatistic;
021:
022: /**
023: * Computes the first moment (arithmetic mean). Uses the definitional formula:
024: * <p>
025: * mean = sum(x_i) / n
026: * <p>
027: * where <code>n</code> is the number of observations.
028: * <p>
029: * To limit numeric errors, the value of the statistic is computed using the
030: * following recursive updating algorithm:
031: * <p>
032: * <ol>
033: * <li>Initialize <code>m = </code> the first value</li>
034: * <li>For each additional value, update using <br>
035: * <code>m = m + (new value - m) / (number of observations)</code></li>
036: * </ol>
037: * <p>
038: * Returns <code>Double.NaN</code> if the dataset is empty.
039: * <p>
040: * <strong>Note that this implementation is not synchronized.</strong> If
041: * multiple threads access an instance of this class concurrently, and at least
042: * one of the threads invokes the <code>increment()</code> or
043: * <code>clear()</code> method, it must be synchronized externally.
044: *
045: * @version $Revision: 348519 $ $Date: 2005-11-23 12:12:18 -0700 (Wed, 23 Nov 2005) $
046: */
047: public class FirstMoment extends AbstractStorelessUnivariateStatistic
048: implements Serializable {
049:
050: /** Serializable version identifier */
051: private static final long serialVersionUID = -803343206421984070L;
052:
053: /** Count of values that have been added */
054: protected long n;
055:
056: /** First moment of values that have been added */
057: protected double m1;
058:
059: /**
060: * Deviation of most recently added value from previous first moment.
061: * Retained to prevent repeated computation in higher order moments.
062: */
063: protected double dev;
064:
065: /**
066: * Deviation of most recently added value from previous first moment,
067: * normalized by previous sample size. Retained to prevent repeated
068: * computation in higher order moments
069: */
070: protected double nDev;
071:
072: /**
073: * Create a FirstMoment instance
074: */
075: public FirstMoment() {
076: n = 0;
077: m1 = Double.NaN;
078: dev = Double.NaN;
079: nDev = Double.NaN;
080: }
081:
082: /**
083: * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#increment(double)
084: */
085: public void increment(final double d) {
086: if (n == 0) {
087: m1 = 0.0;
088: }
089: n++;
090: double n0 = (double) n;
091: dev = d - m1;
092: nDev = dev / n0;
093: m1 += nDev;
094: }
095:
096: /**
097: * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#clear()
098: */
099: public void clear() {
100: m1 = Double.NaN;
101: n = 0;
102: dev = Double.NaN;
103: nDev = Double.NaN;
104: }
105:
106: /**
107: * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#getResult()
108: */
109: public double getResult() {
110: return m1;
111: }
112:
113: /**
114: * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#getN()
115: */
116: public long getN() {
117: return n;
118: }
119: }
|