org.apache.commons.math4.stat.correlation

## Class KendallsCorrelation

• java.lang.Object
• org.apache.commons.math4.stat.correlation.KendallsCorrelation

• public class KendallsCorrelation
extends Object
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:

• n0 = n * (n - 1) / 2
• nc = Number of concordant pairs
• nd = Number of discordant pairs
• n1 = sum of ti * (ti - 1) / 2 for all i
• n2 = sum of uj * (uj - 1) / 2 for all j
• ti = Number of tied values in the ith group of ties in x
• uj = Number of tied values in the jth group of ties in y

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
Kendall tau rank correlation coefficient (Wikipedia), A Computer Method for Calculating Kendall's Tau with Ungrouped Data
• ### Constructor Summary

Constructors
Constructor and Description
KendallsCorrelation()
Create a KendallsCorrelation instance without data.
KendallsCorrelation(double[][] data)
Create a KendallsCorrelation from a rectangular array whose columns represent values of variables to be correlated.
KendallsCorrelation(RealMatrix matrix)
Create a KendallsCorrelation from a RealMatrix whose columns represent variables to be correlated.
• ### Method Summary

All Methods
Modifier and Type Method and Description
RealMatrix computeCorrelationMatrix(double[][] matrix)
Computes the Kendall's Tau rank correlation matrix for the columns of the input rectangular array.
RealMatrix computeCorrelationMatrix(RealMatrix matrix)
Computes the Kendall's Tau rank correlation matrix for the columns of the input matrix.
double correlation(double[] xArray, double[] yArray)
Computes the Kendall's Tau rank correlation coefficient between the two arrays.
RealMatrix getCorrelationMatrix()
Returns the correlation matrix.
• ### Methods inherited from class java.lang.Object

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

• #### KendallsCorrelation

public KendallsCorrelation()
Create a KendallsCorrelation instance without data.
• #### KendallsCorrelation

public KendallsCorrelation(double[][] data)
Create a KendallsCorrelation from a rectangular array whose columns represent values of variables to be correlated.
Parameters:
data - rectangular array with columns representing variables
Throws:
IllegalArgumentException - if the input data array is not rectangular with at least two rows and two columns.
• #### KendallsCorrelation

public KendallsCorrelation(RealMatrix matrix)
Create a KendallsCorrelation from a RealMatrix whose columns represent variables to be correlated.
Parameters:
matrix - matrix with columns representing variables to correlate
• ### Method Detail

• #### getCorrelationMatrix

public RealMatrix getCorrelationMatrix()
Returns the correlation matrix.
Returns:
correlation matrix
• #### computeCorrelationMatrix

public RealMatrix computeCorrelationMatrix(RealMatrix matrix)
Computes the Kendall's Tau rank correlation matrix for the columns of the input matrix.
Parameters:
matrix - matrix with columns representing variables to correlate
Returns:
correlation matrix
• #### computeCorrelationMatrix

public RealMatrix computeCorrelationMatrix(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 - matrix with columns representing variables to correlate
Returns:
correlation matrix
• #### correlation

public double correlation(double[] xArray,
double[] yArray)
throws DimensionMismatchException
Computes the Kendall's Tau rank correlation coefficient between the two arrays.
Parameters:
xArray - first data array
yArray - second data array
Returns:
Returns Kendall's Tau rank correlation coefficient for the two arrays
Throws:
DimensionMismatchException - if the arrays lengths do not match