| 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.evomodel.treelikelihood.BeagleTreeLikelihood; |
| 29 |
import dr.inference.operators.GibbsOperator; |
| 30 |
import dr.inference.operators.SimpleMCMCOperator; |
| 31 |
import jebl.math.Random; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
@Deprecated |
| 38 |
public class PatternWeightIncrementOperator extends SimpleMCMCOperator implements GibbsOperator { |
| 39 |
|
| 40 |
public static final String PATTERN_WEIGHT_INCREMENT_OPERATOR = "patternWeightIncrementOperator"; |
| 41 |
|
| 42 |
public PatternWeightIncrementOperator(BeagleTreeLikelihood treeLikelihood, double weight) { |
| 43 |
this.treeLikelihood = treeLikelihood; |
| 44 |
setWeight(weight); |
| 45 |
|
| 46 |
finalPatternWeights = treeLikelihood.getPatternWeights(); |
| 47 |
|
| 48 |
double[] weights = new double[finalPatternWeights.length]; |
| 49 |
treeLikelihood.setPatternWeights(finalPatternWeights); |
| 50 |
|
| 51 |
} |
| 52 |
|
| 53 |
public double doOperation() { |
| 54 |
|
| 55 |
if (allPatternsAdded) { |
| 56 |
return 0.0; |
| 57 |
} |
| 58 |
|
| 59 |
double[] weights = treeLikelihood.getPatternWeights(); |
| 60 |
double[] w = new double[weights.length]; |
| 61 |
|
| 62 |
double sum = 0.0; |
| 63 |
for (int i = 0; i < weights.length; i++) { |
| 64 |
w[i] = finalPatternWeights[i] - weights[i]; |
| 65 |
sum += w[i]; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
if (sum < 1.0) { |
| 70 |
allPatternsAdded = true; |
| 71 |
System.out.println("PatternWeightIncrementOperator - All sites added"); |
| 72 |
return 0.0; |
| 73 |
} |
| 74 |
|
| 75 |
for (int i = 0; i < weights.length; i++) { |
| 76 |
w[i] /= sum; |
| 77 |
if (i > 0) { |
| 78 |
w[i] += w[i - 1]; |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
int r = Random.randomChoice(w); |
| 83 |
|
| 84 |
weights[r] ++; |
| 85 |
|
| 86 |
if (weights[r] > finalPatternWeights[r]) { |
| 87 |
throw new RuntimeException("Pattern weight exceeding final weight"); |
| 88 |
} |
| 89 |
|
| 90 |
treeLikelihood.setPatternWeights(weights); |
| 91 |
|
| 92 |
treeLikelihood.makeDirty(); |
| 93 |
|
| 94 |
return 0; |
| 95 |
} |
| 96 |
|
| 97 |
public void reject() { |
| 98 |
super.reject(); |
| 99 |
} |
| 100 |
|
| 101 |
public String getOperatorName() { |
| 102 |
return PATTERN_WEIGHT_INCREMENT_OPERATOR; |
| 103 |
} |
| 104 |
|
| 105 |
private final BeagleTreeLikelihood treeLikelihood; |
| 106 |
private final double[] finalPatternWeights; |
| 107 |
|
| 108 |
private boolean allPatternsAdded = false; |
| 109 |
|
| 110 |
} |