public void testConsistency() {
final RealMatrix matrix = createRealMatrix(swissData, 47, 5);
final RealMatrix covarianceMatrix = new Covariance(matrix).getCovarianceMatrix();
// Variances on the diagonal
Variance variance = new Variance();
for (int i = 0; i < 5; i++) {
assertEquals(variance.evaluate(matrix.getColumn(i)), covarianceMatrix.getEntry(i,i), 10E-14);
}
// Symmetry, column-consistency
assertEquals(covarianceMatrix.getEntry(2, 3),
new Covariance().covariance(matrix.getColumn(2), matrix.getColumn(3), true), 10E-14);
assertEquals(covarianceMatrix.getEntry(2, 3), covarianceMatrix.getEntry(3, 2), Double.MIN_VALUE);
// All columns same -> all entries = column variance
RealMatrix repeatedColumns = new Array2DRowRealMatrix(47, 3);
for (int i = 0; i < 3; i++) {
repeatedColumns.setColumnMatrix(i, matrix.getColumnMatrix(0));
}
RealMatrix repeatedCovarianceMatrix = new Covariance(repeatedColumns).getCovarianceMatrix();
double columnVariance = variance.evaluate(matrix.getColumn(0));
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
assertEquals(columnVariance, repeatedCovarianceMatrix.getEntry(i, j), 10E-14);
}
}