Package org.hipparchus.stat.correlation
Class SpearmansCorrelation
- java.lang.Object
-
- org.hipparchus.stat.correlation.SpearmansCorrelation
-
public class SpearmansCorrelation extends Object
Spearman's rank correlation. This implementation performs a rank transformation on the input data and then computesPearsonsCorrelation
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.
-
-
Constructor Summary
Constructors Constructor Description SpearmansCorrelation()
Create 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
All Methods Instance Methods Concrete Methods Modifier and Type Method Description RealMatrix
computeCorrelationMatrix(double[][] matrix)
Computes the Spearman's rank correlation matrix for the columns of the input rectangular array.RealMatrix
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.RealMatrix
getCorrelationMatrix()
Calculate the Spearman Rank Correlation Matrix.PearsonsCorrelation
getRankCorrelation()
Returns aPearsonsCorrelation
instance constructed from the ranked input data.
-
-
-
Constructor Detail
-
SpearmansCorrelation
public SpearmansCorrelation()
Create a SpearmansCorrelation without data.
-
SpearmansCorrelation
public SpearmansCorrelation(RankingAlgorithm rankingAlgorithm) throws MathIllegalArgumentException
Create a SpearmansCorrelation with the given ranking algorithm.- Parameters:
rankingAlgorithm
- ranking algorithm- Throws:
MathIllegalArgumentException
- if the providedRankingAlgorithm
is of typeNaturalRanking
and uses aNaNStrategy.REMOVED
strategy
-
SpearmansCorrelation
public SpearmansCorrelation(RealMatrix dataMatrix)
Create a SpearmansCorrelation from the given data matrix.- Parameters:
dataMatrix
- matrix of data with columns representing variables to correlate
-
SpearmansCorrelation
public SpearmansCorrelation(RealMatrix dataMatrix, RankingAlgorithm rankingAlgorithm) throws MathIllegalArgumentException
Create a SpearmansCorrelation with the given input data matrix and ranking algorithm.- Parameters:
dataMatrix
- matrix of data with columns representing variables to correlaterankingAlgorithm
- ranking algorithm- Throws:
MathIllegalArgumentException
- if the providedRankingAlgorithm
is of typeNaturalRanking
and uses aNaNStrategy.REMOVED
strategy
-
-
Method Detail
-
getCorrelationMatrix
public RealMatrix getCorrelationMatrix()
Calculate the Spearman Rank Correlation Matrix.- Returns:
- Spearman Rank Correlation Matrix
- Throws:
NullPointerException
- if this instance was created with no data
-
getRankCorrelation
public PearsonsCorrelation 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
public RealMatrix computeCorrelationMatrix(RealMatrix matrix)
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
public RealMatrix computeCorrelationMatrix(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
- 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:
MathIllegalArgumentException
- if the arrays lengths do not matchMathIllegalArgumentException
- if the array length is less than 2
-
-