SoftNet-Consult Java Utility Library

com.softnetConsult.utils.math
Class StatsTools

java.lang.Object
  extended by com.softnetConsult.utils.math.StatsTools

public final class StatsTools
extends java.lang.Object

This class is a collection of static statistical utility methods and functions.

This product includes software developed by the SoftNet-Consult Java Utility Library project and its contributors.
(http://java-tools.sourceforge.net)
Copyright (c) 2007-2008 SoftNet-Consult.
Copyright (c) 2007-2008 G. Paperin.
All rights reserved.

File: StatsTools.java
Library API version: "2.02"
Java compliance version: "1.5"

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following terms and conditions are met:

1. Redistributions of source code must retain the above acknowledgement of the SoftNet-Consult Java Utility Library project, the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above acknowledgement of the SoftNet-Consult Java Utility Library project, the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. All advertising materials mentioning features or use of this software or any derived software must display the following acknowledgement:
This product includes software developed by the SoftNet-Consult Java Utility Library project and its contributors.

THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS, CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Version:
"2.02"
Author:
Greg Paperin (http://www.paperin.org)

Constructor Summary
private StatsTools()
          Prevents instances of this class from being created as this class contains only static utility methods.
 
Method Summary
static double correlation(double[] X, double[] Y)
          Computes the linear resgression correlation of the specified data.
static double covariance(double[] X, double[] Y)
          Computes the covariance of the specified data sequences.
static double linearLeastSquaresDisplacement(double[] X, double[] Y)
          Computes the displacement of the linear least squares fit to the specified data.
static double linearLeastSquaresDisplacement(double[] X, double[] Y, double slope)
          Computes the displacement of the linear least squares fit to the specified data.
static double linearLeastSquaresSlope(double[] X, double[] Y)
          Computes the slope of the linear least squares fit to the specified data.
static Pair<double[],double[]> lnTransform(Distribution<? extends java.lang.Number> dist)
          Computes a linear transform of the specified distribution sample by using the observed values as x-values and observation frequencies as y-values of a data series.
static
<T1 extends java.lang.Number,T2 extends java.lang.Number>
Pair<double[],double[]>
lnTransform(T1[] X, T2[] Y)
          Computes a linear transform of the specified data.
static double mean(double[] sequence)
          Computes the mean of an double number sequence.
static double mean(int[] sequence)
          Computes the mean of an integer sequence.
static double mean(java.lang.Iterable<? extends java.lang.Number> sequence)
          Computes the mean of a specified number sequence.
static double mean(java.util.Iterator<? extends java.lang.Number> iterator)
          Computes the mean of a number sequence returned by the specified iterator.
static double rSquared(double[] X, double[] Y)
          Computes "R-squared" - the coefficient of determination for a linear regression, in other words - the square of the linear correlation coefficient.
static double stdDeviation(double[] sequence)
          Computes the standard deviation of a specified sequence.
static double stdDeviation(double[] sequence, double mean)
          Computes the standard deviation of a specified sequence.
static double stdDeviation(int[] sequence)
          Computes the standard deviation of a specified sequence.
static double stdDeviation(int[] sequence, double mean)
          Computes the standard deviation of a specified sequence.
static double stdDeviation(java.lang.Iterable<? extends java.lang.Number> sequence)
          Computes the standard deviation of a specified sequence.
static double stdDeviation(java.lang.Iterable<? extends java.lang.Number> sequence, double mean)
          Computes the standard deviation of a specified sequence.
static double stdDeviation(java.util.Iterator<? extends java.lang.Number> iterator)
          Computes the standard deviation of a number sequence returned by the specified iterator.
static double stdDeviation(java.util.Iterator<? extends java.lang.Number> iterator, double mean)
          Computes the standard deviation of a number sequence returned by the specified iterator.
static double variance(double[] sequence)
          Computes the variance of a specified sequence.
static double variance(double[] sequence, double mean)
          Computes the variance of a specified sequence.
static double variance(int[] sequence)
          Computes the variance of a specified sequence.
static double variance(int[] sequence, double mean)
          Computes the variance of a specified sequence.
static double variance(java.lang.Iterable<? extends java.lang.Number> sequence)
          Computes the variance of a specified sequence.
static double variance(java.lang.Iterable<? extends java.lang.Number> sequence, double mean)
          Computes the variance of a specified sequence assuming that the mean is already known.
static double variance(java.util.Iterator<? extends java.lang.Number> iterator)
          Computes the variance of a number sequence returned by the specified iterator.
static double variance(java.util.Iterator<? extends java.lang.Number> iterator, double mean)
          Computes the variance of a number sequence returned by the specified iterator.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

StatsTools

private StatsTools()
Prevents instances of this class from being created as this class contains only static utility methods.

Method Detail

mean

public static double mean(java.lang.Iterable<? extends java.lang.Number> sequence)
Computes the mean of a specified number sequence. Null elements and Double.NaN elements are ignored, however elements that are Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY are not ignored.

Parameters:
sequence - A number sequence that must contain at least one element that is not null and not Double.NaN.
Returns:
The mean of the specified sequence.
Throws:
java.lang.IllegalArgumentException - if the specified sequence contains zero elements that are not null and not Double.NaN.

mean

public static double mean(java.util.Iterator<? extends java.lang.Number> iterator)
Computes the mean of a number sequence returned by the specified iterator. Null elements and Double.NaN elements are ignored, however elements that are Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY are not ignored.

Parameters:
iterator - An iterator over a number sequence that must contain at least one element that is not null and not Double.NaN.
Returns:
The mean of the sequence specified by the iterator.
Throws:
java.lang.IllegalArgumentException - if the sequence specified by the iterator contains zero elements that are not null and not Double.NaN.

mean

public static double mean(double[] sequence)
Computes the mean of an double number sequence. Double.NaN-elements are ignored, however elements that are Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY are not ignored.

Parameters:
sequence - A double number sequence that must contain at least one element that is not Double.NaN.
Returns:
The mean of the specified sequence.
Throws:
java.lang.IllegalArgumentException - If the specified sequence contains zero elements that are not Double.NaN.

mean

public static double mean(int[] sequence)
Computes the mean of an integer sequence.

Parameters:
sequence - An integer sequence that must be longer than zero.
Returns:
The mean of the specified sequence.
Throws:
java.lang.IllegalArgumentException - If the specified sequence contains no elements.

variance

public static double variance(java.lang.Iterable<? extends java.lang.Number> sequence)
Computes the variance of a specified sequence. Null elements and Double.NaN elements are ignored, however elements that are Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY are not ignored.

Parameters:
sequence - A number sequence that must contain two or more elements that are not null and not Double.NaN.
Returns:
The variance of the speciefied sequence.
Throws:
java.lang.IllegalArgumentException - If the specified sequence contains less than 2 elements that are not null and not Double.NaN.

variance

public static double variance(java.lang.Iterable<? extends java.lang.Number> sequence,
                              double mean)
Computes the variance of a specified sequence assuming that the mean is already known. Null elements and Double.NaN elements are ignored, however elements that are Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY are not ignored. This method does not attempt to compute the mean required for the computation of the variance but uses a specified value instead; if the mean is already known this may save a complete iteration over the sequence. However, if the specified mean is incorrect, the result of this method is undefined.

Parameters:
sequence - A number sequence that must contain two or more elements that are not null and not Double.NaN.
mean - The mean of the specified sequence as computed by mean(Iterable).
Returns:
The variance of the speciefied sequence.
Throws:
java.lang.IllegalArgumentException - If the specified sequence contains less than 2 elements that are not null and not Double.NaN.

variance

public static double variance(java.util.Iterator<? extends java.lang.Number> iterator)
Computes the variance of a number sequence returned by the specified iterator. Null elements and Double.NaN elements are ignored, however elements that are Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY are not ignored.

Parameters:
iterator - An iterator over a number sequence that must contain two or more elements that are not null and not Double.NaN.
Returns:
The variance of the sequence specified by the iterator.
Throws:
java.lang.IllegalArgumentException - If the specified sequence contains less than 2 elements that are not null and not Double.NaN.

variance

public static double variance(java.util.Iterator<? extends java.lang.Number> iterator,
                              double mean)
Computes the variance of a number sequence returned by the specified iterator. Null elements and Double.NaN elements are ignored, however elements that are Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY are not ignored. This method does not attempt to compute the mean required for the computation of the variance but uses a specified value instead; if the mean is already known this may save a complete iteration over the sequence. However, if the specified mean is incorrect, the result of this method is undefined.

Parameters:
iterator - An iterator over a number sequence that must contain two or more elements that are not null and not Double.NaN.
mean - The mean of the specified sequence as computed by mean(Iterator).
Returns:
The variance of the sequence specified by the iterator.
Throws:
java.lang.IllegalArgumentException - If the specified sequence contains less than 2 elements that are not null and not Double.NaN.

variance

public static double variance(double[] sequence)
Computes the variance of a specified sequence. Double.NaN-elements are ignored, however elements that are Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY are not ignored.

Parameters:
sequence - A numer sequence that must contain at least two elements that are not Double.NaN.
Returns:
The variance of the speciefied sequence.
Throws:
java.lang.IllegalArgumentException - If the specified sequence contains less than 2 elements that are not Double.NaN.

variance

public static double variance(double[] sequence,
                              double mean)
Computes the variance of a specified sequence. Double.NaN-elements are ignored, however elements that are Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY are not ignored. This method does not attempt to compute the mean required for the computation of the variance but uses a specified value instead; if the mean is already known this may save a complete iteration over the sequence. However, if the specified mean is incorrect, the result of this method is undefined.

Parameters:
sequence - A numer sequence that must contain at least two elements that are not Double.NaN.
mean - The mean of the specified sequence as computed by mean(double[]).
Returns:
The variance of the speciefied sequence.
Throws:
java.lang.IllegalArgumentException - If the specified sequence contains less than 2 elements that are not Double.NaN.

variance

public static double variance(int[] sequence)
Computes the variance of a specified sequence.

Parameters:
sequence - A numer sequence that must contain at least two elements.
Returns:
The variance of the speciefied sequence.
Throws:
java.lang.IllegalArgumentException - If the specified sequence contains less than 2 elements.

variance

public static double variance(int[] sequence,
                              double mean)
Computes the variance of a specified sequence. This method does not attempt to compute the mean required for the computation of the variance but uses a specified value instead; if the mean is already known this may save a complete iteration over the sequence. However, if the specified mean is incorrect, the result of this method is undefined.

Parameters:
sequence - A numer sequence that must contain at least two elements.
mean - The mean of the specified sequence as computed by mean(int[]).
Returns:
The variance of the speciefied sequence.
Throws:
java.lang.IllegalArgumentException - If the specified sequence contains less than 2 elements.

stdDeviation

public static double stdDeviation(java.lang.Iterable<? extends java.lang.Number> sequence)
Computes the standard deviation of a specified sequence. Null elements and Double.NaN elements are ignored, however elements that are Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY are not ignored.

Parameters:
sequence - A number sequence that must contain two or more elements that are not null and not Double.NaN.
Returns:
The standard deviation of the speciefied sequence.
Throws:
java.lang.IllegalArgumentException - If the specified sequence contains less than 2 elements that are not null and not Double.NaN.

stdDeviation

public static double stdDeviation(java.lang.Iterable<? extends java.lang.Number> sequence,
                                  double mean)
Computes the standard deviation of a specified sequence. Null elements and Double.NaN elements are ignored, however elements that are Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY are not ignored. This method does not attempt to compute the mean required for the computation of the standard deviation but uses a specified value instead; if the mean is already known this may save a complete iteration over the sequence. However, if the specified mean is incorrect, the result of this method is undefined.

Parameters:
sequence - A number sequence that must contain two or more elements that are not null and not Double.NaN.
mean - The mean of the specified sequence as computed by mean(Iterable).
Returns:
The standard deviation of the speciefied sequence.
Throws:
java.lang.IllegalArgumentException - If the specified sequence contains less than 2 elements that are not null and not Double.NaN.

stdDeviation

public static double stdDeviation(java.util.Iterator<? extends java.lang.Number> iterator)
Computes the standard deviation of a number sequence returned by the specified iterator. Null elements and Double.NaN elements are ignored, however elements that are Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY are not ignored.

Parameters:
iterator - An iterator over a number sequence that must contain two or more elements that are not null and not Double.NaN. computation of the variance and uses this value instead; if the mean is already known this may save a complete iteration over the sequence.
Returns:
The standard deviation of the sequence specified by the iterator.
Throws:
java.lang.IllegalArgumentException - If the specified sequence contains less than 2 elements that are not null and not Double.NaN.

stdDeviation

public static double stdDeviation(java.util.Iterator<? extends java.lang.Number> iterator,
                                  double mean)
Computes the standard deviation of a number sequence returned by the specified iterator. Null elements and Double.NaN elements are ignored, however elements that are Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY are not ignored. This method does not attempt to compute the mean required for the computation of the standard deviation but uses a specified value instead; if the mean is already known this may save a complete iteration over the sequence. However, if the specified mean is incorrect, the result of this method is undefined.

Parameters:
iterator - An iterator over a number sequence that must contain two or more elements that are not null and not Double.NaN. computation of the variance and uses this value instead; if the mean is already known this may save a complete iteration over the sequence.
mean - The mean of the specified sequence as computed by mean(Iterable).
Returns:
The standard deviation of the sequence specified by the iterator.
Throws:
java.lang.IllegalArgumentException - If the specified sequence contains less than 2 elements that are not null and not Double.NaN.

stdDeviation

public static double stdDeviation(double[] sequence)
Computes the standard deviation of a specified sequence. Double.NaN-elements are ignored, however elements that are Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY are not ignored.

Parameters:
sequence - A numer sequence that must contain at least two elements that are not Double.NaN.
Returns:
The standard deviation of the speciefied sequence.
Throws:
java.lang.IllegalArgumentException - If the specified sequence contains less than 2 elements that are not Double.NaN.

stdDeviation

public static double stdDeviation(double[] sequence,
                                  double mean)
Computes the standard deviation of a specified sequence. Double.NaN-elements are ignored, however elements that are Double.POSITIVE_INFINITY or Double.NEGATIVE_INFINITY are not ignored. This method does not attempt to compute the mean required for the computation of the standard deviation but uses a specified value instead; if the mean is already known this may save a complete iteration over the sequence. However, if the specified mean is incorrect, the result of this method is undefined.

Parameters:
sequence - A numer sequence that must contain at least two elements that are not Double.NaN.
mean - The mean of the specified sequence as computed by mean(Iterable).
Returns:
The standard deviation of the speciefied sequence.
Throws:
java.lang.IllegalArgumentException - If the specified sequence contains less than 2 elements that are not Double.NaN.

stdDeviation

public static double stdDeviation(int[] sequence)
Computes the standard deviation of a specified sequence.

Parameters:
sequence - A numer sequence that must contain at least two elements.
Returns:
The standard deviation of the speciefied sequence.
Throws:
java.lang.IllegalArgumentException - If the specified sequence contains less than 2 elements.

stdDeviation

public static double stdDeviation(int[] sequence,
                                  double mean)
Computes the standard deviation of a specified sequence. This method does not attempt to compute the mean required for the computation of the standard deviation but uses a specified value instead; if the mean is already known this may save a complete iteration over the sequence. However, if the specified mean is incorrect, the result of this method is undefined.

Parameters:
sequence - A numer sequence that must contain at least two elements.
mean - The mean of the specified sequence as computed by mean(Iterable).
Returns:
The standard deviation of the speciefied sequence.
Throws:
java.lang.IllegalArgumentException - If the specified sequence contains less than 2 elements.

covariance

public static double covariance(double[] X,
                                double[] Y)
Computes the covariance of the specified data sequences.

Parameters:
X - x-values of a series of data points.
Y - y-values of a series of data points.
Returns:
The covariance if the specified data.
Throws:
java.lang.NullPointerException - If X or Y is null.
java.lang.IllegalArgumentException - If X or Y are not of equal length.

rSquared

public static double rSquared(double[] X,
                              double[] Y)
Computes "R-squared" - the coefficient of determination for a linear regression, in other words - the square of the linear correlation coefficient.

Parameters:
X - x-values of a series of data points.
Y - y-values of a series of data points.
Returns:
The coefficient of determination for a linear regression for the specified data.
Throws:
java.lang.NullPointerException - If X or Y is null.
java.lang.IllegalArgumentException - If X or Y are not of equal length.

correlation

public static double correlation(double[] X,
                                 double[] Y)
Computes the linear resgression correlation of the specified data.

Parameters:
X - x-values of a series of data points.
Y - y-values of a series of data points.
Returns:
The linear correlation of the specified data.
Throws:
java.lang.NullPointerException - If X or Y is null.
java.lang.IllegalArgumentException - If X or Y are not of equal length.

linearLeastSquaresSlope

public static double linearLeastSquaresSlope(double[] X,
                                             double[] Y)
Computes the slope of the linear least squares fit to the specified data.

Parameters:
X - x-values of a series of data points.
Y - y-values of a series of data points.
Returns:
The slope of the linear least squares fit to the specified data.
Throws:
java.lang.NullPointerException - If X or Y is null.
java.lang.IllegalArgumentException - If X or Y are not of equal length.

linearLeastSquaresDisplacement

public static double linearLeastSquaresDisplacement(double[] X,
                                                    double[] Y,
                                                    double slope)
Computes the displacement of the linear least squares fit to the specified data. This method does not attempt to compute the sloped of the fitted line required for the computation of the displacement but uses a specified value instead; if the slope is already known this may save a computing time. However, if the specified slope is incorrect, the result of this method is undefined.

Parameters:
X - x-values of a series of data points.
Y - y-values of a series of data points.
slope - The slope of the of the linear least squares fit to the specified data as computed by linearLeastSquaresSlope(double[], double[]).
Returns:
Tthe displacement of the linear least squares fit to the specified data.
Throws:
java.lang.NullPointerException - If X or Y is null.
java.lang.IllegalArgumentException - If X or Y are not of equal length.

linearLeastSquaresDisplacement

public static double linearLeastSquaresDisplacement(double[] X,
                                                    double[] Y)
Computes the displacement of the linear least squares fit to the specified data.

Parameters:
X - x-values of a series of data points.
Y - y-values of a series of data points.
Returns:
The displacement of the linear least squares fit to the specified data.
Throws:
java.lang.NullPointerException - If X or Y is null.
java.lang.IllegalArgumentException - If X or Y are not of equal length.

lnTransform

public static <T1 extends java.lang.Number,T2 extends java.lang.Number> Pair<double[],double[]> lnTransform(T1[] X,
                                                                                                            T2[] Y)
Computes a linear transform of the specified data.

Type Parameters:
T1 - The specific Number type of the x-values.
T2 - The specific Number type of the y-values.
Parameters:
X - x-values of a series of data points.
Y - y-values of a series of data points.
Returns:
A Pair of two arrays - the first containing the natural logarithm values of the specified x-values, the second containing the natural logarithm values of the specified y-values. Both arrays will always have the same lengths, but that length may be shorter than the length of the input data arrays as each pair (X[i], Y[i]) where (X[i]) or (X[i]) is null is discarded and not included in the result array (note that if this happens, data indices of the input data do not correspond to data indices of the result).
Throws:
java.lang.NullPointerException - If X or Y is null.
java.lang.IllegalArgumentException - If X or Y are not of equal length.

lnTransform

public static Pair<double[],double[]> lnTransform(Distribution<? extends java.lang.Number> dist)
Computes a linear transform of the specified distribution sample by using the observed values as x-values and observation frequencies as y-values of a data series.

Parameters:
dist - An observed distribution sample.
Returns:
A Pair of two arrays - the first containing the natural logarithm values of the specified x-values, the second containing the natural logarithm values of the specified y-values. Both arrays will always have the same lengths, but that length may be shorter than the length of the input data arrays as each pair (X[i], Y[i]) where (X[i]) or (X[i]) is null is discarded and not included in the result array (note that if this happens, data indices of the input data do not correspond to data indices of the result).
Throws:
java.lang.NullPointerException - If X or Y is null.
java.lang.IllegalArgumentException - If X or Y are not of equal length.

SoftNet-Consult Java Utility Library is a member of SourceForge.net