| 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.operators; |
| 27 |
|
| 28 |
import dr.evolution.tree.NodeRef; |
| 29 |
import dr.evolution.tree.Tree; |
| 30 |
import dr.evomodel.tree.TreeModel; |
| 31 |
import dr.evomodelxml.operators.SubtreeSlideOperatorParser; |
| 32 |
import dr.inference.model.Statistic; |
| 33 |
import dr.inference.operators.*; |
| 34 |
import dr.math.MathUtils; |
| 35 |
|
| 36 |
import java.util.ArrayList; |
| 37 |
import java.util.List; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public class SubtreeSlideOperator extends AbstractTreeOperator implements AdaptableMCMCOperator { |
| 46 |
|
| 47 |
private static final boolean DEBUG = false; |
| 48 |
|
| 49 |
private TreeModel tree = null; |
| 50 |
private double size = 1.0; |
| 51 |
private boolean gaussian = false; |
| 52 |
private final boolean swapInRandomRate; |
| 53 |
private final boolean swapInRandomTrait; |
| 54 |
private final boolean scaledDirichletBranches; |
| 55 |
private AdaptationMode mode = AdaptationMode.DEFAULT; |
| 56 |
private final double targetAcceptance; |
| 57 |
|
| 58 |
public SubtreeSlideOperator(TreeModel tree, double weight, double size, boolean gaussian, |
| 59 |
boolean swapRates, boolean swapTraits, boolean scaleDirichletBranches, |
| 60 |
AdaptationMode mode, double targetAcceptance) { |
| 61 |
this.tree = tree; |
| 62 |
setWeight(weight); |
| 63 |
|
| 64 |
if (size == 0.0) { |
| 65 |
double b = 0.0; |
| 66 |
for (int k = 0; k < tree.getNodeCount(); ++k) { |
| 67 |
b += tree.getBranchLength(tree.getNode(k)); |
| 68 |
} |
| 69 |
size = b / (2 * tree.getNodeCount()); |
| 70 |
} |
| 71 |
|
| 72 |
this.size = size; |
| 73 |
this.gaussian = gaussian; |
| 74 |
this.swapInRandomRate = swapRates; |
| 75 |
this.swapInRandomTrait = swapTraits; |
| 76 |
this.scaledDirichletBranches = scaleDirichletBranches; |
| 77 |
|
| 78 |
this.mode = mode; |
| 79 |
this.targetAcceptance = targetAcceptance; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public double doOperation() { |
| 88 |
|
| 89 |
double logq; |
| 90 |
|
| 91 |
final NodeRef root = tree.getRoot(); |
| 92 |
final double oldTreeHeight = tree.getNodeHeight(root); |
| 93 |
|
| 94 |
NodeRef i; |
| 95 |
|
| 96 |
|
| 97 |
do { |
| 98 |
i = tree.getNode(MathUtils.nextInt(tree.getNodeCount())); |
| 99 |
} while (root == i); |
| 100 |
|
| 101 |
final NodeRef iP = tree.getParent(i); |
| 102 |
final NodeRef CiP = getOtherChild(tree, iP, i); |
| 103 |
final NodeRef PiP = tree.getParent(iP); |
| 104 |
|
| 105 |
|
| 106 |
final double delta = getDelta(); |
| 107 |
final double oldHeight = tree.getNodeHeight(iP); |
| 108 |
final double newHeight = oldHeight + delta; |
| 109 |
|
| 110 |
if (DEBUG) { |
| 111 |
System.out.println("\nSubTreeSlideOperator: oldTreeHeight = " + oldTreeHeight); |
| 112 |
System.out.println("Node selected: " + i + " ; delta = " + delta); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
if (delta > 0) { |
| 117 |
|
| 118 |
|
| 119 |
if (PiP != null && tree.getNodeHeight(PiP) < newHeight) { |
| 120 |
|
| 121 |
NodeRef newParent = PiP; |
| 122 |
NodeRef newChild = iP; |
| 123 |
while (tree.getNodeHeight(newParent) < newHeight) { |
| 124 |
newChild = newParent; |
| 125 |
newParent = tree.getParent(newParent); |
| 126 |
if (newParent == null) break; |
| 127 |
} |
| 128 |
|
| 129 |
tree.beginTreeEdit(); |
| 130 |
|
| 131 |
|
| 132 |
if (tree.isRoot(newChild)) { |
| 133 |
tree.removeChild(iP, CiP); |
| 134 |
tree.removeChild(PiP, iP); |
| 135 |
tree.addChild(iP, newChild); |
| 136 |
tree.addChild(PiP, CiP); |
| 137 |
tree.setRoot(iP); |
| 138 |
|
| 139 |
|
| 140 |
if (tree.hasNodeTraits()) { |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
tree.swapAllTraits(newChild, iP); |
| 146 |
|
| 147 |
} |
| 148 |
|
| 149 |
if (tree.hasRates()) { |
| 150 |
final double rootNodeRate = tree.getNodeRate(newChild); |
| 151 |
tree.setNodeRate(newChild, tree.getNodeRate(iP)); |
| 152 |
tree.setNodeRate(iP, rootNodeRate); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
} |
| 158 |
|
| 159 |
else { |
| 160 |
tree.removeChild(iP, CiP); |
| 161 |
tree.removeChild(PiP, iP); |
| 162 |
tree.removeChild(newParent, newChild); |
| 163 |
tree.addChild(iP, newChild); |
| 164 |
tree.addChild(PiP, CiP); |
| 165 |
tree.addChild(newParent, iP); |
| 166 |
|
| 167 |
} |
| 168 |
|
| 169 |
tree.setNodeHeight(iP, newHeight); |
| 170 |
|
| 171 |
tree.endTreeEdit(); |
| 172 |
|
| 173 |
|
| 174 |
final int possibleSources = intersectingEdges(tree, newChild, oldHeight, null); |
| 175 |
|
| 176 |
|
| 177 |
logq = -Math.log(possibleSources); |
| 178 |
|
| 179 |
} else { |
| 180 |
|
| 181 |
tree.setNodeHeight(iP, newHeight); |
| 182 |
logq = 0.0; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
else { |
| 187 |
|
| 188 |
|
| 189 |
if (tree.getNodeHeight(i) > newHeight) { |
| 190 |
return Double.NEGATIVE_INFINITY; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
if (tree.getNodeHeight(CiP) > newHeight) { |
| 195 |
|
| 196 |
List<NodeRef> newChildren = new ArrayList<NodeRef>(); |
| 197 |
final int possibleDestinations = intersectingEdges(tree, CiP, newHeight, newChildren); |
| 198 |
|
| 199 |
if (DEBUG) { |
| 200 |
System.out.println("possibleDestinations = " + possibleDestinations); |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
if (newChildren.size() == 0) { |
| 205 |
return Double.NEGATIVE_INFINITY; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
final int childIndex = MathUtils.nextInt(newChildren.size()); |
| 210 |
if (DEBUG) { |
| 211 |
for (NodeRef ref : newChildren) { |
| 212 |
System.out.println("child: " + ref.getNumber()); |
| 213 |
} |
| 214 |
} |
| 215 |
NodeRef newChild = newChildren.get(childIndex); |
| 216 |
NodeRef newParent = tree.getParent(newChild); |
| 217 |
|
| 218 |
if (DEBUG) { |
| 219 |
System.out.println("childIndex: " + childIndex); |
| 220 |
System.out.println(newChild); |
| 221 |
System.out.println(newParent); |
| 222 |
} |
| 223 |
|
| 224 |
tree.beginTreeEdit(); |
| 225 |
|
| 226 |
|
| 227 |
if (tree.isRoot(iP)) { |
| 228 |
if (DEBUG) { |
| 229 |
System.out.println("isRoot"); |
| 230 |
} |
| 231 |
|
| 232 |
tree.removeChild(iP, CiP); |
| 233 |
tree.removeChild(newParent, newChild); |
| 234 |
tree.addChild(iP, newChild); |
| 235 |
tree.addChild(newParent, iP); |
| 236 |
tree.setRoot(CiP); |
| 237 |
|
| 238 |
if (tree.hasNodeTraits()) { |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
tree.swapAllTraits(iP, CiP); |
| 244 |
|
| 245 |
} |
| 246 |
|
| 247 |
if (tree.hasRates()) { |
| 248 |
final double rootNodeRate = tree.getNodeRate(iP); |
| 249 |
tree.setNodeRate(iP, tree.getNodeRate(CiP)); |
| 250 |
tree.setNodeRate(CiP, rootNodeRate); |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
} else { |
| 257 |
tree.removeChild(iP, CiP); |
| 258 |
tree.removeChild(PiP, iP); |
| 259 |
tree.removeChild(newParent, newChild); |
| 260 |
tree.addChild(iP, newChild); |
| 261 |
tree.addChild(PiP, CiP); |
| 262 |
tree.addChild(newParent, iP); |
| 263 |
|
| 264 |
} |
| 265 |
|
| 266 |
tree.setNodeHeight(iP, newHeight); |
| 267 |
|
| 268 |
tree.endTreeEdit(); |
| 269 |
|
| 270 |
logq = Math.log(possibleDestinations); |
| 271 |
} else { |
| 272 |
tree.setNodeHeight(iP, newHeight); |
| 273 |
logq = 0.0; |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
if (swapInRandomRate) { |
| 278 |
final NodeRef j = tree.getNode(MathUtils.nextInt(tree.getNodeCount())); |
| 279 |
if (j != i) { |
| 280 |
final double tmp = tree.getNodeRate(i); |
| 281 |
tree.setNodeRate(i, tree.getNodeRate(j)); |
| 282 |
tree.setNodeRate(j, tmp); |
| 283 |
} |
| 284 |
|
| 285 |
} |
| 286 |
|
| 287 |
if (swapInRandomTrait) { |
| 288 |
final NodeRef j = tree.getNode(MathUtils.nextInt(tree.getNodeCount())); |
| 289 |
if (j != i) { |
| 290 |
|
| 291 |
tree.swapAllTraits(i, j); |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
} |
| 297 |
|
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
if (scaledDirichletBranches) { |
| 304 |
if (oldTreeHeight != tree.getNodeHeight(tree.getRoot())) |
| 305 |
throw new UnsupportedOperationException("Temporarily disabled."); |
| 306 |
} |
| 307 |
|
| 308 |
return logq; |
| 309 |
} |
| 310 |
|
| 311 |
private double getDelta() { |
| 312 |
if (DEBUG) { |
| 313 |
System.out.println("size = " + size); |
| 314 |
} |
| 315 |
if (!gaussian) { |
| 316 |
return (MathUtils.nextDouble() * size) - (size / 2.0); |
| 317 |
} else { |
| 318 |
return MathUtils.nextGaussian() * size; |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
private int intersectingEdges(Tree tree, NodeRef node, double height, List<NodeRef> directChildren) { |
| 323 |
|
| 324 |
final NodeRef parent = tree.getParent(node); |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
if (tree.getNodeHeight(parent) < height) return 0; |
| 332 |
|
| 333 |
if (tree.getNodeHeight(node) < height) { |
| 334 |
if (directChildren != null) directChildren.add(node); |
| 335 |
return 1; |
| 336 |
} |
| 337 |
|
| 338 |
int count = 0; |
| 339 |
for (int i = 0; i < tree.getChildCount(node); i++) { |
| 340 |
count += intersectingEdges(tree, tree.getChild(node, i), height, directChildren); |
| 341 |
} |
| 342 |
return count; |
| 343 |
} |
| 344 |
|
| 345 |
public double getSize() { |
| 346 |
return size; |
| 347 |
} |
| 348 |
|
| 349 |
public void setSize(double size) { |
| 350 |
this.size = size; |
| 351 |
} |
| 352 |
|
| 353 |
public double getAdaptableParameter() { |
| 354 |
return Math.log(getSize()); |
| 355 |
} |
| 356 |
|
| 357 |
public void setAdaptableParameter(double value) { |
| 358 |
setSize(Math.exp(value)); |
| 359 |
} |
| 360 |
|
| 361 |
public double getRawParameter() { |
| 362 |
return getSize(); |
| 363 |
} |
| 364 |
|
| 365 |
@Override |
| 366 |
public final double getTargetAcceptanceProbability() { |
| 367 |
return targetAcceptance; |
| 368 |
} |
| 369 |
|
| 370 |
public double getMinimumAcceptanceLevel() { |
| 371 |
return 0.1; |
| 372 |
} |
| 373 |
|
| 374 |
public double getMaximumAcceptanceLevel() { |
| 375 |
return 0.4; |
| 376 |
} |
| 377 |
|
| 378 |
public double getMinimumGoodAcceptanceLevel() { |
| 379 |
return 0.20; |
| 380 |
} |
| 381 |
|
| 382 |
public double getMaximumGoodAcceptanceLevel() { |
| 383 |
return 0.30; |
| 384 |
} |
| 385 |
|
| 386 |
public AdaptationMode getMode() { |
| 387 |
return mode; |
| 388 |
} |
| 389 |
|
| 390 |
public String getAdaptableParameterName() { |
| 391 |
return "size"; |
| 392 |
} |
| 393 |
|
| 394 |
public final String getPerformanceSuggestion() { |
| 395 |
return null; |
| 396 |
} |
| 397 |
|
| 398 |
public String getOperatorName() { |
| 399 |
return SubtreeSlideOperatorParser.SUBTREE_SLIDE + "(" + tree.getId() + ")"; |
| 400 |
} |
| 401 |
|
| 402 |
} |