| 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.evomodel.tree.TreeModel; |
| 31 |
import dr.evomodelxml.operators.FunkyPriorMixerOperatorParser; |
| 32 |
import dr.inference.model.Bounds; |
| 33 |
import dr.inference.model.Parameter; |
| 34 |
import dr.inference.operators.*; |
| 35 |
import dr.inferencexml.operators.RandomWalkOperatorParser; |
| 36 |
import dr.math.MathUtils; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
@Deprecated |
| 44 |
public class FunkyPriorMixerOperator extends |
| 45 |
SimpleMCMCOperator { |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public FunkyPriorMixerOperator(TreeModel treeModel, Parameter parameter, double windowSize, RandomWalkOperator.BoundaryCondition bc, |
| 50 |
double weight, AdaptationMode mode) { |
| 51 |
|
| 52 |
|
| 53 |
this.treeModel = treeModel; |
| 54 |
|
| 55 |
|
| 56 |
this.parameter = parameter; |
| 57 |
this.windowSize = windowSize; |
| 58 |
this.condition = bc; |
| 59 |
|
| 60 |
setWeight(weight); |
| 61 |
} |
| 62 |
|
| 63 |
public final double doOperation() { |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
int iterations = 100; |
| 115 |
|
| 116 |
for (int i = 0; i < iterations; i++) { |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
final int index = MathUtils.nextInt(parameter.getDimension()); |
| 124 |
final Bounds<Double> bounds = parameter.getBounds(); |
| 125 |
final double lower = bounds.getLowerLimit(index); |
| 126 |
final double upper = bounds.getUpperLimit(index); |
| 127 |
final double newValue = (MathUtils.nextDouble() * (upper - lower)) + lower; |
| 128 |
|
| 129 |
|
| 130 |
parameter.setParameterValue(index, newValue); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
return |
| 138 |
0.0; |
| 139 |
|
| 140 |
|
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
private double recursivelyFindNodeMinHeights(Tree tree, NodeRef node, double[] minNodeHeights) { |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
double minHeight; |
| 149 |
|
| 150 |
if (tree.isExternal(node)) |
| 151 |
minHeight = tree.getNodeHeight(node); |
| 152 |
else { |
| 153 |
double minHeightChild0 = recursivelyFindNodeMinHeights(tree, tree.getChild(node, 0), minNodeHeights); |
| 154 |
double minHeightChild1 = recursivelyFindNodeMinHeights(tree, tree.getChild(node, 1), minNodeHeights); |
| 155 |
minHeight = (minHeightChild0 > minHeightChild1) ? minHeightChild0 : minHeightChild1; |
| 156 |
} |
| 157 |
|
| 158 |
minNodeHeights[node.getNumber()] = minHeight; |
| 159 |
return minHeight; |
| 160 |
} |
| 161 |
|
| 162 |
private void recursivelyDrawNodeHeights(TreeModel tree, NodeRef node, |
| 163 |
double oldParentHeight, double newParentHeight, double[] minNodeHeights) { |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
if (tree.isExternal(node)) |
| 168 |
return; |
| 169 |
|
| 170 |
final double oldNodeHeight = tree.getNodeHeight(node); |
| 171 |
double newNodeHeight = oldNodeHeight; |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
if (!tree.isRoot(node)) { |
| 176 |
|
| 177 |
double minHeight = minNodeHeights[node.getNumber()]; |
| 178 |
|
| 179 |
double oldDiff = oldParentHeight - minHeight; |
| 180 |
double newDiff = newParentHeight - minHeight; |
| 181 |
|
| 182 |
newNodeHeight = MathUtils.nextDouble() * newDiff + minHeight; |
| 183 |
logForwardDensity -= Math.log(newDiff); |
| 184 |
logBackwardDensity -= Math.log(oldDiff); |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
tree.setNodeHeight(node, newNodeHeight); |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
recursivelyDrawNodeHeights(tree, tree.getChild(node, 0), oldNodeHeight, newNodeHeight, minNodeHeights); |
| 194 |
|
| 195 |
recursivelyDrawNodeHeights(tree, tree.getChild(node, 1), oldNodeHeight, newNodeHeight, minNodeHeights); |
| 196 |
|
| 197 |
|
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
public final String getOperatorName() { |
| 203 |
return FunkyPriorMixerOperatorParser.FUNKY_OPERATOR; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
public double getRawParameter() { |
| 217 |
return windowSize; |
| 218 |
} |
| 219 |
|
| 220 |
public double getTargetAcceptanceProbability() { |
| 221 |
return 0.234; |
| 222 |
} |
| 223 |
|
| 224 |
public double getMinimumAcceptanceLevel() { |
| 225 |
return 0.1; |
| 226 |
} |
| 227 |
|
| 228 |
public double getMaximumAcceptanceLevel() { |
| 229 |
return 0.4; |
| 230 |
} |
| 231 |
|
| 232 |
public double getMinimumGoodAcceptanceLevel() { |
| 233 |
return 0.20; |
| 234 |
} |
| 235 |
|
| 236 |
public double getMaximumGoodAcceptanceLevel() { |
| 237 |
return 0.30; |
| 238 |
} |
| 239 |
|
| 240 |
public final String getPerformanceSuggestion() { |
| 241 |
|
| 242 |
double prob = MCMCOperator.Utils.getAcceptanceProbability(this); |
| 243 |
double targetProb = getTargetAcceptanceProbability(); |
| 244 |
|
| 245 |
|
| 246 |
double ws = 2; |
| 247 |
|
| 248 |
if (prob < getMinimumGoodAcceptanceLevel()) { |
| 249 |
return "Try decreasing windowSize to about " + ws; |
| 250 |
} else if (prob > getMaximumGoodAcceptanceLevel()) { |
| 251 |
return "Try increasing windowSize to about " + ws; |
| 252 |
} else return ""; |
| 253 |
} |
| 254 |
|
| 255 |
public String toString() { |
| 256 |
return RandomWalkOperatorParser.RANDOM_WALK_OPERATOR + "(" + parameter.getParameterName() + ", " + windowSize + ", " + getWeight() + ")"; |
| 257 |
} |
| 258 |
|
| 259 |
public int getStepCount() { |
| 260 |
return 0; |
| 261 |
} |
| 262 |
|
| 263 |
private final TreeModel treeModel; |
| 264 |
private final Parameter parameter; |
| 265 |
private double windowSize; |
| 266 |
private final RandomWalkOperator.BoundaryCondition condition; |
| 267 |
|
| 268 |
private Double logForwardDensity; |
| 269 |
private Double logBackwardDensity; |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
} |