Debian Codegraph @

src/dr/evolution/tree/SubtreeSlide.java

raw symbols SCIP
1 /*
2 * SubtreeSlide.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.evolution.tree;
27
28 import dr.math.MathUtils;
29
30 import java.util.ArrayList;
31
32 /**
33 * A move that selects a subtree and slides it up or down the tree.
34 * This move can result in tree rearrangments. With small moves it will affect only
35 * a single node height.
36 *
37 * @author Alexei Drummond
38 *
39 * @version $Id: SubtreeSlide.java,v 1.7 2005/05/24 20:25:57 rambaut Exp $
40 */
41 @Deprecated
42 public class SubtreeSlide {
43
44 public SubtreeSlide(double size, boolean gaussian) {
45 this.size = size;
46 this.gaussian = gaussian;
47 }
48
49 public double getSize() { return size; }
50 public void setSize(double size) { this.size = size; }
51
52 /**
53 * Do a probablistic subtree slide move.
54 * @return the log-transformed hastings ratio
55 */
56 public double slideSubtree(MutableTree tree) {
57
58 double logHastingsRatio;
59
60 NodeRef i, newParent, newChild;
61
62 // 1. choose a random node avoiding root
63 do {
64 i = tree.getNode(MathUtils.nextInt(tree.getNodeCount()));
65 } while (tree.getRoot() == i);
66 NodeRef iP = tree.getParent(i);
67 NodeRef CiP = getOtherChild(tree,iP, i);
68 NodeRef PiP = tree.getParent(iP);
69
70 // 2. choose a delta to move
71 double delta = getDelta();
72 double oldHeight = tree.getNodeHeight(iP);
73 double newHeight = oldHeight + delta;
74
75 // 3. if the move is up
76 if (delta > 0) {
77
78 // 3.1 if the topology will change
79 if (PiP != null && tree.getNodeHeight(PiP) < newHeight) {
80
81 // find new parent
82 newParent = PiP; newChild = iP;
83 while (tree.getNodeHeight(newParent) < newHeight) {
84 newChild = newParent;
85 newParent = tree.getParent(newParent);
86 if (newParent == null) break;
87 }
88
89 tree.beginTreeEdit();
90
91 // 3.1.1 if creating a new root
92 if (tree.isRoot(newChild)) {
93 tree.removeChild(iP, CiP); tree.removeChild(PiP, iP);
94 tree.addChild(iP, newChild); tree.addChild(PiP, CiP);
95 tree.setRoot(iP);
96 //System.err.println("Creating new root!");
97 }
98 // 3.1.2 no new root
99 else {
100 tree.removeChild(iP, CiP); tree.removeChild(PiP, iP);
101 tree.removeChild(newParent, newChild);
102 tree.addChild(iP, newChild); tree.addChild(PiP, CiP);
103 tree.addChild(newParent, iP);
104 //System.err.println("No new root!");
105 }
106
107 tree.setNodeHeight(iP, newHeight);
108
109 tree.endTreeEdit();
110
111 // 3.1.3 count the hypothetical sources of this destination.
112 int possibleSources = intersectingEdges(tree, newChild, oldHeight, null);
113 //System.out.println("possible sources = " + possibleSources);
114
115 logHastingsRatio = Math.log(1.0/(double)possibleSources);
116
117 } else {
118 // just change the node height
119 tree.setNodeHeight(iP, newHeight);
120 logHastingsRatio = 0.0;
121 }
122 }
123 // 4 if we are sliding the subtree down.
124 else {
125
126 // 4.0 is it a valid move?
127 if (tree.getNodeHeight(i) > newHeight) {
128 return Double.NEGATIVE_INFINITY;
129 }
130
131 // 4.1 will the move change the topology
132 if (tree.getNodeHeight(CiP) > newHeight) {
133
134 ArrayList newChildren = new ArrayList();
135 int possibleDestinations = intersectingEdges(tree, CiP, newHeight, newChildren);
136
137 // if no valid destinations then return a failure
138 if (newChildren.size() == 0) { return Double.NEGATIVE_INFINITY; }
139
140 // pick a random parent/child destination edge uniformly from options
141 int childIndex = MathUtils.nextInt(newChildren.size());
142 newChild = (NodeRef)newChildren.get(childIndex);
143 newParent = tree.getParent(newChild);
144
145 tree.beginTreeEdit();
146
147 // 4.1.1 if iP was root
148 if (tree.isRoot(iP)) {
149 // new root is CiP
150 tree.removeChild(iP, CiP); tree.removeChild(newParent, newChild);
151 tree.addChild(iP, newChild); tree.addChild(newParent, iP);
152 tree.setRoot(CiP);
153 //System.err.println("DOWN: Creating new root!");
154 } else {
155 tree.removeChild(iP, CiP); tree.removeChild(PiP, iP);
156 tree.removeChild(newParent, newChild);
157 tree.addChild(iP, newChild); tree.addChild(PiP, CiP);
158 tree.addChild(newParent, iP);
159 //System.err.println("DOWN: no new root!");
160 }
161
162 tree.setNodeHeight(iP, newHeight);
163
164 tree.endTreeEdit();
165
166 logHastingsRatio = Math.log((double)possibleDestinations);
167 } else {
168 tree.setNodeHeight(iP, newHeight);
169 logHastingsRatio = 0.0;
170 }
171 }
172
173 return logHastingsRatio;
174 }
175
176 private double getDelta() {
177 if (!gaussian) {
178 return (MathUtils.nextDouble() * size) - (size/2.0);
179 } else {
180 return MathUtils.nextGaussian() * size;
181 }
182 }
183
184 private int intersectingEdges(Tree tree, NodeRef node, double height, ArrayList directChildren) {
185
186 NodeRef parent = tree.getParent(node);
187
188 if (tree.getNodeHeight(parent) < height) return 0;
189
190 if (tree.getNodeHeight(node) < height) {
191 if (directChildren != null) directChildren.add(node);
192 return 1;
193 }
194
195 int count = 0;
196 for (int i = 0; i < tree.getChildCount(node); i++) {
197 count += intersectingEdges(tree, tree.getChild(node, i), height, directChildren);
198 }
199 return count;
200 }
201
202 /**
203 * @return the other child of the given parent.
204 */
205 private NodeRef getOtherChild(Tree tree, NodeRef parent, NodeRef child) {
206
207 if (tree.getChild(parent, 0) == child) {
208 return tree.getChild(parent, 1);
209 } else {
210 return tree.getChild(parent, 0);
211 }
212 }
213
214 private double size = 1.0;
215 private boolean gaussian = false;
216 }