| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 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 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 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 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public double doOperation() { |
| 80 |
double logq; |
| 81 |
|
| 82 |
final NodeRef root = tree.getRoot(); |
| 83 |
|
| 84 |
|
| 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 |
|
| 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 |
|
| 103 |
double parentHeight = tree.getNodeHeight(iP); |
| 104 |
|
| 105 |
|
| 106 |
destinations = getIntersectingEdges(tree, parentHeight); |
| 107 |
|
| 108 |
if (destinations.size() == 0) { |
| 109 |
|
| 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 |
|
| 123 |
r = MathUtils.randomChoicePDF(pdf); |
| 124 |
forwardProbability = pdf[r]; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 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 |
|
| 137 |
tree.removeChild(iP, CiP); |
| 138 |
tree.removeChild(PiP, iP); |
| 139 |
tree.addChild(PiP, CiP); |
| 140 |
|
| 141 |
|
| 142 |
tree.removeChild(jP, j); |
| 143 |
|
| 144 |
|
| 145 |
tree.addChild(iP, j); |
| 146 |
|
| 147 |
|
| 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 |
|
| 160 |
logq = Math.log(reverseProbability) - Math.log(forwardProbability); |
| 161 |
} |
| 162 |
|
| 163 |
return logq; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 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 |
|
| 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 |
|
| 215 |
lpdfs[i] = dr.math.distributions.TDistribution.logPDF(heights[i], 0, size, 1); |
| 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 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 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 |
} |