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