diff --git a/src/java/org/apache/commons/math/stat/StatUtils.java b/src/java/org/apache/commons/math/stat/StatUtils.java index a29abcf58..211e37aff 100644 --- a/src/java/org/apache/commons/math/stat/StatUtils.java +++ b/src/java/org/apache/commons/math/stat/StatUtils.java @@ -27,10 +27,10 @@ import org.apache.commons.math.stat.univariate.summary.SumOfLogs; import org.apache.commons.math.stat.univariate.summary.SumOfSquares; /** - * StatUtils provides static implementations of common double[] based - * statistical methods. These return a single result value or in some cases, as - * identified in the javadoc for each method, Double.NaN. - * @version $Revision: 1.29 $ $Date: 2004/06/23 16:26:17 $ + * StatUtils provides static methods for computing statistics based on data + * stored in double[] arrays. + * + * @version $Revision: 1.30 $ $Date: 2004/07/11 18:41:19 $ */ public final class StatUtils { @@ -56,7 +56,7 @@ public final class StatUtils { private static UnivariateStatistic mean = new Mean(); /** variance */ - private static UnivariateStatistic variance = new Variance(); + private static Variance variance = new Variance(); /** variance */ private static Percentile percentile = new Percentile(); @@ -68,251 +68,429 @@ public final class StatUtils { } /** - * The sum of the values that have been added to Univariate. - * @param values Is a double[] containing the values - * @return the sum of the values or Double.NaN if the array is empty + * Returns the sum of the values in the input array, or + * Double.NaN if the array is empty. + *

+ * Throws IllegalArgumentException if the input array + * is null. + * + * @param values array of values to sum + * @return the sum of the values or Double.NaN if the array + * is empty + * @throws IllegalArgumentException if the array is null */ public static double sum(final double[] values) { return sum.evaluate(values); } /** - * The sum of the values that have been added to Univariate. - * @param values Is a double[] containing the values - * @param begin processing at this point in the array + * Returns the sum of the entries in the specified portion of + * the input array, or Double.NaN if the designated subarray + * is empty. + *

+ * Throws IllegalArgumentException if the array is null. + * + * @param values the input array + * @param begin index of the first array element to include * @param length the number of elements to include - * @return the sum of the values or Double.NaN if the array is empty + * @return the sum of the values or Double.NaN if length = 0 + * @throws IllegalArgumentException if the array is null or the array index + * parameters are not valid */ - public static double sum( - final double[] values, - final int begin, - final int length) { + public static double sum(final double[] values, final int begin, + final int length) { return sum.evaluate(values, begin, length); } /** - * Returns the sum of the squares of the available values. - * @param values Is a double[] containing the values - * @return the sum of the squared values or Double.NaN if the array is empty + * Returns the sum of the squares of the entries in the input array, or + * Double.NaN if the array is empty. + *

+ * Throws IllegalArgumentException if the array is null. + * + * @param values input array + * @return the sum of the squared values or Double.NaN if the + * array is empty + * @throws IllegalArgumentException if the array is null */ public static double sumSq(final double[] values) { return sumSq.evaluate(values); } /** - * Returns the sum of the squares of the available values. - * @param values Is a double[] containing the values - * @param begin processing at this point in the array + * Returns the sum of the squares of the entries in the specified portion of + * the input array, or Double.NaN if the designated subarray + * is empty. + *

+ * Throws IllegalArgumentException if the array is null. + * + * @param values the input array + * @param begin index of the first array element to include * @param length the number of elements to include - * @return the sum of the squared values or Double.NaN if the array is empty + * @return the sum of the squares of the values or Double.NaN if length = 0 + * @throws IllegalArgumentException if the array is null or the array index + * parameters are not valid */ - public static double sumSq( - final double[] values, - final int begin, - final int length) { + public static double sumSq(final double[] values, final int begin, + final int length) { return sumSq.evaluate(values, begin, length); } /** - * Returns the product for this collection of values - * @param values Is a double[] containing the values - * @return the product values or Double.NaN if the array is empty + * Returns the product of the entries in the input array, or + * Double.NaN if the array is empty. + *

+ * Throws IllegalArgumentException if the array is null. + * + * @param values the input array + * @return the product of the values or Double.NaN if the array is empty + * @throws IllegalArgumentException if the array is null */ public static double product(final double[] values) { return prod.evaluate(values); } /** - * Returns the product for this collection of values - * @param values Is a double[] containing the values - * @param begin processing at this point in the array + * Returns the product of the entries in the specified portion of + * the input array, or Double.NaN if the designated subarray + * is empty. + *

+ * Throws IllegalArgumentException if the array is null. + * + * @param values the input array + * @param begin index of the first array element to include * @param length the number of elements to include - * @return the product values or Double.NaN if the array is empty + * @return the product of the values or Double.NaN if length = 0 + * @throws IllegalArgumentException if the array is null or the array index + * parameters are not valid */ - public static double product( - final double[] values, - final int begin, - final int length) { + public static double product(final double[] values, final int begin, + final int length) { return prod.evaluate(values, begin, length); } /** - * Returns the sum of the natural logs for this collection of values - * @param values Is a double[] containing the values - * @return the sumLog value or Double.NaN if the array is empty + * Returns the sum of the natural logs of the entries in the input array, or + * Double.NaN if the array is empty. + *

+ * Throws IllegalArgumentException if the array is null. + *

+ * See {@link org.apache.commons.math.stat.univariate.summary.SumOfLogs}. + * + * @param values the input array + * @return the sum of the natural logs of the values or Double.NaN if + * the array is empty + * @throws IllegalArgumentException if the array is null */ public static double sumLog(final double[] values) { return sumLog.evaluate(values); } /** - * Returns the sum of the natural logs for this collection of values - * @param values Is a double[] containing the values - * @param begin processing at this point in the array + * Returns the sum of the natural logs of the entries in the specified portion of + * the input array, or Double.NaN if the designated subarray + * is empty. + *

+ * Throws IllegalArgumentException if the array is null. + *

+ * See {@link org.apache.commons.math.stat.univariate.summary.SumOfLogs}. + * + * @param values the input array + * @param begin index of the first array element to include * @param length the number of elements to include - * @return the sumLog value or Double.NaN if the array is empty + * @return the sum of the natural logs of the values or Double.NaN if + * length = 0 + * @throws IllegalArgumentException if the array is null or the array index + * parameters are not valid */ - public static double sumLog( - final double[] values, - final int begin, - final int length) { + public static double sumLog(final double[] values, final int begin, + final int length) { return sumLog.evaluate(values, begin, length); } /** - * Returns the - * arithmetic mean of the available values - * @param values Is a double[] containing the values - * @return the mean of the values or Double.NaN if the array is empty + * Returns the arithmetic mean of the entries in the input array, or + * Double.NaN if the array is empty. + *

+ * Throws IllegalArgumentException if the array is null. + *

+ * See {@link org.apache.commons.math.stat.univariate.moment.Mean} for + * details on the computing algorithm. + * + * @param values the input array + * @return the mean of the values or Double.NaN if the array is empty + * @throws IllegalArgumentException if the array is null */ public static double mean(final double[] values) { return mean.evaluate(values); } /** - * Returns the - * arithmetic mean of the available values - * @param values Is a double[] containing the values - * @param begin processing at this point in the array + * Returns the arithmetic mean of the entries in the specified portion of + * the input array, or Double.NaN if the designated subarray + * is empty. + *

+ * Throws IllegalArgumentException if the array is null. + *

+ * See {@link org.apache.commons.math.stat.univariate.moment.Mean} for + * details on the computing algorithm. + * + * @param values the input array + * @param begin index of the first array element to include * @param length the number of elements to include - * @return the mean of the values or Double.NaN if the array is empty - */ - public static double mean( - final double[] values, - final int begin, - final int length) { + * @return the mean of the values or Double.NaN if length = 0 + * @throws IllegalArgumentException if the array is null or the array index + * parameters are not valid + */ + public static double mean(final double[] values, final int begin, + final int length) { return mean.evaluate(values, begin, length); } /** - * Returns the variance of the available values. This uses a corrected - * two pass algorithm as described in: + * Returns the variance of the entries in the input array, or + * Double.NaN if the array is empty. *

- * "Algorithms for Computing the Sample Variance: Analysis and - * Recommendations", Chan, T.F., Golub, G.H., and LeVeque, R.J. - * 1983, American Statistician, vol. 37, pp. 242-247. - * - * @param values Is a double[] containing the values - * @return the result, Double.NaN for an empty array - * or 0.0 for a single value set. + * See {@link org.apache.commons.math.stat.univariate.moment.Variance} for + * details on the computing algorithm. + *

+ * Returns 0 for a single-value (i.e. length = 1) sample. + *

+ * Throws IllegalArgumentException if the array is null. + * + * @param values the input array + * @return the variance of the values or Double.NaN if the array is empty + * @throws IllegalArgumentException if the array is null */ public static double variance(final double[] values) { return variance.evaluate(values); } /** - * Returns the variance of the available values. This uses a corrected - * two pass algorithm as described in: + * Returns the variance of the entries in the specified portion of + * the input array, or Double.NaN if the designated subarray + * is empty. *

- * "Algorithms for Computing the Sample Variance: Analysis and - * Recommendations", Chan, T.F., Golub, G.H., and LeVeque, R.J. - * 1983, American Statistician, vol. 37, pp. 242-247. - * - * @param values Is a double[] containing the values - * @param begin processing at this point in the array + * See {@link org.apache.commons.math.stat.univariate.moment.Variance} for + * details on the computing algorithm. + *

+ * Returns 0 for a single-value (i.e. length = 1) sample. + *

+ * Throws IllegalArgumentException if the array is null or the + * array index parameters are not valid. + * + * @param values the input array + * @param begin index of the first array element to include * @param length the number of elements to include - * @return the result, Double.NaN for an empty array - * or 0.0 for a single value set. + * @return the variance of the values or Double.NaN if length = 0 + * @throws IllegalArgumentException if the array is null or the array index + * parameters are not valid */ - public static double variance( - final double[] values, - final int begin, - final int length) { + public static double variance(final double[] values, final int begin, + final int length) { return variance.evaluate(values, begin, length); } + + /** + * Returns the variance of the entries in the specified portion of + * the input array, using the precomputed mean value. Returns + * Double.NaN if the designated subarray is empty. + *

+ * See {@link org.apache.commons.math.stat.univariate.moment.Variance} for + * details on the computing algorithm. + *

+ * Returns 0 for a single-value (i.e. length = 1) sample. + *

+ * Throws IllegalArgumentException if the array is null or the + * array index parameters are not valid. + * + * @param values the input array + * @param mean the precomputed mean value + * @param begin index of the first array element to include + * @param length the number of elements to include + * @return the variance of the values or Double.NaN if length = 0 + * @throws IllegalArgumentException if the array is null or the array index + * parameters are not valid + */ + public static double variance(final double[] values, final double mean, + final int begin, final int length) { + return variance.evaluate(values, mean, begin, length); + } + + /** + * Returns the variance of the entries in the input array, using the + * precomputed mean value. Returns Double.NaN if the array + * is empty. + *

+ * See {@link org.apache.commons.math.stat.univariate.moment.Variance} for + * details on the computing algorithm. + *

+ * Returns 0 for a single-value (i.e. length = 1) sample. + *

+ * Throws IllegalArgumentException if the array is null. + * + * @param values the input array + * @param mean the precomputed mean value + * @return the variance of the values or Double.NaN if the array is empty + * @throws IllegalArgumentException if the array is null + */ + public static double variance(final double[] values, final double mean) { + return variance.evaluate(values, mean); + } /** - * Returns the maximum of the available values - * @param values Is a double[] containing the values - * @return the maximum of the values or Double.NaN if the array is empty + * Returns the maximum of the entries in the input array, or + * Double.NaN if the array is empty. + *

+ * Throws IllegalArgumentException if the array is null. + *

+ *

+ * + * @param values the input array + * @return the maximum of the values or Double.NaN if the array is empty + * @throws IllegalArgumentException if the array is null */ public static double max(final double[] values) { return max.evaluate(values); } /** - * Returns the maximum of the available values - * @param values Is a double[] containing the values - * @param begin processing at this point in the array + * Returns the maximum of the entries in the specified portion of + * the input array, or Double.NaN if the designated subarray + * is empty. + *

+ * Throws IllegalArgumentException if the array is null or + * the array index parameters are not valid. + *

+ *

+ * + * @param values the input array + * @param begin index of the first array element to include * @param length the number of elements to include - * @return the maximum of the values or Double.NaN if the array is empty + * @return the maximum of the values or Double.NaN if length = 0 + * @throws IllegalArgumentException if the array is null or the array index + * parameters are not valid */ - public static double max( - final double[] values, - final int begin, - final int length) { + public static double max(final double[] values, final int begin, + final int length) { return max.evaluate(values, begin, length); } - /** - * Returns the minimum of the available values - * @param values Is a double[] containing the values - * @return the minimum of the values or Double.NaN if the array is empty + /** + * Returns the minimum of the entries in the input array, or + * Double.NaN if the array is empty. + *

+ * Throws IllegalArgumentException if the array is null. + *

+ *

+ * + * @param values the input array + * @return the minimum of the values or Double.NaN if the array is empty + * @throws IllegalArgumentException if the array is null */ public static double min(final double[] values) { return min.evaluate(values); } - /** - * Returns the minimum of the available values - * @param values Is a double[] containing the values - * @param begin processing at this point in the array + /** + * Returns the minimum of the entries in the specified portion of + * the input array, or Double.NaN if the designated subarray + * is empty. + *

+ * Throws IllegalArgumentException if the array is null or + * the array index parameters are not valid. + *

+ *

+ * + * @param values the input array + * @param begin index of the first array element to include * @param length the number of elements to include - * @return the minimum of the values or Double.NaN if the array is empty + * @return the minimum of the values or Double.NaN if length = 0 + * @throws IllegalArgumentException if the array is null or the array index + * parameters are not valid */ - public static double min( - final double[] values, - final int begin, - final int length) { + public static double min(final double[] values, final int begin, + final int length) { return min.evaluate(values, begin, length); } /** - * Returns an estimate for the pth percentile of the stored values. + * Returns an estimate of the pth percentile of the values + * in the values array. *

- * The implementation provided here follows the first estimation procedure presented - * here. - *

- * Preconditions: