| 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.inference.model.BayesianStochasticSearchVariableSelection; |
| 29 |
import dr.inference.model.Parameter; |
| 30 |
import dr.inference.operators.*; |
| 31 |
import dr.math.MathUtils; |
| 32 |
import dr.xml.*; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public class BitFlipInSubstitutionModelOperator extends AbstractAdaptableOperator { |
| 41 |
|
| 42 |
public static final String BIT_FLIP_OPERATOR = "bitFlipInSubstitutionModelOperator"; |
| 43 |
public static final String SCALE_FACTOR = "scaleFactor"; |
| 44 |
|
| 45 |
public BitFlipInSubstitutionModelOperator(BayesianStochasticSearchVariableSelection subModel, Parameter rateParameter, double weight, double scaleFactor, AdaptationMode mode) { |
| 46 |
super(mode); |
| 47 |
this.model = subModel; |
| 48 |
this.indicatorParameter = subModel.getIndicators(); |
| 49 |
this.rateParameter = rateParameter; |
| 50 |
this.scaleFactor = scaleFactor; |
| 51 |
setWeight(weight); |
| 52 |
|
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public Parameter getIndicatorParameter() { |
| 59 |
return indicatorParameter; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public final double doOperation() { |
| 69 |
final int dim = indicatorParameter.getDimension(); |
| 70 |
double sum = 0.0; |
| 71 |
|
| 72 |
for (int i = 0; i < dim; i++) { |
| 73 |
sum += indicatorParameter.getParameterValue(i); |
| 74 |
} |
| 75 |
|
| 76 |
final int pos = MathUtils.nextInt(dim); |
| 77 |
|
| 78 |
final int value = (int) indicatorParameter.getParameterValue(pos); |
| 79 |
|
| 80 |
double rand = 0; |
| 81 |
if (rateParameter != null) |
| 82 |
rand = MathUtils.nextDouble(); |
| 83 |
|
| 84 |
double logq; |
| 85 |
if (value == 0) { |
| 86 |
indicatorParameter.setParameterValue(pos, 1.0); |
| 87 |
|
| 88 |
logq = -Math.log((dim - sum) / (sum + 1)); |
| 89 |
|
| 90 |
|
| 91 |
} else if (value == 1) { |
| 92 |
indicatorParameter.setParameterValue(pos, 0.0); |
| 93 |
|
| 94 |
logq = -Math.log(sum / (dim - sum + 1)); |
| 95 |
|
| 96 |
rand *= -1; |
| 97 |
|
| 98 |
} else { |
| 99 |
throw new RuntimeException("expected 1 or 0"); |
| 100 |
} |
| 101 |
|
| 102 |
if (rateParameter != null) { |
| 103 |
final double scale = Math.exp((rand) * scaleFactor); |
| 104 |
logq += Math.log(scale); |
| 105 |
|
| 106 |
final double oldValue = rateParameter.getParameterValue(0); |
| 107 |
final double newValue = scale * oldValue; |
| 108 |
|
| 109 |
rateParameter.setParameterValue(0, newValue); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
return logq; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
public final String getOperatorName() { |
| 124 |
return "bitflip(" + indicatorParameter.getParameterName() + ")"; |
| 125 |
} |
| 126 |
|
| 127 |
public double getAdaptableParameter() { |
| 128 |
|
| 129 |
return Math.log(scaleFactor); |
| 130 |
} |
| 131 |
|
| 132 |
public void setAdaptableParameter(double value) { |
| 133 |
|
| 134 |
scaleFactor = Math.exp(value); |
| 135 |
} |
| 136 |
|
| 137 |
public double getRawParameter() { |
| 138 |
return scaleFactor; |
| 139 |
} |
| 140 |
|
| 141 |
public double getScaleFactor() { |
| 142 |
return scaleFactor; |
| 143 |
} |
| 144 |
|
| 145 |
public String getAdaptableParameterName() { |
| 146 |
return "scaleFactor"; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
public String toString() { |
| 151 |
return getOperatorName(); |
| 152 |
} |
| 153 |
|
| 154 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 155 |
|
| 156 |
public String getParserName() { |
| 157 |
return BIT_FLIP_OPERATOR; |
| 158 |
} |
| 159 |
|
| 160 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 161 |
|
| 162 |
double weight = xo.getDoubleAttribute(WEIGHT); |
| 163 |
|
| 164 |
AdaptationMode mode = AdaptationMode.parseMode(xo); |
| 165 |
final double scaleFactor = xo.getDoubleAttribute(SCALE_FACTOR); |
| 166 |
|
| 167 |
if (scaleFactor <= 0.0 || scaleFactor >= 1.0) { |
| 168 |
throw new XMLParseException("scaleFactor must be between 0.0 and 1.0"); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
Parameter rateParameter = (Parameter) xo.getChild(Parameter.class); |
| 173 |
BayesianStochasticSearchVariableSelection subModel = (BayesianStochasticSearchVariableSelection) xo.getChild(BayesianStochasticSearchVariableSelection.class); |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
return new BitFlipInSubstitutionModelOperator(subModel, rateParameter, weight, scaleFactor, mode); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
public String getParserDescription() { |
| 189 |
return "This element returns a bit-flip operator on a given indicatorParameter."; |
| 190 |
} |
| 191 |
|
| 192 |
public Class getReturnType() { |
| 193 |
return MCMCOperator.class; |
| 194 |
} |
| 195 |
|
| 196 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 197 |
return rules; |
| 198 |
} |
| 199 |
|
| 200 |
private final XMLSyntaxRule[] rules = { |
| 201 |
AttributeRule.newDoubleRule(WEIGHT), |
| 202 |
AttributeRule.newDoubleRule(SCALE_FACTOR), |
| 203 |
AttributeRule.newBooleanRule(AUTO_OPTIMIZE, true), |
| 204 |
new ElementRule(Parameter.class,true), |
| 205 |
new ElementRule(BayesianStochasticSearchVariableSelection.class) |
| 206 |
}; |
| 207 |
|
| 208 |
}; |
| 209 |
|
| 210 |
|
| 211 |
private Parameter indicatorParameter = null; |
| 212 |
private Parameter rateParameter = null; |
| 213 |
private BayesianStochasticSearchVariableSelection model; |
| 214 |
private double scaleFactor; |
| 215 |
} |