Search in sources :

Example 11 with MultivariateTraitTree

use of dr.evolution.tree.MultivariateTraitTree in project beast-mcmc by beast-dev.

the class MultivariateTraitUtils method computeTreeTraitMean.

public static double[] computeTreeTraitMean(FullyConjugateMultivariateTraitLikelihood trait, double[] rootValue, boolean conditionOnRoot) {
    double[] root = trait.getPriorMean();
    if (conditionOnRoot) {
        System.err.println("WARNING: Not yet fully implemented (conditioning on root in simulator)");
        //root = new double[root.length];
        root = rootValue;
    }
    final int nTaxa = trait.getTreeModel().getExternalNodeCount();
    double[] mean = new double[root.length * nTaxa];
    for (int i = 0; i < nTaxa; ++i) {
        System.arraycopy(root, 0, mean, i * root.length, root.length);
    }
    if (trait.driftModels != null) {
        MultivariateTraitTree myTreeModel = trait.getTreeModel();
        for (int i = 0; i < nTaxa; ++i) {
            double[] shiftContribution = getShiftContributionToMean(myTreeModel.getExternalNode(i), trait);
            for (int j = 0; j < trait.dimTrait; ++j) {
                mean[i * trait.dimTrait + j] = mean[i * trait.dimTrait + j] + shiftContribution[j];
            }
        }
    }
    return mean;
}
Also used : MultivariateTraitTree(dr.evolution.tree.MultivariateTraitTree)

Example 12 with MultivariateTraitTree

use of dr.evolution.tree.MultivariateTraitTree in project beast-mcmc by beast-dev.

the class MultivariateTraitUtils method computeTreeVariance.

public static double[][] computeTreeVariance(FullyConjugateMultivariateTraitLikelihood trait, boolean conditionOnRoot) {
    MultivariateTraitTree treeModel = trait.getTreeModel();
    final int tipCount = treeModel.getExternalNodeCount();
    double[][] variance = new double[tipCount][tipCount];
    for (int i = 0; i < tipCount; i++) {
        // Fill in diagonal
        double marginalTime = trait.getRescaledLengthToRoot(treeModel.getExternalNode(i));
        variance[i][i] = marginalTime;
        for (int j = i + 1; j < tipCount; j++) {
            NodeRef mrca = findMRCA(trait, i, j);
            if (DEBUG) {
                System.err.println(trait.getTreeModel().getRoot().getNumber());
                System.err.print("Taxa pair: " + i + " : " + j + " (" + mrca.getNumber() + ") = ");
            }
            double length = trait.getRescaledLengthToRoot(mrca);
            if (DEBUG) {
                System.err.println(length);
            }
            variance[i][j] = length;
        }
    }
    // Make symmetric
    for (int i = 0; i < tipCount; i++) {
        for (int j = i + 1; j < tipCount; j++) {
            variance[j][i] = variance[i][j];
        }
    }
    if (DEBUG) {
        System.err.println("");
        System.err.println("New tree conditional variance:\n" + new Matrix(variance));
    }
    if (!conditionOnRoot) {
        double priorSampleSize = trait.getPriorSampleSize();
        for (int i = 0; i < tipCount; ++i) {
            for (int j = 0; j < tipCount; ++j) {
                variance[i][j] += 1.0 / priorSampleSize;
            }
        }
        if (DEBUG) {
            System.err.println("");
            System.err.println("New tree unconditional variance:\n" + new Matrix(variance));
        }
    }
    return variance;
}
Also used : NodeRef(dr.evolution.tree.NodeRef) SymmetricMatrix(dr.math.matrixAlgebra.SymmetricMatrix) Matrix(dr.math.matrixAlgebra.Matrix) MultivariateTraitTree(dr.evolution.tree.MultivariateTraitTree)

Aggregations

MultivariateTraitTree (dr.evolution.tree.MultivariateTraitTree)12 NodeRef (dr.evolution.tree.NodeRef)8 SphericalPolarCoordinates (dr.geo.math.SphericalPolarCoordinates)3 BranchRateModel (dr.evomodel.branchratemodel.BranchRateModel)2 TreeUtils (dr.evolution.tree.TreeUtils)1 TreeTraitParserUtilities (dr.evomodelxml.treelikelihood.TreeTraitParserUtilities)1 MatrixParameter (dr.inference.model.MatrixParameter)1 Parameter (dr.inference.model.Parameter)1 Matrix (dr.math.matrixAlgebra.Matrix)1 SymmetricMatrix (dr.math.matrixAlgebra.SymmetricMatrix)1 Regression (dr.stats.Regression)1 HashSet (java.util.HashSet)1