org.apache.commons.math3.stat.correlation
Class SpearmansCorrelation

java.lang.Object
  extended by org.apache.commons.math3.stat.correlation.SpearmansCorrelation

public class SpearmansCorrelation
extends Object

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
Version:
$Id: SpearmansCorrelation.java 1461822 2013-03-27 19:44:22Z tn $

Field Summary
private  RealMatrix data
          Input data
private  PearsonsCorrelation rankCorrelation
          Rank correlation
private  RankingAlgorithm rankingAlgorithm
          Ranking algorithm
 
Constructor Summary
SpearmansCorrelation()
          Create a SpearmansCorrelation without data.
SpearmansCorrelation(RankingAlgorithm rankingAlgorithm)
          Create a SpearmansCorrelation with the given ranking algorithm.
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.
 
Method Summary
 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.
private  List<Integer> getNaNPositions(double[] input)
          Returns a list containing the indices of NaN values in the input array.
 PearsonsCorrelation getRankCorrelation()
          Returns a PearsonsCorrelation instance constructed from the ranked input data.
private  RealMatrix rankTransform(RealMatrix matrix)
          Applies rank transform to each of the columns of matrix using the current rankingAlgorithm.
private  double[] removeValues(double[] input, Set<Integer> indices)
          Removes all values from the input array at the specified indices.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

data

private final RealMatrix data
Input data


rankingAlgorithm

private final RankingAlgorithm rankingAlgorithm
Ranking algorithm


rankCorrelation

private final PearsonsCorrelation rankCorrelation
Rank correlation

Constructor Detail

SpearmansCorrelation

public SpearmansCorrelation()
Create a SpearmansCorrelation without data.


SpearmansCorrelation

public SpearmansCorrelation(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 - ranking algorithm
Since:
3.1

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)
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 - matrix of data with columns representing variables to correlate
rankingAlgorithm - ranking algorithm
Method Detail

getCorrelationMatrix

public RealMatrix getCorrelationMatrix()
Calculate the Spearman Rank Correlation Matrix.

Returns:
Spearman Rank Correlation Matrix

getRankCorrelation

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:
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 array
yArray - second data array
Returns:
Returns Spearman's rank correlation coefficient for the two arrays
Throws:
DimensionMismatchException - if the arrays lengths do not match
MathIllegalArgumentException - if the array length is less than 2

rankTransform

private RealMatrix rankTransform(RealMatrix matrix)
Applies rank transform to each of the columns of matrix using the current rankingAlgorithm.

Parameters:
matrix - matrix to transform
Returns:
a rank-transformed matrix

getNaNPositions

private List<Integer> getNaNPositions(double[] input)
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

private double[] removeValues(double[] input,
                              Set<Integer> indices)
Removes all values from the input array at the specified indices.

Parameters:
input - the input array
indices - a set containing the indices to be removed
Returns:
the input array without the values at the specified indices


Copyright (c) 2003-2013 Apache Software Foundation