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.statistics.descriptive;
018
019/**
020 * Computes the arithmetic mean of the available values. Uses the following definition
021 * of the <em>sample mean</em>:
022 *
023 * <p>\[ \frac{1}{n} \sum_{i=1}^n x_i \]
024 *
025 * <p>where \( n \) is the number of samples.
026 *
027 * <ul>
028 *   <li>The result is {@code NaN} if no values are added.
029 *   <li>The result is {@code NaN} if any of the values is {@code NaN}, or the values include
030 *       infinite values of opposite sign.
031 *   <li>The result is {@code +/-infinity} if values include infinite values of same sign.
032 *   <li>The result is finite if all input values are finite.
033 * </ul>
034 *
035 * <p>The {@link #accept(double)} method uses the following recursive updating algorithm
036 * that protects the mean from overflow:
037 * <ol>
038 * <li>Initialize \( m_1 \) using the first value</li>
039 * <li>For each additional value, update using <br>
040 *     \( m_{i+1} = m_i + (x - m_i) / (i + 1) \)</li>
041 * </ol>
042 *
043 * <p>The {@link #of(double...)} method uses an extended precision sum if the sum is finite.
044 * Otherwise uses a corrected two-pass algorithm, starting with
045 * the recursive updating algorithm mentioned above, and then correcting this by adding the
046 * mean deviation of the data values from the one-pass mean (see Ling (1974)).
047 *
048 * <p>Supports up to 2<sup>63</sup> (exclusive) observations.
049 * This implementation does not check for overflow of the count.
050 *
051 * <p>This class is designed to work with (though does not require)
052 * {@linkplain java.util.stream streams}.
053 *
054 * <p><strong>Note that this implementation is not synchronized.</strong> If
055 * multiple threads access an instance of this class concurrently, and at least
056 * one of the threads invokes the {@link java.util.function.DoubleConsumer#accept(double) accept} or
057 * {@link StatisticAccumulator#combine(StatisticResult) combine} method, it must be synchronized externally.
058 *
059 * <p>However, it is safe to use {@link java.util.function.DoubleConsumer#accept(double) accept}
060 * and {@link StatisticAccumulator#combine(StatisticResult) combine}
061 * as {@code accumulator} and {@code combiner} functions of
062 * {@link java.util.stream.Collector Collector} on a parallel stream,
063 * because the parallel implementation of {@link java.util.stream.Stream#collect Stream.collect()}
064 * provides the necessary partitioning, isolation, and merging of results for
065 * safe and efficient parallel execution.
066 *
067 * <p>References:
068 * <ul>
069 *   <li>Ling, R.F. (1974)
070 *       Comparison of Several Algorithms for Computing Sample Means and Variances.
071 *       Journal of the American Statistical Association, 69, 859-866.
072 *       <a href="https://doi.org/10.2307/2286154">doi: 10.2307/2286154</a>
073 * </ul>
074 *
075 * @see <a href="https://en.wikipedia.org/wiki/Mean">Mean (Wikipedia)</a>
076 * @since 1.1
077 */
078public final class Mean implements DoubleStatistic, StatisticAccumulator<Mean> {
079
080    /**
081     * First moment used to compute the mean.
082     */
083    private final FirstMoment firstMoment;
084
085    /**
086     * Create an instance.
087     */
088    private Mean() {
089        this(new FirstMoment());
090    }
091
092    /**
093     * Creates an instance with a moment.
094     *
095     * @param m1 First moment.
096     */
097    Mean(FirstMoment m1) {
098        firstMoment = m1;
099    }
100
101    /**
102     * Creates an instance.
103     *
104     * <p>The initial result is {@code NaN}.
105     *
106     * @return {@code Mean} instance.
107     */
108    public static Mean create() {
109        return new Mean();
110    }
111
112    /**
113     * Returns an instance populated using the input {@code values}.
114     *
115     * <p>Note: {@code Mean} computed using {@link #accept(double) accept} may be
116     * different from this mean.
117     *
118     * <p>See {@link Mean} for details on the computing algorithm.
119     *
120     * @param values Values.
121     * @return {@code Mean} instance.
122     */
123    public static Mean of(double... values) {
124        return new Mean(FirstMoment.of(values));
125    }
126
127    /**
128     * Updates the state of the statistic to reflect the addition of {@code value}.
129     *
130     * @param value Value.
131     */
132    @Override
133    public void accept(double value) {
134        firstMoment.accept(value);
135    }
136
137    /**
138     * Gets the mean of all input values.
139     *
140     * <p>When no values have been added, the result is {@code NaN}.
141     *
142     * @return mean of all values.
143     */
144    @Override
145    public double getAsDouble() {
146        return firstMoment.getFirstMoment();
147    }
148
149    @Override
150    public Mean combine(Mean other) {
151        firstMoment.combine(other.firstMoment);
152        return this;
153    }
154}