1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.math3.stat.descriptive.moment;
18
19 import java.io.Serializable;
20
21 import org.apache.commons.math3.exception.MathIllegalArgumentException;
22 import org.apache.commons.math3.exception.NullArgumentException;
23 import org.apache.commons.math3.stat.descriptive.AbstractStorelessUnivariateStatistic;
24 import org.apache.commons.math3.stat.descriptive.WeightedEvaluation;
25 import org.apache.commons.math3.stat.descriptive.summary.Sum;
26 import org.apache.commons.math3.util.MathUtils;
27
28 /**
29 * <p>Computes the arithmetic mean of a set of values. Uses the definitional
30 * formula:</p>
31 * <p>
32 * mean = sum(x_i) / n
33 * </p>
34 * <p>where <code>n</code> is the number of observations.
35 * </p>
36 * <p>When {@link #increment(double)} is used to add data incrementally from a
37 * stream of (unstored) values, the value of the statistic that
38 * {@link #getResult()} returns is computed using the following recursive
39 * updating algorithm: </p>
40 * <ol>
41 * <li>Initialize <code>m = </code> the first value</li>
42 * <li>For each additional value, update using <br>
43 * <code>m = m + (new value - m) / (number of observations)</code></li>
44 * </ol>
45 * <p> If {@link #evaluate(double[])} is used to compute the mean of an array
46 * of stored values, a two-pass, corrected algorithm is used, starting with
47 * the definitional formula computed using the array of stored values and then
48 * correcting this by adding the mean deviation of the data values from the
49 * arithmetic mean. See, e.g. "Comparison of Several Algorithms for Computing
50 * Sample Means and Variances," Robert F. Ling, Journal of the American
51 * Statistical Association, Vol. 69, No. 348 (Dec., 1974), pp. 859-866. </p>
52 * <p>
53 * Returns <code>Double.NaN</code> if the dataset is empty.
54 * </p>
55 * <strong>Note that this implementation is not synchronized.</strong> If
56 * multiple threads access an instance of this class concurrently, and at least
57 * one of the threads invokes the <code>increment()</code> or
58 * <code>clear()</code> method, it must be synchronized externally.
59 *
60 * @version $Id: Mean.java 1416643 2012-12-03 19:37:14Z tn $
61 */
62 public class Mean extends AbstractStorelessUnivariateStatistic
63 implements Serializable, WeightedEvaluation {
64
65 /** Serializable version identifier */
66 private static final long serialVersionUID = -1296043746617791564L;
67
68 /** First moment on which this statistic is based. */
69 protected FirstMoment moment;
70
71 /**
72 * Determines whether or not this statistic can be incremented or cleared.
73 * <p>
74 * Statistics based on (constructed from) external moments cannot
75 * be incremented or cleared.</p>
76 */
77 protected boolean incMoment;
78
79 /** Constructs a Mean. */
80 public Mean() {
81 incMoment = true;
82 moment = new FirstMoment();
83 }
84
85 /**
86 * Constructs a Mean with an External Moment.
87 *
88 * @param m1 the moment
89 */
90 public Mean(final FirstMoment m1) {
91 this.moment = m1;
92 incMoment = false;
93 }
94
95 /**
96 * Copy constructor, creates a new {@code Mean} identical
97 * to the {@code original}
98 *
99 * @param original the {@code Mean} instance to copy
100 * @throws NullArgumentException if original is null
101 */
102 public Mean(Mean original) throws NullArgumentException {
103 copy(original, this);
104 }
105
106 /**
107 * {@inheritDoc}
108 * <p>Note that when {@link #Mean(FirstMoment)} is used to
109 * create a Mean, this method does nothing. In that case, the
110 * FirstMoment should be incremented directly.</p>
111 */
112 @Override
113 public void increment(final double d) {
114 if (incMoment) {
115 moment.increment(d);
116 }
117 }
118
119 /**
120 * {@inheritDoc}
121 */
122 @Override
123 public void clear() {
124 if (incMoment) {
125 moment.clear();
126 }
127 }
128
129 /**
130 * {@inheritDoc}
131 */
132 @Override
133 public double getResult() {
134 return moment.m1;
135 }
136
137 /**
138 * {@inheritDoc}
139 */
140 public long getN() {
141 return moment.getN();
142 }
143
144 /**
145 * Returns the arithmetic mean of the entries in the specified portion of
146 * the input array, or <code>Double.NaN</code> if the designated subarray
147 * is empty.
148 * <p>
149 * Throws <code>IllegalArgumentException</code> if the array is null.</p>
150 * <p>
151 * See {@link Mean} for details on the computing algorithm.</p>
152 *
153 * @param values the input array
154 * @param begin index of the first array element to include
155 * @param length the number of elements to include
156 * @return the mean of the values or Double.NaN if length = 0
157 * @throws MathIllegalArgumentException if the array is null or the array index
158 * parameters are not valid
159 */
160 @Override
161 public double evaluate(final double[] values,final int begin, final int length)
162 throws MathIllegalArgumentException {
163 if (test(values, begin, length)) {
164 Sum sum = new Sum();
165 double sampleSize = length;
166
167 // Compute initial estimate using definitional formula
168 double xbar = sum.evaluate(values, begin, length) / sampleSize;
169
170 // Compute correction factor in second pass
171 double correction = 0;
172 for (int i = begin; i < begin + length; i++) {
173 correction += values[i] - xbar;
174 }
175 return xbar + (correction/sampleSize);
176 }
177 return Double.NaN;
178 }
179
180 /**
181 * Returns the weighted arithmetic mean of the entries in the specified portion of
182 * the input array, or <code>Double.NaN</code> if the designated subarray
183 * is empty.
184 * <p>
185 * Throws <code>IllegalArgumentException</code> if either array is null.</p>
186 * <p>
187 * See {@link Mean} for details on the computing algorithm. The two-pass algorithm
188 * described above is used here, with weights applied in computing both the original
189 * estimate and the correction factor.</p>
190 * <p>
191 * Throws <code>IllegalArgumentException</code> if any of the following are true:
192 * <ul><li>the values array is null</li>
193 * <li>the weights array is null</li>
194 * <li>the weights array does not have the same length as the values array</li>
195 * <li>the weights array contains one or more infinite values</li>
196 * <li>the weights array contains one or more NaN values</li>
197 * <li>the weights array contains negative values</li>
198 * <li>the start and length arguments do not determine a valid array</li>
199 * </ul></p>
200 *
201 * @param values the input array
202 * @param weights the weights array
203 * @param begin index of the first array element to include
204 * @param length the number of elements to include
205 * @return the mean of the values or Double.NaN if length = 0
206 * @throws MathIllegalArgumentException if the parameters are not valid
207 * @since 2.1
208 */
209 public double evaluate(final double[] values, final double[] weights,
210 final int begin, final int length) throws MathIllegalArgumentException {
211 if (test(values, weights, begin, length)) {
212 Sum sum = new Sum();
213
214 // Compute initial estimate using definitional formula
215 double sumw = sum.evaluate(weights,begin,length);
216 double xbarw = sum.evaluate(values, weights, begin, length) / sumw;
217
218 // Compute correction factor in second pass
219 double correction = 0;
220 for (int i = begin; i < begin + length; i++) {
221 correction += weights[i] * (values[i] - xbarw);
222 }
223 return xbarw + (correction/sumw);
224 }
225 return Double.NaN;
226 }
227
228 /**
229 * Returns the weighted arithmetic mean of the entries in the input array.
230 * <p>
231 * Throws <code>MathIllegalArgumentException</code> if either array is null.</p>
232 * <p>
233 * See {@link Mean} for details on the computing algorithm. The two-pass algorithm
234 * described above is used here, with weights applied in computing both the original
235 * estimate and the correction factor.</p>
236 * <p>
237 * Throws <code>MathIllegalArgumentException</code> if any of the following are true:
238 * <ul><li>the values array is null</li>
239 * <li>the weights array is null</li>
240 * <li>the weights array does not have the same length as the values array</li>
241 * <li>the weights array contains one or more infinite values</li>
242 * <li>the weights array contains one or more NaN values</li>
243 * <li>the weights array contains negative values</li>
244 * </ul></p>
245 *
246 * @param values the input array
247 * @param weights the weights array
248 * @return the mean of the values or Double.NaN if length = 0
249 * @throws MathIllegalArgumentException if the parameters are not valid
250 * @since 2.1
251 */
252 public double evaluate(final double[] values, final double[] weights)
253 throws MathIllegalArgumentException {
254 return evaluate(values, weights, 0, values.length);
255 }
256
257 /**
258 * {@inheritDoc}
259 */
260 @Override
261 public Mean copy() {
262 Mean result = new Mean();
263 // No try-catch or advertised exception because args are guaranteed non-null
264 copy(this, result);
265 return result;
266 }
267
268
269 /**
270 * Copies source to dest.
271 * <p>Neither source nor dest can be null.</p>
272 *
273 * @param source Mean to copy
274 * @param dest Mean to copy to
275 * @throws NullArgumentException if either source or dest is null
276 */
277 public static void copy(Mean source, Mean dest)
278 throws NullArgumentException {
279 MathUtils.checkNotNull(source);
280 MathUtils.checkNotNull(dest);
281 dest.setData(source.getDataRef());
282 dest.incMoment = source.incMoment;
283 dest.moment = source.moment.copy();
284 }
285 }