Debian Codegraph @

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

raw symbols SCIP
1 package dr.evomodel.antigenic.phyloclustering.operators;
2
3 import cern.jet.random.Beta;
4 import dr.evomodel.antigenic.phyloclustering.TreeClusteringVirusesPrior;
5 import dr.inference.model.Parameter;
6 import dr.inference.operators.GibbsOperator;
7 import dr.inference.operators.MCMCOperator;
8 import dr.inference.operators.SimpleMCMCOperator;
9 import dr.xml.AbstractXMLObjectParser;
10 import dr.xml.AttributeRule;
11 import dr.xml.ElementRule;
12 import dr.xml.XMLObject;
13 import dr.xml.XMLObjectParser;
14 import dr.xml.XMLParseException;
15 import dr.xml.XMLSyntaxRule;
16
17
18 /**
19 * A Gibbs operator for allocation of items to clusters under a distance dependent Chinese restaurant process.
20 *
21 * @author Charles Cheung
22 * @author Trevor Bedford
23 */
24 public class ProbGenericSiteGibbsOperator extends SimpleMCMCOperator implements GibbsOperator {
25
26 public final static String CLASSNAME_OPERATOR = "probGenericSiteGibbsOperator";
27
28 private TreeClusteringVirusesPrior clusterPrior;
29 private Parameter probSites;
30 private int numSites;
31
32 private double probSite_alpha = 1;
33 private double probSite_beta = 1;
34
35
36 public ProbGenericSiteGibbsOperator(double weight, TreeClusteringVirusesPrior clusterPrior_in, Parameter probSites_in,
37 double probSite_alpha_in,
38 double probSite_beta_in) {
39 clusterPrior = clusterPrior_in;
40 probSites = probSites_in;
41 numSites = clusterPrior.getNumSites();
42 setWeight(weight);
43 this.probSite_alpha = probSite_alpha_in;
44 this.probSite_beta = probSite_beta_in;
45
46 //System.out.println("shape = "+ probSite_alpha_in);
47 //System.out.println("shapeB = " + probSite_beta_in);
48
49 }
50
51
52 public double doOperation() {
53
54 // clusterPrior.sampleCausativeStates();
55 int[] causalCount = clusterPrior.getCausalCount();
56 int[] nonCausalCount = clusterPrior.getNonCausalCount();
57
58 //int numSites = 330;
59 int numSites = clusterPrior.getNumSites();
60
61 double numCausals = 0;
62 double numNonCausals = 0;
63 for(int i=0; i < numSites; i++){
64 numCausals = numCausals + causalCount[i];
65 numNonCausals = numNonCausals + nonCausalCount[i];
66 }
67 //System.out.println("perform a Gibbs move on the number of sites");
68 //System.out.println("# causals = " + numCausals);
69 //System.out.println("# noncausals = " + numNonCausals);
70
71 //SHOULD GET IT FROM THE PRIOR SPECIFICATION COZ THEY SHOULD MATCH
72 double value = Beta.staticNextDouble(numCausals+ probSite_alpha, numNonCausals+probSite_beta); //posterior
73
74 probSites.setParameterValue(0, value);
75
76 // System.out.println("hehe: " + whichSite + "," + probSites.getParameterValue(whichSite));
77
78
79 return 0;
80 }
81
82 public void accept(double deviation) {
83 super.accept(deviation);
84 }
85
86 public void reject() {
87 super.reject();
88 }
89
90
91 //MCMCOperator INTERFACE
92 public final String getOperatorName() {
93 return CLASSNAME_OPERATOR;
94 }
95
96
97
98 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
99
100
101 public final static String PROBSITES = "probSites";
102 public final static String PROBSITE_ALPHA = "shape";
103 public final static String PROBSITE_BETA = "shapeB";
104
105 public String getParserName() {
106 return CLASSNAME_OPERATOR;
107 }
108
109 /* (non-Javadoc)
110 * @see dr.xml.AbstractXMLObjectParser#parseXMLObject(dr.xml.XMLObject)
111 */
112 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
113
114 //System.out.println("Parser run. Exit now");
115 //System.exit(0);
116
117 double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
118
119 XMLObject cxo = xo.getChild(PROBSITES);
120 Parameter probSites = (Parameter) cxo.getChild(Parameter.class);
121
122 TreeClusteringVirusesPrior clusterPrior = (TreeClusteringVirusesPrior) xo.getChild(TreeClusteringVirusesPrior.class);
123
124 double probSite_alpha = 1;
125 if (xo.hasAttribute(PROBSITE_ALPHA)) {
126 probSite_alpha = xo.getDoubleAttribute(PROBSITE_ALPHA);
127 }
128 double probSite_beta = 1;
129 if (xo.hasAttribute(PROBSITE_BETA)) {
130 probSite_beta = xo.getDoubleAttribute(PROBSITE_BETA);
131 }
132
133
134 return new ProbGenericSiteGibbsOperator(weight, clusterPrior, probSites, probSite_alpha, probSite_beta);
135
136 }
137
138 //************************************************************************
139 // AbstractXMLObjectParser implementation
140 //************************************************************************
141
142 public String getParserDescription() {
143 return "An operator that updates the probability of sites given a beta distribution.";
144 }
145
146 public Class getReturnType() {
147 return ProbSitesGibbsOperator.class;
148 }
149
150
151 public XMLSyntaxRule[] getSyntaxRules() {
152 return rules;
153 }
154
155 private final XMLSyntaxRule[] rules = {
156 AttributeRule.newDoubleRule(MCMCOperator.WEIGHT),
157 new ElementRule(TreeClusteringVirusesPrior.class),
158 new ElementRule(PROBSITES, Parameter.class),
159 AttributeRule.newDoubleRule(PROBSITE_ALPHA, true, "the alpha parameter in the Beta prior"),
160 AttributeRule.newDoubleRule(PROBSITE_BETA, true, "the beta parameter in the Beta prior"),
161
162 };
163
164 };
165
166
167
168 public int getStepCount() {
169 return 1;
170 }
171
172
173 }