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.
+ *
+ *
NaN
iff all values are NaN
+ * (i.e. NaN
values have no impact on the value of the statistic).Double.POSITIVE_INFINITY
,
+ * the result is Double.POSITIVE_INFINITY.
Double.NaN
if the designated subarray
+ * is empty.
+ *
+ * Throws IllegalArgumentException
if the array is null or
+ * the array index parameters are not valid.
+ *
+ *
NaN
iff all values are NaN
+ * (i.e. NaN
values have no impact on the value of the statistic).Double.POSITIVE_INFINITY
,
+ * the result is Double.POSITIVE_INFINITY.
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.
+ *
+ *
NaN
iff all values are NaN
+ * (i.e. NaN
values have no impact on the value of the statistic).Double.NEGATIVE_INFINITY
,
+ * the result is Double.NEGATIVE_INFINITY.
Double.NaN
if the designated subarray
+ * is empty.
+ *
+ * Throws IllegalArgumentException
if the array is null or
+ * the array index parameters are not valid.
+ *
+ *
NaN
iff all values are NaN
+ * (i.e. NaN
values have no impact on the value of the statistic).Double.NEGATIVE_INFINITY
,
+ * the result is Double.NEGATIVE_INFINITY.
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 p
th percentile of the values
+ * in the values
array.
* - * The implementation provided here follows the first estimation procedure presented - * here. - *
- * Preconditions:
0 < p < 100
(otherwise an
- * IllegalArgumentException
is thrown)Double.NaN
- *
otherwise)Double.NaN
if values
has length
+ * 0
p
) values[0]
+ * if values
has length 1
IllegalArgumentException
if values
+ * is null or p is not a valid quantile value (p must be greater than 0
+ * and less than or equal to 100)
+ * See {@link org.apache.commons.math.stat.univariate.rank.Percentile} for
+ * a description of the percentile estimation algorithm used.
*
- * @param values Is a double[] containing the values
- * @param p the requested percentile (scaled from 0 - 100)
- * @return An estimate for the pth percentile of the data values
+ * @param values input array of values
+ * @param p the percentile value to compute
+ * @return the percentile value or Double.NaN if the array is empty
+ * @throws IllegalArgumentException if values
is null
+ * or p is invalid
*/
public static double percentile(final double[] values, final double p) {
return percentile.evaluate(values,p);
}
- /**
- * Returns an estimate for the pth percentile of the stored values.
- *
- * The implementation provided here follows the first estimation procedure presented
- * here.
+ /**
+ * Returns an estimate of the p
th percentile of the values
+ * in the values
array, starting with the element in (0-based)
+ * position begin
in the array and including length
+ * values.
*
- * Preconditions:
0 < p < 100
(otherwise an
- * IllegalArgumentException
is thrown)Double.NaN
- *
otherwise)Double.NaN
if length = 0
p
) values[begin]
+ * if length = 1
IllegalArgumentException
if values
+ * is null , begin
or length
is invalid, or
+ * p
is not a valid quantile value (p must be greater than 0
+ * and less than or equal to 100)+ * See {@link org.apache.commons.math.stat.univariate.rank.Percentile} for + * a description of the percentile estimation algorithm used. * - * @param values Is a double[] containing the values - * @param begin processing at this point in the array - * @param length the number of elements to include - * @param p the requested percentile (scaled from 0 - 100) - * @return An estimate for the pth percentile of the data values + * @param values array of input values + * @param p the percentile to compute + * @param begin the first (0-based) element to include in the computation + * @param length the number of array elements to include + * @return the percentile value + * @throws IllegalArgumentException if the parameters are not valid or the + * input array is null */ - public static double percentile( - final double[] values, - final int begin, - final int length, - final double p) { - return percentile.evaluate(values, begin, length, p); + public static double percentile(final double[] values, final int begin, + final int length, final double p) { + return percentile.evaluate(values, begin, length, p); } /**