| 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.EmpiricalDistributionLikelihood; |
| 29 |
import dr.inference.distribution.SplineInterpolatedLikelihood; |
| 30 |
import dr.inference.model.Likelihood; |
| 31 |
import dr.inference.model.Statistic; |
| 32 |
import dr.xml.*; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class EmpiricalDistributionLikelihoodParser extends AbstractXMLObjectParser { |
| 39 |
|
| 40 |
public static final String FILE_NAME = "fileName"; |
| 41 |
public static final String DATA = "data"; |
| 42 |
public static final String FROM = "from"; |
| 43 |
public static final String TO = "to"; |
| 44 |
public static final String SPLINE_INTERPOLATION = "splineInterpolation"; |
| 45 |
public static final String DEGREE = "degree"; |
| 46 |
public static final String INVERSE = "inverse"; |
| 47 |
public static final String READ_BY_COLUMN = "readByColumn"; |
| 48 |
public static final String OFFSET="offset"; |
| 49 |
public static final String LOWER = "lower"; |
| 50 |
public static final String UPPER = "upper"; |
| 51 |
|
| 52 |
public String getParserName() { |
| 53 |
return EmpiricalDistributionLikelihood.EMPIRICAL_DISTRIBUTION_LIKELIHOOD; |
| 54 |
} |
| 55 |
|
| 56 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 57 |
|
| 58 |
String fileName = xo.getStringAttribute(FILE_NAME); |
| 59 |
|
| 60 |
boolean splineInterpolation = xo.getAttribute(SPLINE_INTERPOLATION,false); |
| 61 |
int degree = xo.getAttribute(DEGREE,3); |
| 62 |
|
| 63 |
boolean inverse = xo.getAttribute(INVERSE,false); |
| 64 |
|
| 65 |
boolean byColumn = xo.getAttribute(READ_BY_COLUMN,true); |
| 66 |
|
| 67 |
EmpiricalDistributionLikelihood likelihood; |
| 68 |
|
| 69 |
if (splineInterpolation) { |
| 70 |
if( degree < 1 ) |
| 71 |
throw new XMLParseException("Spline degree must be greater than zero!"); |
| 72 |
likelihood = new SplineInterpolatedLikelihood(fileName,degree,inverse,byColumn); |
| 73 |
} else |
| 74 |
|
| 75 |
throw new XMLParseException("Only spline-interpolated empirical distributions are currently support"); |
| 76 |
|
| 77 |
XMLObject cxo1 = xo.getChild(DATA); |
| 78 |
final int from = cxo1.getAttribute(FROM, -1); |
| 79 |
int to = cxo1.getAttribute(TO, -1); |
| 80 |
if (from >= 0 || to >= 0) { |
| 81 |
if (to < 0) { |
| 82 |
to = Integer.MAX_VALUE; |
| 83 |
} |
| 84 |
if (!(from >= 0 && to >= 0 && from < to)) { |
| 85 |
throw new XMLParseException("ill formed from-to"); |
| 86 |
} |
| 87 |
likelihood.setRange(from, to); |
| 88 |
} |
| 89 |
|
| 90 |
for (int j = 0; j < cxo1.getChildCount(); j++) { |
| 91 |
if (cxo1.getChild(j) instanceof Statistic) { |
| 92 |
|
| 93 |
likelihood.addData((Statistic) cxo1.getChild(j)); |
| 94 |
} else { |
| 95 |
throw new XMLParseException("illegal element in " + cxo1.getName() + " element"); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
double offset = cxo1.getAttribute(OFFSET,0); |
| 100 |
likelihood.setOffset(offset); |
| 101 |
|
| 102 |
if (cxo1.hasAttribute(LOWER) || cxo1.hasAttribute(UPPER)) { |
| 103 |
likelihood.setBounds( |
| 104 |
cxo1.getAttribute(LOWER, Double.NEGATIVE_INFINITY), |
| 105 |
cxo1.getAttribute(UPPER, Double.POSITIVE_INFINITY) |
| 106 |
); |
| 107 |
} |
| 108 |
|
| 109 |
return likelihood; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 117 |
return rules; |
| 118 |
} |
| 119 |
|
| 120 |
private final XMLSyntaxRule[] rules = { |
| 121 |
AttributeRule.newStringRule(FILE_NAME), |
| 122 |
AttributeRule.newBooleanRule(SPLINE_INTERPOLATION,true), |
| 123 |
AttributeRule.newIntegerRule(DEGREE,true), |
| 124 |
AttributeRule.newBooleanRule(INVERSE,true), |
| 125 |
AttributeRule.newBooleanRule(READ_BY_COLUMN,true), |
| 126 |
new ElementRule(DATA, new XMLSyntaxRule[]{ |
| 127 |
AttributeRule.newIntegerRule(FROM, true), |
| 128 |
AttributeRule.newIntegerRule(TO, true), |
| 129 |
AttributeRule.newDoubleRule(OFFSET,true), |
| 130 |
AttributeRule.newDoubleRule(LOWER, true), |
| 131 |
AttributeRule.newDoubleRule(UPPER, true), |
| 132 |
new ElementRule(Statistic.class, 1, Integer.MAX_VALUE) |
| 133 |
}) |
| 134 |
}; |
| 135 |
|
| 136 |
public String getParserDescription() { |
| 137 |
return "Calculates the likelihood of some data given some empirically-generated distribution."; |
| 138 |
} |
| 139 |
|
| 140 |
public Class getReturnType() { |
| 141 |
return Likelihood.class; |
| 142 |
} |
| 143 |
} |