| 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 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 52 |
int[] causalCount = clusterPrior.getCausalCount(); |
| 53 |
int[] nonCausalCount = clusterPrior.getNonCausalCount(); |
| 54 |
|
| 55 |
|
| 56 |
int numSites = probSites.getDimension(); |
| 57 |
|
| 58 |
int whichSite = (int) (Math.floor( MathUtils.nextDouble()*numSites)); |
| 59 |
|
| 60 |
|
| 61 |
double value = Beta.staticNextDouble(causalCount[whichSite]+probSite_alpha, nonCausalCount[whichSite]+probSite_beta); |
| 62 |
|
| 63 |
probSites.setParameterValue(whichSite, value); |
| 64 |
|
| 65 |
|
| 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 |
|
| 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 |
|
| 99 |
|
| 100 |
|
| 101 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 102 |
|
| 103 |
|
| 104 |
|
| 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 |
|
| 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 |
} |