Search in sources :

Example 16 with MutableTreeModel

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

the class MultivariateTraitUtils method computeTreeVariance.

public static double[][] computeTreeVariance(FullyConjugateMultivariateTraitLikelihood trait, boolean conditionOnRoot) {
    MutableTreeModel 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) MutableTreeModel(dr.evolution.tree.MutableTreeModel)

Example 17 with MutableTreeModel

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

the class DiffusionRateCovarianceStatistic method getStatisticValue.

/**
 * @return whatever Philippe wants
 */
public double getStatisticValue(int dim) {
    String traitName = traitLikelihoods.get(0).getTraitName();
    for (AbstractMultivariateTraitLikelihood traitLikelihood : traitLikelihoods) {
        MutableTreeModel tree = traitLikelihood.getTreeModel();
        int counter = 0;
        int index = 0;
        for (int i = 0; i < tree.getNodeCount(); i++) {
            NodeRef child = tree.getNode(i);
            NodeRef parent = tree.getParent(child);
            if (parent != null & !tree.isRoot(parent)) {
                double[] childTrait = traitLikelihood.getTraitForNode(tree, child, traitName);
                double[] parentTrait = traitLikelihood.getTraitForNode(tree, parent, traitName);
                double childTime = tree.getBranchLength(child);
                double parentTime = tree.getBranchLength(parent);
                NodeRef grandParent = tree.getParent(parent);
                double[] grandParentTrait = traitLikelihood.getTraitForNode(tree, grandParent, traitName);
                if (useGreatCircleDistances && (childTrait.length == 2)) {
                    // Great Circle distance
                    SphericalPolarCoordinates coord1 = new SphericalPolarCoordinates(childTrait[0], childTrait[1]);
                    SphericalPolarCoordinates coord2 = new SphericalPolarCoordinates(parentTrait[0], parentTrait[1]);
                    double childDistance = coord1.distance(coord2);
                    SphericalPolarCoordinates coord3 = new SphericalPolarCoordinates(grandParentTrait[0], grandParentTrait[1]);
                    double parentDistance = coord2.distance(coord3);
                    if (!diffusionCoefficient) {
                        childRate[index] = childDistance / childTime;
                        parentRate[index] = parentDistance / parentTime;
                    } else {
                        childRate[index] = Math.pow(childDistance, 2) / (4 * childTime);
                        parentRate[index] = Math.pow(parentDistance, 2) / (4 * parentTime);
                    }
                } else {
                    double childDistance = getNativeDistance(childTrait, parentTrait);
                    double parentDistance = getNativeDistance(parentTrait, grandParentTrait);
                    if (!diffusionCoefficient) {
                        childRate[index] = childDistance / childTime;
                        parentRate[index] = parentDistance / parentTime;
                    } else {
                        childRate[index] = Math.pow(childDistance, 2) / (4 * childTime);
                        parentRate[index] = Math.pow(parentDistance, 2) / (4 * parentTime);
                    }
                }
                index += 1;
            }
        }
    }
    return DiscreteStatistics.covariance(childRate, parentRate);
}
Also used : SphericalPolarCoordinates(dr.geo.math.SphericalPolarCoordinates) NodeRef(dr.evolution.tree.NodeRef) MutableTreeModel(dr.evolution.tree.MutableTreeModel)

Aggregations

MutableTreeModel (dr.evolution.tree.MutableTreeModel)17 NodeRef (dr.evolution.tree.NodeRef)9 BranchRateModel (dr.evomodel.branchratemodel.BranchRateModel)4 Parameter (dr.inference.model.Parameter)4 SphericalPolarCoordinates (dr.geo.math.SphericalPolarCoordinates)3 ArrayList (java.util.ArrayList)3 Patterns (dr.evolution.alignment.Patterns)2 TreeUtils (dr.evolution.tree.TreeUtils)2 BranchModel (dr.evomodel.branchmodel.BranchModel)2 EpochBranchModel (dr.evomodel.branchmodel.EpochBranchModel)2 GammaSiteRateModel (dr.evomodel.siteratemodel.GammaSiteRateModel)2 SubstitutionModel (dr.evomodel.substmodel.SubstitutionModel)2 BeagleTreeLikelihood (dr.evomodel.treelikelihood.BeagleTreeLikelihood)2 PartialsRescalingScheme (dr.evomodel.treelikelihood.PartialsRescalingScheme)2 Set (java.util.Set)2 PatternList (dr.evolution.alignment.PatternList)1 SitePatterns (dr.evolution.alignment.SitePatterns)1 TaxonList (dr.evolution.util.TaxonList)1 HomogeneousBranchModel (dr.evomodel.branchmodel.HomogeneousBranchModel)1 AncestralTaxonInTree (dr.evomodel.continuous.AncestralTaxonInTree)1