use of dr.inference.operators.RandomWalkOperator in project beast-mcmc by beast-dev.
the class RandomWalkOperatorParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
CoercionMode mode = CoercionMode.parseMode(xo);
double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
double windowSize = xo.getDoubleAttribute(WINDOW_SIZE);
Parameter parameter = (Parameter) xo.getChild(Parameter.class);
Double lower = null;
Double upper = null;
if (xo.hasAttribute(LOWER)) {
lower = xo.getDoubleAttribute(LOWER);
}
if (xo.hasAttribute(UPPER)) {
upper = xo.getDoubleAttribute(UPPER);
}
if (lower != null || upper != null) {
throw new XMLParseException("Do not provide lower/upper bounds on for a RandomWalkOperator; set these values are parameter bounds");
}
RandomWalkOperator.BoundaryCondition condition = RandomWalkOperator.BoundaryCondition.valueOf(xo.getAttribute(BOUNDARY_CONDITION, RandomWalkOperator.BoundaryCondition.reflecting.name()));
if (condition == RandomWalkOperator.BoundaryCondition.logit) {
final Bounds<Double> bounds = parameter.getBounds();
final int dim = parameter.getDimension();
boolean boundsSet = true;
for (int i = 0; i < dim; ++i) {
if (bounds.getLowerLimit(i) == null || Double.isInfinite(bounds.getLowerLimit(i))) {
boundsSet = false;
}
if (bounds.getUpperLimit(i) == null || Double.isInfinite(bounds.getUpperLimit(i))) {
boundsSet = false;
}
}
if (!boundsSet) {
throw new XMLParseException("The logit transformed RandomWalkOperator cannot be used on a parameter without bounds.");
}
}
if (xo.hasChildNamed(UPDATE_INDEX)) {
XMLObject cxo = xo.getChild(UPDATE_INDEX);
Parameter updateIndex = (Parameter) cxo.getChild(Parameter.class);
if (updateIndex.getDimension() != parameter.getDimension())
throw new RuntimeException("Parameter to update and missing indices must have the same dimension");
return new RandomWalkOperator(parameter, updateIndex, windowSize, condition, weight, mode, lower, upper);
}
return new RandomWalkOperator(parameter, null, windowSize, condition, weight, mode, lower, upper);
}
use of dr.inference.operators.RandomWalkOperator in project beast-mcmc by beast-dev.
the class RandomWalkOperatorTest method testRandomWalkOperator.
public void testRandomWalkOperator() {
Parameter parameter = new Parameter.Default("test", 0.5, 0.0, 1.0);
RandomWalkOperator rwo = new RandomWalkOperator(parameter, 1.0, RandomWalkOperator.BoundaryCondition.reflecting, 1.0, CoercionMode.COERCION_OFF);
double test1 = rwo.reflectValue(8.7654321, 3.14159265, Double.POSITIVE_INFINITY);
double test2 = rwo.reflectValue(8.7654321, Double.NEGATIVE_INFINITY, 3.14159265);
double test3 = rwo.reflectValue(1.2345678, 3.14159265, 2.0 * 3.14159265);
double test4 = rwo.reflectValue(12345678.987654321, 3.14159265, 2.0 * 3.14159265);
double test1b = rwo.reflectValueLoop(8.7654321, 3.14159265, Double.POSITIVE_INFINITY);
double test2b = rwo.reflectValueLoop(8.7654321, Double.NEGATIVE_INFINITY, 3.14159265);
double test3b = rwo.reflectValueLoop(1.2345678, 3.14159265, 2.0 * 3.14159265);
double test4b = rwo.reflectValueLoop(12345678.987654321, 3.14159265, 2.0 * 3.14159265);
assertEquals(test1, test1b);
assertEquals(test2, test2b);
assertEquals(test3, test3b);
assertTrue(Math.abs(test4 - test4b) < 0.001);
}
Aggregations