| 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.HierarchicalGraphLikelihood; |
| 29 |
import dr.inference.model.Likelihood; |
| 30 |
import dr.inference.model.MatrixParameter; |
| 31 |
import dr.inference.model.Parameter; |
| 32 |
import dr.xml.*; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class HierarchicalGraphLikelihoodParser extends AbstractXMLObjectParser { |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public static final String HIERARCHICAL_INDICATOR = "hierarchicalIndicator"; |
| 44 |
public static final String STRATA_INDICATOR = "strataIndicator"; |
| 45 |
public static final String PROB = "prob"; |
| 46 |
|
| 47 |
public String getParserName() { |
| 48 |
return HierarchicalGraphLikelihood.HIERARCHICAL_GRAPH_LIKELIHOOD; |
| 49 |
} |
| 50 |
|
| 51 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 52 |
|
| 53 |
XMLObject cxo = xo.getChild(HIERARCHICAL_INDICATOR); |
| 54 |
Parameter hierarchicalIndicator = (Parameter) cxo.getChild(Parameter.class); |
| 55 |
|
| 56 |
cxo = xo.getChild(STRATA_INDICATOR); |
| 57 |
MatrixParameter strataIndicatorMatrix = new MatrixParameter("matrix"); |
| 58 |
int dim = 0; |
| 59 |
for (int i = 0; i < cxo.getChildCount(); i++) { |
| 60 |
Parameter parameter = (Parameter) cxo.getChild(i); |
| 61 |
strataIndicatorMatrix.addParameter(parameter); |
| 62 |
if (i == 0) |
| 63 |
dim = parameter.getDimension(); |
| 64 |
else if (dim != parameter.getDimension()) |
| 65 |
throw new XMLParseException("All parameters must have the same dimension to construct a rectangular matrix"); |
| 66 |
} |
| 67 |
|
| 68 |
if (hierarchicalIndicator.getDimension()!= strataIndicatorMatrix.getRowDimension()) |
| 69 |
throw new XMLParseException("Hierarchical and starta parameters don't have the same dimentions"); |
| 70 |
|
| 71 |
|
| 72 |
cxo = xo.getChild(PROB); |
| 73 |
Parameter prob = (Parameter) cxo.getChild(Parameter.class); |
| 74 |
|
| 75 |
return new HierarchicalGraphLikelihood(hierarchicalIndicator, strataIndicatorMatrix, prob); |
| 76 |
|
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 84 |
return rules; |
| 85 |
} |
| 86 |
|
| 87 |
private final XMLSyntaxRule[] rules = { |
| 88 |
new ElementRule(HIERARCHICAL_INDICATOR, |
| 89 |
new XMLSyntaxRule[]{new ElementRule(Parameter.class)}), |
| 90 |
new ElementRule(STRATA_INDICATOR, |
| 91 |
new XMLSyntaxRule[]{new ElementRule(Parameter.class, 1, Integer.MAX_VALUE)}), |
| 92 |
new ElementRule(PROB, |
| 93 |
new XMLSyntaxRule[]{new ElementRule(Parameter.class)}), |
| 94 |
|
| 95 |
}; |
| 96 |
|
| 97 |
public String getParserDescription() { |
| 98 |
return "Calculates the likelihood of strata graph given hierarchical graph and p."; |
| 99 |
} |
| 100 |
|
| 101 |
public Class getReturnType() { |
| 102 |
return Likelihood.class; |
| 103 |
} |
| 104 |
} |