| 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.oldevomodel.clock.RateEvolutionLikelihood; |
| 30 |
import dr.evomodel.tree.TreeModel; |
| 31 |
import dr.evomodelxml.operators.RateSampleOperatorParser; |
| 32 |
import dr.inference.operators.SimpleMCMCOperator; |
| 33 |
import dr.math.MathUtils; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class RateSampleOperator extends SimpleMCMCOperator { |
| 42 |
|
| 43 |
private TreeModel tree; |
| 44 |
|
| 45 |
private boolean sampleAll; |
| 46 |
|
| 47 |
RateEvolutionLikelihood rateEvolution; |
| 48 |
|
| 49 |
public RateSampleOperator(TreeModel tree, boolean sampleAll, RateEvolutionLikelihood rateEvolution) { |
| 50 |
|
| 51 |
this.tree = tree; |
| 52 |
this.sampleAll = sampleAll; |
| 53 |
this.rateEvolution = rateEvolution; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public final double doOperation() { |
| 60 |
|
| 61 |
int index; |
| 62 |
if (sampleAll) { |
| 63 |
index = tree.getRoot().getNumber(); |
| 64 |
} else { |
| 65 |
do { |
| 66 |
index = MathUtils.nextInt(tree.getNodeCount()); |
| 67 |
} while (tree.isExternal(tree.getNode(index))); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
double logBackward = rateEvolution.getLogLikelihood(); |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
sampleNode(tree.getNode(index)); |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
double logForward = rateEvolution.getLogLikelihood(); |
| 81 |
|
| 82 |
return logBackward - logForward; |
| 83 |
} |
| 84 |
|
| 85 |
void sampleSubtree(NodeRef parent) { |
| 86 |
|
| 87 |
int nbChildren = tree.getChildCount(parent); |
| 88 |
for (int c = 0; c < nbChildren; c++) { |
| 89 |
final NodeRef node = tree.getChild(parent, c); |
| 90 |
rateEvolution.sampleRate(node); |
| 91 |
sampleSubtree(node); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
void sampleSister(NodeRef parent) { |
| 96 |
|
| 97 |
int nbChildren = tree.getChildCount(parent); |
| 98 |
for (int c = 0; c < nbChildren; c++) { |
| 99 |
final NodeRef node = tree.getChild(parent, c); |
| 100 |
rateEvolution.sampleRate(node); |
| 101 |
} |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
void sampleNode(NodeRef parent) { |
| 106 |
|
| 107 |
int nbChildren = tree.getChildCount(parent); |
| 108 |
|
| 109 |
if (nbChildren > 0) { |
| 110 |
final int c = MathUtils.nextInt(nbChildren); |
| 111 |
|
| 112 |
final NodeRef node = tree.getChild(parent, c); |
| 113 |
rateEvolution.sampleRate(node); |
| 114 |
} |
| 115 |
|
| 116 |
} |
| 117 |
|
| 118 |
void sampleOne(NodeRef parent) { |
| 119 |
|
| 120 |
int nbChildren = tree.getChildCount(parent); |
| 121 |
|
| 122 |
if (nbChildren > 0) { |
| 123 |
|
| 124 |
final NodeRef node = tree.getChild(parent, 0); |
| 125 |
rateEvolution.sampleRate(node); |
| 126 |
} |
| 127 |
|
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
void cleanupOperation(double newValue, double oldValue) { |
| 137 |
|
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
public final String getOperatorName() { |
| 142 |
return "rateSample"; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
public double getTargetAcceptanceProbability() { |
| 147 |
return 0.234; |
| 148 |
} |
| 149 |
|
| 150 |
public double getMinimumAcceptanceLevel() { |
| 151 |
return 0.1; |
| 152 |
} |
| 153 |
|
| 154 |
public double getMaximumAcceptanceLevel() { |
| 155 |
return 0.4; |
| 156 |
} |
| 157 |
|
| 158 |
public double getMinimumGoodAcceptanceLevel() { |
| 159 |
return 0.20; |
| 160 |
} |
| 161 |
|
| 162 |
public double getMaximumGoodAcceptanceLevel() { |
| 163 |
return 0.30; |
| 164 |
} |
| 165 |
|
| 166 |
public final String getPerformanceSuggestion() { |
| 167 |
|
| 168 |
return "No suggestions"; |
| 169 |
} |
| 170 |
|
| 171 |
public String toString() { |
| 172 |
return RateSampleOperatorParser.SAMPLE_OPERATOR + "("; |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
} |