uaa.feral.util
Class Statistics

java.lang.Object
  extended by uaa.feral.util.Statistics

public class Statistics
extends java.lang.Object

A number of static methods to compute statistical properties of an array of double values. Implements the computation of mean, variance and standard deviation for double values.

Since:
0.11.0
Author:
Marco Schmidt and Mark Altaweel (added a method)

Method Summary
static double[] combineData(double[] values1, double[] values2)
           
static double computeMean(double[] values)
          Computes the mean value for the argument array.
static double computeMean(double[] values, int offset, int number)
          Computes the mean value for some elements of the argument array.
static double computeMedian(double[] a, int from, int to)
          Find the median value of the specified interval of the argument array.
static double computeStandardDeviation(double[] values)
          Computes the standard deviation for the argument array of values.
static double computeStandardDeviation(double[] values, double mean)
          Computes the standard deviation for the argument array of values.
static double computeStandardDeviation(double[] values, int offset, int number)
          Computes the standard deviation for some of the argument array's values.
static double computeStandardDeviation(double[] values, int offset, int number, double mean)
          Computes the standard deviation for some of the argument array's values.
static double computeSum(double[] values)
          Method to compute the sum of a double array.
static double computeSum(int[] values)
          Method to compute the sum of a double array.
static double computeVariance(double[] values)
          Computes the variance for the argument array.
static double computeVariance(double[] values, double mean)
          Computes the variance for some of the argument array's values.
static double computeVariance(double[] values, int offset, int number)
          Computes the variance for some of the argument array's values.
static double computeVariance(double[] values, int offset, int number, double mean)
          Computes the variance for some of the argument array's values.
static void swap(double[] a, int i1, int i2)
          Exchange two elements in the argument array.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

computeMean

public static double computeMean(double[] values)
Computes the mean value for the argument array. Adds all values and divides them by the number of array elements.

Parameters:
values - double array on which the mean is to be determined
Returns:
computed mean value
Throws:
java.lang.IllegalArgumentException - if the array has not at least one element

computeMean

public static double computeMean(double[] values,
                                 int offset,
                                 int number)
Computes the mean value for some elements of the argument array. Adds all values and divides them by the number of array elements.

Parameters:
values - array from which elements are read
offset - index of the first element to be used
number - number of elements to be used
Returns:
computed mean value
Throws:
java.lang.IllegalArgumentException - if the array has not at least one element

computeSum

public static double computeSum(double[] values)
Method to compute the sum of a double array.

Parameters:
values - the double array
Returns:
a sum of the array values

computeSum

public static double computeSum(int[] values)
Method to compute the sum of a double array.

Parameters:
values - the double array
Returns:
a sum of the array values

computeStandardDeviation

public static double computeStandardDeviation(double[] values)
Computes the standard deviation for the argument array of values.

Parameters:
values - array from which elements are read
Returns:
computed standard deviation
Throws:
java.lang.IllegalArgumentException - if the array has not at least two elements

computeStandardDeviation

public static double computeStandardDeviation(double[] values,
                                              double mean)
Computes the standard deviation for the argument array of values. Reuses the mean value for that argument which must have been computed before.

Parameters:
values - array from which elements are read
mean - the mean value for the array, possibly computed with a call to computeMean(double[]).
Returns:
computed standard deviation
Throws:
java.lang.IllegalArgumentException - if the array has not at least two elements

computeStandardDeviation

public static double computeStandardDeviation(double[] values,
                                              int offset,
                                              int number)
Computes the standard deviation for some of the argument array's values. If you already have computed a mean value using computeMean(double[], int, int), better call computeStandardDeviation(double[], int, int, double). Otherwise, this method has to compute mean again.

Parameters:
values - array from which elements are read
offset - first element to be used
number - number of elements used starting at values[offset]
Returns:
computed standard deviation
Throws:
java.lang.IllegalArgumentException - if the array has not at least two elements

computeStandardDeviation

public static double computeStandardDeviation(double[] values,
                                              int offset,
                                              int number,
                                              double mean)
Computes the standard deviation for some of the argument array's values. Use this version of the method if you already have a mean value, otherwise this method must be computed again.

Parameters:
values - array from which elements are read
offset - first element to be used
number - number of elements used starting at values[offset]
mean - value of the elements
Returns:
computed standard deviation
Throws:
java.lang.IllegalArgumentException - if the array has not at least two elements

computeVariance

public static double computeVariance(double[] values)
Computes the variance for the argument array.

Parameters:
values - array from which elements are read
Returns:
variance for the array elements
Throws:
java.lang.IllegalArgumentException - if the array has not at least two elements

computeVariance

public static double computeVariance(double[] values,
                                     double mean)
Computes the variance for some of the argument array's values.

Parameters:
values - array from which elements are read
mean - the mean for the array elements
Returns:
variance for the array elements
Throws:
java.lang.IllegalArgumentException - if the array has not at least two elements

computeVariance

public static double computeVariance(double[] values,
                                     int offset,
                                     int number)
Computes the variance for some of the argument array's values. If you already have computed a mean value using computeMean(double[], int, int), better call computeVariance(double[], int, int, double). Otherwise, this method has to compute mean again.

Parameters:
values - array from which elements are read
offset - first element to be used
number - number of elements used starting at values[offset]
Returns:
computed variance
Throws:
java.lang.IllegalArgumentException - if the array has not at least two elements

computeVariance

public static double computeVariance(double[] values,
                                     int offset,
                                     int number,
                                     double mean)
Computes the variance for some of the argument array's values. Use this version of the method in case mean has already been computed.

Parameters:
values - array from which elements are read
offset - first element to be used
number - number of elements used starting at values[offset]
mean - the mean for the array elements
Returns:
computed variance
Throws:
java.lang.IllegalArgumentException - if the array has not at least two elements

combineData

public static double[] combineData(double[] values1,
                                   double[] values2)

swap

public static void swap(double[] a,
                        int i1,
                        int i2)
Exchange two elements in the argument array. A temporary variable is used so that a[i1] will hold the value that was previously stored at a[i2] and vice versa.

Parameters:
a - the array in which two elements are swapped
i1 - index of the first element
i2 - index of the second element
Throws:
java.lang.ArrayIndexOutOfBoundsException - if either i1 or i2 are not valid index values into a (from 0 to a.length - 1)

computeMedian

public static double computeMedian(double[] a,
                                   int from,
                                   int to)
Find the median value of the specified interval of the argument array. The interval starts at index from and goes to to; the values at these positions are included. Note that the array will be modified while searching, so you might want to backup your data.

This implementation is a port of the C function from quickselect.c, provided at http://ndevilla.free.fr/median/. The page is a good resource for various median value algorithms, including implementations and benchmarks.

The original code on which this class is based was written in C++ by Martin Leese. It was ported to C and optimized by Nicolas Devillard (author of the above mentioned page). The algorithm is from Numerical recipes in C, Second Edition, Cambridge University Press, 1992, Section 8.5, ISBN 0-521-43108-5.

Parameters:
a - the array
from - the index of the start of the interval in which the median value will be searched
to - the index of the end of the interval in which the median value will be searched
Returns:
the median value