Debian Codegraph @

src/dr/evomodel/operators/TipLeapOperator.java

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