Search in sources :

Example 1 with MutableTreeModel

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

the class DiffusionRateStatistic method getStatisticValue.

public double getStatisticValue(int dim) {
    String traitName = traitLikelihoods.get(0).getTraitName();
    double treelength = 0;
    double treeDistance = 0;
    double maxDistanceFromRoot = 0;
    double maxDistanceOverTimeFromRoot = 0;
    // double[] rates =  null;
    List<Double> rates = new ArrayList<Double>();
    // double[] diffusionCoefficients =  null;
    List<Double> diffusionCoefficients = new ArrayList<Double>();
    double waDiffusionCoefficient = 0;
    double lowerHeight = heightLowers[dim];
    double upperHeight = Double.MAX_VALUE;
    if (heightLowers.length == 1) {
        upperHeight = heightUpper;
    } else {
        if (dim > 0) {
            if (!cumulative) {
                upperHeight = heightLowers[dim - 1];
            }
        }
    }
    for (AbstractMultivariateTraitLikelihood traitLikelihood : traitLikelihoods) {
        MutableTreeModel tree = traitLikelihood.getTreeModel();
        BranchRateModel branchRates = traitLikelihood.getBranchRateModel();
        for (int i = 0; i < tree.getNodeCount(); i++) {
            NodeRef node = tree.getNode(i);
            if (node != tree.getRoot()) {
                NodeRef parentNode = tree.getParent(node);
                if ((tree.getNodeHeight(parentNode) > lowerHeight) && (tree.getNodeHeight(node) < upperHeight)) {
                    double[] trait = traitLikelihood.getTraitForNode(tree, node, traitName);
                    double[] parentTrait = traitLikelihood.getTraitForNode(tree, parentNode, traitName);
                    double[] traitUp = parentTrait;
                    double[] traitLow = trait;
                    double timeUp = tree.getNodeHeight(parentNode);
                    double timeLow = tree.getNodeHeight(node);
                    double rate = (branchRates != null ? branchRates.getBranchRate(tree, node) : 1.0);
                    MultivariateDiffusionModel diffModel = traitLikelihood.diffusionModel;
                    double[] precision = diffModel.getPrecisionParameter().getParameterValues();
                    if (tree.getNodeHeight(parentNode) > upperHeight) {
                        timeUp = upperHeight;
                        // TODO: implement TrueNoise??
                        traitUp = imputeValue(trait, parentTrait, upperHeight, tree.getNodeHeight(node), tree.getNodeHeight(parentNode), precision, rate, false);
                    }
                    if (tree.getNodeHeight(node) < lowerHeight) {
                        timeLow = lowerHeight;
                        traitLow = imputeValue(trait, parentTrait, lowerHeight, tree.getNodeHeight(node), tree.getNodeHeight(parentNode), precision, rate, false);
                    }
                    double time = timeUp - timeLow;
                    treelength += time;
                    double[] rootTrait = traitLikelihood.getTraitForNode(tree, tree.getRoot(), traitName);
                    if (useGreatCircleDistances && (trait.length == 2)) {
                        // Great Circle distance
                        SphericalPolarCoordinates coord1 = new SphericalPolarCoordinates(traitLow[0], traitLow[1]);
                        SphericalPolarCoordinates coord2 = new SphericalPolarCoordinates(traitUp[0], traitUp[1]);
                        double distance = coord1.distance(coord2);
                        treeDistance += distance;
                        double dc = Math.pow(distance, 2) / (4 * time);
                        diffusionCoefficients.add(dc);
                        waDiffusionCoefficient += dc * time;
                        rates.add(distance / time);
                        SphericalPolarCoordinates rootCoord = new SphericalPolarCoordinates(rootTrait[0], rootTrait[1]);
                        double tempDistanceFromRoot = rootCoord.distance(coord2);
                        if (tempDistanceFromRoot > maxDistanceFromRoot) {
                            maxDistanceFromRoot = tempDistanceFromRoot;
                            maxDistanceOverTimeFromRoot = tempDistanceFromRoot / (tree.getNodeHeight(tree.getRoot()) - timeLow);
                            // distance between traitLow and traitUp for maxDistanceFromRoot
                            if (timeUp == upperHeight) {
                                maxDistanceFromRoot = distance;
                                maxDistanceOverTimeFromRoot = distance / time;
                            }
                        }
                    } else {
                        double distance = getNativeDistance(traitLow, traitUp);
                        treeDistance += distance;
                        double dc = Math.pow(distance, 2) / (4 * time);
                        diffusionCoefficients.add(dc);
                        waDiffusionCoefficient += dc * time;
                        rates.add(distance / time);
                        double tempDistanceFromRoot = getNativeDistance(traitLow, rootTrait);
                        if (tempDistanceFromRoot > maxDistanceFromRoot) {
                            maxDistanceFromRoot = tempDistanceFromRoot;
                            maxDistanceOverTimeFromRoot = tempDistanceFromRoot / (tree.getNodeHeight(tree.getRoot()) - timeLow);
                            // distance between traitLow and traitUp for maxDistanceFromRoot
                            if (timeUp == upperHeight) {
                                maxDistanceFromRoot = distance;
                                maxDistanceOverTimeFromRoot = distance / time;
                            }
                        }
                    }
                }
            }
        }
    }
    if (summaryStat == summaryStatistic.DIFFUSION_RATE) {
        if (summaryMode == Mode.AVERAGE) {
            return DiscreteStatistics.mean(toArray(rates));
        } else if (summaryMode == Mode.MEDIAN) {
            return DiscreteStatistics.median(toArray(rates));
        } else if (summaryMode == Mode.COEFFICIENT_OF_VARIATION) {
            // don't compute mean twice
            final double mean = DiscreteStatistics.mean(toArray(rates));
            return Math.sqrt(DiscreteStatistics.variance(toArray(rates), mean)) / mean;
        } else {
            return treeDistance / treelength;
        }
    } else if (summaryStat == summaryStatistic.DIFFUSION_COEFFICIENT) {
        if (summaryMode == Mode.AVERAGE) {
            return DiscreteStatistics.mean(toArray(diffusionCoefficients));
        } else if (summaryMode == Mode.MEDIAN) {
            return DiscreteStatistics.median(toArray(diffusionCoefficients));
        } else if (summaryMode == Mode.COEFFICIENT_OF_VARIATION) {
            // don't compute mean twice
            final double mean = DiscreteStatistics.mean(toArray(diffusionCoefficients));
            return Math.sqrt(DiscreteStatistics.variance(toArray(diffusionCoefficients), mean)) / mean;
        } else {
            return waDiffusionCoefficient / treelength;
        }
    } else if (summaryStat == summaryStatistic.WAVEFRONT_DISTANCE) {
        return maxDistanceFromRoot;
    } else {
        return maxDistanceOverTimeFromRoot;
    }
}
Also used : SphericalPolarCoordinates(dr.geo.math.SphericalPolarCoordinates) NodeRef(dr.evolution.tree.NodeRef) BranchRateModel(dr.evomodel.branchratemodel.BranchRateModel) MutableTreeModel(dr.evolution.tree.MutableTreeModel)

Example 2 with MutableTreeModel

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

the class MultivariateTraitUtils method findMRCA.

public static NodeRef findMRCA(FullyConjugateMultivariateTraitLikelihood trait, int iTip, int jTip) {
    MutableTreeModel treeModel = trait.getTreeModel();
    Set<String> leafNames = new HashSet<String>();
    leafNames.add(treeModel.getTaxonId(iTip));
    leafNames.add(treeModel.getTaxonId(jTip));
    return TreeUtils.getCommonAncestorNode(treeModel, leafNames);
}
Also used : MutableTreeModel(dr.evolution.tree.MutableTreeModel) HashSet(java.util.HashSet)

Example 3 with MutableTreeModel

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

the class MultivariateTraitUtils method computeLinCombMatrix.

private static double[][] computeLinCombMatrix(FullyConjugateMultivariateTraitLikelihood trait) {
    MutableTreeModel treeModel = trait.getTreeModel();
    final int tipCount = treeModel.getExternalNodeCount();
    final int branchCount = 2 * tipCount - 2;
    double[][] linCombMatrix = new double[tipCount][branchCount];
    double tempScalar;
    NodeRef tempNode;
    int tempNodeIndex;
    for (int k = 0; k < tipCount; k++) {
        tempNode = treeModel.getExternalNode(k);
        // check if treeModel.getExternalNode(k).getNumber() == k
        tempScalar = 1;
        tempNodeIndex = k;
        for (int r = 0; r < branchCount; r++) {
            if (r == tempNodeIndex) {
                linCombMatrix[k][r] = tempScalar;
                // tempScalar = tempScalar * (1 - treeModel.getBranchLength(tempNode) * trait.getTimeScaledSelection(tempNode));
                tempScalar = tempScalar * Math.exp(-trait.getTimeScaledSelection(tempNode));
                tempNode = treeModel.getParent(tempNode);
                tempNodeIndex = tempNode.getNumber();
            } else {
                linCombMatrix[k][r] = 0;
            }
        }
    }
    return linCombMatrix;
}
Also used : NodeRef(dr.evolution.tree.NodeRef) MutableTreeModel(dr.evolution.tree.MutableTreeModel)

Example 4 with MutableTreeModel

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

the class MultivariateTraitUtils method computeTreeVarianceOU.

public static double[][] computeTreeVarianceOU(FullyConjugateMultivariateTraitLikelihood trait, boolean conditionOnRoot) {
    MutableTreeModel treeModel = trait.getTreeModel();
    final int tipCount = treeModel.getExternalNodeCount();
    final int branchCount = 2 * tipCount - 2;
    double[][] variance = new double[tipCount][tipCount];
    double[][] tempMatrix = new double[tipCount][branchCount];
    double[][] diagMatrix = new double[branchCount][branchCount];
    /*
        double tempScalar;
        NodeRef tempNode;
        int tempNodeIndex;

        for(int k = 0; k < tipCount; k++){
           tempNode = treeModel.getExternalNode(k);
            //check if treeModel.getExternalNode(k).getNumber() == k
           tempScalar = 1;
           tempNodeIndex = k;

           for(int r = 0; r < branchCount; r++){
               if(r == tempNodeIndex){
                   tempMatrix[k][r] = tempScalar;
                   tempScalar = tempScalar*(1-treeModel.getBranchLength(tempNode)*trait.getTimeScaledSelection(tempNode));
                   tempNode = treeModel.getParent(tempNode);
                   tempNodeIndex = tempNode.getNumber();
               }else{
                   tempMatrix[k][r]= 0;
               }

           }
        }
        */
    tempMatrix = computeLinCombMatrix(trait);
    for (int i = 0; i < branchCount; i++) {
        // diagMatrix[i][i] = treeModel.getBranchLength(treeModel.getNode(i));
        // diagMatrix[i][i] = (2*strengthOfSelection.getBranchRate(treeModel, node) ) / (1 - Math.exp(-2*getTimeScaledSelection(node)));
        diagMatrix[i][i] = (1 - Math.exp(-2 * trait.getTimeScaledSelection(treeModel.getNode(i)))) / (2 * trait.strengthOfSelection.getBranchRate(treeModel, treeModel.getNode(i)));
    }
    variance = productMatrices(productMatrices(tempMatrix, diagMatrix), transposeMatrix(tempMatrix));
    return variance;
}
Also used : MutableTreeModel(dr.evolution.tree.MutableTreeModel)

Example 5 with MutableTreeModel

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

the class MultivariateTraitUtils method computeRootMultipliers.

private static double[] computeRootMultipliers(FullyConjugateMultivariateTraitLikelihood trait) {
    MutableTreeModel myTreeModel = trait.getTreeModel();
    final int tipCount = myTreeModel.getExternalNodeCount();
    double[] multiplierVect = new double[tipCount];
    NodeRef tempNode;
    for (int i = 0; i < tipCount; i++) {
        tempNode = myTreeModel.getExternalNode(i);
        multiplierVect[i] = Math.exp(-trait.getTimeScaledSelection(tempNode));
        tempNode = myTreeModel.getParent(tempNode);
        while (!myTreeModel.isRoot(tempNode)) {
            multiplierVect[i] = multiplierVect[i] * Math.exp(-trait.getTimeScaledSelection(tempNode));
            tempNode = myTreeModel.getParent(tempNode);
        }
    }
    return multiplierVect;
}
Also used : 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