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