Java Code Examples for org.apache.commons.math.linear.RealMatrix#getData()

The following examples show how to use org.apache.commons.math.linear.RealMatrix#getData() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractLeastSquaresOptimizer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Get the covariance matrix of the optimized parameters.
 *
 * @return the covariance matrix.
 * @throws org.apache.commons.math.linear.SingularMatrixException
 * if the covariance matrix cannot be computed (singular problem).
 * @throws org.apache.commons.math.exception.MathUserException if the
 * jacobian function throws one.
 */
public double[][] getCovariances() {
    // set up the jacobian
    updateJacobian();

    // compute transpose(J).J, avoiding building big intermediate matrices
    double[][] jTj = new double[cols][cols];
    for (int i = 0; i < cols; ++i) {
        for (int j = i; j < cols; ++j) {
            double sum = 0;
            for (int k = 0; k < rows; ++k) {
                sum += weightedResidualJacobian[k][i] * weightedResidualJacobian[k][j];
            }
            jTj[i][j] = sum;
            jTj[j][i] = sum;
        }
    }

    // compute the covariances matrix
    RealMatrix inverse =
        new LUDecompositionImpl(MatrixUtils.createRealMatrix(jTj)).getSolver().getInverse();
    return inverse.getData();
}
 
Example 2
Source File: Math_65_AbstractLeastSquaresOptimizer_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Get the covariance matrix of optimized parameters.
 * @return covariance matrix
 * @exception FunctionEvaluationException if the function jacobian cannot
 * be evaluated
 * @exception OptimizationException if the covariance matrix
 * cannot be computed (singular problem)
 */
public double[][] getCovariances()
    throws FunctionEvaluationException, OptimizationException {

    // set up the jacobian
    updateJacobian();

    // compute transpose(J).J, avoiding building big intermediate matrices
    double[][] jTj = new double[cols][cols];
    for (int i = 0; i < cols; ++i) {
        for (int j = i; j < cols; ++j) {
            double sum = 0;
            for (int k = 0; k < rows; ++k) {
                sum += jacobian[k][i] * jacobian[k][j];
            }
            jTj[i][j] = sum;
            jTj[j][i] = sum;
        }
    }

    try {
        // compute the covariances matrix
        RealMatrix inverse =
            new LUDecompositionImpl(MatrixUtils.createRealMatrix(jTj)).getSolver().getInverse();
        return inverse.getData();
    } catch (InvalidMatrixException ime) {
        throw new OptimizationException(LocalizedFormats.UNABLE_TO_COMPUTE_COVARIANCE_SINGULAR_PROBLEM);
    }

}
 
Example 3
Source File: PearsonsCorrelationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 4
Source File: AbstractEstimator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the covariance matrix of unbound estimated parameters.
 * @param problem estimation problem
 * @return covariance matrix
 * @exception EstimationException if the covariance matrix
 * cannot be computed (singular problem)
 */
public double[][] getCovariances(EstimationProblem problem)
  throws EstimationException {

    // set up the jacobian
    updateJacobian();

    // compute transpose(J).J, avoiding building big intermediate matrices
    final int n = problem.getMeasurements().length;
    final int m = problem.getUnboundParameters().length;
    final int max  = m * n;
    double[][] jTj = new double[m][m];
    for (int i = 0; i < m; ++i) {
        for (int j = i; j < m; ++j) {
            double sum = 0;
            for (int k = 0; k < max; k += m) {
                sum += jacobian[k + i] * jacobian[k + j];
            }
            jTj[i][j] = sum;
            jTj[j][i] = sum;
        }
    }

    try {
        // compute the covariances matrix
        RealMatrix inverse =
            new LUDecompositionImpl(MatrixUtils.createRealMatrix(jTj)).getSolver().getInverse();
        return inverse.getData();
    } catch (InvalidMatrixException ime) {
        throw new EstimationException(LocalizedFormats.UNABLE_TO_COMPUTE_COVARIANCE_SINGULAR_PROBLEM);
    }

}
 
Example 5
Source File: AbstractLeastSquaresOptimizer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the covariance matrix of optimized parameters.
 * @return covariance matrix
 * @exception FunctionEvaluationException if the function jacobian cannot
 * be evaluated
 * @exception OptimizationException if the covariance matrix
 * cannot be computed (singular problem)
 */
public double[][] getCovariances()
    throws FunctionEvaluationException, OptimizationException {

    // set up the jacobian
    updateJacobian();

    // compute transpose(J).J, avoiding building big intermediate matrices
    double[][] jTj = new double[cols][cols];
    for (int i = 0; i < cols; ++i) {
        for (int j = i; j < cols; ++j) {
            double sum = 0;
            for (int k = 0; k < rows; ++k) {
                sum += jacobian[k][i] * jacobian[k][j];
            }
            jTj[i][j] = sum;
            jTj[j][i] = sum;
        }
    }

    try {
        // compute the covariances matrix
        RealMatrix inverse =
            new LUDecompositionImpl(MatrixUtils.createRealMatrix(jTj)).getSolver().getInverse();
        return inverse.getData();
    } catch (InvalidMatrixException ime) {
        throw new OptimizationException("unable to compute covariances: singular problem");
    }

}
 
Example 6
Source File: AbstractLeastSquaresOptimizer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the covariance matrix of optimized parameters.
 * @return covariance matrix
 * @exception FunctionEvaluationException if the function jacobian cannot
 * be evaluated
 * @exception OptimizationException if the covariance matrix
 * cannot be computed (singular problem)
 */
public double[][] getCovariances()
    throws FunctionEvaluationException, OptimizationException {

    // set up the jacobian
    updateJacobian();

    // compute transpose(J).J, avoiding building big intermediate matrices
    double[][] jTj = new double[cols][cols];
    for (int i = 0; i < cols; ++i) {
        for (int j = i; j < cols; ++j) {
            double sum = 0;
            for (int k = 0; k < rows; ++k) {
                sum += jacobian[k][i] * jacobian[k][j];
            }
            jTj[i][j] = sum;
            jTj[j][i] = sum;
        }
    }

    try {
        // compute the covariances matrix
        RealMatrix inverse =
            new LUDecompositionImpl(MatrixUtils.createRealMatrix(jTj)).getSolver().getInverse();
        return inverse.getData();
    } catch (InvalidMatrixException ime) {
        throw new OptimizationException("unable to compute covariances: singular problem");
    }

}
 
Example 7
Source File: AbstractLeastSquaresOptimizer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the covariance matrix of optimized parameters.
 * @return covariance matrix
 * @exception FunctionEvaluationException if the function jacobian cannot
 * be evaluated
 * @exception OptimizationException if the covariance matrix
 * cannot be computed (singular problem)
 */
public double[][] getCovariances()
    throws FunctionEvaluationException, OptimizationException {

    // set up the jacobian
    updateJacobian();

    // compute transpose(J).J, avoiding building big intermediate matrices
    double[][] jTj = new double[cols][cols];
    for (int i = 0; i < cols; ++i) {
        for (int j = i; j < cols; ++j) {
            double sum = 0;
            for (int k = 0; k < rows; ++k) {
                sum += jacobian[k][i] * jacobian[k][j];
            }
            jTj[i][j] = sum;
            jTj[j][i] = sum;
        }
    }

    try {
        // compute the covariances matrix
        RealMatrix inverse =
            new LUDecompositionImpl(MatrixUtils.createRealMatrix(jTj)).getSolver().getInverse();
        return inverse.getData();
    } catch (InvalidMatrixException ime) {
        throw new OptimizationException("unable to compute covariances: singular problem");
    }

}
 
Example 8
Source File: SpearmansRankCorrelationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void testConsistency() {
    RealMatrix matrix = createRealMatrix(longleyData, 16, 7);
    SpearmansCorrelation corrInstance = new SpearmansCorrelation(matrix);
    double[][] data = matrix.getData();
    double[] x = matrix.getColumn(0);
    double[] y = matrix.getColumn(1);
    assertEquals(new SpearmansCorrelation().correlation(x, y),
            corrInstance.getCorrelationMatrix().getEntry(0, 1), Double.MIN_VALUE);
    TestUtils.assertEquals("Correlation matrix", corrInstance.getCorrelationMatrix(),
            new SpearmansCorrelation().computeCorrelationMatrix(data), Double.MIN_VALUE);
}
 
Example 9
Source File: AbstractEstimator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the covariance matrix of unbound estimated parameters.
 * @param problem estimation problem
 * @return covariance matrix
 * @exception EstimationException if the covariance matrix
 * cannot be computed (singular problem)
 */
public double[][] getCovariances(EstimationProblem problem)
  throws EstimationException {

    // set up the jacobian
    updateJacobian();

    // compute transpose(J).J, avoiding building big intermediate matrices
    final int n = problem.getMeasurements().length;
    final int m = problem.getUnboundParameters().length;
    final int max  = m * n;
    double[][] jTj = new double[m][m];
    for (int i = 0; i < m; ++i) {
        for (int j = i; j < m; ++j) {
            double sum = 0;
            for (int k = 0; k < max; k += m) {
                sum += jacobian[k + i] * jacobian[k + j];
            }
            jTj[i][j] = sum;
            jTj[j][i] = sum;
        }
    }

    try {
        // compute the covariances matrix
        RealMatrix inverse =
            new LUDecompositionImpl(MatrixUtils.createRealMatrix(jTj)).getSolver().getInverse();
        return inverse.getData();
    } catch (InvalidMatrixException ime) {
        throw new EstimationException("unable to compute covariances: singular problem");
    }

}
 
Example 10
Source File: SpearmansRankCorrelationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void testConsistency() {
    RealMatrix matrix = createRealMatrix(longleyData, 16, 7);
    SpearmansCorrelation corrInstance = new SpearmansCorrelation(matrix);
    double[][] data = matrix.getData();
    double[] x = matrix.getColumn(0);
    double[] y = matrix.getColumn(1);
    assertEquals(new SpearmansCorrelation().correlation(x, y),
            corrInstance.getCorrelationMatrix().getEntry(0, 1), Double.MIN_VALUE);
    TestUtils.assertEquals("Correlation matrix", corrInstance.getCorrelationMatrix(),
            new SpearmansCorrelation().computeCorrelationMatrix(data), Double.MIN_VALUE);
}
 
Example 11
Source File: SpearmansRankCorrelationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void testConsistency() {
    RealMatrix matrix = createRealMatrix(longleyData, 16, 7);
    SpearmansCorrelation corrInstance = new SpearmansCorrelation(matrix);
    double[][] data = matrix.getData();
    double[] x = matrix.getColumn(0);
    double[] y = matrix.getColumn(1);
    assertEquals(new SpearmansCorrelation().correlation(x, y),
            corrInstance.getCorrelationMatrix().getEntry(0, 1), Double.MIN_VALUE);
    TestUtils.assertEquals("Correlation matrix", corrInstance.getCorrelationMatrix(),
            new SpearmansCorrelation().computeCorrelationMatrix(data), Double.MIN_VALUE);
}
 
Example 12
Source File: SpearmansRankCorrelationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void testConsistency() {
    RealMatrix matrix = createRealMatrix(longleyData, 16, 7);
    SpearmansCorrelation corrInstance = new SpearmansCorrelation(matrix);
    double[][] data = matrix.getData();
    double[] x = matrix.getColumn(0);
    double[] y = matrix.getColumn(1);
    assertEquals(new SpearmansCorrelation().correlation(x, y),
            corrInstance.getCorrelationMatrix().getEntry(0, 1), Double.MIN_VALUE);
    TestUtils.assertEquals("Correlation matrix", corrInstance.getCorrelationMatrix(),
            new SpearmansCorrelation().computeCorrelationMatrix(data), Double.MIN_VALUE);
}
 
Example 13
Source File: SpearmansRankCorrelationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void testConsistency() {
    RealMatrix matrix = createRealMatrix(longleyData, 16, 7);
    SpearmansCorrelation corrInstance = new SpearmansCorrelation(matrix);
    double[][] data = matrix.getData();
    double[] x = matrix.getColumn(0);
    double[] y = matrix.getColumn(1);
    assertEquals(new SpearmansCorrelation().correlation(x, y),
            corrInstance.getCorrelationMatrix().getEntry(0, 1), Double.MIN_VALUE);
    TestUtils.assertEquals("Correlation matrix", corrInstance.getCorrelationMatrix(),
            new SpearmansCorrelation().computeCorrelationMatrix(data), Double.MIN_VALUE);
}
 
Example 14
Source File: AbstractLeastSquaresOptimizer.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Get the covariance matrix of optimized parameters.
 * @return covariance matrix
 * @exception FunctionEvaluationException if the function jacobian cannot
 * be evaluated
 * @exception OptimizationException if the covariance matrix
 * cannot be computed (singular problem)
 */
public double[][] getCovariances()
    throws FunctionEvaluationException, OptimizationException {

    // set up the jacobian
    updateJacobian();

    // compute transpose(J).J, avoiding building big intermediate matrices
    double[][] jTj = new double[cols][cols];
    for (int i = 0; i < cols; ++i) {
        for (int j = i; j < cols; ++j) {
            double sum = 0;
            for (int k = 0; k < rows; ++k) {
                sum += jacobian[k][i] * jacobian[k][j];
            }
            jTj[i][j] = sum;
            jTj[j][i] = sum;
        }
    }

    try {
        // compute the covariances matrix
        RealMatrix inverse =
            new LUDecompositionImpl(MatrixUtils.createRealMatrix(jTj)).getSolver().getInverse();
        return inverse.getData();
    } catch (InvalidMatrixException ime) {
        throw new OptimizationException("unable to compute covariances: singular problem");
    }

}
 
Example 15
Source File: PearsonsCorrelationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example 16
Source File: CovarianceTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Verify that diagonal entries are consistent with Variance computation and matrix matches
 * column-by-column covariances
 */
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);
        }
    }
    
    // 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); 
}
 
Example 17
Source File: CovarianceTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Verify that diagonal entries are consistent with Variance computation and matrix matches
 * column-by-column covariances
 */
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);
        }
    }

    // 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);
}
 
Example 18
Source File: CovarianceTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Verify that diagonal entries are consistent with Variance computation and matrix matches
 * column-by-column covariances
 */
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);
        }
    }

    // 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);
}
 
Example 19
Source File: CovarianceTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Verify that diagonal entries are consistent with Variance computation and matrix matches
 * column-by-column covariances
 */
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);
        }
    }

    // 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);
}
 
Example 20
Source File: CovarianceTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Verify that diagonal entries are consistent with Variance computation and matrix matches
 * column-by-column covariances
 */
@Test
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++) {
        Assert.assertEquals(variance.evaluate(matrix.getColumn(i)), covarianceMatrix.getEntry(i,i), 10E-14);
    }

    // Symmetry, column-consistency
    Assert.assertEquals(covarianceMatrix.getEntry(2, 3),
            new Covariance().covariance(matrix.getColumn(2), matrix.getColumn(3), true), 10E-14);
    Assert.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++) {
            Assert.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];
    Assert.assertEquals(new Covariance().covariance(x, y),
            new Covariance().covariance(x, y, true), Double.MIN_VALUE);
}