| 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 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 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 |
|
| 47 |
|
| 48 |
|
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
public double doOperation() { |
| 53 |
|
| 54 |
|
| 55 |
int[] causalCount = clusterPrior.getCausalCount(); |
| 56 |
int[] nonCausalCount = clusterPrior.getNonCausalCount(); |
| 57 |
|
| 58 |
|
| 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 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
double value = Beta.staticNextDouble(numCausals+ probSite_alpha, numNonCausals+probSite_beta); |
| 73 |
|
| 74 |
probSites.setParameterValue(0, value); |
| 75 |
|
| 76 |
|
| 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 |
|
| 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 |
|
| 110 |
|
| 111 |
|
| 112 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 113 |
|
| 114 |
|
| 115 |
|
| 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 |
|
| 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 |
} |