| 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 |
|
| 27 |
|
| 28 |
|
| 29 |
package dr.evomodel.operators; |
| 30 |
|
| 31 |
import dr.evolution.tree.NodeRef; |
| 32 |
import dr.evolution.tree.Tree; |
| 33 |
import dr.evomodel.tree.TreeModel; |
| 34 |
import dr.evomodelxml.operators.GibbsPruneAndRegraftParser; |
| 35 |
import dr.inference.model.Likelihood; |
| 36 |
import dr.inference.operators.SimpleMetropolizedGibbsOperator; |
| 37 |
import dr.math.MathUtils; |
| 38 |
|
| 39 |
import java.util.ArrayList; |
| 40 |
import java.util.List; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
@Deprecated |
| 48 |
public class GibbsPruneAndRegraft extends SimpleMetropolizedGibbsOperator { |
| 49 |
|
| 50 |
private int MAX_DISTANCE = 10; |
| 51 |
|
| 52 |
private final TreeModel tree; |
| 53 |
|
| 54 |
private int[] distances; |
| 55 |
|
| 56 |
private double[] scores; |
| 57 |
|
| 58 |
private boolean pruned = true; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public GibbsPruneAndRegraft(TreeModel tree, boolean pruned, double weight) { |
| 64 |
this.tree = tree; |
| 65 |
this.pruned = pruned; |
| 66 |
setWeight(weight); |
| 67 |
scores = new double[tree.getNodeCount()]; |
| 68 |
MAX_DISTANCE = tree.getNodeCount() / 10; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
@Override |
| 79 |
public double doOperation(Likelihood likelihood) { |
| 80 |
if (pruned) { |
| 81 |
return prunedGibbsProposal(likelihood); |
| 82 |
} else { |
| 83 |
return gibbsProposal(likelihood); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
private double gibbsProposal(Likelihood likelihood) { |
| 88 |
|
| 89 |
final int nodeCount = tree.getNodeCount(); |
| 90 |
final NodeRef root = tree.getRoot(); |
| 91 |
|
| 92 |
NodeRef i; |
| 93 |
|
| 94 |
do { |
| 95 |
int indexI = MathUtils.nextInt(nodeCount); |
| 96 |
i = tree.getNode(indexI); |
| 97 |
} while (root == i || tree.getParent(i) == root); |
| 98 |
|
| 99 |
List<Integer> secondNodeIndices = new ArrayList<Integer>(); |
| 100 |
List<Double> probabilities = new ArrayList<Double>(); |
| 101 |
NodeRef j, jP; |
| 102 |
final NodeRef iP = tree.getParent(i); |
| 103 |
final double heightIP = tree.getNodeHeight(iP); |
| 104 |
double sum = 0.0; |
| 105 |
double backwardLikelihood = calculateTreeLikelihood(likelihood, tree); |
| 106 |
int offset = (int) -backwardLikelihood; |
| 107 |
double backward = Math.exp(backwardLikelihood + offset); |
| 108 |
final NodeRef oldBrother = getOtherChild(tree, iP, i); |
| 109 |
final NodeRef oldGrandfather = tree.getParent(iP); |
| 110 |
for (int n = 0; n < nodeCount; n++) { |
| 111 |
j = tree.getNode(n); |
| 112 |
if (j != root) { |
| 113 |
jP = tree.getParent(j); |
| 114 |
|
| 115 |
if ((i != j) && (tree.getNodeHeight(j) < heightIP) |
| 116 |
&& (heightIP < tree.getNodeHeight(jP))) { |
| 117 |
secondNodeIndices.add(n); |
| 118 |
|
| 119 |
pruneAndRegraft(tree, i, iP, j, jP); |
| 120 |
double prob = Math.exp(calculateTreeLikelihood(likelihood, tree) |
| 121 |
+ offset); |
| 122 |
probabilities.add(prob); |
| 123 |
sum += prob; |
| 124 |
|
| 125 |
pruneAndRegraft(tree, i, iP, oldBrother, oldGrandfather); |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
if (sum <= 1E-100) { |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
throw new RuntimeException( |
| 135 |
"Couldn't find another proposal with a decent likelihood."); |
| 136 |
} |
| 137 |
|
| 138 |
double ran = MathUtils.nextDouble() * sum; |
| 139 |
int index = 0; |
| 140 |
while (ran > 0.0) { |
| 141 |
ran -= probabilities.get(index); |
| 142 |
index++; |
| 143 |
} |
| 144 |
index--; |
| 145 |
|
| 146 |
j = tree.getNode(secondNodeIndices.get(index)); |
| 147 |
jP = tree.getParent(j); |
| 148 |
|
| 149 |
pruneAndRegraft(tree, i, iP, j, jP); |
| 150 |
|
| 151 |
double forward = probabilities.get(index); |
| 152 |
|
| 153 |
double forwardProb = (forward / sum); |
| 154 |
double backwardProb = (backward / (sum - forward + backward)); |
| 155 |
final double hastingsRatio = Math.log(backwardProb / forwardProb); |
| 156 |
|
| 157 |
return hastingsRatio; |
| 158 |
} |
| 159 |
|
| 160 |
private double prunedGibbsProposal(Likelihood likelihood) { |
| 161 |
final int nodeCount = tree.getNodeCount(); |
| 162 |
final NodeRef root = tree.getRoot(); |
| 163 |
|
| 164 |
for (int i = 0; i < nodeCount; i++) { |
| 165 |
scores[i] = Double.NEGATIVE_INFINITY; |
| 166 |
} |
| 167 |
|
| 168 |
NodeRef i; |
| 169 |
|
| 170 |
do { |
| 171 |
int indexI = MathUtils.nextInt(nodeCount); |
| 172 |
i = tree.getNode(indexI); |
| 173 |
} while (root == i || tree.getParent(i) == root); |
| 174 |
|
| 175 |
List<Integer> secondNodeIndices = new ArrayList<Integer>(); |
| 176 |
List<Double> probabilities = new ArrayList<Double>(); |
| 177 |
NodeRef j, jP; |
| 178 |
final NodeRef iP = tree.getParent(i); |
| 179 |
final double heightIP = tree.getNodeHeight(iP); |
| 180 |
double sum = 0.0; |
| 181 |
double backwardLikelihood = calculateTreeLikelihood(likelihood, |
| 182 |
tree); |
| 183 |
int offset = (int) -backwardLikelihood; |
| 184 |
double backward = Math.exp(backwardLikelihood + offset); |
| 185 |
final NodeRef oldBrother = getOtherChild(tree, iP, i); |
| 186 |
final NodeRef oldGrandfather = tree.getParent(iP); |
| 187 |
for (int n = 0; n < nodeCount; n++) { |
| 188 |
j = tree.getNode(n); |
| 189 |
if (j != root) { |
| 190 |
jP = tree.getParent(j); |
| 191 |
|
| 192 |
if ((i != j) && (tree.getNodeHeight(j) < heightIP) |
| 193 |
&& (heightIP < tree.getNodeHeight(jP)) |
| 194 |
&& getNodeDistance(iP, jP) <= MAX_DISTANCE) { |
| 195 |
secondNodeIndices.add(n); |
| 196 |
|
| 197 |
pruneAndRegraft(tree, i, iP, j, jP); |
| 198 |
double prob = Math.exp(calculateTreeLikelihood( |
| 199 |
likelihood, tree) |
| 200 |
+ offset); |
| 201 |
probabilities.add(prob); |
| 202 |
scores[n] = prob; |
| 203 |
sum += prob; |
| 204 |
|
| 205 |
pruneAndRegraft(tree, i, iP, oldBrother, oldGrandfather); |
| 206 |
} |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
if (sum <= 1E-100) { |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
throw new RuntimeException( |
| 215 |
"Couldn't find another proposal with a decent likelihood."); |
| 216 |
} |
| 217 |
|
| 218 |
double ran = MathUtils.nextDouble() * sum; |
| 219 |
int index = 0; |
| 220 |
while (ran > 0.0) { |
| 221 |
ran -= probabilities.get(index); |
| 222 |
index++; |
| 223 |
} |
| 224 |
index--; |
| 225 |
|
| 226 |
j = tree.getNode(secondNodeIndices.get(index)); |
| 227 |
jP = tree.getParent(j); |
| 228 |
|
| 229 |
pruneAndRegraft(tree, i, iP, j, jP); |
| 230 |
|
| 231 |
|
| 232 |
double sumBackward = 0.0; |
| 233 |
final NodeRef newBrother = j; |
| 234 |
final NodeRef newGrandfather = jP; |
| 235 |
for (int n = 0; n < nodeCount; n++) { |
| 236 |
j = tree.getNode(n); |
| 237 |
if (j != root) { |
| 238 |
jP = tree.getParent(j); |
| 239 |
|
| 240 |
if ((i != j) && (tree.getNodeHeight(j) < heightIP) |
| 241 |
&& (heightIP < tree.getNodeHeight(jP)) |
| 242 |
&& getNodeDistance(iP, jP) <= MAX_DISTANCE) { |
| 243 |
|
| 244 |
if (scores[n] != Double.NEGATIVE_INFINITY) { |
| 245 |
sumBackward += scores[n]; |
| 246 |
} else { |
| 247 |
pruneAndRegraft(tree, i, iP, j, jP); |
| 248 |
double prob = Math.exp(calculateTreeLikelihood( |
| 249 |
likelihood, tree) |
| 250 |
+ offset); |
| 251 |
sumBackward += prob; |
| 252 |
|
| 253 |
pruneAndRegraft(tree, i, iP, newBrother, newGrandfather); |
| 254 |
evaluate(likelihood, 1.0); |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
double forward = probabilities.get(index); |
| 261 |
|
| 262 |
final double forwardProb = (forward / sum); |
| 263 |
final double backwardProb = (backward / (sumBackward)); |
| 264 |
final double hastingsRatio = Math.log(backwardProb / forwardProb); |
| 265 |
|
| 266 |
return hastingsRatio; |
| 267 |
} |
| 268 |
|
| 269 |
private int getNodeDistance(NodeRef i, NodeRef j) { |
| 270 |
int count = 0; |
| 271 |
double heightI = tree.getNodeHeight(i); |
| 272 |
double heightJ = tree.getNodeHeight(j); |
| 273 |
|
| 274 |
while (i != j) { |
| 275 |
count++; |
| 276 |
if (heightI < heightJ) { |
| 277 |
i = tree.getParent(i); |
| 278 |
heightI = tree.getNodeHeight(i); |
| 279 |
} else { |
| 280 |
j = tree.getParent(j); |
| 281 |
heightJ = tree.getNodeHeight(j); |
| 282 |
} |
| 283 |
} |
| 284 |
return count; |
| 285 |
} |
| 286 |
|
| 287 |
public void printDistances() { |
| 288 |
System.out.println("Number of proposed trees in distances:"); |
| 289 |
for (int i = 0; i < distances.length; i++) { |
| 290 |
System.out.println(i + ")\t\t" + distances[i]); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
private double calculateTreeLikelihood(Likelihood likelihood, |
| 295 |
TreeModel tree) { |
| 296 |
return evaluate(likelihood, 1.0); |
| 297 |
} |
| 298 |
|
| 299 |
private void pruneAndRegraft(TreeModel tree, NodeRef i, NodeRef iP, NodeRef j, NodeRef jP) { |
| 300 |
tree.beginTreeEdit(); |
| 301 |
|
| 302 |
|
| 303 |
NodeRef iG = tree.getParent(iP); |
| 304 |
|
| 305 |
NodeRef iB = getOtherChild(tree, iP, i); |
| 306 |
|
| 307 |
tree.removeChild(iP, iB); |
| 308 |
tree.removeChild(iG, iP); |
| 309 |
tree.addChild(iG, iB); |
| 310 |
|
| 311 |
|
| 312 |
tree.removeChild(jP, j); |
| 313 |
tree.addChild(iP, j); |
| 314 |
tree.addChild(jP, iP); |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
tree.endTreeEdit(); |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
protected NodeRef getOtherChild(Tree tree, NodeRef parent, NodeRef child) { |
| 331 |
|
| 332 |
if (tree.getChild(parent, 0) == child) { |
| 333 |
return tree.getChild(parent, 1); |
| 334 |
} else { |
| 335 |
return tree.getChild(parent, 0); |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
@Override |
| 345 |
public String getOperatorName() { |
| 346 |
return GibbsPruneAndRegraftParser.GIBBS_PRUNE_AND_REGRAFT; |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
@Override |
| 355 |
public int getStepCount() { |
| 356 |
return 0; |
| 357 |
} |
| 358 |
|
| 359 |
} |