Debian Codegraph @

src/dr/evomodel/operators/SubtreeLeapOperator.java

raw symbols SCIP
1 /*
2 * SubtreeLeapOperator.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.SubtreeLeapOperatorParser;
32 import dr.inference.operators.*;
33 import dr.math.MathUtils;
34
35 import java.util.ArrayList;
36 import java.util.HashMap;
37 import java.util.List;
38 import java.util.Map;
39
40 /**
41 * Implements the Subtree Leap move.
42 *
43 * This move picks a node at random (except for the root) and then moves the parent to any location
44 * that is a certain patristic distance from its starting point (the distance is drawn from a Gaussian).
45 *
46 * It is always possible for the node to move up (potentially becoming the root) but the destination can't
47 * be younger than the original node. All possible destinations are collected and then picked amongst
48 * uniformly.
49 *
50 * @author Andrew Rambaut
51 * @author Luiz Max Carvalho
52 * @version $Id$
53 */
54 public class SubtreeLeapOperator extends AbstractTreeOperator implements AdaptableMCMCOperator {
55
56 private double size = 1.0;
57
58 private final TreeModel tree;
59 private final AdaptationMode mode;
60 private final double targetAcceptance;
61
62 /**
63 * Constructor
64 *
65 * @param tree the tree
66 * @param weight the weight
67 * @param size scaling on a unit Gaussian to draw the patristic distance from
68 * @param mode coercion mode
69 */
70 public SubtreeLeapOperator(TreeModel tree, double weight, double size, double targetAcceptance, AdaptationMode mode) {
71 this.tree = tree;
72 setWeight(weight);
73 this.size = size;
74 this.targetAcceptance = targetAcceptance;
75 this.mode = mode;
76 }
77
78
79
80 /**
81 * Do a subtree leap move.
82 *
83 * @return the log-transformed hastings ratio
84 */
85 public double doOperation() {
86 double logq;
87
88 final double delta = getDelta();
89
90 final NodeRef root = tree.getRoot();
91
92 NodeRef node;
93
94 do {
95 // choose a random node avoiding root
96 node = tree.getNode(MathUtils.nextInt(tree.getNodeCount()));
97
98 } while (node == root);
99
100 // get its parent - this is the node we will prune/graft
101 final NodeRef parent = tree.getParent(node);
102
103 // get the node's sibling
104 final NodeRef sibling = getOtherChild(tree, parent, node);
105
106 // and its grand parent
107 final NodeRef grandParent = tree.getParent(parent);
108
109 final Map<NodeRef, Double> destinations = getDestinations(node, parent, sibling, delta);
110 final List<NodeRef> destinationNodes = new ArrayList<NodeRef>(destinations.keySet());
111
112 // pick uniformly from this list
113 int r = MathUtils.nextInt(destinations.size());
114
115 double forwardProbability = 1.0 / destinations.size();
116
117 final NodeRef j = destinationNodes.get(r);
118 final double newHeight = destinations.get(j);
119
120 final NodeRef jParent = tree.getParent(j);
121
122 if (jParent != null && newHeight > tree.getNodeHeight(jParent)) {
123 throw new IllegalArgumentException("height error");
124 }
125
126 if (newHeight < tree.getNodeHeight(j)) {
127 throw new IllegalArgumentException("height error");
128 }
129
130 tree.beginTreeEdit();
131
132 if (j == parent || jParent == parent) {
133 // the subtree is not actually moving but the height will change
134 } else {
135 if (grandParent == null) {
136 // if the parent of the original node is the root then the sibling becomes
137 // the root.
138 tree.removeChild(parent, sibling);
139 tree.setRoot(sibling);
140
141 } else {
142 // remove the parent of node by connecting its sibling to its grandparent.
143 tree.removeChild(parent, sibling);
144 tree.removeChild(grandParent, parent);
145 tree.addChild(grandParent, sibling);
146 }
147
148 if (jParent == null) {
149 // adding the node to the root of the tree
150 tree.addChild(parent, j);
151 tree.setRoot(parent);
152 } else {
153 // remove destination edge j from its parent
154 tree.removeChild(jParent, j);
155
156 // add destination edge to the parent of node
157 tree.addChild(parent, j);
158
159 // and add the parent of i as a child of the former parent of j
160 tree.addChild(jParent, parent);
161 }
162 }
163 tree.endTreeEdit();
164
165 tree.setNodeHeight(parent, newHeight);
166
167 if (tree.getParent(parent) != null && newHeight > tree.getNodeHeight(tree.getParent(parent))) {
168 throw new IllegalArgumentException("height error");
169 }
170
171 if (newHeight < tree.getNodeHeight(node)) {
172 throw new IllegalArgumentException("height error");
173 }
174
175 if (newHeight < tree.getNodeHeight(getOtherChild(tree, parent, node))) {
176 throw new IllegalArgumentException("height error");
177 }
178
179 final Map<NodeRef, Double> reverseDestinations = getDestinations(node, parent, getOtherChild(tree, parent, node), delta);
180 double reverseProbability = 1.0 / reverseDestinations.size();
181
182 // hastings ratio = reverse Prob / forward Prob
183 logq = Math.log(reverseProbability) - Math.log(forwardProbability);
184 return logq;
185 }
186
187 private Map<NodeRef, Double> getDestinations(NodeRef node, NodeRef parent, NodeRef sibling, double delta) {
188
189 final Map<NodeRef, Double> destinations = new HashMap<NodeRef, Double>();
190
191 // get the parent's height
192 final double height = tree.getNodeHeight(parent);
193
194 final double heightBelow = height - delta;
195
196 if (heightBelow > tree.getNodeHeight(node)) {
197 // the destination height below the parent is compatible with the node
198 // see if there are any destinations on the sibling's branch
199 final List<NodeRef> edges = new ArrayList<NodeRef>();
200
201 getIntersectingEdges(tree, sibling, heightBelow, edges);
202
203 // add the intersecting edges and the height
204 for (NodeRef n : edges) {
205 destinations.put(n, heightBelow);
206 }
207 }
208
209 final double heightAbove = height + delta;
210
211 NodeRef node1 = parent;
212
213 // walk up to root
214 boolean done = false;
215 while (!done) {
216 NodeRef parent1 = tree.getParent(node1);
217
218 if (parent1 != null) {
219 final double height1 = tree.getNodeHeight(parent1);
220 if (height1 < heightAbove) {
221 // haven't reached the height above the original height so go down
222 // the sibling subtree
223 NodeRef sibling1 = getOtherChild(tree, parent1, node1);
224
225 double heightBelow1 = height1 - (heightAbove - height1);
226
227 if (heightBelow1 > tree.getNodeHeight(node)) {
228
229 final List<NodeRef> edges = new ArrayList<NodeRef>();
230
231 getIntersectingEdges(tree, sibling1, heightBelow1, edges);
232
233 // add the intersecting edges and the height
234 for (NodeRef n : edges) {
235 destinations.put(n, heightBelow1);
236 }
237 }
238 } else {
239 // add the current node as a destination
240 destinations.put(node1, heightAbove);
241 done = true;
242 }
243
244 node1 = parent1;
245 } else {
246 // node1 is the root - add it as a destination and stop loop
247 destinations.put(node1, heightAbove);
248 done = true;
249 }
250 }
251
252 return destinations;
253 }
254
255 private double getDelta() {
256 return Math.abs(MathUtils.nextGaussian() * size);
257 }
258
259 private int getIntersectingEdges(Tree tree, NodeRef node, double height, List<NodeRef> edges) {
260
261 final NodeRef parent = tree.getParent(node);
262
263 if (tree.getNodeHeight(parent) < height) return 0;
264
265 if (tree.getNodeHeight(node) < height) {
266 edges.add(node);
267 return 1;
268 }
269
270 int count = 0;
271 for (int i = 0; i < tree.getChildCount(node); i++) {
272 count += getIntersectingEdges(tree, tree.getChild(node, i), height, edges);
273 }
274 return count;
275 }
276
277 public double getSize() {
278 return size;
279 }
280
281 public void setSize(double size) {
282 this.size = size;
283 }
284
285 public double getAdaptableParameter() {
286 return Math.log(getSize());
287 }
288
289 public void setAdaptableParameter(double value) {
290 setSize(Math.exp(value));
291 }
292
293 public double getRawParameter() {
294 return getSize();
295 }
296
297 public AdaptationMode getMode() {
298 return mode;
299 }
300
301 public double getMinimumAcceptanceLevel() {
302 return 0.1;
303 }
304
305 public double getMaximumAcceptanceLevel() {
306 return 0.4;
307 }
308
309 public double getMinimumGoodAcceptanceLevel() {
310 return 0.20;
311 }
312
313 public double getMaximumGoodAcceptanceLevel() {
314 return 0.30;
315 }
316
317 public final String getPerformanceSuggestion() {
318 return null;
319 }
320
321 @Override
322 public double getTargetAcceptanceProbability() {
323 return targetAcceptance;
324 }
325
326 public String getAdaptableParameterName() {
327 return "size";
328 }
329
330 public String getOperatorName() {
331 return SubtreeLeapOperatorParser.SUBTREE_LEAP + "(" + tree.getId() + ")";
332 }
333
334
335 }