Debian Codegraph @

src/dr/evomodel/antigenic/phyloclustering/misc/obsolete/ClusterAlgorithmOperator.java

raw symbols SCIP
1 package dr.evomodel.antigenic.phyloclustering.misc.obsolete;
2
3 import dr.inference.model.MatrixParameter;
4 import dr.inference.model.Parameter;
5 import dr.inference.operators.MCMCOperator;
6 import dr.inference.operators.SimpleMCMCOperator;
7 import dr.math.distributions.MultivariateNormalDistribution;
8 import dr.xml.*;
9
10
11 /**
12 * A Gibbs operator for allocation of items to clusters under a distance dependent Chinese restaurant process.
13 *
14 * @author Charles Cheung
15 * @author Trevor Bedford
16 */
17 public class ClusterAlgorithmOperator extends SimpleMCMCOperator {
18
19 //Parameter locationDrift; // no longer need to know
20 Parameter virusOffsetsParameter;
21 private double sigmaSq =1;
22 private int numdata = 0; //NEED TO UPDATE
23 //private double[] groupSize;
24
25 private MatrixParameter mu = null;
26 private Parameter clusterLabels = null;
27 private Parameter K = null;
28
29 private MatrixParameter virusLocations = null;
30
31 private int maxLabel = 0;
32 private int[] muLabels = null;
33
34 private int[] groupSize;
35 // public ClusterViruses clusterLikelihood = null;
36
37
38 private double numAcceptMoveMu = 0;
39 private double numProposeMoveMu = 0;
40
41 private double numAcceptMoveC = 0;
42 private double numProposeMoveC = 0;
43
44 private int isMoveMu = -1;
45
46
47 private double[] old_vLoc0 ;
48 private double[] old_vLoc1 ;
49
50 private Parameter clusterOffsetsParameter;
51
52 private int groupSelectedChange = -1;
53 private int virusIndexChange = -1;
54 private double originalValueChange = -1;
55 private int dimSelectChange = -1;
56
57 private double[] mu0_offset;
58
59 //public ClusterAlgorithmOperator(MatrixParameter virusLocations, MatrixParameter mu, Parameter clusterLabels, Parameter K, double weight, Parameter virusOffsetsParameter, Parameter locationDrift_in, Parameter clusterOffsetsParameter) {
60 public ClusterAlgorithmOperator(MatrixParameter virusLocations, MatrixParameter mu, Parameter clusterLabels, Parameter K, double weight, Parameter virusOffsetsParameter, Parameter clusterOffsetsParameter) {
61
62
63 System.out.println("Loading the constructor for ClusterAlgorithmOperator");
64 this.mu = mu;
65 this.K = K;
66 this.clusterLabels = clusterLabels;
67 // this.clusterLikelihood = clusterLikelihood;
68 this.virusLocations = virusLocations;
69 this.virusOffsetsParameter = virusOffsetsParameter;
70 // this.locationDrift = locationDrift_in; //no longer need
71 this.clusterOffsetsParameter = clusterOffsetsParameter;
72
73 numdata = virusOffsetsParameter.getSize();
74 System.out.println("numdata="+ numdata);
75
76
77 int K_int = (int) K.getParameterValue(0);
78
79
80 System.out.println("K_int=" + K_int);
81 groupSize = new int[K_int];
82 for(int i=0; i < K_int; i++){
83 groupSize[i] = 0;
84 }
85
86
87 for(int i=0; i < numdata; i++){
88 //System.out.println("i="+ i);
89 int index = (int) clusterLabels.getParameterValue(i);
90 groupSize[ index]++;
91 }
92
93 for(int i=0; i < numdata;i++){
94 if(maxLabel < (int) clusterLabels.getParameterValue(i)){
95 maxLabel = (int) clusterLabels.getParameterValue(i);
96 }
97 }
98
99 //NEED maxGROUP
100
101 //for(int i=0; i < K_int; i++){
102 //System.out.println("groupSize=" + groupSize[i]);
103 //}
104
105
106 muLabels = new int[K_int];
107
108 for(int i=0; i < maxLabel; i++){
109 int j=0;
110 if(groupSize[i] >0){
111 muLabels[j] = i;
112 j++;
113 }
114 }
115
116 //muLabels ...
117
118
119 setWeight(weight);
120
121 System.out.println("Finished loading the constructor for ClusterAlgorithmOperator");
122
123 }
124
125
126
127 /**
128 * change the parameter and return the log hastings ratio.
129 */
130 public final double doOperation() {
131
132 //System.out.println("Do operation!");
133
134
135 double[] zeroVector2D = {0,0};
136 double[][] identityMatrix2D = new double[][]{
137 { 1, 0 },
138 { 0, 1 }};
139
140 double[][] sigmaSqMatrix2D = new double[][]{
141 { sigmaSq, 0 },
142 { 0, sigmaSq }};
143
144 double logHastingRatio = 0;
145 double chooseOperator = Math.random();
146
147 int K_int = (int) K.getParameterValue(0);
148
149
150
151 double[] original_groupSize = new double[groupSize.length];
152 //recalculate groupSize
153 for(int i=0; i < groupSize.length; i++){
154 original_groupSize[i] = 0;
155 }
156 for(int i=0; i < numdata; i++){
157 int label = (int) clusterLabels.getParameterValue(i);
158 original_groupSize[label ]++;
159 }
160
161
162 // for(int i=0; i < K_int; i++){
163 // System.out.println("group " + i + " has size=" + original_groupSize[i]);
164 // }
165
166
167 for(int i=0; i < K_int; i++){
168 double muk_0 = mu.getParameter(i).getParameterValue(0);
169 double muk_1 = mu.getParameter(i).getParameterValue(1);
170 //System.out.println("size=" + groupSize[i] + " mu_k_0=" + muk_0+ " , muk_1=" + muk_1);
171 }
172
173 // System.out.println("propose a change in mu only");
174 if(chooseOperator < 0.5){
175 // if(chooseOperator < 1){
176 //change nothing
177 isMoveMu = 1;
178 int groupSelect = (int) Math.floor( Math.random()* K_int );
179 groupSelectedChange = groupSelect;
180 int dimSelect = (int) Math.floor( Math.random()* 2 );
181 dimSelectChange = dimSelect;
182 // System.out.println("Group selected = " + groupSelectedChange + " mu=" + mu.getParameter(groupSelectedChange).getParameterValue(0)+ "\t" + mu.getParameter(groupSelectedChange).getParameterValue(1) + " (before change...)" );
183 double change = Math.random()*2-1 ;
184 //System.out.println(change);
185 double originalValue = mu.getParameter(groupSelect).getParameterValue(dimSelect);
186 originalValueChange = originalValue;
187 mu.getParameter(groupSelect).setParameterValue(dimSelect, originalValue + change);
188 // System.out.println("Group selected = " + groupSelectedChange + " mu=" + mu.getParameter(groupSelectedChange).getParameterValue(0)+ "\t" + mu.getParameter(groupSelectedChange).getParameterValue(1) + " (propsed to...)");
189
190 logHastingRatio = 0;
191 }
192
193 // System.out.println("propose a change in both C and mu");
194 else{
195 isMoveMu = 0;
196
197 int virusIndex = (int) Math.floor( Math.random()*numdata );
198 virusIndexChange = virusIndex;
199 int toBin = (int) Math.floor(Math.random()*K_int);
200 // System.out.println("toBin=" + toBin);
201 int fromBin = (int) clusterLabels.getParameterValue(virusIndex);
202 // System.out.println("fromBin=" + fromBin);
203 // if(virusIndex < 5){
204 // System.out.println("virus " + virusIndex + " from bin=" + fromBin + " to bin " + toBin);
205 // }
206 clusterLabels.setParameterValue( virusIndex, toBin); //the proposal
207
208
209
210
211
212
213 //recalculate groupSize
214 for(int i=0; i < groupSize.length; i++){
215 groupSize[i] = 0;
216 }
217 for(int i=0; i < numdata; i++){
218 int label = (int) clusterLabels.getParameterValue(i);
219 groupSize[label ]++;
220 }
221
222 //special case that needs attention on the virus label
223 if( (original_groupSize[fromBin] > 0) && ( groupSize[fromBin] == 0)){
224
225 K.setParameterValue(0, K_int - 1);
226 System.out.println("propose the fromBin " + fromBin + "becomes 0 in size - death of a bin");
227 //actually that label is no longer used..
228 double[] ranNormal = MultivariateNormalDistribution.nextMultivariateNormalVariance( zeroVector2D, sigmaSqMatrix2D);
229
230 mu.getParameter(fromBin).setParameterValue(0, ranNormal[0]);
231 mu.getParameter(fromBin).setParameterValue(1, ranNormal[1]);
232 //logHastingRatio += 0; //this move doesn't change
233 }
234
235 //birth of a new bin.. assign an offset to it
236 if( (original_groupSize[toBin] == 0) && (groupSize[toBin] == 1)){
237
238 K.setParameterValue(0, K_int + 1);
239
240 System.out.println("propose the birth of bin" + toBin);
241 double offset = 0;
242 // double drift = locationDrift.getParameterValue(0); // no longer need to do this here
243 // System.out.println("drift=" + drift);
244 if (virusOffsetsParameter != null) {
245 // System.out.print("virus Offeset Parameter present"+ ": ");
246 // System.out.print( virusOffsetsParameter.getParameterValue(i) + " ");
247 // System.out.print(" drift= " + drift + " ");
248 // offset = drift * virusOffsetsParameter.getParameterValue(virusIndex);
249 //make sure that it is equivalent to double offset = year[virusIndex] - firstYear;
250 }
251 else{
252 System.out.println("virus Offeset Parameter NOT present. We expect one though. Something is wrong.");
253 }
254 double[] ranNormal = MultivariateNormalDistribution.nextMultivariateNormalVariance( zeroVector2D, sigmaSqMatrix2D);
255 mu.getParameter(toBin).setParameterValue(0, ranNormal[0] ); // no need to assign offset anymore.. it's getting taken care of in the ClusterViruses by default
256 // mu.getParameter(toBin).setParameterValue(0, ranNormal[0] + offset);
257 mu.getParameter(toBin).setParameterValue(1, ranNormal[1]);
258
259 //this move should change the Hasting Ratio!
260 //CODE HERE
261
262 }
263
264 } //else
265
266
267
268 /*
269 for(int i=0; i < K_int; i++){
270 double muValue = mu.getParameter(i).getParameterValue(0);
271 double muValue2 = mu.getParameter(i).getParameterValue(1);
272 System.out.println("Group " + i + "\t" + muValue + "\t" + muValue2);
273 }
274 System.out.println("=============================");
275 */
276
277
278
279 //change the mu in the toBin and fromBIn
280 //borrow from getLogLikelihood:
281
282 double[] meanYear = new double[K_int];
283 double[] groupCount = new double[K_int];
284 for(int i=0; i < numdata; i++){
285 int label = (int) clusterLabels.getParameterValue(i);
286 double year = 0;
287 if (virusOffsetsParameter != null) {
288 // System.out.print("virus Offeset Parameter present"+ ": ");
289 // System.out.print( virusOffsetsParameter.getParameterValue(i) + " ");
290 // System.out.print(" drift= " + drift + " ");
291 year = virusOffsetsParameter.getParameterValue(i); //just want year[i]
292 //make sure that it is equivalent to double offset = year[virusIndex] - firstYear;
293 }
294 else{
295 System.out.println("virus Offeset Parameter NOT present. We expect one though. Something is wrong.");
296 }
297 meanYear[ label] = meanYear[ label] + year;
298
299 groupCount[ label ] = groupCount[ label ] +1;
300 }
301 int maxLabel=0;
302 for(int i=0;i< numdata; i++){
303 if(maxLabel < (int) clusterLabels.getParameterValue(i)){
304 maxLabel = (int) clusterLabels.getParameterValue(i);
305 }
306 }
307
308 for(int i=0; i <= maxLabel; i++){
309 meanYear[i] = meanYear[i]/groupCount[i];
310 //System.out.println(meanYear[i]);
311 }
312
313
314 //System.out.println("beta=" + beta);
315 //beta = 1;
316
317 mu0_offset = new double[maxLabel+1];
318 //double[] mu1 = new double[maxLabel];
319
320
321 //System.out.println("maxLabel=" + maxLabel);
322 //now, change the mu..
323 for(int i=0; i <= maxLabel; i++){
324 //System.out.println(meanYear[i]*beta);
325 mu0_offset[i] = meanYear[i];
326 // System.out.println("group " + i + "\t" + mu0_offset[i]);
327 }
328 // System.out.println("=====================");
329
330
331
332
333 //Set the vLoc to be the corresponding mu values , and clusterOffsetsParameter to be the corresponding offsets
334 //virus in the same cluster has the same position
335 for(int i=0; i < numdata; i++){
336 int label = (int) clusterLabels.getParameterValue(i);
337 Parameter vLoc = virusLocations.getParameter(i);
338
339 //setting the virus locs to be equal to the corresponding mu
340 double muValue = mu.getParameter(label).getParameterValue(0);
341 vLoc.setParameterValue(0, muValue);
342
343 double muValue2 = mu.getParameter(label).getParameterValue(1);
344 vLoc.setParameterValue(1, muValue2);
345
346
347 //if we want to apply the mean year virus cluster offset to the cluster
348 if(clusterOffsetsParameter != null){
349 //setting the clusterOffsets to be equal to the mean year of the virus cluster
350 // by doing this, the virus changes cluster AND updates the offset simultaneously
351 clusterOffsetsParameter.setParameterValue( i , mu0_offset[label]);
352 }
353 // System.out.println("mu0_offset[label]=" + mu0_offset[label]);
354 // System.out.println("clusterOffsets now becomes =" + clusterOffsetsParameter.getParameterValue(i) );
355 }
356 // System.out.println("");
357
358
359 //Hasting's Ratio is p(old |new)/ p(new|old)
360
361 //System.out.println("Done doing operation!");
362
363
364
365 //return(logHastingRatio); //log hasting ratio
366 return(logHastingRatio);
367
368 }
369
370
371 public void accept(double deviation) {
372 super.accept(deviation);
373
374 /*
375 if(isMoveMu==1){
376 numAcceptMoveMu++;
377 numProposeMoveMu++;
378 System.out.println("% accept move Mu = " + numAcceptMoveMu/(double)numProposeMoveMu);
379 }
380 else{
381 numAcceptMoveC++;
382 numProposeMoveC++;
383 System.out.println("% accept move C = " + numAcceptMoveC/(double)numProposeMoveC);
384 }
385 */
386
387 // if(virusIndexChange <5){
388 // System.out.println(" - Accepted!");
389 // }
390
391 }
392
393 public void reject(){
394 super.reject();
395
396
397 /*
398 //manually change mu back..
399 if(isMoveMu==1){
400 mu.getParameter(groupSelectedChange).setParameterValue(dimSelectChange, originalValueChange);
401 }
402 //manually change all the affected vLoc back...
403 for(int i=0; i < numdata; i++){
404 int label = (int) clusterLabels.getParameterValue(i);
405 Parameter vLoc = virusLocations.getParameter(i);
406 // double muValue = mu.getParameter(label).getParameterValue(0);
407 // vLoc.setParameterValue(0, muValue);
408 // double muValue2 = mu.getParameter(label).getParameterValue(1);
409 // vLoc.setParameterValue(1, muValue2);
410
411 clusterOffsetsParameter.setParameterValue( i , mu0_offset[label]);
412 }
413 */
414
415
416 /*
417 if(isMoveMu==1){
418 numProposeMoveMu++;
419 System.out.println("% accept move Mu = " + numAcceptMoveMu/(double)numProposeMoveMu);
420 }
421 else{
422 numProposeMoveC++;
423 System.out.println("% accept move C = " + numAcceptMoveC/(double)numProposeMoveC);
424 }
425 */
426 //if(virusIndexChange < 5){
427 // System.out.println(" - Rejected!");
428 //}
429
430
431 /*
432 for(int i=0; i < numdata; i++){
433 Parameter vLoc = virusLocations.getParameter(i);
434
435 if( vLoc.getParameterValue(0) != old_vLoc0[i]){
436
437 System.out.println("virus " + i + " is different: " + vLoc.getParameterValue(0) + " and " + old_vLoc0[i]);
438 }
439
440 //System.out.println(old_vLoc0[i] + ", " + old_vLoc1[i]);
441 vLoc.setParameterValue(0, old_vLoc0[i]);
442 vLoc.setParameterValue(1, old_vLoc1[i]);
443
444 }
445 */
446 //System.exit(0);
447
448
449
450
451 }
452
453
454
455 public final static String CLUSTERALGORITHM_OPERATOR = "ClusterAlgorithmOperator";
456
457
458 //MCMCOperator INTERFACE
459 public final String getOperatorName() {
460 return CLUSTERALGORITHM_OPERATOR;
461 }
462
463 public final void optimize(double targetProb) {
464
465 throw new RuntimeException("This operator cannot be optimized!");
466 }
467
468 public boolean isOptimizing() {
469 return false;
470 }
471
472 public void setOptimizing(boolean opt) {
473 throw new RuntimeException("This operator cannot be optimized!");
474 }
475
476 public double getMinimumAcceptanceLevel() {
477 return 0.1;
478 }
479
480 public double getMaximumAcceptanceLevel() {
481 return 0.4;
482 }
483
484 public double getMinimumGoodAcceptanceLevel() {
485 return 0.20;
486 }
487
488 public double getMaximumGoodAcceptanceLevel() {
489 return 0.30;
490 }
491
492 public String getPerformanceSuggestion() {
493 if (Utils.getAcceptanceProbability(this) < getMinimumAcceptanceLevel()) {
494 return "";
495 } else if (Utils.getAcceptanceProbability(this) > getMaximumAcceptanceLevel()) {
496 return "";
497 } else {
498 return "";
499 }
500 }
501
502
503
504
505
506 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
507
508
509 public final static String VIRUSLOCATIONS = "virusLocations";
510 public final static String MU = "mu";
511 public final static String CLUSTERLABELS = "clusterLabels";
512 public final static String K = "k";
513 public final static String OFFSETS = "offsets";
514 // public final static String LOCATION_DRIFT = "locationDrift"; //no longer need
515
516 public final static String CLUSTER_OFFSETS = "clusterOffsetsParameter";
517
518
519 public String getParserName() {
520 return CLUSTERALGORITHM_OPERATOR;
521 }
522
523 /* (non-Javadoc)
524 * @see dr.xml.AbstractXMLObjectParser#parseXMLObject(dr.xml.XMLObject)
525 */
526 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
527
528 //System.out.println("Parser run. Exit now");
529 //System.exit(0);
530
531 double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
532
533
534 XMLObject cxo = xo.getChild(VIRUSLOCATIONS);
535 MatrixParameter virusLocations = (MatrixParameter) cxo.getChild(MatrixParameter.class);
536
537
538 cxo = xo.getChild(MU);
539 MatrixParameter mu = (MatrixParameter) cxo.getChild(MatrixParameter.class);
540
541 cxo = xo.getChild(CLUSTERLABELS);
542 Parameter clusterLabels = (Parameter) cxo.getChild(Parameter.class);
543
544 cxo = xo.getChild(K);
545 Parameter k = (Parameter) cxo.getChild(Parameter.class);
546
547 cxo = xo.getChild(OFFSETS);
548 Parameter offsets = (Parameter) cxo.getChild(Parameter.class);
549
550 // cxo = xo.getChild(LOCATION_DRIFT);
551 // Parameter locationDrift = (Parameter) cxo.getChild(Parameter.class);
552
553 Parameter clusterOffsetsParameter = null;
554 if (xo.hasChildNamed(CLUSTER_OFFSETS)) {
555 clusterOffsetsParameter = (Parameter) xo.getElementFirstChild(CLUSTER_OFFSETS);
556 }
557
558
559
560 //return new ClusterAlgorithmOperator(virusLocations, mu, clusterLabels, k, weight, offsets, locationDrift, clusterOffsetsParameter);
561 return new ClusterAlgorithmOperator(virusLocations, mu, clusterLabels, k, weight, offsets, clusterOffsetsParameter);
562
563 }
564
565 //************************************************************************
566 // AbstractXMLObjectParser implementation
567 //************************************************************************
568
569 public String getParserDescription() {
570 return "An operator that picks a new allocation of an item to a cluster under the Dirichlet process.";
571 }
572
573 public Class getReturnType() {
574 return ClusterAlgorithmOperator.class;
575 }
576
577
578 public XMLSyntaxRule[] getSyntaxRules() {
579 return rules;
580 }
581
582 private final XMLSyntaxRule[] rules = {
583 AttributeRule.newDoubleRule(MCMCOperator.WEIGHT),
584
585 new ElementRule(VIRUSLOCATIONS, Parameter.class),
586 new ElementRule(MU, Parameter.class),
587 new ElementRule(CLUSTERLABELS, Parameter.class),
588 new ElementRule(K, Parameter.class),
589 new ElementRule(OFFSETS, Parameter.class),
590 // new ElementRule(LOCATION_DRIFT, Parameter.class), //no longer needed
591 //
592 new ElementRule(CLUSTER_OFFSETS, Parameter.class, "Parameter of cluster offsets of all virus"), // no longer REQUIRED
593
594
595 };
596
597 };
598
599
600
601 public int getStepCount() {
602 return 1;
603 }
604
605 }
606
607
608
609