| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.inferencexml.distribution; |
| 27 |
|
| 28 |
import dr.inference.distribution.BinomialLikelihood; |
| 29 |
import dr.inference.model.Likelihood; |
| 30 |
import dr.inference.model.Parameter; |
| 31 |
import dr.xml.*; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public class BinomialLikelihoodParser extends AbstractXMLObjectParser { |
| 37 |
|
| 38 |
public static final String TRIALS = "trials"; |
| 39 |
public static final String COUNTS = "counts"; |
| 40 |
public static final String PROPORTION = "proportion"; |
| 41 |
public static final String VALUES = "values"; |
| 42 |
public static final String ON_LOGIT_SCALE = "onLogitScale"; |
| 43 |
|
| 44 |
public String getParserName() { |
| 45 |
return BinomialLikelihood.BINOMIAL_LIKELIHOOD; |
| 46 |
} |
| 47 |
|
| 48 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 49 |
|
| 50 |
final boolean onLogitScale = xo.getAttribute(ON_LOGIT_SCALE, false); |
| 51 |
|
| 52 |
XMLObject cxo = xo.getChild(COUNTS); |
| 53 |
Parameter countsParam = (Parameter) cxo.getChild(Parameter.class); |
| 54 |
|
| 55 |
cxo = xo.getChild(PROPORTION); |
| 56 |
Parameter proportionParam = (Parameter) cxo.getChild(Parameter.class); |
| 57 |
|
| 58 |
if (proportionParam.getDimension() != 1 && proportionParam.getDimension() != countsParam.getDimension()) { |
| 59 |
throw new XMLParseException("Proportion dimension (" + proportionParam.getDimension() + ") " + |
| 60 |
"must equal 1 or counts dimension (" + countsParam.getDimension() + ")"); |
| 61 |
} |
| 62 |
|
| 63 |
Parameter trialsParam; |
| 64 |
cxo = xo.getChild(TRIALS); |
| 65 |
if (cxo != null) { |
| 66 |
if (cxo.hasAttribute(VALUES)) { |
| 67 |
int[] tmp = cxo.getIntegerArrayAttribute(VALUES); |
| 68 |
double[] v = new double[tmp.length]; |
| 69 |
for (int i = 0; i < tmp.length; ++i) { |
| 70 |
v[i] = tmp[i]; |
| 71 |
} |
| 72 |
trialsParam = new Parameter.Default(v); |
| 73 |
} else { |
| 74 |
trialsParam = (Parameter) cxo.getChild(Parameter.class); |
| 75 |
} |
| 76 |
} else { |
| 77 |
trialsParam = new Parameter.Default(1.0); |
| 78 |
} |
| 79 |
|
| 80 |
if (trialsParam.getDimension() <= 1) { |
| 81 |
try { |
| 82 |
trialsParam.setDimension(countsParam.getDimension()); |
| 83 |
} catch (UnsupportedOperationException uoe) { |
| 84 |
|
| 85 |
|
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
if (trialsParam.getDimension() != countsParam.getDimension()) { |
| 90 |
throw new XMLParseException("Trials dimension (" + trialsParam.getDimension() |
| 91 |
+ ") must equal counts dimension (" + countsParam.getDimension() + ")"); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
return new BinomialLikelihood(trialsParam, proportionParam, countsParam, onLogitScale); |
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 104 |
return rules; |
| 105 |
} |
| 106 |
|
| 107 |
private final XMLSyntaxRule[] rules = { |
| 108 |
AttributeRule.newBooleanRule(ON_LOGIT_SCALE, true), |
| 109 |
new ElementRule(COUNTS, |
| 110 |
new XMLSyntaxRule[]{new ElementRule(Parameter.class)}), |
| 111 |
new ElementRule(PROPORTION, |
| 112 |
new XMLSyntaxRule[]{new ElementRule(Parameter.class)}), |
| 113 |
new XORRule( |
| 114 |
new ElementRule(TRIALS, |
| 115 |
new XMLSyntaxRule[]{AttributeRule.newIntegerArrayRule(VALUES, false),}) |
| 116 |
, |
| 117 |
new ElementRule(TRIALS, |
| 118 |
new XMLSyntaxRule[]{new ElementRule(Parameter.class)} |
| 119 |
)), |
| 120 |
}; |
| 121 |
|
| 122 |
public String getParserDescription() { |
| 123 |
return "Calculates the likelihood of some data given some parametric or empirical distribution."; |
| 124 |
} |
| 125 |
|
| 126 |
public Class getReturnType() { |
| 127 |
return Likelihood.class; |
| 128 |
} |
| 129 |
} |