Debian Codegraph @

src/dr/evomodel/antigenic/phyloclustering/operators/serumDriftActiveScaledMu1Operator.java

raw symbols SCIP
1
2 package dr.evomodel.antigenic.phyloclustering.operators;
3
4 import dr.evomodel.antigenic.phyloclustering.TreeClusteringSharedRoutines;
5 import dr.evomodel.tree.TreeModel;
6 import dr.inference.model.MatrixParameter;
7 import dr.inference.model.Parameter;
8 import dr.inference.operators.AbstractAdaptableOperator;
9 import dr.inference.operators.AdaptationMode;
10 import dr.inference.operators.MCMCOperator;
11 import dr.inference.operators.OperatorUtils;
12 import dr.math.MathUtils;
13 import dr.xml.AbstractXMLObjectParser;
14 import dr.xml.AttributeRule;
15 import dr.xml.ElementRule;
16 import dr.xml.XMLObject;
17 import dr.xml.XMLObjectParser;
18 import dr.xml.XMLParseException;
19 import dr.xml.XMLSyntaxRule;
20
21 public class serumDriftActiveScaledMu1Operator extends AbstractAdaptableOperator {
22
23
24 private MatrixParameter mu = null;
25 private Parameter serumDrift = null;
26 private MatrixParameter virusLocations = null;
27 private MatrixParameter virusLocationsTreeNode = null;
28 private Parameter indicators;
29 private TreeModel treeModel;
30
31 private int numdata;
32 private int numNodes;
33 private int []correspondingTreeIndexForVirus = null; //relates treeModels's indexing system to cluster label's indexing system of viruses. Gets assigned
34
35 private double scaleFactor;
36
37
38 public serumDriftActiveScaledMu1Operator(double weight, MatrixParameter virusLocations, MatrixParameter mu, Parameter indicators, Parameter serumDrift, TreeModel treeModel_in, double scale, MatrixParameter virusLocationsTreeNode_in){
39
40 super(AdaptationMode.ADAPTATION_ON);
41
42 setWeight(weight);
43 this.virusLocations = virusLocations;
44 this.mu = mu;
45 this.indicators = indicators;
46 this.serumDrift = serumDrift;
47 this.treeModel= treeModel_in;
48 this.scaleFactor = scale;
49 this.virusLocationsTreeNode = virusLocationsTreeNode_in;
50
51 numNodes = treeModel.getNodeCount();
52 numdata = virusLocations.getColumnDimension();
53
54 correspondingTreeIndexForVirus = TreeClusteringSharedRoutines.setMembershipTreeToVirusIndexes(numdata, virusLocations, numNodes, treeModel);
55 TreeClusteringSharedRoutines.updateUndriftedVirusLocations(numNodes, numdata, treeModel, virusLocationsTreeNode, indicators, mu, virusLocations, correspondingTreeIndexForVirus);
56 }
57
58
59
60 public double doOperation() {
61
62
63 final double scale = (scaleFactor + (MathUtils.nextDouble() * ((1.0 / scaleFactor) - scaleFactor)));
64
65
66 //change serum drift
67 //double WALK_SIZE = 0.2; //when the walk size becomes 0.5, .... things become weird.. too big
68 //double change = Math.random()*WALK_SIZE- WALK_SIZE/2 ;
69 double original_serumDrift_Val = serumDrift.getParameterValue(0);
70 // System.out.println("original_serumDrift_Val = " + original_serumDrift_Val);
71 //double new_serumDrift_Val = change + original_serumDrift_Val;
72 double new_serumDrift_Val = scale * original_serumDrift_Val;
73
74
75 serumDrift.setParameterValue(0, new_serumDrift_Val);
76
77 // System.out.println("new_serumDrift_Val=" + serumDrift.getParameterValue(0));
78 //make sure all the active mu's first dimension stays intact
79 for(int i=0; i < numNodes; i++){
80 if( (int) indicators.getParameterValue(i) == 1){
81 double oldValue = mu.getParameter(i).getParameterValue(0);
82 double newValue = oldValue * new_serumDrift_Val/original_serumDrift_Val;
83 mu.getParameter(i).setParameterValue(0, newValue);
84 // System.out.println("indicator" + i + " oldValue = " + oldValue + " and newValue=" + mu.getParameter(i).getParameterValue(0));
85 }
86 }
87
88 //the virus location needs to be updated because the mu's are updated
89 TreeClusteringSharedRoutines.updateUndriftedVirusLocations(numNodes, numdata, treeModel, virusLocationsTreeNode, indicators, mu, virusLocations, correspondingTreeIndexForVirus);
90
91
92 double logq = -Math.log(scale);
93 // System.out.println("logq=" + logq);
94 // System.out.println("================================================");
95 //return 0;
96 return logq;
97 }
98
99
100
101
102
103 //copied from the original ScaleOperator
104 public double getAdaptableParameter() {
105 return Math.log(1.0 / scaleFactor - 1.0);
106 }
107
108 //copied from the original ScaleOperator
109 public void setAdaptableParameter(double value) {
110 scaleFactor = 1.0 / (Math.exp(value) + 1.0);
111 }
112
113 //copied from the original ScaleOperator
114 public double getRawParameter() {
115 return scaleFactor;
116 }
117
118
119
120 public String getAdaptableParameterName() {
121 return "scaleFactor";
122 }
123
124
125
126 public final static String SERUMDRIFTACTIVESCALEDMU1Operator = "serumDriftActiveScaledMu1Operator";
127
128 public final String getOperatorName() {
129 return SERUMDRIFTACTIVESCALEDMU1Operator;
130 }
131
132
133
134
135
136
137
138 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
139
140
141 public final static String VIRUSLOCATIONS = "virusLocations";
142 public final static String VIRUSLOCATIONSTREENODE = "virusLocationsTreeNodes";
143 public final static String MU = "mu";
144 public final static String SERUMDRIFT = "serumDrift";
145 public final static String INDICATORS = "indicators";
146 public final static String SCALE = "scaleFactor";
147
148
149 public String getParserName() {
150 return SERUMDRIFTACTIVESCALEDMU1Operator;
151 }
152
153 /* (non-Javadoc)
154 * @see dr.xml.AbstractXMLObjectParser#parseXMLObject(dr.xml.XMLObject)
155 */
156 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
157
158
159 double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
160 double scale = xo.getDoubleAttribute(SCALE);
161
162
163 XMLObject cxo = xo.getChild(VIRUSLOCATIONS);
164 MatrixParameter virusLocations = (MatrixParameter) cxo.getChild(MatrixParameter.class);
165
166 cxo = xo.getChild(VIRUSLOCATIONSTREENODE);
167 MatrixParameter virusLocationsTreeNode = (MatrixParameter) cxo.getChild(MatrixParameter.class);
168
169
170 cxo = xo.getChild(MU);
171 MatrixParameter mu = (MatrixParameter) cxo.getChild(MatrixParameter.class);
172
173 cxo = xo.getChild(INDICATORS);
174 Parameter indicators = (Parameter) cxo.getChild(Parameter.class);
175
176 cxo = xo.getChild(SERUMDRIFT);
177 Parameter serumDrift = (Parameter) cxo.getChild(Parameter.class);
178
179 TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
180
181 return new serumDriftActiveScaledMu1Operator(weight, virusLocations, mu, indicators, serumDrift, treeModel, scale, virusLocationsTreeNode);
182
183
184 }
185
186 //************************************************************************
187 // AbstractXMLObjectParser implementation
188 //************************************************************************
189
190 public String getParserDescription() {
191 return "changes serum drift and make sure the first dimension of the active drifted mus stay the same";
192 }
193
194 public Class getReturnType() {
195 return serumDriftActiveScaledMu1Operator.class;
196 }
197
198
199 public XMLSyntaxRule[] getSyntaxRules() {
200 return rules;
201 }
202
203 private final XMLSyntaxRule[] rules = {
204 AttributeRule.newDoubleRule(MCMCOperator.WEIGHT),
205 AttributeRule.newDoubleRule(SCALE),
206 new ElementRule(VIRUSLOCATIONS, Parameter.class),
207 new ElementRule(VIRUSLOCATIONSTREENODE, MatrixParameter.class),
208 new ElementRule(MU, Parameter.class),
209 new ElementRule(INDICATORS, Parameter.class),
210 new ElementRule(SERUMDRIFT, Parameter.class),
211 new ElementRule(TreeModel.class),
212
213 };
214
215 };
216
217
218
219 public int getStepCount() {
220 return 1;
221 }
222
223
224 }