Changed method name to be consistent with Covariance, added double[][] compute method.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@764312 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
53b912cb49
commit
3b26eea983
|
@ -77,7 +77,7 @@ public class PearsonsCorrelation {
|
||||||
public PearsonsCorrelation(RealMatrix matrix) {
|
public PearsonsCorrelation(RealMatrix matrix) {
|
||||||
checkSufficientData(matrix);
|
checkSufficientData(matrix);
|
||||||
nObs = matrix.getRowDimension();
|
nObs = matrix.getRowDimension();
|
||||||
correlationMatrix = computeCorrelation(matrix);
|
correlationMatrix = computeCorrelationMatrix(matrix);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -183,7 +183,7 @@ public class PearsonsCorrelation {
|
||||||
* @param matrix matrix with columns representing variables to correlate
|
* @param matrix matrix with columns representing variables to correlate
|
||||||
* @return correlation matrix
|
* @return correlation matrix
|
||||||
*/
|
*/
|
||||||
public RealMatrix computeCorrelation(RealMatrix matrix) {
|
public RealMatrix computeCorrelationMatrix(RealMatrix matrix) {
|
||||||
int nVars = matrix.getColumnDimension();
|
int nVars = matrix.getColumnDimension();
|
||||||
RealMatrix outMatrix = new DenseRealMatrix(nVars, nVars);
|
RealMatrix outMatrix = new DenseRealMatrix(nVars, nVars);
|
||||||
for (int i = 0; i < nVars; i++) {
|
for (int i = 0; i < nVars; i++) {
|
||||||
|
@ -197,6 +197,18 @@ public class PearsonsCorrelation {
|
||||||
return outMatrix;
|
return outMatrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Computes the correlation matrix for the columns of the
|
||||||
|
* input rectangular array. The colums of the array represent values
|
||||||
|
* of variables to be correlated.
|
||||||
|
*
|
||||||
|
* @param data matrix with columns representing variables to correlate
|
||||||
|
* @return correlation matrix
|
||||||
|
*/
|
||||||
|
public RealMatrix computeCorrelationMatrix(double[][] data) {
|
||||||
|
return computeCorrelationMatrix(new DenseRealMatrix(data));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Computes the Pearson's product-moment correlation coefficient between the two arrays.
|
* Computes the Pearson's product-moment correlation coefficient between the two arrays.
|
||||||
*
|
*
|
||||||
|
|
|
@ -240,6 +240,19 @@ public class PearsonsCorrelationTest extends TestCase {
|
||||||
corrFromCovInstance2.getCorrelationStandardErrors(), 10E-15);
|
corrFromCovInstance2.getCorrelationStandardErrors(), 10E-15);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void testConsistency() {
|
||||||
|
RealMatrix matrix = createRealMatrix(longleyData, 16, 7);
|
||||||
|
PearsonsCorrelation corrInstance = new PearsonsCorrelation(matrix);
|
||||||
|
double[][] data = matrix.getData();
|
||||||
|
double[] x = matrix.getColumn(0);
|
||||||
|
double[] y = matrix.getColumn(1);
|
||||||
|
assertEquals(new PearsonsCorrelation().correlation(x, y),
|
||||||
|
corrInstance.getCorrelationMatrix().getEntry(0, 1), Double.MIN_VALUE);
|
||||||
|
TestUtils.assertEquals("Correlation matrix", corrInstance.getCorrelationMatrix(),
|
||||||
|
new PearsonsCorrelation().computeCorrelationMatrix(data), Double.MIN_VALUE);
|
||||||
|
}
|
||||||
|
|
||||||
protected RealMatrix createRealMatrix(double[] data, int nRows, int nCols) {
|
protected RealMatrix createRealMatrix(double[] data, int nRows, int nCols) {
|
||||||
double[][] matrixData = new double[nRows][nCols];
|
double[][] matrixData = new double[nRows][nCols];
|
||||||
int ptr = 0;
|
int ptr = 0;
|
||||||
|
|
Loading…
Reference in New Issue