Debian Codegraph @

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

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