Class SpearmansCorrelation
java.lang.Object
org.apache.commons.math3.stat.correlation.SpearmansCorrelation
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
FieldsModifier and TypeFieldDescriptionprivate final RealMatrix
Input dataprivate final PearsonsCorrelation
Rank correlationprivate final RankingAlgorithm
Ranking algorithm -
Constructor Summary
ConstructorsConstructorDescriptionCreate a SpearmansCorrelation without data.SpearmansCorrelation
(RealMatrix dataMatrix) Create a SpearmansCorrelation from the given data matrix.SpearmansCorrelation
(RealMatrix dataMatrix, RankingAlgorithm rankingAlgorithm) Create a SpearmansCorrelation with the given input data matrix and ranking algorithm.SpearmansCorrelation
(RankingAlgorithm rankingAlgorithm) Create a SpearmansCorrelation with the given ranking algorithm. -
Method Summary
Modifier and TypeMethodDescriptioncomputeCorrelationMatrix
(double[][] matrix) Computes the Spearman's rank correlation matrix for the columns of the input rectangular array.computeCorrelationMatrix
(RealMatrix matrix) Computes the Spearman's rank correlation matrix for the columns of the input matrix.double
correlation
(double[] xArray, double[] yArray) Computes the Spearman's rank correlation coefficient between the two arrays.Calculate the Spearman Rank Correlation Matrix.getNaNPositions
(double[] input) Returns a list containing the indices of NaN values in the input array.Returns aPearsonsCorrelation
instance constructed from the ranked input data.private RealMatrix
rankTransform
(RealMatrix matrix) Applies rank transform to each of the columns ofmatrix
using the currentrankingAlgorithm
.private double[]
removeValues
(double[] input, Set<Integer> indices) Removes all values from the input array at the specified indices.
-
Field Details
-
data
Input data -
rankingAlgorithm
Ranking algorithm -
rankCorrelation
Rank correlation
-
-
Constructor Details
-
SpearmansCorrelation
public SpearmansCorrelation()Create a SpearmansCorrelation without data. -
SpearmansCorrelation
Create a SpearmansCorrelation with the given ranking algorithm.From version 4.0 onwards this constructor will throw an exception if the provided
NaturalRanking
uses aNaNStrategy.REMOVED
strategy.- Parameters:
rankingAlgorithm
- ranking algorithm- Since:
- 3.1
-
SpearmansCorrelation
Create a SpearmansCorrelation from the given data matrix.- Parameters:
dataMatrix
- matrix of data with columns representing variables to correlate
-
SpearmansCorrelation
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 aNaNStrategy.REMOVED
strategy.- Parameters:
dataMatrix
- matrix of data with columns representing variables to correlaterankingAlgorithm
- ranking algorithm
-
-
Method Details
-
getCorrelationMatrix
Calculate the Spearman Rank Correlation Matrix.- Returns:
- Spearman Rank Correlation Matrix
- Throws:
NullPointerException
- if this instance was created with no data
-
getRankCorrelation
Returns aPearsonsCorrelation
instance constructed from the ranked input data. That is,new SpearmansCorrelation(matrix).getRankCorrelation()
is equivalent tonew PearsonsCorrelation(rankTransform(matrix))
whererankTransform(matrix)
is the result of applying the configuredRankingAlgorithm
to each of the columns ofmatrix.
Returns null if this instance was created with no data.
- Returns:
- PearsonsCorrelation among ranked column data
-
computeCorrelationMatrix
Computes the Spearman's rank correlation matrix for the columns of the input matrix.- Parameters:
matrix
- matrix with columns representing variables to correlate- Returns:
- correlation matrix
-
computeCorrelationMatrix
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
- matrix with columns representing variables to correlate- Returns:
- correlation matrix
-
correlation
public double correlation(double[] xArray, double[] yArray) Computes the Spearman's rank correlation coefficient between the two arrays.- Parameters:
xArray
- first data arrayyArray
- second data array- Returns:
- Returns Spearman's rank correlation coefficient for the two arrays
- Throws:
DimensionMismatchException
- if the arrays lengths do not matchMathIllegalArgumentException
- if the array length is less than 2
-
rankTransform
Applies rank transform to each of the columns ofmatrix
using the currentrankingAlgorithm
.- Parameters:
matrix
- matrix to transform- Returns:
- a rank-transformed matrix
-
getNaNPositions
Returns a list containing the indices of NaN values in the input array.- Parameters:
input
- the input array- Returns:
- a list of NaN positions in the input array
-
removeValues
Removes all values from the input array at the specified indices.- Parameters:
input
- the input arrayindices
- a set containing the indices to be removed- Returns:
- the input array without the values at the specified indices
-