Class StatisticUtils

java.lang.Object
org.apache.commons.statistics.inference.StatisticUtils

final class StatisticUtils extends Object
Utility computation methods.
Since:
1.1
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    private
    No instances.
  • Method Summary

    Modifier and Type
    Method
    Description
    private static org.apache.commons.numbers.core.DD
    add(org.apache.commons.numbers.core.DD sum, long v)
    Adds the value to the sum.
    (package private) static int
    computeDegreesOfFreedom(int n, int m)
    Compute the degrees of freedom as n - 1 - m.
    (package private) static double
    computeRatio(double[] expected, long[] observed)
    Gets the ratio between the sum of the observed and expected values.
    (package private) static double
    mean(Collection<double[]> samples)
    Returns the arithmetic mean of the entries in the input arrays, or NaN if the combined length of the arrays is zero.
    (package private) static double
    meanDifference(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)
    Compute x - y.
    (package private) static double
    varianceDifference(double[] x, double[] y, double mean)
    Returns the variance of the (signed) differences between corresponding elements of the input arrays, or NaN if the arrays are empty.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • StatisticUtils

      private StatisticUtils()
      No instances.
  • Method Details

    • subtract

      static double[] subtract(double[] x, double y)
      Compute x - y.

      If y is zero the original array is returned, else a new array is created with the difference.

      Parameters:
      x - Array.
      y - Value.
      Returns:
      x - y
      Throws:
      NullPointerException - if x is null and y is non-zero
    • computeDegreesOfFreedom

      static int computeDegreesOfFreedom(int n, int m)
      Compute the degrees of freedom as n - 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:
      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:
      IllegalArgumentException - if the sample size is less than 2; the array sizes do not match; expected has entries that are not strictly positive; observed has 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(Collection<double[]> samples)
      Returns the arithmetic mean of the entries in the input arrays, or NaN if 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:
      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, or NaN if the arrays are empty.
       var(x[i] - y[i])
       

      Returns the bias-corrected sample variance (using n - 1 in 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:
      IllegalArgumentException - if the arrays do not have the same length.
      See Also: