Debian Codegraph @

src/dr/evomodel/operators/GibbsPruneAndRegraft.java

raw symbols SCIP
1 /*
2 * GibbsPruneAndRegraft.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.NodeRef;
32 import dr.evolution.tree.Tree;
33 import dr.evomodel.tree.TreeModel;
34 import dr.evomodelxml.operators.GibbsPruneAndRegraftParser;
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 GibbsPruneAndRegraft extends SimpleMetropolizedGibbsOperator {
49
50 private int MAX_DISTANCE = 10;
51
52 private final TreeModel tree;
53
54 private int[] distances;
55
56 private double[] scores;
57
58 private boolean pruned = true;
59
60 /**
61 *
62 */
63 public GibbsPruneAndRegraft(TreeModel tree, boolean pruned, double weight) {
64 this.tree = tree;
65 this.pruned = pruned;
66 setWeight(weight);
67 scores = new double[tree.getNodeCount()];
68 MAX_DISTANCE = tree.getNodeCount() / 10;
69 }
70
71 /*
72 * (non-Javadoc)
73 *
74 * @see
75 * dr.evomodel.operators.SimpleGibbsOperator#doOperation(dr.inference.prior
76 * .Prior, dr.inference.model.Likelihood)
77 */
78 @Override
79 public double doOperation(Likelihood likelihood) {
80 if (pruned) {
81 return prunedGibbsProposal(likelihood);
82 } else {
83 return gibbsProposal(likelihood);
84 }
85 }
86
87 private double gibbsProposal(Likelihood likelihood) {
88
89 final int nodeCount = tree.getNodeCount();
90 final NodeRef root = tree.getRoot();
91
92 NodeRef i;
93
94 do {
95 int indexI = MathUtils.nextInt(nodeCount);
96 i = tree.getNode(indexI);
97 } while (root == i || tree.getParent(i) == root);
98
99 List<Integer> secondNodeIndices = new ArrayList<Integer>();
100 List<Double> probabilities = new ArrayList<Double>();
101 NodeRef j, jP;
102 final NodeRef iP = tree.getParent(i);
103 final double heightIP = tree.getNodeHeight(iP);
104 double sum = 0.0;
105 double backwardLikelihood = calculateTreeLikelihood(likelihood, tree);
106 int offset = (int) -backwardLikelihood;
107 double backward = Math.exp(backwardLikelihood + offset);
108 final NodeRef oldBrother = getOtherChild(tree, iP, i);
109 final NodeRef oldGrandfather = tree.getParent(iP);
110 for (int n = 0; n < nodeCount; n++) {
111 j = tree.getNode(n);
112 if (j != root) {
113 jP = tree.getParent(j);
114
115 if ((i != j) && (tree.getNodeHeight(j) < heightIP)
116 && (heightIP < tree.getNodeHeight(jP))) {
117 secondNodeIndices.add(n);
118
119 pruneAndRegraft(tree, i, iP, j, jP);
120 double prob = Math.exp(calculateTreeLikelihood(likelihood, tree)
121 + offset);
122 probabilities.add(prob);
123 sum += prob;
124
125 pruneAndRegraft(tree, i, iP, oldBrother, oldGrandfather);
126 }
127 }
128 }
129
130 if (sum <= 1E-100) {
131 // hack
132 // the proposals have such a small likelihood that they can be
133 // neglected
134 throw new RuntimeException(
135 "Couldn't find another proposal with a decent likelihood.");
136 }
137
138 double ran = MathUtils.nextDouble() * sum;
139 int index = 0;
140 while (ran > 0.0) {
141 ran -= probabilities.get(index);
142 index++;
143 }
144 index--;
145
146 j = tree.getNode(secondNodeIndices.get(index));
147 jP = tree.getParent(j);
148
149 pruneAndRegraft(tree, i, iP, j, jP);
150
151 double forward = probabilities.get(index);
152
153 double forwardProb = (forward / sum);
154 double backwardProb = (backward / (sum - forward + backward));
155 final double hastingsRatio = Math.log(backwardProb / forwardProb);
156
157 return hastingsRatio;
158 }
159
160 private double prunedGibbsProposal(Likelihood likelihood) {
161 final int nodeCount = tree.getNodeCount();
162 final NodeRef root = tree.getRoot();
163
164 for (int i = 0; i < nodeCount; i++) {
165 scores[i] = Double.NEGATIVE_INFINITY;
166 }
167
168 NodeRef i;
169
170 do {
171 int indexI = MathUtils.nextInt(nodeCount);
172 i = tree.getNode(indexI);
173 } while (root == i || tree.getParent(i) == root);
174
175 List<Integer> secondNodeIndices = new ArrayList<Integer>();
176 List<Double> probabilities = new ArrayList<Double>();
177 NodeRef j, jP;
178 final NodeRef iP = tree.getParent(i);
179 final double heightIP = tree.getNodeHeight(iP);
180 double sum = 0.0;
181 double backwardLikelihood = calculateTreeLikelihood(likelihood,
182 tree);
183 int offset = (int) -backwardLikelihood;
184 double backward = Math.exp(backwardLikelihood + offset);
185 final NodeRef oldBrother = getOtherChild(tree, iP, i);
186 final NodeRef oldGrandfather = tree.getParent(iP);
187 for (int n = 0; n < nodeCount; n++) {
188 j = tree.getNode(n);
189 if (j != root) {
190 jP = tree.getParent(j);
191
192 if ((i != j) && (tree.getNodeHeight(j) < heightIP)
193 && (heightIP < tree.getNodeHeight(jP))
194 && getNodeDistance(iP, jP) <= MAX_DISTANCE) {
195 secondNodeIndices.add(n);
196
197 pruneAndRegraft(tree, i, iP, j, jP);
198 double prob = Math.exp(calculateTreeLikelihood(
199 likelihood, tree)
200 + offset);
201 probabilities.add(prob);
202 scores[n] = prob;
203 sum += prob;
204
205 pruneAndRegraft(tree, i, iP, oldBrother, oldGrandfather);
206 }
207 }
208 }
209
210 if (sum <= 1E-100) {
211 // hack
212 // the proposals have such a small likelihood that they can be
213 // neglected
214 throw new RuntimeException(
215 "Couldn't find another proposal with a decent likelihood.");
216 }
217
218 double ran = MathUtils.nextDouble() * sum;
219 int index = 0;
220 while (ran > 0.0) {
221 ran -= probabilities.get(index);
222 index++;
223 }
224 index--;
225
226 j = tree.getNode(secondNodeIndices.get(index));
227 jP = tree.getParent(j);
228
229 pruneAndRegraft(tree, i, iP, j, jP);
230
231 // now simulate the backward move
232 double sumBackward = 0.0;
233 final NodeRef newBrother = j;
234 final NodeRef newGrandfather = jP;
235 for (int n = 0; n < nodeCount; n++) {
236 j = tree.getNode(n);
237 if (j != root) {
238 jP = tree.getParent(j);
239
240 if ((i != j) && (tree.getNodeHeight(j) < heightIP)
241 && (heightIP < tree.getNodeHeight(jP))
242 && getNodeDistance(iP, jP) <= MAX_DISTANCE) {
243
244 if (scores[n] != Double.NEGATIVE_INFINITY) {
245 sumBackward += scores[n];
246 } else {
247 pruneAndRegraft(tree, i, iP, j, jP);
248 double prob = Math.exp(calculateTreeLikelihood(
249 likelihood, tree)
250 + offset);
251 sumBackward += prob;
252
253 pruneAndRegraft(tree, i, iP, newBrother, newGrandfather);
254 evaluate(likelihood, 1.0);
255 }
256 }
257 }
258 }
259
260 double forward = probabilities.get(index);
261
262 final double forwardProb = (forward / sum);
263 final double backwardProb = (backward / (sumBackward));
264 final double hastingsRatio = Math.log(backwardProb / forwardProb);
265
266 return hastingsRatio;
267 }
268
269 private int getNodeDistance(NodeRef i, NodeRef j) {
270 int count = 0;
271 double heightI = tree.getNodeHeight(i);
272 double heightJ = tree.getNodeHeight(j);
273
274 while (i != j) {
275 count++;
276 if (heightI < heightJ) {
277 i = tree.getParent(i);
278 heightI = tree.getNodeHeight(i);
279 } else {
280 j = tree.getParent(j);
281 heightJ = tree.getNodeHeight(j);
282 }
283 }
284 return count;
285 }
286
287 public void printDistances() {
288 System.out.println("Number of proposed trees in distances:");
289 for (int i = 0; i < distances.length; i++) {
290 System.out.println(i + ")\t\t" + distances[i]);
291 }
292 }
293
294 private double calculateTreeLikelihood(Likelihood likelihood,
295 TreeModel tree) {
296 return evaluate(likelihood, 1.0);
297 }
298
299 private void pruneAndRegraft(TreeModel tree, NodeRef i, NodeRef iP, NodeRef j, NodeRef jP) {
300 tree.beginTreeEdit();
301
302 // the grandfather
303 NodeRef iG = tree.getParent(iP);
304 // the brother
305 NodeRef iB = getOtherChild(tree, iP, i);
306 // prune
307 tree.removeChild(iP, iB);
308 tree.removeChild(iG, iP);
309 tree.addChild(iG, iB);
310
311 // reattach
312 tree.removeChild(jP, j);
313 tree.addChild(iP, j);
314 tree.addChild(jP, iP);
315
316 // ****************************************************
317
318 tree.endTreeEdit();
319 }
320
321 /**
322 * @param tree
323 * the tree
324 * @param parent
325 * the parent
326 * @param child
327 * the child that you want the sister of
328 * @return the other child of the given parent.
329 */
330 protected NodeRef getOtherChild(Tree tree, NodeRef parent, NodeRef child) {
331
332 if (tree.getChild(parent, 0) == child) {
333 return tree.getChild(parent, 1);
334 } else {
335 return tree.getChild(parent, 0);
336 }
337 }
338
339 /*
340 * (non-Javadoc)
341 *
342 * @see dr.evomodel.operators.SimpleGibbsOperator#getOperatorName()
343 */
344 @Override
345 public String getOperatorName() {
346 return GibbsPruneAndRegraftParser.GIBBS_PRUNE_AND_REGRAFT;
347 }
348
349 /*
350 * (non-Javadoc)
351 *
352 * @see dr.evomodel.operators.SimpleGibbsOperator#getStepCount()
353 */
354 @Override
355 public int getStepCount() {
356 return 0;
357 }
358
359 }