| 1 |
package dr.evomodel.antigenic.phyloclustering.operators; |
| 2 |
|
| 3 |
|
| 4 |
import dr.evomodel.tree.TreeModel; |
| 5 |
import dr.inference.model.MatrixParameter; |
| 6 |
import dr.inference.model.Parameter; |
| 7 |
import dr.inference.operators.MCMCOperator; |
| 8 |
import dr.inference.operators.SimpleMCMCOperator; |
| 9 |
import dr.math.MathUtils; |
| 10 |
import dr.xml.*; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
public class randomWalkSerumDriftAndMu extends SimpleMCMCOperator { |
| 20 |
|
| 21 |
|
| 22 |
public static final String SERUMDRIFT_AND_MU_OPERATOR = "serumDriftAndMuOperator"; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
Parameter indicators; |
| 27 |
MatrixParameter mu; |
| 28 |
Parameter serumDrift; |
| 29 |
private TreeModel treeModel; |
| 30 |
|
| 31 |
private double maxWalkSize; |
| 32 |
|
| 33 |
|
| 34 |
public randomWalkSerumDriftAndMu( MatrixParameter mu, |
| 35 |
double weight, |
| 36 |
Parameter indicatorsParameter, |
| 37 |
Parameter serumDrift_in, |
| 38 |
double max_walk_size_in, |
| 39 |
TreeModel treeModel_in) { |
| 40 |
this.mu = mu; |
| 41 |
this.indicators = indicatorsParameter; |
| 42 |
this.serumDrift = serumDrift_in; |
| 43 |
this.maxWalkSize = max_walk_size_in; |
| 44 |
this.treeModel= treeModel_in; |
| 45 |
|
| 46 |
setWeight(weight); |
| 47 |
|
| 48 |
System.out.println("Finished loading the constructor for SERUMDRIFT_AND_MU_OPERATOR"); |
| 49 |
|
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public final double doOperation() { |
| 59 |
|
| 60 |
double logHastingRatio = 0; |
| 61 |
|
| 62 |
|
| 63 |
int rootNode = treeModel.getRoot().getNumber(); |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
double change = MathUtils.nextDouble()*maxWalkSize- maxWalkSize/2 ; |
| 68 |
double originalValue = serumDrift.getParameterValue(0); |
| 69 |
double newValue = originalValue + change; |
| 70 |
serumDrift.setParameterValue(0, newValue); |
| 71 |
|
| 72 |
for(int i=0; i < mu.getParameterCount(); i++){ |
| 73 |
if( (int) indicators.getParameterValue(i) == 1 && i != rootNode ){ |
| 74 |
Parameter curMu = mu.getParameter(i); |
| 75 |
double originalMu0 = curMu.getParameterValue(0); |
| 76 |
double newMu0 = originalMu0 * newValue/originalValue; |
| 77 |
curMu.setParameterValue(0, newMu0); |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
return(logHastingRatio); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public void accept(double deviation) { |
| 88 |
super.accept(deviation); |
| 89 |
} |
| 90 |
|
| 91 |
public void reject(){ |
| 92 |
super.reject(); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public final String getOperatorName() { |
| 100 |
return SERUMDRIFT_AND_MU_OPERATOR; |
| 101 |
} |
| 102 |
|
| 103 |
public final void optimize(double targetProb) { |
| 104 |
|
| 105 |
throw new RuntimeException("This operator cannot be optimized!"); |
| 106 |
} |
| 107 |
|
| 108 |
public boolean isOptimizing() { |
| 109 |
return false; |
| 110 |
} |
| 111 |
|
| 112 |
public void setOptimizing(boolean opt) { |
| 113 |
throw new RuntimeException("This operator cannot be optimized!"); |
| 114 |
} |
| 115 |
|
| 116 |
public double getMinimumAcceptanceLevel() { |
| 117 |
return 0.1; |
| 118 |
} |
| 119 |
|
| 120 |
public double getMaximumAcceptanceLevel() { |
| 121 |
return 0.4; |
| 122 |
} |
| 123 |
|
| 124 |
public double getMinimumGoodAcceptanceLevel() { |
| 125 |
return 0.20; |
| 126 |
} |
| 127 |
|
| 128 |
public double getMaximumGoodAcceptanceLevel() { |
| 129 |
return 0.30; |
| 130 |
} |
| 131 |
|
| 132 |
public String getPerformanceSuggestion() { |
| 133 |
if (Utils.getAcceptanceProbability(this) < getMinimumAcceptanceLevel()) { |
| 134 |
return ""; |
| 135 |
} else if (Utils.getAcceptanceProbability(this) > getMaximumAcceptanceLevel()) { |
| 136 |
return ""; |
| 137 |
} else { |
| 138 |
return ""; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 147 |
|
| 148 |
|
| 149 |
public final static String MU = "mu"; |
| 150 |
public final static String SERUMDRIFT = "serumDrift"; |
| 151 |
public final static String INDICATORS = "indicators"; |
| 152 |
public final static String WALKSIZE = "walkSize"; |
| 153 |
|
| 154 |
public String getParserName() { |
| 155 |
return SERUMDRIFT_AND_MU_OPERATOR; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 162 |
|
| 163 |
double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT); |
| 164 |
|
| 165 |
double walk_size = 0.05; |
| 166 |
if (xo.hasAttribute(WALKSIZE)) { |
| 167 |
walk_size = xo.getDoubleAttribute(WALKSIZE); |
| 168 |
} |
| 169 |
|
| 170 |
XMLObject cxo = xo.getChild(MU); |
| 171 |
MatrixParameter mu = (MatrixParameter) cxo.getChild(MatrixParameter.class); |
| 172 |
|
| 173 |
cxo = xo.getChild(SERUMDRIFT); |
| 174 |
Parameter serumDrift = (Parameter) cxo.getChild(Parameter.class); |
| 175 |
|
| 176 |
cxo = xo.getChild(INDICATORS); |
| 177 |
Parameter indicators = (Parameter) cxo.getChild(Parameter.class); |
| 178 |
|
| 179 |
TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class); |
| 180 |
|
| 181 |
|
| 182 |
return new randomWalkSerumDriftAndMu(mu, weight, indicators, serumDrift, walk_size, treeModel); |
| 183 |
|
| 184 |
|
| 185 |
} |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
public String getParserDescription() { |
| 192 |
return "An operator that picks a new allocation of an item to a cluster under the Dirichlet process."; |
| 193 |
} |
| 194 |
|
| 195 |
public Class getReturnType() { |
| 196 |
return TreeClusterAlgorithmOperator.class; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 201 |
return rules; |
| 202 |
} |
| 203 |
|
| 204 |
private final XMLSyntaxRule[] rules = { |
| 205 |
AttributeRule.newDoubleRule(MCMCOperator.WEIGHT), |
| 206 |
AttributeRule.newDoubleRule(WALKSIZE), |
| 207 |
new ElementRule(MU, Parameter.class), |
| 208 |
new ElementRule(SERUMDRIFT, Parameter.class), |
| 209 |
new ElementRule(INDICATORS, Parameter.class), |
| 210 |
new ElementRule(TreeModel.class), |
| 211 |
}; |
| 212 |
|
| 213 |
}; |
| 214 |
|
| 215 |
|
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|