Debian Codegraph @

src/dr/evomodel/antigenic/phyloclustering/operators/Mu2ScaleActiveScaledMu2IntactOperator.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 Mu2ScaleActiveScaledMu2IntactOperator extends AbstractAdaptableOperator {
21
22
23 private MatrixParameter mu = null;
24 private Parameter mu2Scale = 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 Mu2ScaleActiveScaledMu2IntactOperator(double weight, MatrixParameter virusLocations, MatrixParameter mu, Parameter indicators, Parameter mu2Scale, 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.mu2Scale = mu2Scale;
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_mu2Scale_Val = mu2Scale.getParameterValue(0);
68 //double new_mu1Scale_Val = change + original_mu1Scale_Val;
69 double new_mu2Scale_Val = scale * original_mu2Scale_Val;
70
71 mu2Scale.setParameterValue(0, new_mu2Scale_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(1);
78 double newValue = oldValue * original_mu2Scale_Val/new_mu2Scale_Val;
79 mu.getParameter(i).setParameterValue(1, 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
97 //copied from the original ScaleOperator
98 public double getAdaptableParameter() {
99 return Math.log(1.0 / scaleFactor - 1.0);
100 }
101
102 //copied from the original ScaleOperator
103 public void setAdaptableParameter(double value) {
104 scaleFactor = 1.0 / (Math.exp(value) + 1.0);
105 }
106
107 //copied from the original ScaleOperator
108 public double getRawParameter() {
109 return scaleFactor;
110 }
111
112
113
114 public String getAdaptableParameterName() {
115 return "scaleFactor";
116 }
117
118
119 public final static String MU2SCALEACTIVEACTIVEMU2INTACTOPERATOR = "Mu2ScaleActiveScaledMu2IntactOperator";
120
121 public final String getOperatorName() {
122 return MU2SCALEACTIVEACTIVEMU2INTACTOPERATOR;
123 }
124
125
126
127
128
129
130
131 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
132
133
134 public final static String VIRUSLOCATIONS = "virusLocations";
135 public final static String VIRUSLOCATIONSTREENODE = "virusLocationsTreeNodes";
136 public final static String MU = "mu";
137 public final static String MU2SCALE = "mu2Scale";
138 public final static String INDICATORS = "indicators";
139 public final static String SCALE = "scaleFactor";
140
141 public String getParserName() {
142 return MU2SCALEACTIVEACTIVEMU2INTACTOPERATOR;
143 }
144
145 /* (non-Javadoc)
146 * @see dr.xml.AbstractXMLObjectParser#parseXMLObject(dr.xml.XMLObject)
147 */
148 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
149
150
151 double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
152 double scale = xo.getDoubleAttribute(SCALE);
153
154 XMLObject cxo = xo.getChild(VIRUSLOCATIONS);
155 MatrixParameter virusLocations = (MatrixParameter) cxo.getChild(MatrixParameter.class);
156
157 cxo = xo.getChild(VIRUSLOCATIONSTREENODE);
158 MatrixParameter virusLocationsTreeNode = (MatrixParameter) cxo.getChild(MatrixParameter.class);
159
160 cxo = xo.getChild(MU);
161 MatrixParameter mu = (MatrixParameter) cxo.getChild(MatrixParameter.class);
162
163 cxo = xo.getChild(INDICATORS);
164 Parameter indicators = (Parameter) cxo.getChild(Parameter.class);
165
166 cxo = xo.getChild(MU2SCALE);
167 Parameter mu2Scale = (Parameter) cxo.getChild(Parameter.class);
168
169 TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
170
171 return new Mu2ScaleActiveScaledMu2IntactOperator(weight, virusLocations, mu, indicators, mu2Scale, treeModel, scale, virusLocationsTreeNode);
172
173
174 }
175
176 //************************************************************************
177 // AbstractXMLObjectParser implementation
178 //************************************************************************
179
180 public String getParserDescription() {
181 return "changes mu1Scale and make sure the first dimension of the active drifted mus stay the same";
182 }
183
184 public Class getReturnType() {
185 return Mu2ScaleActiveScaledMu2IntactOperator.class;
186 }
187
188
189 public XMLSyntaxRule[] getSyntaxRules() {
190 return rules;
191 }
192
193 private final XMLSyntaxRule[] rules = {
194 AttributeRule.newDoubleRule(MCMCOperator.WEIGHT),
195 AttributeRule.newDoubleRule(SCALE),
196 new ElementRule(VIRUSLOCATIONS, Parameter.class),
197 new ElementRule(VIRUSLOCATIONSTREENODE, MatrixParameter.class),
198 new ElementRule(MU, Parameter.class),
199 new ElementRule(INDICATORS, Parameter.class),
200 new ElementRule(MU2SCALE, Parameter.class),
201 new ElementRule(TreeModel.class),
202
203 };
204
205 };
206
207
208
209 public int getStepCount() {
210 return 1;
211 }
212
213
214 }