Search in sources :

Example 6 with SymmetricMatrix

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

the class ModeIndependenceOperator method formXtXInverse.

private double[][] formXtXInverse(double[][] X) {
    int N = X.length;
    int P = X[0].length;
    double[][] matrix = new double[P][P];
    for (int i = 0; i < P; i++) {
        for (int j = 0; j < P; j++) {
            double total = 0.0;
            for (int k = 0; k < N; k++) {
                total += X[k][i] * X[k][j];
            }
            matrix[i][j] = total;
        }
    }
    //        System.err.println("XtX:");
    //        System.err.println(new Matrix(matrix));
    // Take inverse
    matrix = new SymmetricMatrix(matrix).inverse().toComponents();
    return matrix;
}
Also used : SymmetricMatrix(dr.math.matrixAlgebra.SymmetricMatrix)

Example 7 with SymmetricMatrix

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

the class PrecisionMatrixGibbsOperator method setupWishartStatistics.

private void setupWishartStatistics(WishartStatistics priorDistribution) {
    this.priorDf = priorDistribution.getDF();
    this.priorInverseScaleMatrix = null;
    double[][] scale = priorDistribution.getScaleMatrix();
    if (scale != null)
        this.priorInverseScaleMatrix = (SymmetricMatrix) (new SymmetricMatrix(scale)).inverse();
}
Also used : SymmetricMatrix(dr.math.matrixAlgebra.SymmetricMatrix)

Example 8 with SymmetricMatrix

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

the class GaussianProcessFromTree method nextRandomFast.

//    boolean firstTime=true;
public double[] nextRandomFast() {
    double[] random = new double[traitModel.getTreeModel().getExternalNodeCount() * traitModel.getDimTrait()];
    NodeRef root = traitModel.getTreeModel().getRoot();
    double[] traitStart = traitModel.getPriorMean();
    double[][] varianceCholesky = null;
    double[][] temp = new SymmetricMatrix(traitModel.getDiffusionModel().getPrecisionmatrix()).inverse().toComponents();
    try {
        varianceCholesky = (new CholeskyDecomposition(temp).getL());
    } catch (IllegalDimension illegalDimension) {
        illegalDimension.printStackTrace();
    }
    //        }
    if (USE_BUFFER) {
        final int length = traitModel.getDimTrait();
        final int nodeCount = traitModel.getTreeModel().getNodeCount();
        double[] currentValue = new double[(nodeCount + 1) * length];
        double[] epsilon = new double[length];
        final int priorOffset = nodeCount * length;
        System.arraycopy(traitStart, 0, currentValue, priorOffset, length);
        nextRandomFast2(currentValue, priorOffset, root, random, varianceCholesky, epsilon);
    } else {
        nextRandomFast(traitStart, root, random, varianceCholesky);
    }
    //        }
    return random;
}
Also used : CholeskyDecomposition(dr.math.matrixAlgebra.CholeskyDecomposition) NodeRef(dr.evolution.tree.NodeRef) IllegalDimension(dr.math.matrixAlgebra.IllegalDimension) SymmetricMatrix(dr.math.matrixAlgebra.SymmetricMatrix)

Example 9 with SymmetricMatrix

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

the class FactorTreeGibbsOperator method getMean.

double[] getMean(int column, double[][] precision) {
    Matrix variance = (new SymmetricMatrix(precision)).inverse();
    double[] midMean = new double[lfm.getLoadings().getColumnDimension()];
    double[] condMean = getTreeMean(column);
    double[][] condPrec = getTreePrec(column);
    for (int i = 0; i < midMean.length; i++) {
        //            for (int j = 0; j < midMean.length; j++) {
        midMean[i] += condPrec[i][i] * condMean[i];
    //            }
    }
    for (int i = 0; i < lfm.getLoadings().getRowDimension(); i++) {
        for (int j = 0; j < lfm.getLoadings().getColumnDimension(); j++) {
            if (missingIndicator == null || missingIndicator.getParameterValue(column * lfm.getScaledData().getRowDimension() + i) != 1)
                midMean[j] += lfm.getScaledData().getParameterValue(i, column) * errorPrec.getParameterValue(i, i) * lfm.getLoadings().getParameterValue(i, j) * pathParameter;
        }
    }
    double[] mean = new double[midMean.length];
    for (int i = 0; i < mean.length; i++) {
        for (int j = 0; j < mean.length; j++) {
            mean[i] += variance.component(i, j) * midMean[j];
        }
    }
    return mean;
}
Also used : SymmetricMatrix(dr.math.matrixAlgebra.SymmetricMatrix) Matrix(dr.math.matrixAlgebra.Matrix) SymmetricMatrix(dr.math.matrixAlgebra.SymmetricMatrix)

Example 10 with SymmetricMatrix

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

the class FactorIndependenceOperator method doOperation.

@Override
public double doOperation() {
    setupParameters();
    getPrecision(precision);
    double[][] variance = (new SymmetricMatrix(precision)).inverse().toComponents();
    if (randomScan) {
        int i = MathUtils.nextInt(LFM.getFactors().getColumnDimension());
        randomDraw(i, variance);
    }
    for (int i = 0; i < LFM.getFactors().getColumnDimension(); i++) {
        randomDraw(i, variance);
    }
    LFM.getFactors().fireParameterChangedEvent();
    //To change body of implemented methods use File | Settings | File Templates.
    return 0;
}
Also used : SymmetricMatrix(dr.math.matrixAlgebra.SymmetricMatrix)

Aggregations

SymmetricMatrix (dr.math.matrixAlgebra.SymmetricMatrix)12 NodeRef (dr.evolution.tree.NodeRef)2 NormalDistribution (dr.math.distributions.NormalDistribution)2 IllegalDimension (dr.math.matrixAlgebra.IllegalDimension)2 MomentDistributionModel (dr.inference.distribution.MomentDistributionModel)1 CholeskyDecomposition (dr.math.matrixAlgebra.CholeskyDecomposition)1 Matrix (dr.math.matrixAlgebra.Matrix)1