| 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.DistributionLikelihood; |
| 29 |
import dr.inference.distribution.ParametricDistributionModel; |
| 30 |
import dr.inference.model.Statistic; |
| 31 |
import dr.math.distributions.RandomGenerator; |
| 32 |
import dr.xml.*; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class DistributionLikelihoodParser extends AbstractXMLObjectParser { |
| 38 |
|
| 39 |
public static final String DISTRIBUTION = "distribution"; |
| 40 |
public static final String DATA = "data"; |
| 41 |
public static final String FROM = "from"; |
| 42 |
public static final String TO = "to"; |
| 43 |
|
| 44 |
|
| 45 |
public String getParserName() { |
| 46 |
return DistributionLikelihood.DISTRIBUTION_LIKELIHOOD; |
| 47 |
} |
| 48 |
|
| 49 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 50 |
|
| 51 |
final XMLObject cxo = xo.getChild(DISTRIBUTION); |
| 52 |
ParametricDistributionModel model = (ParametricDistributionModel) cxo.getChild(ParametricDistributionModel.class); |
| 53 |
|
| 54 |
DistributionLikelihood likelihood = new DistributionLikelihood(model); |
| 55 |
|
| 56 |
XMLObject cxo1 = xo.getChild(DATA); |
| 57 |
final int from = cxo1.getAttribute(FROM, -1); |
| 58 |
int to = cxo1.getAttribute(TO, -1); |
| 59 |
if (from >= 0 || to >= 0) { |
| 60 |
if (to < 0) { |
| 61 |
to = Integer.MAX_VALUE; |
| 62 |
} |
| 63 |
if (!(from >= 0 && to >= 0 && from < to)) { |
| 64 |
throw new XMLParseException("ill formed from-to"); |
| 65 |
} |
| 66 |
likelihood.setRange(from, to); |
| 67 |
} |
| 68 |
|
| 69 |
for (int j = 0; j < cxo1.getChildCount(); j++) { |
| 70 |
if (cxo1.getChild(j) instanceof Statistic) { |
| 71 |
|
| 72 |
likelihood.addData((Statistic) cxo1.getChild(j)); |
| 73 |
} else { |
| 74 |
throw new XMLParseException("illegal element in " + cxo1.getName() + " element"); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
return likelihood; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 86 |
return rules; |
| 87 |
} |
| 88 |
|
| 89 |
private final XMLSyntaxRule[] rules = { |
| 90 |
new ElementRule(DISTRIBUTION, |
| 91 |
new XMLSyntaxRule[]{new ElementRule(ParametricDistributionModel.class)}), |
| 92 |
new ElementRule(DATA, new XMLSyntaxRule[]{ |
| 93 |
AttributeRule.newIntegerRule(FROM, true), |
| 94 |
AttributeRule.newIntegerRule(TO, true), |
| 95 |
new ElementRule(Statistic.class, 1, Integer.MAX_VALUE) |
| 96 |
}) |
| 97 |
}; |
| 98 |
|
| 99 |
public String getParserDescription() { |
| 100 |
return "Calculates the likelihood of some data given some parametric or empirical distribution."; |
| 101 |
} |
| 102 |
|
| 103 |
public Class getReturnType() { |
| 104 |
return DistributionLikelihood.class; |
| 105 |
} |
| 106 |
} |