Class StatisticUtils
- java.lang.Object
-
- org.apache.commons.statistics.inference.StatisticUtils
-
final class StatisticUtils extends java.lang.ObjectUtility computation methods.- Since:
- 1.1
-
-
Constructor Summary
Constructors Modifier Constructor Description privateStatisticUtils()No instances.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description private static org.apache.commons.numbers.core.DDadd(org.apache.commons.numbers.core.DD sum, long v)Adds the value to the sum.(package private) static intcomputeDegreesOfFreedom(int n, int m)Compute the degrees of freedom asn - 1 - m.(package private) static doublecomputeRatio(double[] expected, long[] observed)Gets the ratio between the sum of the observed and expected values.(package private) static doublemean(java.util.Collection<double[]> samples)Returns the arithmetic mean of the entries in the input arrays, orNaNif the combined length of the arrays is zero.(package private) static doublemeanDifference(double[] x, double[] y)Returns the mean of the (signed) differences between corresponding elements of the input arrays.(package private) static double[]subtract(double[] x, double y)Computex - y.(package private) static doublevarianceDifference(double[] x, double[] y, double mean)Returns the variance of the (signed) differences between corresponding elements of the input arrays, orNaNif the arrays are empty.
-
-
-
Method Detail
-
subtract
static double[] subtract(double[] x, double y)Computex - y.If
yis zero the original array is returned, else a new array is created with the difference.- Parameters:
x- Array.y- Value.- Returns:
- x - y
- Throws:
java.lang.NullPointerException- ifxis null andyis non-zero
-
computeDegreesOfFreedom
static int computeDegreesOfFreedom(int n, int m)Compute the degrees of freedom asn - 1 - m.This method is common functionality shared between the Chi-square test and G-test. The pre-conditions for those tests are performed by this method.
- Parameters:
n- Number of observations.m- Adjustment (assumed to be positive).- Returns:
- the degrees of freedom
- Throws:
java.lang.IllegalArgumentException- if the degrees of freedom is not strictly positive
-
computeRatio
static double computeRatio(double[] expected, long[] observed)Gets the ratio between the sum of the observed and expected values. The ratio can be used to scale the expected values to have the same sum as the observed values:sum(o) = sum(e * ratio)
This method is common functionality shared between the Chi-square test and G-test. The pre-conditions for those tests are performed by this method.
- Parameters:
expected- Expected values.observed- Observed values.- Returns:
- the ratio
- Throws:
java.lang.IllegalArgumentException- if the sample size is less than 2; the array sizes do not match;expectedhas entries that are not strictly positive;observedhas negative entries; or all the observations are zero.
-
add
private static org.apache.commons.numbers.core.DD add(org.apache.commons.numbers.core.DD sum, long v)Adds the value to the sum.- Parameters:
sum- Sum.v- Value.- Returns:
- the new sum
-
mean
static double mean(java.util.Collection<double[]> samples)
Returns the arithmetic mean of the entries in the input arrays, orNaNif the combined length of the arrays is zero.Supports a combined length above the maximum array size.
A two-pass, corrected algorithm is used, starting with the definitional formula computed using the array of stored values and then correcting this by adding the mean deviation of the data values from the arithmetic mean. See, e.g. "Comparison of Several Algorithms for Computing Sample Means and Variances," Robert F. Ling, Journal of the American Statistical Association, Vol. 69, No. 348 (Dec., 1974), pp. 859-866.
- Parameters:
samples- Values.- Returns:
- the mean of the values or NaN if length = 0
-
meanDifference
static double meanDifference(double[] x, double[] y)Returns the mean of the (signed) differences between corresponding elements of the input arrays.sum(x[i] - y[i]) / x.length
This method avoids intermediate array allocation.
- Parameters:
x- First array.y- Second array.- Returns:
- mean of paired differences
- Throws:
java.lang.IllegalArgumentException- if the arrays do not have the same length.
-
varianceDifference
static double varianceDifference(double[] x, double[] y, double mean)Returns the variance of the (signed) differences between corresponding elements of the input arrays, orNaNif the arrays are empty.var(x[i] - y[i])
Returns the bias-corrected sample variance (using
n - 1in the denominator). Returns 0 for a single-value (i.e. length = 1) sample.This method avoids intermediate array allocation.
Uses a two-pass algorithm. Specifically, these methods use the "corrected two-pass algorithm" from Chan, Golub, Levesque, Algorithms for Computing the Sample Variance, American Statistician, vol. 37, no. 3 (1983) pp. 242-247.
- Parameters:
x- First array.y- Second array.mean- the mean difference between corresponding entries- Returns:
- variance of paired differences
- Throws:
java.lang.IllegalArgumentException- if the arrays do not have the same length.- See Also:
meanDifference(double[], double[])
-
-