Made method names consistent, added methods to default bias-correction.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@764313 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
3b26eea983
commit
6bb4309b69
|
@ -113,7 +113,7 @@ public class Covariance {
|
||||||
public Covariance(RealMatrix matrix, boolean biasCorrected) {
|
public Covariance(RealMatrix matrix, boolean biasCorrected) {
|
||||||
checkSufficientData(matrix);
|
checkSufficientData(matrix);
|
||||||
n = matrix.getRowDimension();
|
n = matrix.getRowDimension();
|
||||||
covarianceMatrix = computeCovariance(matrix, biasCorrected);
|
covarianceMatrix = computeCovarianceMatrix(matrix, biasCorrected);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -150,13 +150,13 @@ public class Covariance {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a covariance matrix from a matrix whose columns represent
|
* Compute a covariance matrix from a matrix whose columns represent
|
||||||
* covariates.
|
* covariates.
|
||||||
* @param matrix input matrix (must have at least two columns and two rows)
|
* @param matrix input matrix (must have at least two columns and two rows)
|
||||||
* @param biasCorrected determines whether or not covariance estimates are bias-corrected
|
* @param biasCorrected determines whether or not covariance estimates are bias-corrected
|
||||||
* @return covariance matrix
|
* @return covariance matrix
|
||||||
*/
|
*/
|
||||||
protected RealMatrix computeCovariance(RealMatrix matrix, boolean biasCorrected) {
|
protected RealMatrix computeCovarianceMatrix(RealMatrix matrix, boolean biasCorrected) {
|
||||||
int dimension = matrix.getColumnDimension();
|
int dimension = matrix.getColumnDimension();
|
||||||
Variance variance = new Variance(biasCorrected);
|
Variance variance = new Variance(biasCorrected);
|
||||||
RealMatrix outMatrix = new DenseRealMatrix(dimension, dimension);
|
RealMatrix outMatrix = new DenseRealMatrix(dimension, dimension);
|
||||||
|
@ -171,6 +171,39 @@ public class Covariance {
|
||||||
return outMatrix;
|
return outMatrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a covariance matrix from a matrix whose columns represent
|
||||||
|
* covariates. Covariances are computed using the bias-corrected formula.
|
||||||
|
* @param matrix input matrix (must have at least two columns and two rows)
|
||||||
|
* @return covariance matrix
|
||||||
|
* @see #Covariance
|
||||||
|
*/
|
||||||
|
protected RealMatrix computeCovarianceMatrix(RealMatrix matrix) {
|
||||||
|
return computeCovarianceMatrix(matrix, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Compute a covariance matrix from a rectangular array whose columns represent
|
||||||
|
* covariates.
|
||||||
|
* @param data input array (must have at least two columns and two rows)
|
||||||
|
* @param biasCorrected determines whether or not covariance estimates are bias-corrected
|
||||||
|
* @return covariance matrix
|
||||||
|
*/
|
||||||
|
protected RealMatrix computeCovarianceMatrix(double[][] data, boolean biasCorrected) {
|
||||||
|
return computeCovarianceMatrix(new DenseRealMatrix(data), biasCorrected);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a covariance matrix from a rectangual array whose columns represent
|
||||||
|
* covariates. Covariances are computed using the bias-corrected formula.
|
||||||
|
* @param data input array (must have at least two columns and two rows)
|
||||||
|
* @return covariance matrix
|
||||||
|
* @see #Covariance
|
||||||
|
*/
|
||||||
|
protected RealMatrix computeCovarianceMatrix(double[][] data) {
|
||||||
|
return computeCovarianceMatrix(data, true);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Computes the covariance between the two arrays.
|
* Computes the covariance between the two arrays.
|
||||||
*
|
*
|
||||||
|
@ -206,6 +239,23 @@ public class Covariance {
|
||||||
return biasCorrected ? result * ((double) length / (double)(length - 1)) : result;
|
return biasCorrected ? result * ((double) length / (double)(length - 1)) : result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Computes the covariance between the two arrays, using the bias-corrected
|
||||||
|
* formula.
|
||||||
|
*
|
||||||
|
* <p>Array lengths must match and the common length must be at least 2.</p>
|
||||||
|
*
|
||||||
|
* @param xArray first data array
|
||||||
|
* @param yArray second data array
|
||||||
|
* @return returns the covariance for the two arrays
|
||||||
|
* @throws IllegalArgumentException if the arrays lengths do not match or
|
||||||
|
* there is insufficient data
|
||||||
|
*/
|
||||||
|
public double covariance(final double[] xArray, final double[] yArray)
|
||||||
|
throws IllegalArgumentException {
|
||||||
|
return covariance(xArray, yArray, true);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Throws IllegalArgumentException of the matrix does not have at least
|
* Throws IllegalArgumentException of the matrix does not have at least
|
||||||
* two columns and two rows
|
* two columns and two rows
|
||||||
|
|
|
@ -184,8 +184,8 @@ public class CovarianceTest extends TestCase {
|
||||||
* column-by-column covariances
|
* column-by-column covariances
|
||||||
*/
|
*/
|
||||||
public void testConsistency() {
|
public void testConsistency() {
|
||||||
RealMatrix matrix = createRealMatrix(swissData, 47, 5);
|
final RealMatrix matrix = createRealMatrix(swissData, 47, 5);
|
||||||
RealMatrix covarianceMatrix = new Covariance(matrix).getCovarianceMatrix();
|
final RealMatrix covarianceMatrix = new Covariance(matrix).getCovarianceMatrix();
|
||||||
|
|
||||||
// Variances on the diagonal
|
// Variances on the diagonal
|
||||||
Variance variance = new Variance();
|
Variance variance = new Variance();
|
||||||
|
@ -203,14 +203,25 @@ public class CovarianceTest extends TestCase {
|
||||||
for (int i = 0; i < 3; i++) {
|
for (int i = 0; i < 3; i++) {
|
||||||
repeatedColumns.setColumnMatrix(i, matrix.getColumnMatrix(0));
|
repeatedColumns.setColumnMatrix(i, matrix.getColumnMatrix(0));
|
||||||
}
|
}
|
||||||
covarianceMatrix = new Covariance(repeatedColumns).getCovarianceMatrix();
|
RealMatrix repeatedCovarianceMatrix = new Covariance(repeatedColumns).getCovarianceMatrix();
|
||||||
double columnVariance = variance.evaluate(matrix.getColumn(0));
|
double columnVariance = variance.evaluate(matrix.getColumn(0));
|
||||||
for (int i = 0; i < 3; i++) {
|
for (int i = 0; i < 3; i++) {
|
||||||
for (int j = 0; j < 3; j++) {
|
for (int j = 0; j < 3; j++) {
|
||||||
assertEquals(columnVariance, covarianceMatrix.getEntry(i, j), 10E-14);
|
assertEquals(columnVariance, repeatedCovarianceMatrix.getEntry(i, j), 10E-14);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check bias-correction defaults
|
||||||
|
double[][] data = matrix.getData();
|
||||||
|
TestUtils.assertEquals("Covariances",
|
||||||
|
covarianceMatrix, new Covariance().computeCovarianceMatrix(data),Double.MIN_VALUE);
|
||||||
|
TestUtils.assertEquals("Covariances",
|
||||||
|
covarianceMatrix, new Covariance().computeCovarianceMatrix(data, true),Double.MIN_VALUE);
|
||||||
|
|
||||||
|
double[] x = data[0];
|
||||||
|
double[] y = data[1];
|
||||||
|
assertEquals(new Covariance().covariance(x, y),
|
||||||
|
new Covariance().covariance(x, y, true), Double.MIN_VALUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected RealMatrix createRealMatrix(double[] data, int nRows, int nCols) {
|
protected RealMatrix createRealMatrix(double[] data, int nRows, int nCols) {
|
||||||
|
|
Loading…
Reference in New Issue