Debian Codegraph @

src/dr/evomodel/operators/SubtreeSlideOperator.java

raw symbols SCIP
1 /*
2 * SubtreeSlideOperator.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
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 * Implements the subtree slide move.
41 *
42 * @author Alexei Drummond
43 * @version $Id: SubtreeSlideOperator.java,v 1.15 2005/06/14 10:40:34 rambaut Exp $
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 * Do a probablistic subtree slide move.
84 *
85 * @return the log-transformed hastings ratio
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 // 1. choose a random node avoiding root
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 // 2. choose a delta to move
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 // 3. if the move is up
116 if (delta > 0) {
117
118 // 3.1 if the topology will change
119 if (PiP != null && tree.getNodeHeight(PiP) < newHeight) {
120 // find new parent
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 // 3.1.1 if creating a new root
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 //System.err.println("Creating new root!");
139
140 if (tree.hasNodeTraits()) {
141 // **********************************************
142 // swap traits and rates so that root keeps it trait and rate values
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 // 3.1.2 no new root
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 //System.err.println("No new root!");
167 }
168
169 tree.setNodeHeight(iP, newHeight);
170
171 tree.endTreeEdit();
172
173 // 3.1.3 count the hypothetical sources of this destination.
174 final int possibleSources = intersectingEdges(tree, newChild, oldHeight, null);
175 //System.out.println("possible sources = " + possibleSources);
176
177 logq = -Math.log(possibleSources);
178
179 } else {
180 // just change the node height
181 tree.setNodeHeight(iP, newHeight);
182 logq = 0.0;
183 }
184 }
185 // 4 if we are sliding the subtree down.
186 else {
187
188 // 4.0 is it a valid move?
189 if (tree.getNodeHeight(i) > newHeight) {
190 return Double.NEGATIVE_INFINITY;
191 }
192
193 // 4.1 will the move change the topology
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 // if no valid destinations then return a failure
204 if (newChildren.size() == 0) {
205 return Double.NEGATIVE_INFINITY;
206 }
207
208 // pick a random parent/child destination edge uniformly from options
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 // 4.1.1 if iP was root
227 if (tree.isRoot(iP)) {
228 if (DEBUG) {
229 System.out.println("isRoot");
230 }
231 // new root is CiP
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 // swap traits and rates, so that root keeps it trait and rate values
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 //System.err.println("DOWN: Creating new root!");
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 //System.err.println("DOWN: no new root!");
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 // final double tmp = tree.getNodeTrait(i, TRAIT);
294 // tree.setNodeTrait(i, TRAIT, tree.getNodeTrait(j, TRAIT));
295 // tree.setNodeTrait(j, TRAIT, tmp);
296 }
297
298 }
299
300 // just return -Inf
301 //if (logq == Double.NEGATIVE_INFINITY) throw new OperatorFailedException("invalid slide");
302
303 if (scaledDirichletBranches) {
304 if (oldTreeHeight != tree.getNodeHeight(tree.getRoot()))
305 throw new UnsupportedOperationException("Temporarily disabled."); // TODO calculate Hastings ratio
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 /*if (DEBUG) {
327 System.out.println("intersectingEdges");
328 System.out.println("parent: " + parent);
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 }