Top Description Fields Constructors Methods
org.apache.commons.math3.stat.correlation

public Class KendallsCorrelation

extends Object
Class Inheritance
Imports
org.apache.commons.math3.exception.DimensionMismatchException, org.apache.commons.math3.linear.BlockRealMatrix, .MatrixUtils, .RealMatrix, org.apache.commons.math3.util.FastMath, .Pair, java.util.Arrays, .Comparator

Implementation of Kendall's Tau-b rank correlation.

A pair of observations (x1, y1) and (x2, y2) are considered concordant if x1 < x2 and y1 < y2 or x2 < x1 and y2 < y1. The pair is discordant if x1 < x2 and y2 < y1 or x2 < x1 and y1 < y2. If either x1 = x2 or y1 = y2, the pair is neither concordant nor discordant.

Kendall's Tau-b is defined as:

taub = (nc - nd) / sqrt((n0 - n1) * (n0 - n2))

where:

This implementation uses the O(n log n) algorithm described in William R. Knight's 1966 paper "A Computer Method for Calculating Kendall's Tau with Ungrouped Data" in the Journal of the American Statistical Association.

Since
3.3
See Also
Kendall tau rank correlation coefficient (Wikipedia), A Computer Method for Calculating Kendall's Tau with Ungrouped Data

Field Summary

Modifier and TypeField and Description
private final RealMatrix
correlationMatrix

correlation matrix

Constructor Summary

AccessConstructor and Description
public
KendallsCorrelation()

Create a KendallsCorrelation instance without data.

public
KendallsCorrelation(double[][]
rectangular array with columns representing variables
data
)

Create a KendallsCorrelation from a rectangular array whose columns represent values of variables to be correlated.

public
KendallsCorrelation(RealMatrix
matrix with columns representing variables to correlate
matrix
)

Create a KendallsCorrelation from a RealMatrix whose columns represent variables to be correlated.

Method Summary

Modifier and TypeMethod and Description
public RealMatrix

Returns:

correlation matrix
computeCorrelationMatrix
(final RealMatrix
matrix with columns representing variables to correlate
matrix
)

Computes the Kendall's Tau rank correlation matrix for the columns of the input matrix.

public RealMatrix

Returns:

correlation matrix
computeCorrelationMatrix
(final double[][]
matrix with columns representing variables to correlate
matrix
)

Computes the Kendall's Tau rank correlation matrix for the columns of the input rectangular array.

public double

Returns:

Returns Kendall's Tau rank correlation coefficient for the two arrays
correlation
(final double[]
first data array
xArray
,
final double[]
second data array
yArray
)

Computes the Kendall's Tau rank correlation coefficient between the two arrays.

public RealMatrix

Returns:

correlation matrix
getCorrelationMatrix
()

Returns the correlation matrix.

private static long

Returns:

the sum of the number from 1 to n
sum
(long
the summation end
n
)

Returns the sum of the number from 1 ..

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

correlationMatrixback to summary
private final RealMatrix correlationMatrix

correlation matrix

Constructor Detail

KendallsCorrelationback to summary
public KendallsCorrelation()

Create a KendallsCorrelation instance without data.

KendallsCorrelationback to summary
public KendallsCorrelation(double[][] data)

Create a KendallsCorrelation from a rectangular array whose columns represent values of variables to be correlated.

Parameters
data:double[][]

rectangular array with columns representing variables

Exceptions
IllegalArgumentException:
if the input data array is not rectangular with at least two rows and two columns.
KendallsCorrelationback to summary
public KendallsCorrelation(RealMatrix matrix)

Create a KendallsCorrelation from a RealMatrix whose columns represent variables to be correlated.

Parameters
matrix:RealMatrix

matrix with columns representing variables to correlate

Method Detail

computeCorrelationMatrixback to summary
public RealMatrix computeCorrelationMatrix(final RealMatrix matrix)

Computes the Kendall's Tau rank correlation matrix for the columns of the input matrix.

Parameters
matrix:RealMatrix

matrix with columns representing variables to correlate

Returns:RealMatrix

correlation matrix

computeCorrelationMatrixback to summary
public RealMatrix computeCorrelationMatrix(final double[][] matrix)

Computes the Kendall's Tau rank correlation matrix for the columns of the input rectangular array. The columns of the array represent values of variables to be correlated.

Parameters
matrix:double[][]

matrix with columns representing variables to correlate

Returns:RealMatrix

correlation matrix

correlationback to summary
public double correlation(final double[] xArray, final double[] yArray) throws DimensionMismatchException

Computes the Kendall's Tau rank correlation coefficient between the two arrays.

Parameters
xArray:double[]

first data array

yArray:double[]

second data array

Returns:double

Returns Kendall's Tau rank correlation coefficient for the two arrays

Exceptions
DimensionMismatchException:
if the arrays lengths do not match
getCorrelationMatrixback to summary
public RealMatrix getCorrelationMatrix()

Returns the correlation matrix.

Returns:RealMatrix

correlation matrix

sumback to summary
private static long sum(long n)

Returns the sum of the number from 1 .. n according to Gauss' summation formula: \[ \sum\limits_{k=1}^n k = \frac{n(n + 1)}{2} \]

Parameters
n:long

the summation end

Returns:long

the sum of the number from 1 to n