Debian Codegraph @

src/dr/evomodel/operators/SubtreeJumpOperator.java

raw symbols SCIP
1 /*
2 * FixedHeightSubtreePruneRegraftOperator.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.evomodel.operators;
27
28 import dr.evolution.tree.NodeRef;
29 import dr.evolution.tree.Tree;
30 import dr.evolution.tree.TreeUtils;
31 import dr.evomodel.tree.TreeModel;
32 import dr.evomodelxml.operators.SubtreeJumpOperatorParser;
33 import dr.inference.operators.AdaptableMCMCOperator;
34 import dr.inference.operators.AdaptationMode;
35 import dr.inference.operators.OperatorUtils;
36 import dr.math.MathUtils;
37
38 import java.util.ArrayList;
39 import java.util.List;
40
41 /**
42 * A tunable version of the fixed height subtree prune regraft move.
43 *
44 * @author Andrew Rambaut
45 * @author Luiz Max Carvalho
46 * @version $Id$
47 */
48 public class SubtreeJumpOperator extends AbstractTreeOperator {
49
50 private double size = 1.0;
51 private double accP = 0.234;
52 private boolean uniform = false;
53
54 private final TreeModel tree;
55 private final AdaptationMode mode;
56
57
58 /**
59 * Constructor
60 * @param tree
61 * @param weight
62 * @param size: the variance of a half normal used to compute distance weights (as a rule, larger size, bolder moves)
63 * @param mode
64 */
65 public SubtreeJumpOperator(TreeModel tree, double weight, double size, double accP, boolean uniform, AdaptationMode mode) {
66 this.tree = tree;
67 setWeight(weight);
68 this.size = size;
69 this.accP = accP;
70 this.uniform = uniform;
71 this.mode = mode;
72
73 }
74 /**
75 * Do a subtree jump move.
76 *
77 * @return the log-transformed hastings ratio
78 */
79 public double doOperation() {
80 double logq;
81
82 final NodeRef root = tree.getRoot();
83
84 // double maxHeight = tree.getNodeHeight(root);
85
86 NodeRef i;
87 NodeRef iP = null;
88 NodeRef CiP = null;
89 NodeRef PiP = null;
90 List<NodeRef> destinations = null;
91
92 do {
93 // 1. choose a random node avoiding root or child of root
94 i = tree.getNode(MathUtils.nextInt(tree.getNodeCount()));
95
96 } while (root == i || tree.getParent(i) == root);
97
98 iP = tree.getParent(i);
99 CiP = getOtherChild(tree, iP, i);
100 PiP = tree.getParent(iP);
101
102 // get the height of the parent
103 double parentHeight = tree.getNodeHeight(iP);
104
105 // get a list of all edges that intersect this height
106 destinations = getIntersectingEdges(tree, parentHeight);
107
108 if (destinations.size() == 0) {
109 // if there are no destinations available then reject the move
110 return Double.NEGATIVE_INFINITY;
111 }
112
113 double forwardProbability;
114 int r;
115
116 if (uniform) {
117 r = MathUtils.nextInt(destinations.size());
118 forwardProbability = 1.0;
119 } else {
120 double[] pdf = getDestinationProbabilities(tree, iP, parentHeight, destinations, size);
121
122 // pick uniformly from this list
123 r = MathUtils.randomChoicePDF(pdf);
124 forwardProbability = pdf[r];
125 }
126
127 // remove the target node and its sibling (shouldn't be there because their parent's height is exactly equal to the target height).
128 destinations.remove(i);
129 destinations.remove(CiP);
130
131 final NodeRef j = destinations.get(r);
132 final NodeRef jP = tree.getParent(j);
133
134 tree.beginTreeEdit();
135
136 // remove the parent of i by connecting its sibling to its grandparent.
137 tree.removeChild(iP, CiP);
138 tree.removeChild(PiP, iP);
139 tree.addChild(PiP, CiP);
140
141 // remove destination edge j from its parent
142 tree.removeChild(jP, j);
143
144 // add destination edge to the parent of i
145 tree.addChild(iP, j);
146
147 // and add the parent of i as a child of the former parent of j
148 tree.addChild(jP, iP);
149
150 tree.endTreeEdit();
151
152 if (uniform) {
153 logq = 0.0;
154 } else {
155
156 final List<NodeRef> reverseDestinations = getIntersectingEdges(tree, parentHeight);
157 double reverseProbability = getReverseProbability(tree, CiP, j, parentHeight, reverseDestinations, size);
158
159 // hastings ratio = reverse Prob / forward Prob
160 logq = Math.log(reverseProbability) - Math.log(forwardProbability);
161 }
162
163 return logq;
164 }
165
166 /**
167 * Gets a list of edges that subtend the given height
168 * @param tree
169 * @param height
170 * @return
171 */
172 private List<NodeRef> getIntersectingEdges(Tree tree, double height) {
173
174 List<NodeRef> intersectingEdges = new ArrayList<NodeRef>();
175
176 for (int i = 0; i < tree.getNodeCount(); i++) {
177 final NodeRef node = tree.getNode(i);
178 final NodeRef parent = tree.getParent(node);
179
180 // The original node and its sibling will not be included because their height is exactly equal to the target height
181 if (parent != null && tree.getNodeHeight(node) < height && tree.getNodeHeight(parent) > height) {
182 intersectingEdges.add(node);
183 }
184 }
185 return intersectingEdges;
186 }
187
188 private double[] getDestinationProbabilities(Tree tree, NodeRef sourceNode, double height,
189 List<NodeRef> intersectingEdges, double size) {
190 double[] weights = new double[intersectingEdges.size()];
191 getNormalizedProbabilities(tree, sourceNode, height, null, intersectingEdges, size, weights);
192 return weights;
193 }
194
195 private double getReverseProbability(Tree tree, NodeRef originalNode, NodeRef targetNode, double height,
196 List<NodeRef> intersectingEdges, double size) {
197
198 double[] weights = new double[intersectingEdges.size()];
199 int originalIndex = getNormalizedProbabilities(tree, targetNode, height, originalNode, intersectingEdges, size, weights);
200 return weights[originalIndex];
201 }
202
203 private int getNormalizedProbabilities(Tree tree, NodeRef node0, double height, NodeRef originalNode,
204 List<NodeRef> intersectingEdges, double size, double[] weights) {
205 double[] heights = new double[intersectingEdges.size()];
206 double[] lpdfs = new double[intersectingEdges.size()];
207 int originalIndex = -1;
208 int i = 0;
209 for (NodeRef node1 : intersectingEdges) {
210 assert (node1 != node0);
211
212 NodeRef mrcaNode = TreeUtils.getCommonAncestor(tree, node0, node1);
213 heights[i] = tree.getNodeHeight(mrcaNode) - height;
214 // lpdfs[i] = NormalDistribution.logPdf(heights[i], 0, size);
215 lpdfs[i] = dr.math.distributions.TDistribution.logPDF(heights[i], 0, size, 1); // Cauchy
216
217 if (node1 == originalNode) {
218 originalIndex = i;
219 }
220 i++;
221 }
222 double[] normWeights = normLog(weights, 1E-20);
223
224 for (int j = 0; j < weights.length; j++) {
225 weights[j] = normWeights[j];
226 }
227
228 return originalIndex;
229 }
230
231 public static double [] normLog(double[] x, double epsilon){
232 /* x is a vector of log values
233 * @return its normalised version y (sum(y) = 1) */
234
235 /* sorting only makes sense for very large n*/
236 // double[] sortedX = x;
237 // java.util.Arrays.sort(sortedX);
238 // double maxVal = sortedX[sortedX.length];
239
240 double maxVal = Double.NEGATIVE_INFINITY;
241 for (double a : x) {
242 maxVal = Math.max(a, maxVal);
243 }
244
245 double[] alpha = new double[x.length];
246
247 double sum = 0.0;
248 double factor = Math.log(epsilon)-Math.log(x.length);
249 for (int k = 0; k < alpha.length; k++) {
250 alpha[k] = ( (x[k]-maxVal) >= factor ? Math.exp(x[k]-maxVal) : 0);
251 sum += alpha[k];
252 }
253 double[] y = new double[alpha.length];
254
255 for (int j = 0; j < y.length; j++) {
256 y[j] = alpha[j]/sum;
257 }
258 return y;
259 }
260
261 public double getSize() {
262 return size;
263 }
264
265 public void setSize(double size) {
266 this.size = size;
267 }
268
269 public double getAdaptableParameter() {
270 return Math.log(getSize());
271 }
272
273 public void setAdaptableParameter(double value) {
274 setSize(Math.exp(value));
275 }
276
277 public double getRawParameter() {
278 return getSize();
279 }
280
281 public AdaptationMode getMode() {
282 return mode;
283 }
284
285 public String getOperatorName() {
286 return SubtreeJumpOperatorParser.SUBTREE_JUMP + "(" + tree.getId() + ")";
287 }
288 }