Debian Codegraph @

src/dr/evomodel/operators/GibbsSubtreeSwap.java

raw symbols SCIP
1 /*
2 * GibbsSubtreeSwap.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.TreeModel;
35 import dr.inference.model.Likelihood;
36 import dr.inference.operators.SimpleMetropolizedGibbsOperator;
37 import dr.math.MathUtils;
38
39 import java.util.ArrayList;
40 import java.util.List;
41
42 /**
43 * @author Sebastian Hoehna
44 *
45 */
46 // Cleaning out untouched stuff. Can be resurrected if needed
47 @Deprecated
48 public class GibbsSubtreeSwap extends SimpleMetropolizedGibbsOperator {
49
50 private int MAX_DISTANCE = 10;
51
52 private final TreeModel tree;
53
54 private final int[] distances;
55
56 private boolean pruned = true;
57
58 /**
59 *
60 */
61 public GibbsSubtreeSwap(TreeModel tree, boolean pruned, double weight) {
62 this.tree = tree;
63 this.pruned = pruned;
64 setWeight(weight);
65 MAX_DISTANCE = tree.getNodeCount() / 10;
66 MAX_DISTANCE = 4;
67 distances = new int[tree.getNodeCount() / 2];
68 }
69
70 /*
71 * (non-Javadoc)
72 *
73 * @see dr.evomodel.operators.SimpleGibbsOperator#getStepCount()
74 */
75 @Override
76 public int getStepCount() {
77 return 1;
78 }
79
80 public double doOperation(Likelihood likelihood) {
81
82 if( pruned ) {
83 return prunedWide(likelihood);
84 } else {
85 return wide(likelihood);
86 }
87 }
88
89 /**
90 * WARNING: Assumes strictly bifurcating tree.
91 *
92 * @throws InvalidTreeException
93 */
94 public double wide(Likelihood likelihood) {
95
96 final int nodeCount = tree.getNodeCount();
97 final NodeRef root = tree.getRoot();
98
99 NodeRef i;
100 int indexI;
101
102 do {
103 indexI = MathUtils.nextInt(nodeCount);
104 i = tree.getNode(indexI);
105 } while( root == i
106 || (tree.getParent(i) == root && tree.getNodeHeight(i) > tree
107 .getNodeHeight(getOtherChild(tree, tree.getParent(i), i))) );
108
109 List<Integer> secondNodeIndices = new ArrayList<Integer>();
110 List<Double> probabilities = new ArrayList<Double>();
111 NodeRef j, jP;
112 NodeRef iP = tree.getParent(i);
113 double heightIP = tree.getNodeHeight(iP);
114 double heightI = tree.getNodeHeight(i);
115 double sum = 0.0;
116 double backward = calculateTreeLikelihood(likelihood, tree);
117 int offset = (int) -backward;
118 backward = Math.exp(backward + offset);
119 for(int n = 0; n < nodeCount; n++) {
120 j = tree.getNode(n);
121 if( j != root ) {
122 jP = tree.getParent(j);
123
124 if( (iP != jP) && (tree.getNodeHeight(j) < heightIP)
125 && (heightI < tree.getNodeHeight(jP)) ) {
126 secondNodeIndices.add(n);
127
128 swap(tree, i, j, iP, jP);
129 double prob = Math.exp(calculateTreeLikelihood(
130 likelihood, tree)
131 + offset);
132 probabilities.add(prob);
133 swap(tree, i, j, jP, iP);
134 sum += prob;
135
136 }
137 }
138 }
139
140 if( sum <= 1E-100 ) {
141 // hack
142 // the proposals have such a small likelihood that they can be
143 // neglected
144 throw new RuntimeException(
145 "Couldn't find another proposal with a decent likelihood.");
146 }
147
148 double ran = Math.random() * sum;
149 int index = 0;
150 while( ran > 0.0 ) {
151 ran -= probabilities.get(index);
152 index++;
153 }
154 index--;
155
156 j = tree.getNode(secondNodeIndices.get(index));
157 jP = tree.getParent(j);
158 double heightJP = tree.getNodeHeight(jP);
159 double heightJ = tree.getNodeHeight(j);
160
161 // int distance = getNodeDistance(i, j);
162 // distances[distance]++;
163
164 // *******************************************
165 // assuming we would have chosen j first
166 double sumForward2 = 0.0;
167 NodeRef k, kP;
168 for(int n = 0; n < nodeCount; n++) {
169 k = tree.getNode(n);
170 if( k != root ) {
171 kP = tree.getParent(k);
172
173 if( (jP != kP) && (tree.getNodeHeight(k) < heightJP)
174 && (heightJ < tree.getNodeHeight(kP)) ) {
175
176 swap(tree, j, k, jP, kP);
177 double prob = Math.exp(calculateTreeLikelihood(
178 likelihood, tree)
179 + offset);
180 sumForward2 += prob;
181 swap(tree, j, k, kP, jP);
182 }
183 }
184 }
185
186 swap(tree, i, j, iP, jP);
187 double forward = probabilities.get(index);
188
189 iP = jP;
190 heightIP = heightJP;
191 double sumBackward = 0.0;
192 for(int n = 0; n < nodeCount; n++) {
193 j = tree.getNode(n);
194 if( j != root ) {
195 jP = tree.getParent(j);
196
197 if( (iP != jP) && (tree.getNodeHeight(j) < heightIP)
198 && (heightI < tree.getNodeHeight(jP)) ) {
199
200 swap(tree, i, j, iP, jP);
201 double prob = Math.exp(calculateTreeLikelihood(
202 likelihood, tree)
203 + offset);
204 sumBackward += prob;
205 swap(tree, i, j, jP, iP);
206
207 }
208 }
209 }
210
211 // *******************************************
212 // assuming we would have chosen j first
213 double sumBackward2 = 0.0;
214 j = tree.getNode(secondNodeIndices.get(index));
215 jP = tree.getParent(j);
216 heightJP = tree.getNodeHeight(jP);
217 heightJ = tree.getNodeHeight(j);
218 for(int n = 0; n < nodeCount; n++) {
219 k = tree.getNode(n);
220 if( k != root ) {
221 kP = tree.getParent(k);
222
223 if( (jP != kP) && (tree.getNodeHeight(k) < heightJP)
224 && (heightJ < tree.getNodeHeight(kP)) ) {
225
226 swap(tree, j, k, jP, kP);
227 double prob = Math.exp(calculateTreeLikelihood(
228 likelihood, tree)
229 + offset);
230 sumBackward2 += prob;
231 swap(tree, j, k, kP, jP);
232 }
233 }
234 }
235
236 double forwardProb = (forward / sum) + (forward / sumForward2);
237 double backwardProb = (backward / sumBackward)
238 + (backward / sumBackward2);
239
240 double hastingsRatio = Math.log(backwardProb / forwardProb);
241
242 // throw new OperatorFailedException(
243 // "Couldn't find valid wide move on this tree!");
244
245 return hastingsRatio;
246 }
247
248 /**
249 * WARNING: Assumes strictly bifurcating tree.
250 *
251 * @throws InvalidTreeException
252 */
253 public double prunedWide(Likelihood likelihood) {
254
255 final int nodeCount = tree.getNodeCount();
256 final NodeRef root = tree.getRoot();
257
258 NodeRef i;
259 int indexI;
260
261 do {
262 indexI = MathUtils.nextInt(nodeCount);
263 i = tree.getNode(indexI);
264 } while( root == i
265 || (tree.getParent(i) == root && tree.getNodeHeight(i) > tree
266 .getNodeHeight(getOtherChild(tree, tree.getParent(i), i))) );
267
268 List<Integer> secondNodeIndices = new ArrayList<Integer>();
269 List<Double> probabilities = new ArrayList<Double>();
270 NodeRef j, jP;
271 NodeRef iP = tree.getParent(i);
272 double heightIP = tree.getNodeHeight(iP);
273 double heightI = tree.getNodeHeight(i);
274 double sum = 0.0;
275 double backward = calculateTreeLikelihood(likelihood, tree);
276 int offset = (int) -backward;
277 backward = Math.exp(backward + offset);
278 for(int n = 0; n < nodeCount; n++) {
279 j = tree.getNode(n);
280 if( j != root ) {
281 jP = tree.getParent(j);
282
283 if( (iP != jP) && (tree.getNodeHeight(j) < heightIP)
284 && (heightI < tree.getNodeHeight(jP))
285 && getNodeDistance(iP, jP) <= MAX_DISTANCE ) {
286 secondNodeIndices.add(n);
287
288 swap(tree, i, j, iP, jP);
289 double prob = Math.exp(calculateTreeLikelihood(
290 likelihood, tree)
291 + offset);
292 probabilities.add(prob);
293 swap(tree, i, j, jP, iP);
294 sum += prob;
295
296 }
297 }
298 }
299
300 if( sum <= 1E-100 ) {
301 // hack
302 // the proposals have such a small likelihood that they can be
303 // neglected
304 throw new RuntimeException(
305 "Couldn't find another proposal with a decent likelihood.");
306 }
307
308 double ran = Math.random() * sum;
309 int index = 0;
310 while( ran > 0.0 ) {
311 ran -= probabilities.get(index);
312 index++;
313 }
314 index--;
315
316 j = tree.getNode(secondNodeIndices.get(index));
317 jP = tree.getParent(j);
318 double heightJP = tree.getNodeHeight(jP);
319 double heightJ = tree.getNodeHeight(j);
320
321 // *******************************************
322 // assuming we would have chosen j first
323 double sumForward2 = 0.0;
324 NodeRef k, kP;
325 for(int n = 0; n < nodeCount; n++) {
326 k = tree.getNode(n);
327 if( k != root ) {
328 kP = tree.getParent(k);
329
330 if( (jP != kP) && (tree.getNodeHeight(k) < heightJP)
331 && (heightJ < tree.getNodeHeight(kP))
332 && getNodeDistance(kP, jP) <= MAX_DISTANCE ) {
333
334 swap(tree, j, k, jP, kP);
335 double prob = Math.exp(calculateTreeLikelihood(
336 likelihood, tree)
337 + offset);
338 sumForward2 += prob;
339 swap(tree, j, k, kP, jP);
340 }
341 }
342 }
343
344 swap(tree, i, j, iP, jP);
345 double forward = probabilities.get(index);
346
347 iP = jP;
348 heightIP = heightJP;
349 double sumBackward = 0.0;
350 for(int n = 0; n < nodeCount; n++) {
351 j = tree.getNode(n);
352 if( j != root ) {
353 jP = tree.getParent(j);
354
355 if( (iP != jP) && (tree.getNodeHeight(j) < heightIP)
356 && (heightI < tree.getNodeHeight(jP))
357 && getNodeDistance(iP, jP) <= MAX_DISTANCE ) {
358
359 swap(tree, i, j, iP, jP);
360 double prob = Math.exp(calculateTreeLikelihood(
361 likelihood, tree)
362 + offset);
363 sumBackward += prob;
364 swap(tree, i, j, jP, iP);
365
366 }
367 }
368 }
369
370 // *******************************************
371 // assuming we would have chosen j first
372 double sumBackward2 = 0.0;
373 j = tree.getNode(secondNodeIndices.get(index));
374 jP = tree.getParent(j);
375 heightJP = tree.getNodeHeight(jP);
376 heightJ = tree.getNodeHeight(j);
377 for(int n = 0; n < nodeCount; n++) {
378 k = tree.getNode(n);
379 if( k != root ) {
380 kP = tree.getParent(k);
381
382 if( (jP != kP) && (tree.getNodeHeight(k) < heightJP)
383 && (heightJ < tree.getNodeHeight(kP))
384 && getNodeDistance(kP, jP) <= MAX_DISTANCE ) {
385
386 swap(tree, j, k, jP, kP);
387 double prob = Math.exp(calculateTreeLikelihood(
388 likelihood, tree)
389 + offset);
390 sumBackward2 += prob;
391 swap(tree, j, k, kP, jP);
392 }
393 }
394 }
395
396 double forwardProb = (forward / sum) + (forward / sumForward2);
397 double backwardProb = (backward / sumBackward)
398 + (backward / sumBackward2);
399
400 double hastingsRatio = Math.log(backwardProb / forwardProb);
401
402 // throw new OperatorFailedException(
403 // "Couldn't find valid wide move on this tree!");
404
405 return hastingsRatio;
406 }
407
408 private double calculateTreeLikelihood(Likelihood likelihood,
409 TreeModel tree) {
410 return evaluate(likelihood, 1.0);
411 // return 0.0;
412 }
413
414 /**
415 * @param tree the tree
416 * @param parent the parent
417 * @param child the child that you want the sister of
418 * @return the other child of the given parent.
419 */
420 protected NodeRef getOtherChild(Tree tree, NodeRef parent, NodeRef child) {
421
422 if( tree.getChild(parent, 0) == child ) {
423 return tree.getChild(parent, 1);
424 } else {
425 return tree.getChild(parent, 0);
426 }
427 }
428
429 /* exchange subtrees whose root are i and j */
430 private TreeModel swap(TreeModel tree, NodeRef i, NodeRef j, NodeRef iP, NodeRef jP) {
431
432 tree.beginTreeEdit();
433 tree.removeChild(iP, i);
434 tree.removeChild(jP, j);
435 tree.addChild(jP, i);
436 tree.addChild(iP, j);
437
438 tree.endTreeEdit();
439
440 return tree;
441 }
442
443 private int getNodeDistance(NodeRef i, NodeRef j) {
444 int count = 0;
445 double heightI = tree.getNodeHeight(i);
446 double heightJ = tree.getNodeHeight(j);
447
448 while( i != j ) {
449 count++;
450 if( heightI < heightJ ) {
451 i = tree.getParent(i);
452 heightI = tree.getNodeHeight(i);
453 } else {
454 j = tree.getParent(j);
455 heightJ = tree.getNodeHeight(j);
456 }
457 }
458 return count;
459 }
460
461 public void printDistances() {
462 System.out.println("Number of proposed trees in distances:");
463 for(int i = 0; i < distances.length; i++) {
464 System.out.println(i + ")\t\t" + distances[i]);
465 }
466 }
467
468 public String getOperatorName() {
469 return "Gibbs Subtree Exchange";
470 }
471
472 }