| 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.Parameter; |
| 29 |
import dr.inference.operators.MCMCOperator; |
| 30 |
import dr.inference.operators.SimpleMCMCOperator; |
| 31 |
import dr.xml.*; |
| 32 |
import dr.math.MathUtils; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public class MicrosatelliteBitFlipOperator extends SimpleMCMCOperator { |
| 41 |
private Parameter parameter; |
| 42 |
private Parameter dependencies; |
| 43 |
private int[] variableIndices; |
| 44 |
public static final int PRESENT = 1; |
| 45 |
public static final int ABSENT = 0; |
| 46 |
public static final int NO_DEPENDENCY = -1; |
| 47 |
public static final String MODEL_CHOOSE = "modelChoose"; |
| 48 |
public static final String DEPENDENCIES = "dependencies"; |
| 49 |
public static final String VARIABLE_INDICES = "variableIndices"; |
| 50 |
|
| 51 |
|
| 52 |
public MicrosatelliteBitFlipOperator(Parameter parameter, Parameter dependencies, double weight, int[] variableIndices){ |
| 53 |
this.parameter = parameter; |
| 54 |
this.dependencies = dependencies; |
| 55 |
this.variableIndices = variableIndices; |
| 56 |
if(parameter.getDimension() != dependencies.getDimension()) |
| 57 |
throw new RuntimeException("Dimenension of the parameter ("+parameter.getDimension()+ |
| 58 |
") does not equal to the dimension of the dependencies parameter("+dependencies.getDimension()+")."); |
| 59 |
setWeight(weight); |
| 60 |
} |
| 61 |
|
| 62 |
public String getOperatorName(){ |
| 63 |
return "msatModelSwitch(" + parameter.getParameterName() + ")"; |
| 64 |
} |
| 65 |
|
| 66 |
public double doOperation() { |
| 67 |
|
| 68 |
double logq = 0.0; |
| 69 |
double[] bitVec = new double[parameter.getDimension()]; |
| 70 |
for(int i = 0; i < bitVec.length; i++){ |
| 71 |
bitVec[i] = parameter.getParameterValue(i); |
| 72 |
} |
| 73 |
|
| 74 |
int index = variableIndices[MathUtils.nextInt(variableIndices.length)]; |
| 75 |
|
| 76 |
int oldVal = (int)parameter.getParameterValue(index); |
| 77 |
int newVal = -1; |
| 78 |
if(oldVal == ABSENT){ |
| 79 |
newVal = PRESENT; |
| 80 |
}else if(oldVal == PRESENT){ |
| 81 |
newVal = ABSENT; |
| 82 |
}else{ |
| 83 |
throw new RuntimeException("The parameter can only take values 0 or 1."); |
| 84 |
} |
| 85 |
bitVec[index] = newVal; |
| 86 |
for(int i = 0; i < bitVec.length; i++){ |
| 87 |
int dependentInd = (int)dependencies.getParameterValue(i); |
| 88 |
if(dependentInd > NO_DEPENDENCY){ |
| 89 |
if(bitVec[dependentInd] == ABSENT && bitVec[i]==PRESENT){ |
| 90 |
|
| 91 |
return Double.NEGATIVE_INFINITY; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
} |
| 96 |
parameter.setParameterValue(index, newVal); |
| 97 |
|
| 98 |
return logq; |
| 99 |
} |
| 100 |
|
| 101 |
public final String getPerformanceSuggestion() { |
| 102 |
return "no suggestions available"; |
| 103 |
} |
| 104 |
|
| 105 |
public static dr.xml.XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 106 |
|
| 107 |
public String getParserName() { |
| 108 |
return "msatModelSwitchOperator"; |
| 109 |
} |
| 110 |
|
| 111 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 112 |
|
| 113 |
double weight = xo.getDoubleAttribute(WEIGHT); |
| 114 |
Parameter modelChoose = (Parameter) xo.getElementFirstChild(MODEL_CHOOSE); |
| 115 |
Parameter dependencies = (Parameter)xo.getElementFirstChild(DEPENDENCIES); |
| 116 |
int[] variableIndices; |
| 117 |
if(xo.hasChildNamed(VARIABLE_INDICES)){ |
| 118 |
|
| 119 |
double[] temp = ((Parameter)xo.getElementFirstChild(VARIABLE_INDICES)).getParameterValues(); |
| 120 |
variableIndices = new int[temp.length]; |
| 121 |
for(int i = 0; i < temp.length;i++){ |
| 122 |
variableIndices[i] = (int)temp[i]; |
| 123 |
} |
| 124 |
|
| 125 |
}else{ |
| 126 |
variableIndices = new int[]{0, 1, 2, 3, 4, 5}; |
| 127 |
} |
| 128 |
|
| 129 |
return new MicrosatelliteBitFlipOperator(modelChoose, dependencies, weight, variableIndices); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public String getParserDescription() { |
| 137 |
return "This element returns a microsatellite averaging operator on a given parameter."; |
| 138 |
} |
| 139 |
|
| 140 |
public Class getReturnType() { |
| 141 |
return MCMCOperator.class; |
| 142 |
} |
| 143 |
|
| 144 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 145 |
return rules; |
| 146 |
} |
| 147 |
|
| 148 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 149 |
AttributeRule.newDoubleRule(WEIGHT), |
| 150 |
new ElementRule(MODEL_CHOOSE, new XMLSyntaxRule[]{new ElementRule(Parameter.class)}), |
| 151 |
new ElementRule(DEPENDENCIES, new XMLSyntaxRule[]{new ElementRule(Parameter.class)}), |
| 152 |
new ElementRule(VARIABLE_INDICES, new XMLSyntaxRule[]{new ElementRule(Parameter.class)},true) |
| 153 |
|
| 154 |
}; |
| 155 |
|
| 156 |
}; |
| 157 |
|
| 158 |
} |