Revision: 6751
Author: msuchard
Date: Fri Apr 24 04:15:36 2015 UTC
Log: SwapParameterOperator throws OperatorException when swap is
out-of-bounds, avoids calls to recompute sequence likelihood with negative
branch lengths.
https://code.google.com/p/beast-mcmc/source/detail?r=6751
Modified:
/trunk/src/dr/inference/operators/SwapParameterOperator.java
=======================================
--- /trunk/src/dr/inference/operators/SwapParameterOperator.java Thu Apr 23
23:20:13 2015 UTC
+++ /trunk/src/dr/inference/operators/SwapParameterOperator.java Fri Apr 24
04:15:36 2015 UTC
@@ -25,6 +25,7 @@
package dr.inference.operators;
+import dr.inference.model.Bounds;
import dr.inference.model.Parameter;
import dr.inferencexml.operators.SwapOperatorParser;
import dr.math.MathUtils;
@@ -59,7 +60,7 @@
/**
* swap all values in two random parameters.
*/
- public final double doOperation() {
+ public final double doOperation() throws OperatorFailedException {
int i = MathUtils.nextInt(parameterList.size());
int j = i;
@@ -68,13 +69,25 @@
j = MathUtils.nextInt(parameterList.size());
}
- Parameter a = parameterList.get(i);
- Parameter b = parameterList.get(j);
+ final Parameter a = parameterList.get(i);
+ final Parameter b = parameterList.get(j);
+
+ Bounds<Double> aBounds = a.getBounds();
+ Bounds<Double> bBounds = b.getBounds();
+
for (int k = 0; k < a.getDimension(); ++k) {
- double tmp = a.getParameterValue(k);
- a.setParameterValueQuietly(k, b.getParameterValue(k));
- b.setParameterValueQuietly(k, tmp);
+ final double ak = a.getParameterValue(k);
+ final double bk = b.getParameterValue(k);
+
+ // Check bk outside of aBounds or ak outside of bBounds
+ if (isOutside(aBounds, bk, k) || isOutside(bBounds, ak, k)) {
+ throw new OperatorFailedException("proposed value outside
boundaries");
+ }
+
+ // Swap
+ a.setParameterValueQuietly(k, bk);
+ b.setParameterValueQuietly(k, ak);
}
a.fireParameterChangedEvent();
@@ -82,6 +95,10 @@
return 0.0;
}
+
+ private boolean isOutside(final Bounds<Double> bounds, final double x,
final int index) {
+ return (x < bounds.getLowerLimit(index) || x >
bounds.getUpperLimit(index));
+ }
private String getParameterNames() {
if (parameterNames == null) {