001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.math4.stat.descriptive.moment;
018
019import java.io.Serializable;
020
021import org.apache.commons.math4.exception.MathIllegalArgumentException;
022import org.apache.commons.math4.exception.NullArgumentException;
023import org.apache.commons.math4.stat.descriptive.AbstractStorelessUnivariateStatistic;
024import org.apache.commons.math4.stat.descriptive.WeightedEvaluation;
025import org.apache.commons.math4.stat.descriptive.summary.Sum;
026import org.apache.commons.math4.util.MathArrays;
027import org.apache.commons.math4.util.MathUtils;
028
029/**
030 * Computes the arithmetic mean of a set of values. Uses the definitional
031 * formula:
032 * <p>
033 * mean = sum(x_i) / n
034 * </p>
035 * <p>where <code>n</code> is the number of observations.
036 * </p>
037 * <p>When {@link #increment(double)} is used to add data incrementally from a
038 * stream of (unstored) values, the value of the statistic that
039 * {@link #getResult()} returns is computed using the following recursive
040 * updating algorithm: </p>
041 * <ol>
042 * <li>Initialize <code>m = </code> the first value</li>
043 * <li>For each additional value, update using <br>
044 *   <code>m = m + (new value - m) / (number of observations)</code></li>
045 * </ol>
046 * <p> If {@link #evaluate(double[])} is used to compute the mean of an array
047 * of stored values, a two-pass, corrected algorithm is used, starting with
048 * the definitional formula computed using the array of stored values and then
049 * correcting this by adding the mean deviation of the data values from the
050 * arithmetic mean. See, e.g. "Comparison of Several Algorithms for Computing
051 * Sample Means and Variances," Robert F. Ling, Journal of the American
052 * Statistical Association, Vol. 69, No. 348 (Dec., 1974), pp. 859-866. </p>
053 * <p>
054 *  Returns <code>Double.NaN</code> if the dataset is empty. Note that
055 *  Double.NaN may also be returned if the input includes NaN and / or infinite
056 *  values.
057 * </p>
058 * <strong>Note that this implementation is not synchronized.</strong> If
059 * multiple threads access an instance of this class concurrently, and at least
060 * one of the threads invokes the <code>increment()</code> or
061 * <code>clear()</code> method, it must be synchronized externally.
062 */
063public class Mean extends AbstractStorelessUnivariateStatistic
064    implements Serializable, WeightedEvaluation {
065
066    /** Serializable version identifier */
067    private static final long serialVersionUID = 20150412L;
068
069    /** First moment on which this statistic is based. */
070    protected FirstMoment moment;
071
072    /**
073     * Determines whether or not this statistic can be incremented or cleared.
074     * <p>
075     * Statistics based on (constructed from) external moments cannot
076     * be incremented or cleared.</p>
077     */
078    protected boolean incMoment;
079
080    /** Constructs a Mean. */
081    public Mean() {
082        incMoment = true;
083        moment = new FirstMoment();
084    }
085
086    /**
087     * Constructs a Mean with an External Moment.
088     *
089     * @param m1 the moment
090     */
091    public Mean(final FirstMoment m1) {
092        this.moment = m1;
093        incMoment = false;
094    }
095
096    /**
097     * Copy constructor, creates a new {@code Mean} identical
098     * to the {@code original}.
099     *
100     * @param original the {@code Mean} instance to copy
101     * @throws NullArgumentException if original is null
102     */
103    public Mean(Mean original) throws NullArgumentException {
104        copy(original, this);
105    }
106
107    /**
108     * {@inheritDoc}
109     * <p>Note that when {@link #Mean(FirstMoment)} is used to
110     * create a Mean, this method does nothing. In that case, the
111     * FirstMoment should be incremented directly.</p>
112     */
113    @Override
114    public void increment(final double d) {
115        if (incMoment) {
116            moment.increment(d);
117        }
118    }
119
120    /**
121     * {@inheritDoc}
122     */
123    @Override
124    public void clear() {
125        if (incMoment) {
126            moment.clear();
127        }
128    }
129
130    /**
131     * {@inheritDoc}
132     */
133    @Override
134    public double getResult() {
135        return moment.m1;
136    }
137
138    /**
139     * {@inheritDoc}
140     */
141    @Override
142    public long getN() {
143        return moment.getN();
144    }
145
146    /**
147     * Returns the arithmetic mean of the entries in the specified portion of
148     * the input array, or <code>Double.NaN</code> if the designated subarray
149     * is empty.
150     * <p>
151     * Throws <code>IllegalArgumentException</code> if the array is null.</p>
152     * <p>
153     * See {@link Mean} for details on the computing algorithm.</p>
154     *
155     * @param values the input array
156     * @param begin index of the first array element to include
157     * @param length the number of elements to include
158     * @return the mean of the values or Double.NaN if length = 0
159     * @throws MathIllegalArgumentException if the array is null or the array index
160     *  parameters are not valid
161     */
162    @Override
163    public double evaluate(final double[] values, final int begin, final int length)
164        throws MathIllegalArgumentException {
165
166        if (MathArrays.verifyValues(values, begin, length)) {
167            Sum sum = new Sum();
168            double sampleSize = length;
169
170            // Compute initial estimate using definitional formula
171            double xbar = sum.evaluate(values, begin, length) / sampleSize;
172
173            // Compute correction factor in second pass
174            double correction = 0;
175            for (int i = begin; i < begin + length; i++) {
176                correction += values[i] - xbar;
177            }
178            return xbar + (correction/sampleSize);
179        }
180        return Double.NaN;
181    }
182
183    /**
184     * Returns the weighted arithmetic mean of the entries in the specified portion of
185     * the input array, or <code>Double.NaN</code> if the designated subarray
186     * is empty.
187     * <p>
188     * Throws <code>IllegalArgumentException</code> if either array is null.</p>
189     * <p>
190     * See {@link Mean} for details on the computing algorithm. The two-pass algorithm
191     * described above is used here, with weights applied in computing both the original
192     * estimate and the correction factor.</p>
193     * <p>
194     * Throws <code>IllegalArgumentException</code> if any of the following are true:
195     * <ul><li>the values array is null</li>
196     *     <li>the weights array is null</li>
197     *     <li>the weights array does not have the same length as the values array</li>
198     *     <li>the weights array contains one or more infinite values</li>
199     *     <li>the weights array contains one or more NaN values</li>
200     *     <li>the weights array contains negative values</li>
201     *     <li>the start and length arguments do not determine a valid array</li>
202     * </ul>
203     *
204     * @param values the input array
205     * @param weights the weights array
206     * @param begin index of the first array element to include
207     * @param length the number of elements to include
208     * @return the mean of the values or Double.NaN if length = 0
209     * @throws MathIllegalArgumentException if the parameters are not valid
210     * @since 2.1
211     */
212    @Override
213    public double evaluate(final double[] values, final double[] weights,
214                           final int begin, final int length) throws MathIllegalArgumentException {
215        if (MathArrays.verifyValues(values, weights, begin, length)) {
216            Sum sum = new Sum();
217
218            // Compute initial estimate using definitional formula
219            double sumw = sum.evaluate(weights,begin,length);
220            double xbarw = sum.evaluate(values, weights, begin, length) / sumw;
221
222            // Compute correction factor in second pass
223            double correction = 0;
224            for (int i = begin; i < begin + length; i++) {
225                correction += weights[i] * (values[i] - xbarw);
226            }
227            return xbarw + (correction/sumw);
228        }
229        return Double.NaN;
230    }
231
232    /**
233     * Returns the weighted arithmetic mean of the entries in the input array.
234     * <p>
235     * Throws <code>MathIllegalArgumentException</code> if either array is null.</p>
236     * <p>
237     * See {@link Mean} for details on the computing algorithm. The two-pass algorithm
238     * described above is used here, with weights applied in computing both the original
239     * estimate and the correction factor.</p>
240     * <p>
241     * Throws <code>MathIllegalArgumentException</code> if any of the following are true:
242     * <ul><li>the values array is null</li>
243     *     <li>the weights array is null</li>
244     *     <li>the weights array does not have the same length as the values array</li>
245     *     <li>the weights array contains one or more infinite values</li>
246     *     <li>the weights array contains one or more NaN values</li>
247     *     <li>the weights array contains negative values</li>
248     * </ul>
249     *
250     * @param values the input array
251     * @param weights the weights array
252     * @return the mean of the values or Double.NaN if length = 0
253     * @throws MathIllegalArgumentException if the parameters are not valid
254     * @since 2.1
255     */
256    @Override
257    public double evaluate(final double[] values, final double[] weights)
258        throws MathIllegalArgumentException {
259        return evaluate(values, weights, 0, values.length);
260    }
261
262    /**
263     * {@inheritDoc}
264     */
265    @Override
266    public Mean copy() {
267        Mean result = new Mean();
268        // No try-catch or advertised exception because args are guaranteed non-null
269        copy(this, result);
270        return result;
271    }
272
273    /**
274     * Copies source to dest.
275     * <p>Neither source nor dest can be null.</p>
276     *
277     * @param source Mean to copy
278     * @param dest Mean to copy to
279     * @throws NullArgumentException if either source or dest is null
280     */
281    public static void copy(Mean source, Mean dest)
282        throws NullArgumentException {
283        MathUtils.checkNotNull(source);
284        MathUtils.checkNotNull(dest);
285        dest.incMoment = source.incMoment;
286        dest.moment = source.moment.copy();
287    }
288}