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;
18dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
19dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.MathRuntimeException;
20dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.exception.util.LocalizedFormats;
21dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.stat.descriptive.DescriptiveStatistics;
22dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.stat.descriptive.UnivariateStatistic;
23dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.stat.descriptive.moment.GeometricMean;
24dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.stat.descriptive.moment.Mean;
25dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.stat.descriptive.moment.Variance;
26dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.stat.descriptive.rank.Max;
27dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.stat.descriptive.rank.Min;
28dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.stat.descriptive.rank.Percentile;
29dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.stat.descriptive.summary.Product;
30dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.stat.descriptive.summary.Sum;
31dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.stat.descriptive.summary.SumOfLogs;
32dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.stat.descriptive.summary.SumOfSquares;
33dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
34dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/**
35dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * StatUtils provides static methods for computing statistics based on data
36dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * stored in double[] arrays.
37dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
38dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @version $Revision: 1073276 $ $Date: 2011-02-22 10:34:52 +0100 (mar. 22 févr. 2011) $
39dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */
40dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpublic final class StatUtils {
41dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
42dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** sum */
43dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static final UnivariateStatistic SUM = new Sum();
44dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
45dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** sumSq */
46dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static final UnivariateStatistic SUM_OF_SQUARES = new SumOfSquares();
47dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
48dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** prod */
49dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static final UnivariateStatistic PRODUCT = new Product();
50dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
51dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** sumLog */
52dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static final UnivariateStatistic SUM_OF_LOGS = new SumOfLogs();
53dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
54dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** min */
55dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static final UnivariateStatistic MIN = new Min();
56dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
57dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** max */
58dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static final UnivariateStatistic MAX = new Max();
59dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
60dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** mean */
61dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static final UnivariateStatistic MEAN = new Mean();
62dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
63dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** variance */
64dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static final Variance VARIANCE = new Variance();
65dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
66dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** percentile */
67dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static final Percentile PERCENTILE = new Percentile();
68dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
69dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** geometric mean */
70dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static final GeometricMean GEOMETRIC_MEAN = new GeometricMean();
71dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
72dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
73dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Private Constructor
74dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
75dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private StatUtils() {
76dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
77dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
78dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
79dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the sum of the values in the input array, or
80dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>Double.NaN</code> if the array is empty.
81dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
82dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Throws <code>IllegalArgumentException</code> if the input array
83dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * is null.</p>
84dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
85dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param values  array of values to sum
86dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the sum of the values or <code>Double.NaN</code> if the array
87dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * is empty
88dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws IllegalArgumentException if the array is null
89dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
90dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double sum(final double[] values) {
91dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return SUM.evaluate(values);
92dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
93dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
94dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
95dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the sum of the entries in the specified portion of
96dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * the input array, or <code>Double.NaN</code> if the designated subarray
97dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * is empty.
98dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
99dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Throws <code>IllegalArgumentException</code> if the array is null.</p>
100dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
101dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param values the input array
102dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param begin index of the first array element to include
103dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param length the number of elements to include
104dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the sum of the values or Double.NaN if length = 0
105dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws IllegalArgumentException if the array is null or the array index
106dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *  parameters are not valid
107dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
108dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double sum(final double[] values, final int begin,
109dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            final int length) {
110dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return SUM.evaluate(values, begin, length);
111dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
112dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
113dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
114dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the sum of the squares of the entries in the input array, or
115dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>Double.NaN</code> if the array is empty.
116dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
117dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Throws <code>IllegalArgumentException</code> if the array is null.</p>
118dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
119dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param values  input array
120dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the sum of the squared values or <code>Double.NaN</code> if the
121dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * array is empty
122dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws IllegalArgumentException if the array is null
123dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
124dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double sumSq(final double[] values) {
125dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return SUM_OF_SQUARES.evaluate(values);
126dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
127dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
128dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
129dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the sum of the squares of the entries in the specified portion of
130dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * the input array, or <code>Double.NaN</code> if the designated subarray
131dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * is empty.
132dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
133dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Throws <code>IllegalArgumentException</code> if the array is null.</p>
134dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
135dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param values the input array
136dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param begin index of the first array element to include
137dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param length the number of elements to include
138dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the sum of the squares of the values or Double.NaN if length = 0
139dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws IllegalArgumentException if the array is null or the array index
140dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * parameters are not valid
141dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
142dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double sumSq(final double[] values, final int begin,
143dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            final int length) {
144dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return SUM_OF_SQUARES.evaluate(values, begin, length);
145dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
146dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
147dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
148dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the product of the entries in the input array, or
149dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>Double.NaN</code> if the array is empty.
150dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
151dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Throws <code>IllegalArgumentException</code> if the array is null.</p>
152dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
153dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param values the input array
154dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the product of the values or Double.NaN if the array is empty
155dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws IllegalArgumentException if the array is null
156dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
157dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double product(final double[] values) {
158dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return PRODUCT.evaluate(values);
159dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
160dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
161dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
162dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the product of the entries in the specified portion of
163dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * the input array, or <code>Double.NaN</code> if the designated subarray
164dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * is empty.
165dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
166dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Throws <code>IllegalArgumentException</code> if the array is null.</p>
167dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
168dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param values the input array
169dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param begin index of the first array element to include
170dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param length the number of elements to include
171dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the product of the values or Double.NaN if length = 0
172dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws IllegalArgumentException if the array is null or the array index
173dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * parameters are not valid
174dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
175dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double product(final double[] values, final int begin,
176dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            final int length) {
177dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return PRODUCT.evaluate(values, begin, length);
178dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
179dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
180dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
181dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the sum of the natural logs of the entries in the input array, or
182dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>Double.NaN</code> if the array is empty.
183dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
184dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Throws <code>IllegalArgumentException</code> if the array is null.</p>
185dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
186dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * See {@link org.apache.commons.math.stat.descriptive.summary.SumOfLogs}.
187dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * </p>
188dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
189dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param values the input array
190dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the sum of the natural logs of the values or Double.NaN if
191dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * the array is empty
192dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws IllegalArgumentException if the array is null
193dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
194dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double sumLog(final double[] values) {
195dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return SUM_OF_LOGS.evaluate(values);
196dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
197dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
198dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
199dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the sum of the natural logs of the entries in the specified portion of
200dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * the input array, or <code>Double.NaN</code> if the designated subarray
201dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * is empty.
202dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
203dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Throws <code>IllegalArgumentException</code> if the array is null.</p>
204dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
205dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * See {@link org.apache.commons.math.stat.descriptive.summary.SumOfLogs}.
206dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * </p>
207dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
208dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param values the input array
209dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param begin index of the first array element to include
210dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param length the number of elements to include
211dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the sum of the natural logs of the values or Double.NaN if
212dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * length = 0
213dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws IllegalArgumentException if the array is null or the array index
214dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * parameters are not valid
215dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
216dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double sumLog(final double[] values, final int begin,
217dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            final int length) {
218dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return SUM_OF_LOGS.evaluate(values, begin, length);
219dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
220dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
221dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
222dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the arithmetic mean of the entries in the input array, or
223dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>Double.NaN</code> if the array is empty.
224dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
225dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Throws <code>IllegalArgumentException</code> if the array is null.</p>
226dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
227dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * See {@link org.apache.commons.math.stat.descriptive.moment.Mean} for
228dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * details on the computing algorithm.</p>
229dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
230dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param values the input array
231dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the mean of the values or Double.NaN if the array is empty
232dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws IllegalArgumentException if the array is null
233dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
234dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double mean(final double[] values) {
235dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return MEAN.evaluate(values);
236dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
237dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
238dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
239dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the arithmetic mean of the entries in the specified portion of
240dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * the input array, or <code>Double.NaN</code> if the designated subarray
241dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * is empty.
242dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
243dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Throws <code>IllegalArgumentException</code> if the array is null.</p>
244dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
245dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * See {@link org.apache.commons.math.stat.descriptive.moment.Mean} for
246dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * details on the computing algorithm.</p>
247dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
248dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param values the input array
249dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param begin index of the first array element to include
250dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param length the number of elements to include
251dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the mean of the values or Double.NaN if length = 0
252dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws IllegalArgumentException if the array is null or the array index
253dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * parameters are not valid
254dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
255dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double mean(final double[] values, final int begin,
256dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            final int length) {
257dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return MEAN.evaluate(values, begin, length);
258dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
259dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
260dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
261dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the geometric mean of the entries in the input array, or
262dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>Double.NaN</code> if the array is empty.
263dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
264dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Throws <code>IllegalArgumentException</code> if the array is null.</p>
265dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
266dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * See {@link org.apache.commons.math.stat.descriptive.moment.GeometricMean}
267dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * for details on the computing algorithm.</p>
268dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
269dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param values the input array
270dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the geometric mean of the values or Double.NaN if the array is empty
271dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws IllegalArgumentException if the array is null
272dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
273dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double geometricMean(final double[] values) {
274dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return GEOMETRIC_MEAN.evaluate(values);
275dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
276dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
277dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
278dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the geometric mean of the entries in the specified portion of
279dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * the input array, or <code>Double.NaN</code> if the designated subarray
280dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * is empty.
281dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
282dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Throws <code>IllegalArgumentException</code> if the array is null.</p>
283dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
284dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * See {@link org.apache.commons.math.stat.descriptive.moment.GeometricMean}
285dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * for details on the computing algorithm.</p>
286dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
287dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param values the input array
288dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param begin index of the first array element to include
289dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param length the number of elements to include
290dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the geometric mean of the values or Double.NaN if length = 0
291dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws IllegalArgumentException if the array is null or the array index
292dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * parameters are not valid
293dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
294dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double geometricMean(final double[] values, final int begin,
295dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            final int length) {
296dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return GEOMETRIC_MEAN.evaluate(values, begin, length);
297dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
298dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
299dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
300dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
301dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the variance of the entries in the input array, or
302dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>Double.NaN</code> if the array is empty.
303dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
304dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * See {@link org.apache.commons.math.stat.descriptive.moment.Variance} for
305dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * details on the computing algorithm.</p>
306dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
307dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns 0 for a single-value (i.e. length = 1) sample.</p>
308dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
309dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Throws <code>IllegalArgumentException</code> if the array is null.</p>
310dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
311dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param values the input array
312dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the variance of the values or Double.NaN if the array is empty
313dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws IllegalArgumentException if the array is null
314dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
315dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double variance(final double[] values) {
316dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return VARIANCE.evaluate(values);
317dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
318dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
319dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
320dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the variance of the entries in the specified portion of
321dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * the input array, or <code>Double.NaN</code> if the designated subarray
322dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * is empty.
323dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
324dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * See {@link org.apache.commons.math.stat.descriptive.moment.Variance} for
325dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * details on the computing algorithm.</p>
326dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
327dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns 0 for a single-value (i.e. length = 1) sample.</p>
328dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
329dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Throws <code>IllegalArgumentException</code> if the array is null or the
330dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * array index parameters are not valid.</p>
331dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
332dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param values the input array
333dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param begin index of the first array element to include
334dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param length the number of elements to include
335dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the variance of the values or Double.NaN if length = 0
336dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws IllegalArgumentException if the array is null or the array index
337dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *  parameters are not valid
338dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
339dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double variance(final double[] values, final int begin,
340dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            final int length) {
341dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return VARIANCE.evaluate(values, begin, length);
342dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
343dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
344dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
345dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the variance of the entries in the specified portion of
346dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * the input array, using the precomputed mean value.  Returns
347dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>Double.NaN</code> if the designated subarray is empty.
348dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
349dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * See {@link org.apache.commons.math.stat.descriptive.moment.Variance} for
350dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * details on the computing algorithm.</p>
351dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
352dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * The formula used assumes that the supplied mean value is the arithmetic
353dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * mean of the sample data, not a known population parameter.  This method
354dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * is supplied only to save computation when the mean has already been
355dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * computed.</p>
356dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
357dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns 0 for a single-value (i.e. length = 1) sample.</p>
358dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
359dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Throws <code>IllegalArgumentException</code> if the array is null or the
360dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * array index parameters are not valid.</p>
361dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
362dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param values the input array
363dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param mean the precomputed mean value
364dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param begin index of the first array element to include
365dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param length the number of elements to include
366dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the variance of the values or Double.NaN if length = 0
367dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws IllegalArgumentException if the array is null or the array index
368dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *  parameters are not valid
369dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
370dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double variance(final double[] values, final double mean,
371dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            final int begin, final int length) {
372dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return VARIANCE.evaluate(values, mean, begin, length);
373dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
374dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
375dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
376dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the variance of the entries in the input array, using the
377dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * precomputed mean value.  Returns <code>Double.NaN</code> if the array
378dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * is empty.
379dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
380dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * See {@link org.apache.commons.math.stat.descriptive.moment.Variance} for
381dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * details on the computing algorithm.</p>
382dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
383dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * The formula used assumes that the supplied mean value is the arithmetic
384dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * mean of the sample data, not a known population parameter.  This method
385dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * is supplied only to save computation when the mean has already been
386dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * computed.</p>
387dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
388dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns 0 for a single-value (i.e. length = 1) sample.</p>
389dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
390dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Throws <code>IllegalArgumentException</code> if the array is null.</p>
391dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
392dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param values the input array
393dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param mean the precomputed mean value
394dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the variance of the values or Double.NaN if the array is empty
395dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws IllegalArgumentException if the array is null
396dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
397dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double variance(final double[] values, final double mean) {
398dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return VARIANCE.evaluate(values, mean);
399dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
400dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
401dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
402dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the maximum of the entries in the input array, or
403dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>Double.NaN</code> if the array is empty.
404dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
405dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Throws <code>IllegalArgumentException</code> if the array is null.</p>
406dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
407dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <ul>
408dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <li>The result is <code>NaN</code> iff all values are <code>NaN</code>
409dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * (i.e. <code>NaN</code> values have no impact on the value of the statistic).</li>
410dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <li>If any of the values equals <code>Double.POSITIVE_INFINITY</code>,
411dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * the result is <code>Double.POSITIVE_INFINITY.</code></li>
412dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * </ul></p>
413dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
414dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param values the input array
415dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the maximum of the values or Double.NaN if the array is empty
416dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws IllegalArgumentException if the array is null
417dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
418dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double max(final double[] values) {
419dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return MAX.evaluate(values);
420dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
421dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
422dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
423dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the maximum of the entries in the specified portion of
424dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * the input array, or <code>Double.NaN</code> if the designated subarray
425dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * is empty.
426dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
427dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Throws <code>IllegalArgumentException</code> if the array is null or
428dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * the array index parameters are not valid.</p>
429dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
430dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <ul>
431dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <li>The result is <code>NaN</code> iff all values are <code>NaN</code>
432dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * (i.e. <code>NaN</code> values have no impact on the value of the statistic).</li>
433dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <li>If any of the values equals <code>Double.POSITIVE_INFINITY</code>,
434dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * the result is <code>Double.POSITIVE_INFINITY.</code></li>
435dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * </ul></p>
436dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
437dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param values the input array
438dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param begin index of the first array element to include
439dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param length the number of elements to include
440dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the maximum of the values or Double.NaN if length = 0
441dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws IllegalArgumentException if the array is null or the array index
442dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * parameters are not valid
443dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
444dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double max(final double[] values, final int begin,
445dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            final int length) {
446dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return MAX.evaluate(values, begin, length);
447dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
448dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
449dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     /**
450dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the minimum of the entries in the input array, or
451dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>Double.NaN</code> if the array is empty.
452dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
453dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Throws <code>IllegalArgumentException</code> if the array is null.</p>
454dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
455dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <ul>
456dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <li>The result is <code>NaN</code> iff all values are <code>NaN</code>
457dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * (i.e. <code>NaN</code> values have no impact on the value of the statistic).</li>
458dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <li>If any of the values equals <code>Double.NEGATIVE_INFINITY</code>,
459dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * the result is <code>Double.NEGATIVE_INFINITY.</code></li>
460dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * </ul> </p>
461dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
462dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param values the input array
463dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the minimum of the values or Double.NaN if the array is empty
464dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws IllegalArgumentException if the array is null
465dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
466dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double min(final double[] values) {
467dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return MIN.evaluate(values);
468dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
469dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
470dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     /**
471dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the minimum of the entries in the specified portion of
472dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * the input array, or <code>Double.NaN</code> if the designated subarray
473dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * is empty.
474dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
475dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Throws <code>IllegalArgumentException</code> if the array is null or
476dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * the array index parameters are not valid.</p>
477dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
478dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <ul>
479dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <li>The result is <code>NaN</code> iff all values are <code>NaN</code>
480dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * (i.e. <code>NaN</code> values have no impact on the value of the statistic).</li>
481dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <li>If any of the values equals <code>Double.NEGATIVE_INFINITY</code>,
482dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * the result is <code>Double.NEGATIVE_INFINITY.</code></li>
483dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * </ul></p>
484dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
485dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param values the input array
486dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param begin index of the first array element to include
487dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param length the number of elements to include
488dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the minimum of the values or Double.NaN if length = 0
489dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws IllegalArgumentException if the array is null or the array index
490dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * parameters are not valid
491dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
492dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double min(final double[] values, final int begin,
493dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            final int length) {
494dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return MIN.evaluate(values, begin, length);
495dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
496dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
497dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
498dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns an estimate of the <code>p</code>th percentile of the values
499dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * in the <code>values</code> array.
500dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
501dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <ul>
502dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <li>Returns <code>Double.NaN</code> if <code>values</code> has length
503dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>0</code></li></p>
504dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <li>Returns (for any value of <code>p</code>) <code>values[0]</code>
505dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *  if <code>values</code> has length <code>1</code></li>
506dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <li>Throws <code>IllegalArgumentException</code> if <code>values</code>
507dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * is null  or p is not a valid quantile value (p must be greater than 0
508dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * and less than or equal to 100)</li>
509dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * </ul></p>
510dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
511dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * See {@link org.apache.commons.math.stat.descriptive.rank.Percentile} for
512dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * a description of the percentile estimation algorithm used.</p>
513dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
514dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param values input array of values
515dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param p the percentile value to compute
516dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return the percentile value or Double.NaN if the array is empty
517dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws IllegalArgumentException if <code>values</code> is null
518dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * or p is invalid
519dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
520dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double percentile(final double[] values, final double p) {
521dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            return PERCENTILE.evaluate(values,p);
522dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
523dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
524dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     /**
525dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns an estimate of the <code>p</code>th percentile of the values
526dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * in the <code>values</code> array, starting with the element in (0-based)
527dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * position <code>begin</code> in the array and including <code>length</code>
528dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * values.
529dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
530dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <ul>
531dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <li>Returns <code>Double.NaN</code> if <code>length = 0</code></li>
532dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <li>Returns (for any value of <code>p</code>) <code>values[begin]</code>
533dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *  if <code>length = 1 </code></li>
534dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <li>Throws <code>IllegalArgumentException</code> if <code>values</code>
535dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *  is null , <code>begin</code> or <code>length</code> is invalid, or
536dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <code>p</code> is not a valid quantile value (p must be greater than 0
537dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * and less than or equal to 100)</li>
538dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * </ul></p>
539dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
540dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond      * See {@link org.apache.commons.math.stat.descriptive.rank.Percentile} for
541dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond      * a description of the percentile estimation algorithm used.</p>
542dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
543dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param values array of input values
544dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param p  the percentile to compute
545dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param begin  the first (0-based) element to include in the computation
546dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param length  the number of array elements to include
547dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return  the percentile value
548dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws IllegalArgumentException if the parameters are not valid or the
549dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * input array is null
550dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
551dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double percentile(final double[] values, final int begin,
552dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            final int length, final double p) {
553dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return PERCENTILE.evaluate(values, begin, length, p);
554dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
555dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
556dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
557dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the sum of the (signed) differences between corresponding elements of the
558dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * input arrays -- i.e., sum(sample1[i] - sample2[i]).
559dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
560dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param sample1  the first array
561dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param sample2  the second array
562dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return sum of paired differences
563dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws IllegalArgumentException if the arrays do not have the same
564dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * (positive) length
565dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
566dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double sumDifference(final double[] sample1, final double[] sample2)
567dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        throws IllegalArgumentException {
568dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        int n = sample1.length;
569dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (n  != sample2.length) {
570dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            throw MathRuntimeException.createIllegalArgumentException(
571dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                  LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE, n, sample2.length);
572dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
573dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (n < 1) {
574dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            throw MathRuntimeException.createIllegalArgumentException(
575dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                  LocalizedFormats.INSUFFICIENT_DIMENSION, sample2.length, 1);
576dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
577dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        double result = 0;
578dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        for (int i = 0; i < n; i++) {
579dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            result += sample1[i] - sample2[i];
580dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
581dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return result;
582dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
583dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
584dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
585dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the mean of the (signed) differences between corresponding elements of the
586dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * input arrays -- i.e., sum(sample1[i] - sample2[i]) / sample1.length.
587dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
588dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param sample1  the first array
589dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param sample2  the second array
590dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return mean of paired differences
591dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws IllegalArgumentException if the arrays do not have the same
592dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * (positive) length
593dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
594dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double meanDifference(final double[] sample1, final double[] sample2)
595dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    throws IllegalArgumentException {
596dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return sumDifference(sample1, sample2) / sample1.length;
597dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
598dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
599dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
600dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Returns the variance of the (signed) differences between corresponding elements of the
601dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * input arrays -- i.e., var(sample1[i] - sample2[i]).
602dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
603dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param sample1  the first array
604dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param sample2  the second array
605dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param meanDifference   the mean difference between corresponding entries
606dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @see #meanDifference(double[],double[])
607dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return variance of paired differences
608dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @throws IllegalArgumentException if the arrays do not have the same
609dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * length or their common length is less than 2.
610dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
611dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double varianceDifference(final double[] sample1, final double[] sample2,
612dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            double meanDifference)  throws IllegalArgumentException {
613dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        double sum1 = 0d;
614dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        double sum2 = 0d;
615dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        double diff = 0d;
616dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        int n = sample1.length;
617dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (n != sample2.length) {
618dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            throw MathRuntimeException.createIllegalArgumentException(
619dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                  LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE, n, sample2.length);
620dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
621dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        if (n < 2) {
622dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            throw MathRuntimeException.createIllegalArgumentException(
623dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                  LocalizedFormats.INSUFFICIENT_DIMENSION, n, 2);
624dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
625dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        for (int i = 0; i < n; i++) {
626dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            diff = sample1[i] - sample2[i];
627dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            sum1 += (diff - meanDifference) *(diff - meanDifference);
628dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            sum2 += diff - meanDifference;
629dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
630dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return (sum1 - (sum2 * sum2 / n)) / (n - 1);
631dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
632dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
633dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
634dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /**
635dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * Normalize (standardize) the series, so in the end it is having a mean of 0 and a standard deviation of 1.
636dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     *
637dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param sample sample to normalize
638dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @return normalized (standardized) sample
639dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @since 2.2
640dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
641dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public static double[] normalize(final double[] sample) {
642dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        DescriptiveStatistics stats = new DescriptiveStatistics();
643dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
644dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // Add the data from the series to stats
645dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        for (int i = 0; i < sample.length; i++) {
646dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            stats.addValue(sample[i]);
647dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
648dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
649dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // Compute mean and standard deviation
650dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        double mean = stats.getMean();
651dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        double standardDeviation = stats.getStandardDeviation();
652dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
653dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        // initialize the standardizedSample, which has the same length as the sample
654dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        double[] standardizedSample = new double[sample.length];
655dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
656dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        for (int i = 0; i < sample.length; i++) {
657dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            // z = (x- mean)/standardDeviation
658dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            standardizedSample[i] = (sample[i] - mean) / standardDeviation;
659dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
660dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return standardizedSample;
661dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
662dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
663dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond}
664