Debian Codegraph @

src/dr/evomodel/operators/ImportancePruneAndRegraft.java

raw symbols SCIP
1 /*
2 * ImportancePruneAndRegraft.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.ImportancePruneAndRegraftParser;
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 */
46 // Cleaning out untouched stuff. Can be resurrected if needed
47 @Deprecated
48 public class ImportancePruneAndRegraft extends AbstractTreeOperator {
49
50 public final int SAMPLE_EVERY = 10;
51
52 private final TreeModel tree;
53
54 private final int samples;
55
56 private int sampleCount;
57
58 private boolean burnin = false;
59
60 private final ConditionalCladeFrequency probabilityEstimater;
61
62 private final OperatorSchedule schedule;
63
64 /**
65 *
66 */
67 public ImportancePruneAndRegraft(TreeModel tree, double weight,
68 int samples, int epsilon) {
69 this.tree = tree;
70 setWeight(weight);
71 this.samples = samples;
72 sampleCount = 0;
73 probabilityEstimater = new ConditionalCladeFrequency(tree, epsilon);
74 schedule = getOperatorSchedule(tree);
75 }
76
77 /**
78 *
79 */
80 public ImportancePruneAndRegraft(TreeModel tree, double weight, int samples) {
81 this.tree = tree;
82 setWeight(weight);
83 this.samples = samples;
84 sampleCount = 0;
85 // double epsilon = 1 - Math.pow(0.5, samples);
86 double epsilon = 1 - Math.pow(0.5, 1.0 / samples);
87 // double epsilon = 1;
88 probabilityEstimater = new ConditionalCladeFrequency(tree, epsilon);
89 schedule = getOperatorSchedule(tree);
90 }
91
92 private OperatorSchedule getOperatorSchedule(TreeModel treeModel) {
93
94 ExchangeOperator narrowExchange = new ExchangeOperator(
95 ExchangeOperator.NARROW, treeModel, 10);
96 ExchangeOperator wideExchange = new ExchangeOperator(
97 ExchangeOperator.WIDE, treeModel, 3);
98 SubtreeSlideOperator subtreeSlide = new SubtreeSlideOperator(treeModel,
99 10.0, 1.0, true, false, false, false, AdaptationMode.ADAPTATION_ON, AdaptableMCMCOperator.DEFAULT_ADAPTATION_TARGET);
100 NNI nni = new NNI(treeModel, 10.0);
101 WilsonBalding wilsonBalding = new WilsonBalding(treeModel, 3.0);
102 FNPR fnpr = new FNPR(treeModel, 5.0);
103
104 OperatorSchedule schedule = new SimpleOperatorSchedule();
105 schedule.addOperator(narrowExchange);
106 schedule.addOperator(wideExchange);
107 schedule.addOperator(subtreeSlide);
108 schedule.addOperator(nni);
109 schedule.addOperator(wilsonBalding);
110 schedule.addOperator(fnpr);
111
112 return schedule;
113 }
114
115 /*
116 * (non-Javadoc)
117 *
118 * @see dr.inference.operators.SimpleMCMCOperator#doOperation()
119 */
120 @Override
121 public double doOperation() {
122 if (!burnin) {
123 if (sampleCount < samples * SAMPLE_EVERY) {
124 sampleCount++;
125 if (sampleCount % SAMPLE_EVERY == 0) {
126 probabilityEstimater.addTree(tree);
127 }
128 setAcceptCount(0);
129 setRejectCount(0);
130 setTransitions(0);
131
132 return doUnguidedOperation();
133
134 } else {
135 return importancePruneAndRegraft();
136 }
137 } else {
138
139 return doUnguidedOperation();
140
141 }
142 }
143
144 private double doUnguidedOperation() {
145 int index = schedule.getNextOperatorIndex();
146 SimpleMCMCOperator operator = (SimpleMCMCOperator) schedule.getOperator(index);
147
148 return operator.doOperation();
149 }
150
151 private double importancePruneAndRegraft() {
152 final int nodeCount = tree.getNodeCount();
153 final NodeRef root = tree.getRoot();
154
155 NodeRef i;
156
157 do {
158 int indexI = MathUtils.nextInt(nodeCount);
159 i = tree.getNode(indexI);
160 } while (root == i || tree.getParent(i) == root);
161
162 List<Integer> secondNodeIndices = new ArrayList<Integer>();
163 List<Double> probabilities = new ArrayList<Double>();
164 NodeRef j, iP, jP;
165 iP = tree.getParent(i);
166 double iParentHeight = tree.getNodeHeight(iP);
167 double sum = 0.0;
168 double backwardLikelihood = calculateTreeProbability(tree);
169 int offset = (int) -backwardLikelihood;
170 double backward = Math.exp(backwardLikelihood + offset);
171 final NodeRef oldBrother = getOtherChild(tree, iP, i);
172 final NodeRef oldGrandfather = tree.getParent(iP);
173
174 tree.beginTreeEdit();
175 for (int n = 0; n < nodeCount; n++) {
176 j = tree.getNode(n);
177 if (j != root) {
178 jP = tree.getParent(j);
179
180 if ((iP != jP)
181 && (tree.getNodeHeight(j) < iParentHeight && iParentHeight < tree
182 .getNodeHeight(jP))) {
183 secondNodeIndices.add(n);
184
185 pruneAndRegraft(tree, i, iP, j, jP);
186 double prob = Math.exp(calculateTreeProbability(tree)
187 + offset);
188 probabilities.add(prob);
189 sum += prob;
190
191 pruneAndRegraft(tree, i, iP, oldBrother, oldGrandfather);
192 }
193 }
194 }
195
196 double ran = MathUtils.nextDouble() * sum;
197 int index = 0;
198 while (ran > 0.0) {
199 ran -= probabilities.get(index);
200 index++;
201 }
202 index--;
203
204 j = tree.getNode(secondNodeIndices.get(index));
205 jP = tree.getParent(j);
206
207 if (iP != jP) {
208 pruneAndRegraft(tree, i, iP, j, jP);
209 tree.pushTreeChangedEvent(i);
210 }
211
212 tree.endTreeEdit();
213
214 // AR - not sure whether this check is necessary
215 try {
216 tree.checkTreeIsValid();
217 } catch (InvalidTreeException e) {
218 throw new RuntimeException(e.getMessage());
219 }
220
221 double forward = probabilities.get(index);
222
223 // tree.pushTreeChangedEvent(jP);
224 // tree.pushTreeChangedEvent(oldGrandfather);
225 tree.pushTreeChangedEvent(i);
226
227 double forwardProb = (forward / sum);
228 double backwardProb = (backward / (sum - forward + backward));
229 final double hastingsRatio = Math.log(backwardProb / forwardProb);
230
231 return hastingsRatio;
232 }
233
234 private void pruneAndRegraft(TreeModel tree, NodeRef i, NodeRef iP,
235 NodeRef j, NodeRef jP) {
236 // tree.beginTreeEdit();
237
238 // the grandfather
239 NodeRef iG = tree.getParent(iP);
240 // the brother
241 NodeRef iB = getOtherChild(tree, iP, i);
242 // prune
243 tree.removeChild(iP, iB);
244 tree.removeChild(iG, iP);
245 tree.addChild(iG, iB);
246
247 // reattach
248 tree.removeChild(jP, j);
249 tree.addChild(iP, j);
250 tree.addChild(jP, iP);
251
252 }
253
254 private double calculateTreeProbability(Tree tree) {
255 return probabilityEstimater.getTreeProbability(tree);
256 }
257
258 public void setBurnin(boolean burnin) {
259 this.burnin = burnin;
260 }
261
262 /*
263 * (non-Javadoc)
264 *
265 * @see dr.inference.operators.SimpleMCMCOperator#getOperatorName()
266 */
267 @Override
268 public String getOperatorName() {
269 return ImportancePruneAndRegraftParser.IMPORTANCE_PRUNE_AND_REGRAFT;
270 }
271
272 /*
273 * (non-Javadoc)
274 *
275 * @see dr.inference.operators.MCMCOperator#getPerformanceSuggestion()
276 */
277 public String getPerformanceSuggestion() {
278 // TODO Auto-generated method stub
279 return "";
280 }
281 }