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

public Class SpearmansCorrelation

extends Object
Class Inheritance
Imports
java.util.ArrayList, .HashSet, .List, .Set, org.apache.commons.math3.exception.DimensionMismatchException, .MathIllegalArgumentException, org.apache.commons.math3.exception.util.LocalizedFormats, org.apache.commons.math3.linear.BlockRealMatrix, .RealMatrix, org.apache.commons.math3.stat.ranking.NaNStrategy, .NaturalRanking, .RankingAlgorithm

Spearman's rank correlation. This implementation performs a rank transformation on the input data and then computes PearsonsCorrelation on the ranked data.

By default, ranks are computed using NaturalRanking with default strategies for handling NaNs and ties in the data (NaNs maximal, ties averaged). The ranking algorithm can be set using a constructor argument.

Since
2.0

Field Summary

Modifier and TypeField and Description
private final RealMatrix
data

Input data

private final PearsonsCorrelation
rankCorrelation

Rank correlation

private final RankingAlgorithm
rankingAlgorithm

Ranking algorithm

Constructor Summary

AccessConstructor and Description
public
SpearmansCorrelation()

Create a SpearmansCorrelation without data.

public
SpearmansCorrelation(final RankingAlgorithm
ranking algorithm
rankingAlgorithm
)

Create a SpearmansCorrelation with the given ranking algorithm.

public
SpearmansCorrelation(final RealMatrix
matrix of data with columns representing variables to correlate
dataMatrix
)

Create a SpearmansCorrelation from the given data matrix.

public
SpearmansCorrelation(final RealMatrix
matrix of data with columns representing variables to correlate
dataMatrix
,
final RankingAlgorithm
ranking algorithm
rankingAlgorithm
)

Create a SpearmansCorrelation with the given input data matrix and ranking algorithm.

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 Spearman's 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 Spearman's rank correlation matrix for the columns of the input rectangular array.

public double

Returns:

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

Computes the Spearman's rank correlation coefficient between the two arrays.

public RealMatrix

Returns:

Spearman Rank Correlation Matrix
getCorrelationMatrix
()

Calculate the Spearman Rank Correlation Matrix.

private List<Integer>

Returns:

a list of NaN positions in the input array
getNaNPositions
(final double[]
the input array
input
)

Returns a list containing the indices of NaN values in the input array.

public PearsonsCorrelation

Returns:

PearsonsCorrelation among ranked column data
getRankCorrelation
()

Returns a PearsonsCorrelation instance constructed from the ranked input data.

private RealMatrix

Returns:

a rank-transformed matrix
rankTransform
(final RealMatrix
matrix to transform
matrix
)

Applies rank transform to each of the columns of matrix using the current rankingAlgorithm.

private double[]

Returns:

the input array without the values at the specified indices
removeValues
(final double[]
the input array
input
,
final Set<Integer>
a set containing the indices to be removed
indices
)

Removes all values from the input array at the specified indices.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

databack to summary
private final RealMatrix data

Input data

rankCorrelationback to summary
private final PearsonsCorrelation rankCorrelation

Rank correlation

rankingAlgorithmback to summary
private final RankingAlgorithm rankingAlgorithm

Ranking algorithm

Constructor Detail

SpearmansCorrelationback to summary
public SpearmansCorrelation()

Create a SpearmansCorrelation without data.

SpearmansCorrelationback to summary
public SpearmansCorrelation(final RankingAlgorithm rankingAlgorithm)

Create a SpearmansCorrelation with the given ranking algorithm.

From version 4.0 onwards this constructor will throw an exception if the provided NaturalRanking uses a NaNStrategy#REMOVED strategy.

Parameters
rankingAlgorithm:RankingAlgorithm

ranking algorithm

Since
3.1
SpearmansCorrelationback to summary
public SpearmansCorrelation(final RealMatrix dataMatrix)

Create a SpearmansCorrelation from the given data matrix.

Parameters
dataMatrix:RealMatrix

matrix of data with columns representing variables to correlate

SpearmansCorrelationback to summary
public SpearmansCorrelation(final RealMatrix dataMatrix, final RankingAlgorithm rankingAlgorithm)

Create a SpearmansCorrelation with the given input data matrix and ranking algorithm.

From version 4.0 onwards this constructor will throw an exception if the provided NaturalRanking uses a NaNStrategy#REMOVED strategy.

Parameters
dataMatrix:RealMatrix

matrix of data with columns representing variables to correlate

rankingAlgorithm:RankingAlgorithm

ranking algorithm

Method Detail

computeCorrelationMatrixback to summary
public RealMatrix computeCorrelationMatrix(final RealMatrix matrix)

Computes the Spearman's 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 Spearman's 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)

Computes the Spearman's rank correlation coefficient between the two arrays.

Parameters
xArray:double[]

first data array

yArray:double[]

second data array

Returns:double

Returns Spearman's rank correlation coefficient for the two arrays

Exceptions
DimensionMismatchException:
if the arrays lengths do not match
MathIllegalArgumentException:
if the array length is less than 2
getCorrelationMatrixback to summary
public RealMatrix getCorrelationMatrix()

Calculate the Spearman Rank Correlation Matrix.

Returns:RealMatrix

Spearman Rank Correlation Matrix

Exceptions
NullPointerException:
if this instance was created with no data
getNaNPositionsback to summary
private List<Integer> getNaNPositions(final double[] input)

Returns a list containing the indices of NaN values in the input array.

Parameters
input:double[]

the input array

Returns:List<Integer>

a list of NaN positions in the input array

getRankCorrelationback to summary
public PearsonsCorrelation getRankCorrelation()

Returns a PearsonsCorrelation instance constructed from the ranked input data. That is, new SpearmansCorrelation(matrix).getRankCorrelation() is equivalent to new PearsonsCorrelation(rankTransform(matrix)) where rankTransform(matrix) is the result of applying the configured RankingAlgorithm to each of the columns of matrix.

Returns null if this instance was created with no data.

Returns:PearsonsCorrelation

PearsonsCorrelation among ranked column data

rankTransformback to summary
private RealMatrix rankTransform(final RealMatrix matrix)

Applies rank transform to each of the columns of matrix using the current rankingAlgorithm.

Parameters
matrix:RealMatrix

matrix to transform

Returns:RealMatrix

a rank-transformed matrix

removeValuesback to summary
private double[] removeValues(final double[] input, final Set<Integer> indices)

Removes all values from the input array at the specified indices.

Parameters
input:double[]

the input array

indices:Set<Integer>

a set containing the indices to be removed

Returns:double[]

the input array without the values at the specified indices