| 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.evomodel.tree.TreeModel; |
| 30 |
import dr.evomodelxml.operators.RateScaleOperatorParser; |
| 31 |
import dr.inference.operators.AbstractAdaptableOperator; |
| 32 |
import dr.inference.operators.AdaptationMode; |
| 33 |
import dr.inference.operators.OperatorUtils; |
| 34 |
import dr.math.MathUtils; |
| 35 |
|
| 36 |
import java.util.ArrayList; |
| 37 |
import java.util.List; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public class RateScaleOperator extends AbstractAdaptableOperator { |
| 45 |
|
| 46 |
private TreeModel tree; |
| 47 |
|
| 48 |
private boolean noRoot; |
| 49 |
|
| 50 |
public RateScaleOperator(TreeModel tree, double scale, boolean noRoot, AdaptationMode mode) { |
| 51 |
|
| 52 |
super(mode); |
| 53 |
|
| 54 |
this.scaleFactor = scale; |
| 55 |
this.tree = tree; |
| 56 |
|
| 57 |
this.noRoot = noRoot; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public final double doOperation() { |
| 65 |
|
| 66 |
final double scale = (scaleFactor + (MathUtils.nextDouble() * ((1.0 / scaleFactor) - scaleFactor))); |
| 67 |
|
| 68 |
int index; |
| 69 |
if (noRoot) { |
| 70 |
do { |
| 71 |
index = MathUtils.nextInt(tree.getNodeCount()); |
| 72 |
} while (index == tree.getRoot().getNumber()); |
| 73 |
} else { |
| 74 |
index = MathUtils.nextInt(tree.getNodeCount()); |
| 75 |
} |
| 76 |
|
| 77 |
List<NodeRef> listNode = new ArrayList<NodeRef>(); |
| 78 |
getSubtree(listNode, tree.getNode(index)); |
| 79 |
|
| 80 |
double oldValue, newValue; |
| 81 |
double logq = 0; |
| 82 |
for (NodeRef node : listNode) { |
| 83 |
|
| 84 |
oldValue = tree.getNodeRate(node); |
| 85 |
newValue = oldValue * scale; |
| 86 |
|
| 87 |
tree.setNodeRate(node, newValue); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
logq = (listNode.size() - 2) * Math.log(scale); |
| 92 |
|
| 93 |
return logq; |
| 94 |
} |
| 95 |
|
| 96 |
void getSubtree(List<NodeRef> listNode, NodeRef parent) { |
| 97 |
|
| 98 |
listNode.add(parent); |
| 99 |
int nbChildren = tree.getChildCount(parent); |
| 100 |
for (int c = 0; c < nbChildren; c++) { |
| 101 |
getSubtree(listNode, tree.getChild(parent, c)); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
void cleanupOperation(double newValue, double oldValue) { |
| 112 |
|
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
public final String getOperatorName() { |
| 117 |
return "rateScale"; |
| 118 |
} |
| 119 |
|
| 120 |
public double getAdaptableParameter() { |
| 121 |
return Math.log(1.0 / scaleFactor - 1.0); |
| 122 |
} |
| 123 |
|
| 124 |
public void setAdaptableParameter(double value) { |
| 125 |
scaleFactor = 1.0 / (Math.exp(value) + 1.0); |
| 126 |
} |
| 127 |
|
| 128 |
public double getRawParameter() { |
| 129 |
return scaleFactor; |
| 130 |
} |
| 131 |
|
| 132 |
public double getScaleFactor() { |
| 133 |
return scaleFactor; |
| 134 |
} |
| 135 |
|
| 136 |
public String getAdaptableParameterName() { |
| 137 |
return "scaleFactor"; |
| 138 |
} |
| 139 |
|
| 140 |
public String toString() { |
| 141 |
return RateScaleOperatorParser.SCALE_OPERATOR + "(" + " [" + scaleFactor + ", " + (1.0 / scaleFactor) + "]"; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
private double scaleFactor = 0.5; |
| 147 |
} |