use of dr.inference.distribution.EmpiricalDistributionData in project beast-mcmc by beast-dev.
the class EmpiricalDistributionLikelihoodParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
boolean splineInterpolation = xo.getAttribute(SPLINE_INTERPOLATION, false);
// Default is cubic-spline
int degree = xo.getAttribute(DEGREE, 3);
boolean inverse = xo.getAttribute(INVERSE, false);
EmpiricalDistributionLikelihood likelihood;
if (xo.hasChildNamed(FILE_INFORMATION)) {
String fileName = xo.getStringAttribute(FILE_NAME);
boolean byColumn = xo.getAttribute(READ_BY_COLUMN, true);
if (splineInterpolation) {
if (degree < 1)
throw new XMLParseException("Spline degree must be greater than zero!");
likelihood = new SplineInterpolatedLikelihood(fileName, degree, inverse, byColumn);
} else
// likelihood = new EmpiricalDistributionLikelihood(fileName,inverse,byColumn);
throw new XMLParseException("Only spline-interpolated empirical distributions are currently support");
} else {
List<EmpiricalDistributionData> dataList = new ArrayList<>();
for (int i = 0; i < xo.getChildCount(); ++i) {
XMLObject cxo = (XMLObject) xo.getChild(i);
if (cxo.getName().equals(RAW_VALUES)) {
Parameter thetaParameter = getRawValues(VALUES, cxo);
Parameter likelihoodParameter = getRawValues(LIKELIHOOD, cxo);
if (likelihoodParameter.getDimension() != thetaParameter.getDimension()) {
throw new XMLParseException("Unequal grid lengths");
}
boolean densityInLogSpace = cxo.getAttribute(DENSITY_IN_LOG_SPACE, true);
dataList.add(new EmpiricalDistributionData(thetaParameter.getParameterValues(), likelihoodParameter.getParameterValues(), densityInLogSpace));
}
}
likelihood = new SplineInterpolatedLikelihood(dataList, degree, inverse);
}
XMLObject cxo1 = xo.getChild(DATA);
final int from = cxo1.getAttribute(FROM, -1);
int to = cxo1.getAttribute(TO, -1);
if (from >= 0 || to >= 0) {
if (to < 0) {
to = Integer.MAX_VALUE;
}
if (!(from >= 0 && to >= 0 && from < to)) {
throw new XMLParseException("ill formed from-to");
}
likelihood.setRange(from, to);
}
for (int j = 0; j < cxo1.getChildCount(); j++) {
if (cxo1.getChild(j) instanceof Statistic) {
likelihood.addData((Statistic) cxo1.getChild(j));
} else {
throw new XMLParseException("illegal element in " + cxo1.getName() + " element");
}
}
if (likelihood.getDistributionDimension() != 1 && (likelihood.getDistributionDimension() != likelihood.getDimension())) {
throw new XMLParseException("Data dimension != distribution dimension");
}
double offset = cxo1.getAttribute(OFFSET, 0);
likelihood.setOffset(offset);
if (cxo1.hasAttribute(LOWER) || cxo1.hasAttribute(UPPER)) {
likelihood.setBounds(cxo1.getAttribute(LOWER, Double.NEGATIVE_INFINITY), cxo1.getAttribute(UPPER, Double.POSITIVE_INFINITY));
}
return likelihood;
}
Aggregations