| 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.evolution.tree; |
| 27 |
|
| 28 |
import dr.math.ConjugateDirectionSearch; |
| 29 |
import dr.math.MultivariateFunction; |
| 30 |
import dr.math.MultivariateMinimum; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class LeastSquaresClockTree extends SimpleTree { |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public LeastSquaresClockTree(Tree sourceTree) { |
| 45 |
|
| 46 |
super(sourceTree); |
| 47 |
this.sourceTree = sourceTree; |
| 48 |
this.mu = 1.0; |
| 49 |
this.optimizeMu = true; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public LeastSquaresClockTree(Tree sourceTree, double mu) { |
| 56 |
|
| 57 |
this.sourceTree = sourceTree; |
| 58 |
this.mu = mu; |
| 59 |
this.optimizeMu = false; |
| 60 |
} |
| 61 |
|
| 62 |
public double getMu() { return mu; } |
| 63 |
|
| 64 |
public void optimize() { |
| 65 |
nodeCount = getInternalNodeCount(); |
| 66 |
int argumentCount = nodeCount; |
| 67 |
|
| 68 |
if (optimizeMu) { |
| 69 |
argumentCount++; |
| 70 |
muIndex = nodeCount; |
| 71 |
} |
| 72 |
|
| 73 |
MultivariateMinimum optimizer = new ConjugateDirectionSearch(); |
| 74 |
|
| 75 |
nodeValues = new double[nodeCount]; |
| 76 |
|
| 77 |
double[] xvec = new double[argumentCount]; |
| 78 |
for (int i = 0; i < nodeCount; i++) { |
| 79 |
xvec[i] = 1.0; |
| 80 |
} |
| 81 |
if (optimizeMu) { |
| 82 |
xvec[muIndex] = mu; |
| 83 |
} |
| 84 |
|
| 85 |
optimizer.optimize(leastSquaresClock, xvec, 1E-8, 1E-8); |
| 86 |
} |
| 87 |
|
| 88 |
public double getSumOfSquares() { |
| 89 |
|
| 90 |
double[] score = new double[] { 0.0 }; |
| 91 |
|
| 92 |
NodeRef root = getRoot(); |
| 93 |
|
| 94 |
if (getChildCount(root) != 2) { |
| 95 |
throw new IllegalArgumentException("The tree must have a bifurcating root node"); |
| 96 |
} |
| 97 |
|
| 98 |
NodeRef node1 = getChild(root, 0); |
| 99 |
NodeRef node2 = getChild(root, 1); |
| 100 |
|
| 101 |
if (!isExternal(node1)) { |
| 102 |
getSumOfSquaresAtNode(node1, score); |
| 103 |
} |
| 104 |
if (!isExternal(node2)) { |
| 105 |
getSumOfSquaresAtNode(node2, score); |
| 106 |
} |
| 107 |
|
| 108 |
double dist1 = sourceTree.getBranchLength(sourceTree.getNode(node1.getNumber())) + |
| 109 |
sourceTree.getBranchLength(sourceTree.getNode(node2.getNumber())); |
| 110 |
|
| 111 |
double time = getNodeHeight(root) - getNodeHeight(node1) + |
| 112 |
getNodeHeight(root) - getNodeHeight(node2); |
| 113 |
double dist2 = time * mu; |
| 114 |
|
| 115 |
double diff = dist1 - dist2; |
| 116 |
|
| 117 |
score[0] += diff * diff; |
| 118 |
|
| 119 |
return score[0]; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
private void getSumOfSquaresAtNode(NodeRef node, double[] score) { |
| 127 |
|
| 128 |
if (!isExternal(node)) { |
| 129 |
for (int i = 0; i < getChildCount(node); i++) { |
| 130 |
NodeRef child = getChild(node, i); |
| 131 |
if (!isExternal(child)) { |
| 132 |
getSumOfSquaresAtNode(child, score); |
| 133 |
score[0] += getScoreAtNode(child); |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
private double getScoreAtNode(NodeRef node) { |
| 143 |
double dist1 = sourceTree.getBranchLength(sourceTree.getNode(node.getNumber())); |
| 144 |
|
| 145 |
double time = getNodeHeight(getParent(node)) - getNodeHeight(node); |
| 146 |
double dist2 = time * mu; |
| 147 |
|
| 148 |
double diff = dist1 - dist2; |
| 149 |
|
| 150 |
return diff * diff; |
| 151 |
} |
| 152 |
|
| 153 |
private double setNodeHeightsFromValues(NodeRef node) { |
| 154 |
|
| 155 |
if (!isExternal(node)) { |
| 156 |
double maxHeight = setNodeHeightsFromValues(getChild(node, 0)); |
| 157 |
|
| 158 |
for (int i = 1; i < getChildCount(node); i++) { |
| 159 |
double height = setNodeHeightsFromValues(getChild(node, i)); |
| 160 |
if (height > maxHeight) maxHeight = height; |
| 161 |
} |
| 162 |
|
| 163 |
setNodeHeight(node, maxHeight + nodeValues[node.getNumber() - getExternalNodeCount()]); |
| 164 |
} |
| 165 |
|
| 166 |
return getNodeHeight(node); |
| 167 |
} |
| 168 |
|
| 169 |
private MultivariateFunction leastSquaresClock = new MultivariateFunction() { |
| 170 |
|
| 171 |
public double evaluate(double[] argument) { |
| 172 |
for (int i = 0; i < getInternalNodeCount(); i++) { |
| 173 |
nodeValues[i] = argument[i]; |
| 174 |
} |
| 175 |
|
| 176 |
setNodeHeightsFromValues(getRoot()); |
| 177 |
|
| 178 |
if (optimizeMu) { |
| 179 |
mu = argument[muIndex]; |
| 180 |
} |
| 181 |
|
| 182 |
double score = getSumOfSquares(); |
| 183 |
return score; |
| 184 |
} |
| 185 |
|
| 186 |
public int getNumArguments() { |
| 187 |
if (optimizeMu) { |
| 188 |
return getInternalNodeCount() + 1; |
| 189 |
} else { |
| 190 |
return getInternalNodeCount(); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
public double getLowerBound(int n) { |
| 195 |
|
| 196 |
if (optimizeMu && n == muIndex) { |
| 197 |
return Double.MIN_VALUE; |
| 198 |
} else { |
| 199 |
return 0.0; |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
public double getUpperBound(int n) { |
| 204 |
|
| 205 |
if (optimizeMu && n == muIndex) { |
| 206 |
return Double.MAX_VALUE; |
| 207 |
} else { |
| 208 |
return Double.MAX_VALUE; |
| 209 |
} |
| 210 |
} |
| 211 |
}; |
| 212 |
|
| 213 |
private int nodeCount; |
| 214 |
private double[] nodeValues; |
| 215 |
private Tree sourceTree; |
| 216 |
private double mu; |
| 217 |
private boolean optimizeMu; |
| 218 |
private int muIndex; |
| 219 |
} |