| 1 |
|
| 2 |
package dr.evomodel.antigenic.phyloclustering.operators; |
| 3 |
|
| 4 |
import dr.inference.model.MatrixParameter; |
| 5 |
import dr.inference.model.Parameter; |
| 6 |
import dr.inference.operators.AbstractAdaptableOperator; |
| 7 |
import dr.inference.operators.AdaptationMode; |
| 8 |
import dr.inference.operators.MCMCOperator; |
| 9 |
import dr.inference.operators.OperatorUtils; |
| 10 |
import dr.math.MathUtils; |
| 11 |
import dr.xml.AbstractXMLObjectParser; |
| 12 |
import dr.xml.AttributeRule; |
| 13 |
import dr.xml.ElementRule; |
| 14 |
import dr.xml.XMLObject; |
| 15 |
import dr.xml.XMLObjectParser; |
| 16 |
import dr.xml.XMLParseException; |
| 17 |
import dr.xml.XMLSyntaxRule; |
| 18 |
|
| 19 |
public class muPrecisionInactiveMuOperator extends AbstractAdaptableOperator { |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
private Parameter muPrecision = null; |
| 24 |
private MatrixParameter mu = null; |
| 25 |
private Parameter indicators = null; |
| 26 |
private double scaleFactor; |
| 27 |
private Parameter muMean = null; |
| 28 |
|
| 29 |
public muPrecisionInactiveMuOperator(double weight, MatrixParameter mu, Parameter muPrec, double scale, Parameter indicators, Parameter muMean){ |
| 30 |
|
| 31 |
super(AdaptationMode.ADAPTATION_ON); |
| 32 |
|
| 33 |
setWeight(weight); |
| 34 |
this.mu = mu; |
| 35 |
this.muPrecision = muPrec; |
| 36 |
this.scaleFactor = scale; |
| 37 |
this.indicators = indicators; |
| 38 |
this.muMean = muMean; |
| 39 |
|
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public double doOperation() { |
| 45 |
|
| 46 |
|
| 47 |
final double scale = (scaleFactor + (MathUtils.nextDouble() * ((1.0 / scaleFactor) - scaleFactor))); |
| 48 |
|
| 49 |
double original_muPrec_Val = muPrecision.getParameterValue(0); |
| 50 |
double new_muPrec_Val = scale * original_muPrec_Val; |
| 51 |
|
| 52 |
|
| 53 |
muPrecision.setParameterValue(0, new_muPrec_Val); |
| 54 |
|
| 55 |
|
| 56 |
for(int i=0; i < mu.getColumnDimension(); i++){ |
| 57 |
|
| 58 |
|
| 59 |
if(indicators.getParameterValue(i) == 0){ |
| 60 |
for(int j=0; j < 2; j++){ |
| 61 |
if(j==0){ |
| 62 |
double translatedValue = (mu.getParameter(i).getParameterValue(j) - muMean.getParameterValue(0) ); |
| 63 |
double newValue = translatedValue * Math.sqrt( original_muPrec_Val/new_muPrec_Val) + muMean.getParameterValue(0); |
| 64 |
mu.getParameter(i).setParameterValue(j, newValue); |
| 65 |
} |
| 66 |
else{ |
| 67 |
double oldValue = mu.getParameter(i).getParameterValue(j); |
| 68 |
double newValue = oldValue * Math.sqrt( original_muPrec_Val/new_muPrec_Val); |
| 69 |
mu.getParameter(i).setParameterValue(j, newValue); |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
double logq = -Math.log(scale); |
| 78 |
return logq; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public double getAdaptableParameter() { |
| 87 |
return Math.log(1.0 / scaleFactor - 1.0); |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
public void setAdaptableParameter(double value) { |
| 92 |
scaleFactor = 1.0 / (Math.exp(value) + 1.0); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
public double getRawParameter() { |
| 97 |
return scaleFactor; |
| 98 |
} |
| 99 |
|
| 100 |
public String getAdaptableParameterName() { |
| 101 |
return "scaleFactor"; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
public final static String CLASSNAME = "muPrecisionInactiveMuOperator"; |
| 107 |
|
| 108 |
public final String getOperatorName() { |
| 109 |
return CLASSNAME; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 119 |
|
| 120 |
|
| 121 |
public final static String MU = "mu"; |
| 122 |
public final static String MUPREC = "muPrec"; |
| 123 |
|
| 124 |
public final static String SCALE = "scaleFactor"; |
| 125 |
|
| 126 |
public final static String MUMEAN = "muMean"; |
| 127 |
public final static String INDICATORS = "indicators"; |
| 128 |
|
| 129 |
|
| 130 |
public String getParserName() { |
| 131 |
return CLASSNAME; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 138 |
|
| 139 |
|
| 140 |
double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT); |
| 141 |
double scale = xo.getDoubleAttribute(SCALE); |
| 142 |
|
| 143 |
|
| 144 |
XMLObject cxo = xo.getChild(MU); |
| 145 |
MatrixParameter mu = (MatrixParameter) cxo.getChild(MatrixParameter.class); |
| 146 |
|
| 147 |
|
| 148 |
cxo = xo.getChild(MUPREC); |
| 149 |
Parameter muPrec = (Parameter) cxo.getChild(Parameter.class); |
| 150 |
|
| 151 |
cxo = xo.getChild(INDICATORS); |
| 152 |
Parameter indicators = (Parameter) cxo.getChild(Parameter.class); |
| 153 |
|
| 154 |
cxo = xo.getChild(MUMEAN); |
| 155 |
Parameter muMean = (Parameter) cxo.getChild(Parameter.class); |
| 156 |
|
| 157 |
return new muPrecisionInactiveMuOperator(weight, mu, muPrec, scale, indicators, muMean); |
| 158 |
|
| 159 |
|
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
public String getParserDescription() { |
| 167 |
return "changes serum drift and make sure the first dimension of the active drifted mus stay the same"; |
| 168 |
} |
| 169 |
|
| 170 |
public Class getReturnType() { |
| 171 |
return muPrecisionInactiveMuOperator.class; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 176 |
return rules; |
| 177 |
} |
| 178 |
|
| 179 |
private final XMLSyntaxRule[] rules = { |
| 180 |
AttributeRule.newDoubleRule(MCMCOperator.WEIGHT), |
| 181 |
AttributeRule.newDoubleRule(SCALE), |
| 182 |
new ElementRule(MU, Parameter.class), |
| 183 |
new ElementRule(MUPREC, Parameter.class), |
| 184 |
new ElementRule(INDICATORS, Parameter.class), |
| 185 |
new ElementRule(MUMEAN, Parameter.class) |
| 186 |
}; |
| 187 |
|
| 188 |
}; |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
public int getStepCount() { |
| 193 |
return 1; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
} |