| 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.arg.operators; |
| 27 |
|
| 28 |
import dr.evomodel.arg.ARGModel; |
| 29 |
import dr.evomodelxml.tree.TreeModelParser; |
| 30 |
import dr.inference.model.CompoundParameter; |
| 31 |
import dr.inference.operators.SimpleMCMCOperator; |
| 32 |
import dr.math.MathUtils; |
| 33 |
|
| 34 |
import java.util.logging.Logger; |
| 35 |
|
| 36 |
public class ARGReassortmentOperator extends SimpleMCMCOperator { |
| 37 |
|
| 38 |
public static final String ADD_PROBABILITY = "addProbability"; |
| 39 |
public static final String ARG_REASSORTMENT_OPERATOR = "argReassortmentOperator"; |
| 40 |
public static final String INTERNAL_NODES = "internalNodes"; |
| 41 |
public static final String INTERNAL_AND_ROOT = "internalNodesPlusRoot"; |
| 42 |
public static final String NODE_RATES = TreeModelParser.NODE_RATES; |
| 43 |
public static final String CHOOSE_BRANCHES_FIRST = "chooseBranchesFirst"; |
| 44 |
public static final double LOG_TWO = Math.log(2.0); |
| 45 |
|
| 46 |
private double singlePartitionProbability = 0.0; |
| 47 |
private double probBelowRoot = 0.9; |
| 48 |
private double size = 0.0; |
| 49 |
private boolean branchesFirst; |
| 50 |
|
| 51 |
private ARGModel arg; |
| 52 |
|
| 53 |
private CompoundParameter internalNodeParameters; |
| 54 |
private CompoundParameter internalAndRootNodeParameters; |
| 55 |
private CompoundParameter nodeRates; |
| 56 |
|
| 57 |
public ARGReassortmentOperator(ARGModel arg, int weight, boolean branchesFirst, |
| 58 |
double singlePartProb, double addProbability, |
| 59 |
CompoundParameter internalNodeParameters, |
| 60 |
CompoundParameter internalAndRootNodeParameters, |
| 61 |
CompoundParameter nodeRates) { |
| 62 |
this.arg = arg; |
| 63 |
this.internalNodeParameters = internalNodeParameters; |
| 64 |
this.internalAndRootNodeParameters = internalAndRootNodeParameters; |
| 65 |
this.nodeRates = nodeRates; |
| 66 |
|
| 67 |
this.branchesFirst = branchesFirst; |
| 68 |
this.singlePartitionProbability = singlePartProb; |
| 69 |
|
| 70 |
setWeight(weight); |
| 71 |
|
| 72 |
this.size = addProbability; |
| 73 |
|
| 74 |
|
| 75 |
probBelowRoot = -Math.log(1 - Math.sqrt(probBelowRoot)); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
public double doOperation() { |
| 80 |
double logHastings = 0; |
| 81 |
|
| 82 |
if (MathUtils.nextDouble() < 1.0 / (1 + Math.exp(-size))) |
| 83 |
logHastings = addOperation() - size; |
| 84 |
else |
| 85 |
logHastings = removeOperation() + size; |
| 86 |
|
| 87 |
return logHastings; |
| 88 |
} |
| 89 |
|
| 90 |
private double addOperation() { |
| 91 |
if (branchesFirst) |
| 92 |
return addOperationBranchesFirst(); |
| 93 |
|
| 94 |
return addOperationHeightsFirst(); |
| 95 |
} |
| 96 |
|
| 97 |
private double addOperationBranchesFirst() { |
| 98 |
|
| 99 |
double logHastings = 0; |
| 100 |
double treeHeight = arg.getNodeHeight(arg.getRoot()); |
| 101 |
double newBifurcationHeight = Double.POSITIVE_INFINITY; |
| 102 |
double newReassortmentHeight = Double.POSITIVE_INFINITY; |
| 103 |
|
| 104 |
double theta = probBelowRoot / treeHeight; |
| 105 |
|
| 106 |
while (newBifurcationHeight > treeHeight && newReassortmentHeight > treeHeight) { |
| 107 |
newBifurcationHeight = MathUtils.nextExponential(theta); |
| 108 |
newReassortmentHeight = MathUtils.nextExponential(theta); |
| 109 |
} |
| 110 |
|
| 111 |
logHastings += theta * (newBifurcationHeight + newReassortmentHeight) - LOG_TWO |
| 112 |
- 2.0 * Math.log(theta) + Math.log(1 - Math.exp(-2.0 * treeHeight * theta)); |
| 113 |
|
| 114 |
if (newBifurcationHeight < newReassortmentHeight) { |
| 115 |
double temp = newBifurcationHeight; |
| 116 |
newBifurcationHeight = newReassortmentHeight; |
| 117 |
newReassortmentHeight = temp; |
| 118 |
} |
| 119 |
|
| 120 |
return 0; |
| 121 |
} |
| 122 |
|
| 123 |
private double addOperationHeightsFirst() { |
| 124 |
|
| 125 |
return 0; |
| 126 |
} |
| 127 |
|
| 128 |
private double removeOperation() { |
| 129 |
|
| 130 |
return 0; |
| 131 |
} |
| 132 |
|
| 133 |
public String getOperatorName() { |
| 134 |
return ARG_REASSORTMENT_OPERATOR; |
| 135 |
} |
| 136 |
|
| 137 |
public String getPerformanceSuggestion() { |
| 138 |
return "Try changing the add probability probability"; |
| 139 |
} |
| 140 |
|
| 141 |
private class NoReassortmentEventException extends Exception { |
| 142 |
private static final long serialVersionUID = 1L; |
| 143 |
|
| 144 |
public NoReassortmentEventException() { |
| 145 |
super(""); |
| 146 |
} |
| 147 |
} |
| 148 |
} |