Debian Codegraph @

src/dr/evomodel/operators/ImportanceSubtreeSwap.java

raw symbols SCIP
1 /*
2 * ImportanceSubtreeSwap.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 /**
27 *
28 */
29 package dr.evomodel.operators;
30
31 import dr.evolution.tree.MutableTree.InvalidTreeException;
32 import dr.evolution.tree.NodeRef;
33 import dr.evolution.tree.Tree;
34 import dr.evomodel.tree.ConditionalCladeFrequency;
35 import dr.evomodel.tree.TreeModel;
36 import dr.evomodelxml.operators.ImportanceSubtreeSwapParser;
37 import dr.inference.operators.*;
38 import dr.math.MathUtils;
39
40 import java.util.ArrayList;
41 import java.util.List;
42
43 /**
44 * @author Sebastian Hoehna
45 * <p/>
46 * This class implements a subtree swap operator. The first subtree is
47 * chosen randomly and the second one is chosen according to the
48 * importance of the new tree. The importance are calculated by the
49 * multiplied clade probabilities.
50 */
51 // Cleaning out untouched stuff. Can be resurrected if needed
52 @Deprecated
53 public class ImportanceSubtreeSwap extends AbstractTreeOperator {
54
55 public final int SAMPLE_EVERY = 10;
56
57 private final TreeModel tree;
58
59 private final int samples;
60
61 private int sampleCount = 0;
62
63 private boolean burnin = false;
64
65 private final ConditionalCladeFrequency probabilityEstimater;
66
67 private final OperatorSchedule schedule;
68
69 /**
70 *
71 */
72 public ImportanceSubtreeSwap(TreeModel tree, double weight, int samples, int epsilon) {
73 this.tree = tree;
74 setWeight(weight);
75 this.samples = samples;
76 sampleCount = 0;
77 probabilityEstimater = new ConditionalCladeFrequency(tree, epsilon);
78 schedule = getOperatorSchedule(tree);
79 }
80
81 /**
82 *
83 */
84 public ImportanceSubtreeSwap(TreeModel tree, double weight, int samples) {
85 this.tree = tree;
86 setWeight(weight);
87 this.samples = samples;
88 sampleCount = 0;
89 double epsilon = 1 - Math.pow(0.5, 1.0 / samples);
90 probabilityEstimater = new ConditionalCladeFrequency(tree, epsilon);
91 schedule = getOperatorSchedule(tree);
92 }
93
94 private OperatorSchedule getOperatorSchedule(TreeModel treeModel) {
95
96 ExchangeOperator narrowExchange = new ExchangeOperator(
97 ExchangeOperator.NARROW, treeModel, 10);
98 ExchangeOperator wideExchange = new ExchangeOperator(
99 ExchangeOperator.WIDE, treeModel, 3);
100 SubtreeSlideOperator subtreeSlide = new SubtreeSlideOperator(treeModel,
101 10.0, 1.0, true, false, false, false, AdaptationMode.ADAPTATION_ON, AdaptableMCMCOperator.DEFAULT_ADAPTATION_TARGET);
102 NNI nni = new NNI(treeModel, 10.0);
103 WilsonBalding wilsonBalding = new WilsonBalding(treeModel, 3.0);
104 FNPR fnpr = new FNPR(treeModel, 5.0);
105
106 OperatorSchedule schedule = new SimpleOperatorSchedule();
107 schedule.addOperator(narrowExchange);
108 schedule.addOperator(wideExchange);
109 schedule.addOperator(subtreeSlide);
110 schedule.addOperator(nni);
111 schedule.addOperator(wilsonBalding);
112 schedule.addOperator(fnpr);
113
114 return schedule;
115 }
116
117 /*
118 * (non-Javadoc)
119 *
120 * @see dr.inference.operators.SimpleMCMCOperator#doOperation()
121 */
122 @Override
123 public double doOperation() {
124 if (!burnin) {
125 if (sampleCount < samples * SAMPLE_EVERY) {
126 sampleCount++;
127 if (sampleCount % SAMPLE_EVERY == 0) {
128 probabilityEstimater.addTree(tree);
129 }
130 setAcceptCount(0);
131 setRejectCount(0);
132 setTransitions(0);
133
134 return doUnguidedOperation();
135
136 } else {
137 return importanceExchange();
138 }
139 } else {
140
141 return doUnguidedOperation();
142
143 }
144 }
145
146 private double doUnguidedOperation() {
147 int index = schedule.getNextOperatorIndex();
148 SimpleMCMCOperator operator = (SimpleMCMCOperator) schedule.getOperator(index);
149
150 return operator.doOperation();
151 }
152
153 /**
154 * WARNING: Assumes strictly bifurcating tree.
155 *
156 * @throws InvalidTreeException
157 */
158 private double importanceExchange() {
159
160 final int nodeCount = tree.getNodeCount();
161 final NodeRef root = tree.getRoot();
162
163 NodeRef i;
164 int indexI;
165 int indexJ;
166
167 do {
168 indexI = MathUtils.nextInt(nodeCount);
169 i = tree.getNode(indexI);
170 } while (root == i
171 || (tree.getParent(i) == root &&
172 tree.getNodeHeight(i) > tree.getNodeHeight(getOtherChild(tree, tree.getParent(i), i))));
173
174 List<Integer> secondNodeIndices = new ArrayList<Integer>();
175 List<Double> probabilities = new ArrayList<Double>();
176 NodeRef j, iP, jP;
177 iP = tree.getParent(i);
178 double sum = 0.0;
179 double backward = calculateTreeProbability(tree);
180 int offset = (int) -backward;
181 backward = Math.exp(backward + offset);
182
183 tree.beginTreeEdit();
184 for (int n = 0; n < nodeCount; n++) {
185 j = tree.getNode(n);
186 if (j != root) {
187 jP = tree.getParent(j);
188
189 if ((iP != jP) && (i != jP) && (j != iP)
190 && (tree.getNodeHeight(j) < tree.getNodeHeight(iP))
191 && (tree.getNodeHeight(i) < tree.getNodeHeight(jP))) {
192 secondNodeIndices.add(n);
193
194 swap(tree, tree.getNode(indexI), tree.getNode(n));
195 double prob = Math.exp(calculateTreeProbability(tree)
196 + offset);
197 probabilities.add(prob);
198 swap(tree, tree.getNode(indexI), tree.getNode(n));
199 sum += prob;
200 }
201 }
202 }
203
204 double ran = MathUtils.nextDouble() * sum;
205 int index = 0;
206 while (ran > 0.0) {
207 ran -= probabilities.get(index);
208 index++;
209 }
210 index--;
211
212 j = tree.getNode(secondNodeIndices.get(index));
213 jP = tree.getParent(j);
214
215 // *******************************************
216 // assuming we would have chosen j first
217 double sumForward2 = 0.0;
218 NodeRef k, kP;
219 indexJ = secondNodeIndices.get(index);
220 for (int n = 0; n < nodeCount; n++) {
221 k = tree.getNode(n);
222 if (k != root) {
223 kP = tree.getParent(k);
224
225 if ((jP != kP) && (j != kP) && (k != jP)
226 && (tree.getNodeHeight(k) < tree.getNodeHeight(jP))
227 && (tree.getNodeHeight(j) < tree.getNodeHeight(kP))) {
228
229 swap(tree, tree.getNode(indexJ), tree.getNode(n));
230 double prob = Math.exp(calculateTreeProbability(tree)
231 + offset);
232 sumForward2 += prob;
233 swap(tree, tree.getNode(indexJ), tree.getNode(n));
234 }
235 }
236 }
237
238 swap(tree, i, j);
239 double forward = probabilities.get(index);
240
241 iP = tree.getParent(i);
242 double sumBackward = 0.0;
243 for (int n = 0; n < nodeCount; n++) {
244 j = tree.getNode(n);
245 if (j != root) {
246 jP = tree.getParent(j);
247
248 if ((iP != jP) && (i != jP) && (j != iP)
249 && (tree.getNodeHeight(j) < tree.getNodeHeight(iP))
250 && (tree.getNodeHeight(i) < tree.getNodeHeight(jP))) {
251
252 swap(tree, tree.getNode(indexI), tree.getNode(n));
253 double prob = Math.exp(calculateTreeProbability(tree)
254 + offset);
255 sumBackward += prob;
256 swap(tree, tree.getNode(indexI), tree.getNode(n));
257
258 }
259 }
260 }
261
262 // *******************************************
263 // assuming we would have chosen j first
264 double sumBackward2 = 0.0;
265 j = tree.getNode(secondNodeIndices.get(index));
266 jP = tree.getParent(j);
267 for (int n = 0; n < nodeCount; n++) {
268 k = tree.getNode(n);
269 if (k != root) {
270 kP = tree.getParent(k);
271
272 if ((jP != kP) && (j != kP) && (k != jP)
273 && (tree.getNodeHeight(k) < tree.getNodeHeight(jP))
274 && (tree.getNodeHeight(j) < tree.getNodeHeight(kP))) {
275
276 swap(tree, tree.getNode(indexJ), tree.getNode(n));
277 double prob = Math.exp(calculateTreeProbability(tree)
278 + offset);
279 sumBackward2 += prob;
280 swap(tree, tree.getNode(indexJ), tree.getNode(n));
281 }
282 }
283 }
284
285 tree.endTreeEdit();
286
287 // AR - not sure whether this check is necessary
288 try {
289 tree.checkTreeIsValid();
290 } catch (InvalidTreeException e) {
291 throw new RuntimeException(e.getMessage());
292 }
293
294
295 double forwardProb = (forward / sum) + (forward / sumForward2);
296 double backwardProb = (backward / sumBackward)
297 + (backward / sumBackward2);
298
299 double hastingsRatio = Math.log(backwardProb / forwardProb);
300
301 // throw new OperatorFailedException(
302 // "Couldn't find valid wide move on this tree!");
303
304 return hastingsRatio;
305 }
306
307 /* exchange subtrees whose root are i and j */
308 private void swap(TreeModel tree, NodeRef i, NodeRef j) {
309
310 NodeRef iP = tree.getParent(i);
311 NodeRef jP = tree.getParent(j);
312
313 tree.removeChild(iP, i);
314 tree.removeChild(jP, j);
315 tree.addChild(jP, i);
316 tree.addChild(iP, j);
317 }
318
319 private double calculateTreeProbability(Tree tree) {
320 // return calculateTreeProbabilityMult(tree);
321 // return calculateTreeProbabilityLog(tree);
322 return probabilityEstimater.getTreeProbability(tree);
323 // return 0.0;
324 }
325
326 public void setBurnin(boolean burnin) {
327 this.burnin = burnin;
328 }
329
330 /*
331 * (non-Javadoc)
332 *
333 * @see dr.inference.operators.SimpleMCMCOperator#getOperatorName()
334 */
335 @Override
336 public String getOperatorName() {
337 return ImportanceSubtreeSwapParser.IMPORTANCE_SUBTREE_SWAP;
338 }
339
340 /*
341 * (non-Javadoc)
342 *
343 * @see dr.inference.operators.MCMCOperator#getPerformanceSuggestion()
344 */
345 public String getPerformanceSuggestion() {
346 // TODO Auto-generated method stub
347 return "";
348 }
349
350 }