Debian Codegraph @

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