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: import org.apache.commons.math.stat.descriptive.summary.Sum;
022:
023: /**
024: * Returns the arithmetic mean of the available values. Uses the definitional
025: * formula:
026: * <p>
027: * mean = sum(x_i) / n
028: * <p>
029: * where <code>n</code> is the number of observations.
030: * <p>
031: * The value of the statistic is computed using the following recursive
032: * updating algorithm:
033: * <p>
034: * <ol>
035: * <li>Initialize <code>m = </code> the first value</li>
036: * <li>For each additional value, update using <br>
037: * <code>m = m + (new value - m) / (number of observations)</code></li>
038: * </ol>
039: * <p>
040: * Returns <code>Double.NaN</code> if the dataset is empty.
041: * <p>
042: * <strong>Note that this implementation is not synchronized.</strong> If
043: * multiple threads access an instance of this class concurrently, and at least
044: * one of the threads invokes the <code>increment()</code> or
045: * <code>clear()</code> method, it must be synchronized externally.
046: *
047: * @version $Revision: 348519 $ $Date: 2005-11-23 12:12:18 -0700 (Wed, 23 Nov 2005) $
048: */
049: public class Mean extends AbstractStorelessUnivariateStatistic
050: implements Serializable {
051:
052: /** Serializable version identifier */
053: private static final long serialVersionUID = -1296043746617791564L;
054:
055: /** First moment on which this statistic is based. */
056: protected FirstMoment moment;
057:
058: /**
059: * Determines whether or not this statistic can be incremented or cleared.
060: * <p>
061: * Statistics based on (constructed from) external moments cannot
062: * be incremented or cleared.
063: */
064: protected boolean incMoment;
065:
066: /** Constructs a Mean. */
067: public Mean() {
068: incMoment = true;
069: moment = new FirstMoment();
070: }
071:
072: /**
073: * Constructs a Mean with an External Moment.
074: *
075: * @param m1 the moment
076: */
077: public Mean(final FirstMoment m1) {
078: this .moment = m1;
079: incMoment = false;
080: }
081:
082: /**
083: * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#increment(double)
084: */
085: public void increment(final double d) {
086: if (incMoment) {
087: moment.increment(d);
088: }
089: }
090:
091: /**
092: * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#clear()
093: */
094: public void clear() {
095: if (incMoment) {
096: moment.clear();
097: }
098: }
099:
100: /**
101: * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#getResult()
102: */
103: public double getResult() {
104: return moment.m1;
105: }
106:
107: /**
108: * @see org.apache.commons.math.stat.descriptive.StorelessUnivariateStatistic#getN()
109: */
110: public long getN() {
111: return moment.getN();
112: }
113:
114: /**
115: * Returns the arithmetic mean of the entries in the specified portion of
116: * the input array, or <code>Double.NaN</code> if the designated subarray
117: * is empty.
118: * <p>
119: * Throws <code>IllegalArgumentException</code> if the array is null.
120: * <p>
121: * See {@link Mean} for details on the computing algorithm.
122: *
123: * @param values the input array
124: * @param begin index of the first array element to include
125: * @param length the number of elements to include
126: * @return the mean of the values or Double.NaN if length = 0
127: * @throws IllegalArgumentException if the array is null or the array index
128: * parameters are not valid
129: */
130: public double evaluate(final double[] values, final int begin,
131: final int length) {
132: if (test(values, begin, length)) {
133: Sum sum = new Sum();
134: return sum.evaluate(values, begin, length)
135: / ((double) length);
136: }
137: return Double.NaN;
138: }
139: }
|