Search in sources :

Example 26 with Matrix

use of dr.math.matrixAlgebra.Matrix in project beast-mcmc by beast-dev.

the class ProcrustesTest method runTest.

private void runTest(double[][] X, double[][] Xstar, double[][] goldStandard, boolean allowTranslation, boolean allowDilation, double tolerance, String header) {
    RealMatrix rmX = new Array2DRowRealMatrix(X);
    RealMatrix rmXstar = new Array2DRowRealMatrix(Xstar);
    Procrustes procrustes = new Procrustes(rmX, rmXstar, allowTranslation, allowDilation);
    RealMatrix rmXnew = procrustes.procrustinate(rmX);
    System.out.println(header);
    System.out.print("Translation:\n" + new Matrix(procrustes.getTranslation().getData()));
    System.out.println("Dilation = " + procrustes.getDilation());
    System.out.println("Xnew:");
    System.out.println(new Matrix(rmXnew.getData()));
    assertEquals(rmXnew.getData(), goldStandard, tolerance);
}
Also used : RealMatrix(org.apache.commons.math.linear.RealMatrix) Array2DRowRealMatrix(org.apache.commons.math.linear.Array2DRowRealMatrix) Matrix(dr.math.matrixAlgebra.Matrix) RealMatrix(org.apache.commons.math.linear.RealMatrix) Array2DRowRealMatrix(org.apache.commons.math.linear.Array2DRowRealMatrix) Array2DRowRealMatrix(org.apache.commons.math.linear.Array2DRowRealMatrix) Procrustes(dr.math.Procrustes)

Example 27 with Matrix

use of dr.math.matrixAlgebra.Matrix in project beast-mcmc by beast-dev.

the class LKJTransformTest method testTransformationRecursion.

public void testTransformationRecursion() {
    System.out.println("\nTest LKJ transform.");
    double[] transformedValue = transform.inverseRecursion(CPCs, 0, CPCs.length);
    double[] transformedValueChol = transform.inverse(CPCs, 0, CPCs.length);
    for (int k = 0; k < transformedValueChol.length; k++) {
        assertEquals("transform chol rec k=" + k, format.format(transformedValueChol[k]), format.format(transformedValue[k]));
    }
    double[] inverseTransformedValues = transform.transformRecursion(transformedValue, 0, CPCs.length);
    SymmetricMatrix R = compoundCorrelationSymmetricMatrix(transformedValue, dim);
    ;
    System.out.println("transformedValue=" + R);
    try {
        assertTrue("Positive Definite", R.isPD());
    } catch (IllegalDimension illegalDimension) {
        illegalDimension.printStackTrace();
    }
    System.out.println("iCPC=" + new Matrix(inverseTransformedValues, dim * (dim - 1) / 2, 1));
    assertEquals("size CPCs", format.format(CPCs.length), format.format(inverseTransformedValues.length));
    for (int k = 0; k < CPCs.length; k++) {
        assertEquals("inverse transform k=" + k, format.format(CPCs[k]), format.format(inverseTransformedValues[k]));
    }
}
Also used : SymmetricMatrix.compoundCorrelationSymmetricMatrix(dr.math.matrixAlgebra.SymmetricMatrix.compoundCorrelationSymmetricMatrix) SymmetricMatrix(dr.math.matrixAlgebra.SymmetricMatrix) Matrix(dr.math.matrixAlgebra.Matrix) IllegalDimension(dr.math.matrixAlgebra.IllegalDimension) SymmetricMatrix.compoundCorrelationSymmetricMatrix(dr.math.matrixAlgebra.SymmetricMatrix.compoundCorrelationSymmetricMatrix) SymmetricMatrix(dr.math.matrixAlgebra.SymmetricMatrix)

Example 28 with Matrix

use of dr.math.matrixAlgebra.Matrix in project beast-mcmc by beast-dev.

the class LKJTransformTest method testJacobianCorrelationToCholesky.

public void testJacobianCorrelationToCholesky() {
    System.out.println("\nTest Correlation to Cholesky Jacobian.");
    // Matrix
    double[] cholValues = transformChol.inverse(CPCs, 0, CPCs.length);
    double[][] jacobianMat = transformCorrToChol.computeJacobianMatrixInverse(cholValues);
    Matrix Jac = new Matrix(jacobianMat);
    System.out.println("Jacobian Matrix=" + Jac.transpose());
    assertEquals("size Jacobian Matrix", format.format(dim * (dim - 1) / 2), format.format(Jac.rows()));
    assertEquals("size Jacobian Matrix", format.format(dim * (dim - 1) / 2), format.format(Jac.columns()));
    // Determinant
    double jacobianDet = (new Transform.InverseMultivariate(transformCorrToChol)).getLogJacobian(cholValues, 0, cholValues.length);
    double jacobianDetBis = 0;
    for (int i = 0; i < jacobianMat[0].length; i++) {
        jacobianDetBis += Math.log(jacobianMat[i][i]);
    }
    System.out.println("Log Jacobiant Det direct=" + jacobianDet);
    System.out.println("Log Jacobiant Det matrix=" + jacobianDetBis);
    assertEquals("jacobian log det", format.format(jacobianDet), format.format(jacobianDetBis));
}
Also used : SymmetricMatrix.compoundCorrelationSymmetricMatrix(dr.math.matrixAlgebra.SymmetricMatrix.compoundCorrelationSymmetricMatrix) SymmetricMatrix(dr.math.matrixAlgebra.SymmetricMatrix) Matrix(dr.math.matrixAlgebra.Matrix)

Example 29 with Matrix

use of dr.math.matrixAlgebra.Matrix in project beast-mcmc by beast-dev.

the class LKJTransformTest method testJacobian.

public void testJacobian() {
    System.out.println("\nTest LKJ Jacobian.");
    // Matrix
    double[][] jacobianMat = transform.computeJacobianMatrixInverse(CPCs);
    Matrix Jac = new Matrix(jacobianMat);
    System.out.println("Jacobian Matrix=" + Jac.transpose());
    assertEquals("size Jacobian Matrix", format.format(dim * (dim - 1) / 2), format.format(Jac.rows()));
    assertEquals("size Jacobian Matrix", format.format(dim * (dim - 1) / 2), format.format(Jac.columns()));
    // Determinant
    double jacobianDet = (new Transform.InverseMultivariate(transform)).getLogJacobian(CPCs, 0, CPCs.length);
    double jacobianDetBis = 0;
    for (int i = 0; i < jacobianMat[0].length; i++) {
        jacobianDetBis += Math.log(jacobianMat[i][i]);
    }
    System.out.println("Log Jacobiant Det direct=" + jacobianDet);
    System.out.println("Log Jacobiant Det matrix=" + jacobianDetBis);
    assertEquals("jacobian log det", format.format(jacobianDet), format.format(jacobianDetBis));
}
Also used : SymmetricMatrix.compoundCorrelationSymmetricMatrix(dr.math.matrixAlgebra.SymmetricMatrix.compoundCorrelationSymmetricMatrix) SymmetricMatrix(dr.math.matrixAlgebra.SymmetricMatrix) Matrix(dr.math.matrixAlgebra.Matrix)

Example 30 with Matrix

use of dr.math.matrixAlgebra.Matrix in project beast-mcmc by beast-dev.

the class LKJTransformTest method testTransformationLimitRecursion.

public void testTransformationLimitRecursion() {
    System.out.println("\nTest LKJ transform on the border.");
    double[] transformedValue = transform.inverseRecursion(CPCsLimit, 0, CPCsLimit.length);
    double[] transformedValueChol = transform.inverse(CPCsLimit, 0, CPCsLimit.length);
    for (int k = 0; k < transformedValueChol.length; k++) {
        assertEquals("transform chol rec k=" + k, format.format(transformedValueChol[k]), format.format(transformedValue[k]));
    }
    double[] inverseTransformedValues = transform.transformRecursion(transformedValue, 0, CPCsLimit.length);
    SymmetricMatrix R = compoundCorrelationSymmetricMatrix(transformedValue, dim);
    ;
    System.out.println("transformedValue=" + R);
    try {
        assertTrue("Positive Definite", R.isPD());
    } catch (IllegalDimension illegalDimension) {
        illegalDimension.printStackTrace();
    }
    System.out.println("iCPC=" + new Matrix(inverseTransformedValues, dim * (dim - 1) / 2, 1));
    assertEquals("size CPCs", format.format(CPCsLimit.length), format.format(inverseTransformedValues.length));
    for (int k = 0; k < CPCsLimit.length; k++) {
        assertEquals("inverse transform k=" + k, format.format(CPCsLimit[k]), format.format(inverseTransformedValues[k]));
    }
}
Also used : SymmetricMatrix.compoundCorrelationSymmetricMatrix(dr.math.matrixAlgebra.SymmetricMatrix.compoundCorrelationSymmetricMatrix) SymmetricMatrix(dr.math.matrixAlgebra.SymmetricMatrix) Matrix(dr.math.matrixAlgebra.Matrix) IllegalDimension(dr.math.matrixAlgebra.IllegalDimension) SymmetricMatrix.compoundCorrelationSymmetricMatrix(dr.math.matrixAlgebra.SymmetricMatrix.compoundCorrelationSymmetricMatrix) SymmetricMatrix(dr.math.matrixAlgebra.SymmetricMatrix)

Aggregations

Matrix (dr.math.matrixAlgebra.Matrix)51 SymmetricMatrix (dr.math.matrixAlgebra.SymmetricMatrix)17 Vector (dr.math.matrixAlgebra.Vector)15 IllegalDimension (dr.math.matrixAlgebra.IllegalDimension)14 SymmetricMatrix.compoundCorrelationSymmetricMatrix (dr.math.matrixAlgebra.SymmetricMatrix.compoundCorrelationSymmetricMatrix)7 NodeRef (dr.evolution.tree.NodeRef)6 MultivariateNormalDistribution (dr.math.distributions.MultivariateNormalDistribution)5 WishartSufficientStatistics (dr.math.distributions.WishartSufficientStatistics)4 Parameter (dr.inference.model.Parameter)3 DoubleMatrix1D (cern.colt.matrix.DoubleMatrix1D)2 DoubleMatrix2D (cern.colt.matrix.DoubleMatrix2D)2 DenseDoubleMatrix2D (cern.colt.matrix.impl.DenseDoubleMatrix2D)2 Tree (dr.evolution.tree.Tree)2 MatrixParameter (dr.inference.model.MatrixParameter)2 RobustEigenDecomposition (dr.math.matrixAlgebra.RobustEigenDecomposition)2 WrappedMatrix (dr.math.matrixAlgebra.WrappedMatrix)2 BranchRates (dr.evolution.tree.BranchRates)1 MutableTreeModel (dr.evolution.tree.MutableTreeModel)1 CompoundSymmetricMatrix (dr.inference.model.CompoundSymmetricMatrix)1 CorrelationSymmetricMatrix (dr.inference.model.CorrelationSymmetricMatrix)1