| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.evomodel.treedatalikelihood.continuous; |
| 27 |
|
| 28 |
import dr.evolution.tree.NodeRef; |
| 29 |
import dr.evolution.tree.Tree; |
| 30 |
import dr.evolution.tree.TreeUtils; |
| 31 |
|
| 32 |
import java.util.HashSet; |
| 33 |
import java.util.Set; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class MultivariateTraitDebugUtilities { |
| 39 |
|
| 40 |
public static double getLengthToRoot(final Tree tree, final NodeRef nodeRef) { |
| 41 |
|
| 42 |
double length = 0; |
| 43 |
|
| 44 |
if (!tree.isRoot(nodeRef)) { |
| 45 |
NodeRef parent = tree.getParent(nodeRef); |
| 46 |
length += tree.getBranchLength(nodeRef) + getLengthToRoot(tree, parent); |
| 47 |
} |
| 48 |
|
| 49 |
return length; |
| 50 |
} |
| 51 |
|
| 52 |
private static NodeRef findMRCA(final Tree tree, final int iTip, final int jTip) { |
| 53 |
Set<String> leafNames = new HashSet<String>(); |
| 54 |
leafNames.add(tree.getTaxonId(iTip)); |
| 55 |
leafNames.add(tree.getTaxonId(jTip)); |
| 56 |
return TreeUtils.getCommonAncestorNode(tree, leafNames); |
| 57 |
} |
| 58 |
|
| 59 |
public static void insertPrecision(Tree tree, NodeRef parent, NodeRef child, double[][] matrix, double normalization) { |
| 60 |
matrix[parent.getNumber()][child.getNumber()] = |
| 61 |
matrix[child.getNumber()][parent.getNumber()] =-1.0 / (tree.getBranchLength(child) * normalization); |
| 62 |
recurseGraph(tree, child, matrix, normalization); |
| 63 |
|
| 64 |
} |
| 65 |
|
| 66 |
public static void recurseGraph(Tree tree, NodeRef node, double[][] matrix, double normalization) { |
| 67 |
if (!tree.isExternal(node)) { |
| 68 |
insertPrecision(tree, node, tree.getChild(node, 0), matrix, normalization); |
| 69 |
insertPrecision(tree, node, tree.getChild(node, 1), matrix, normalization); |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
public static double[][] getGraphVariance(final Tree tree, final double normalization, final double priorSampleSize) { |
| 74 |
|
| 75 |
final int nodeCount = tree.getNodeCount(); |
| 76 |
|
| 77 |
final double[][] variance = new double[nodeCount][nodeCount]; |
| 78 |
|
| 79 |
for (int i = 0; i < nodeCount; i++) { |
| 80 |
|
| 81 |
|
| 82 |
double marginalTime = getLengthToRoot(tree, tree.getNode(i)) * normalization; |
| 83 |
variance[i][i] = marginalTime; |
| 84 |
|
| 85 |
|
| 86 |
for (int j = i + 1; j < nodeCount; j++) { |
| 87 |
NodeRef mrca = TreeUtils.getCommonAncestorSafely(tree, tree.getNode(i), tree.getNode(j)); |
| 88 |
variance[i][j] = getLengthToRoot(tree, mrca) * normalization; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
makeSymmetric(variance); |
| 93 |
|
| 94 |
addPrior(variance, priorSampleSize); |
| 95 |
|
| 96 |
return variance; |
| 97 |
} |
| 98 |
|
| 99 |
public static double[][] getTreeVariance(final Tree tree, final double normalization, final double priorSampleSize) { |
| 100 |
|
| 101 |
final int tipCount = tree.getExternalNodeCount(); |
| 102 |
|
| 103 |
double[][] variance = new double[tipCount][tipCount]; |
| 104 |
|
| 105 |
for (int i = 0; i < tipCount; i++) { |
| 106 |
|
| 107 |
|
| 108 |
double marginalTime = getLengthToRoot(tree, tree.getExternalNode(i)) * normalization; |
| 109 |
variance[i][i] = marginalTime; |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
for (int j = i + 1; j < tipCount; j++) { |
| 114 |
NodeRef mrca = findMRCA(tree, i, j); |
| 115 |
variance[i][j] = getLengthToRoot(tree, mrca) * normalization; |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
makeSymmetric(variance); |
| 120 |
|
| 121 |
addPrior(variance, priorSampleSize); |
| 122 |
|
| 123 |
return variance; |
| 124 |
} |
| 125 |
|
| 126 |
private static void makeSymmetric(final double[][] variance) { |
| 127 |
for (int i = 0; i < variance.length; i++) { |
| 128 |
for (int j = i + 1; j < variance[i].length; j++) { |
| 129 |
variance[j][i] = variance[i][j]; |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
private static void addPrior(final double[][] variance, double priorSampleSize) { |
| 135 |
if (!Double.isInfinite(priorSampleSize)) { |
| 136 |
for (int i = 0; i < variance.length; ++i) { |
| 137 |
for (int j = 0; j < variance[i].length; ++j) { |
| 138 |
variance[i][j] += 1.0 / priorSampleSize; |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
public static double[][] getTreeDrift(Tree tree, DiffusionProcessDelegate diffusion) { |
| 145 |
|
| 146 |
final int dim = diffusion.getDiffusionModel(0).getPrecisionParameter().getColumnDimension(); |
| 147 |
final double[][] drift = new double[tree.getExternalNodeCount()][dim]; |
| 148 |
|
| 149 |
if (diffusion instanceof DriftDiffusionModelDelegate) { |
| 150 |
for (int tip = 0; tip < tree.getExternalNodeCount(); ++tip) { |
| 151 |
drift[tip] = ((DriftDiffusionModelDelegate) diffusion).getAccumulativeDrift( |
| 152 |
tree.getExternalNode(tip)); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
return drift; |
| 157 |
} |
| 158 |
|
| 159 |
public static double[][] getGraphDrift(Tree tree, DiffusionProcessDelegate diffusion) { |
| 160 |
|
| 161 |
final int dim = diffusion.getDiffusionModel(0).getPrecisionParameter().getColumnDimension(); |
| 162 |
final double[][] drift = new double[tree.getNodeCount()][dim]; |
| 163 |
|
| 164 |
if (diffusion instanceof DriftDiffusionModelDelegate) { |
| 165 |
for (int node = 0; node < tree.getNodeCount(); ++node) { |
| 166 |
drift[node] = ((DriftDiffusionModelDelegate) diffusion).getAccumulativeDrift( |
| 167 |
tree.getNode(node)); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
return drift; |
| 172 |
} |
| 173 |
} |