From 5eacf16ad3b1772e24549b32d699c978a01e7349 Mon Sep 17 00:00:00 2001 From: rbouckaert Date: Mon, 30 Sep 2024 11:13:42 +1300 Subject: [PATCH] towards renaming scaleFactor to windowSize in BactrianRandomWalkOperator #1169 --- .../kernel/BactrianRandomWalkOperator.java | 28 +++++++++++++------ 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/beast/base/inference/operator/kernel/BactrianRandomWalkOperator.java b/src/beast/base/inference/operator/kernel/BactrianRandomWalkOperator.java index c12013c0..b359621c 100644 --- a/src/beast/base/inference/operator/kernel/BactrianRandomWalkOperator.java +++ b/src/beast/base/inference/operator/kernel/BactrianRandomWalkOperator.java @@ -17,15 +17,24 @@ + "p(x) = 1/2*N(x;-m,1-m^2) + 1/2*N(x;+m,1-m^2) and more efficient than RealRandomWalkOperator") public class BactrianRandomWalkOperator extends KernelOperator { final public Input parameterInput = new Input<>("parameter", "the parameter to operate a random walk on.", Validate.REQUIRED); - public final Input scaleFactorInput = new Input<>("scaleFactor", "scaling factor: larger means more bold proposals", 1.0); + public final Input scaleFactorInput = new Input<>("scaleFactor", "deprecated -- use windowSize instead"); + public final Input windowSizeInput = new Input<>("windowSize", "window size: larger means more bold proposals"); final public Input optimiseInput = new Input<>("optimise", "flag to indicate that the scale factor is automatically changed in order to achieve a good acceptance rate (default true)", true); - double scaleFactor; + double windowSize; @Override public void initAndValidate() { super.initAndValidate(); - scaleFactor = scaleFactorInput.get(); + + if (scaleFactorInput.get() != null && windowSizeInput.get() != null) { + throw new IllegalArgumentException("Specify at most one of windowSize and scaleFactor, not both -- note scaleFactor is deprecated"); + } + + // windowSize is windowSizeInput or scaleFactorInput if specified, otherwise defaults to 1.0 + windowSize = scaleFactorInput.get() != null ? scaleFactorInput.get(): + windowSizeInput.get() != null ? windowSizeInput.get() : + 1.0; } @Override @@ -35,7 +44,7 @@ public double proposal() { int i = Randomizer.nextInt(param.getDimension()); double value = param.getValue(i); - double newValue = value + kernelDistribution.getRandomDelta(i, value, scaleFactor); + double newValue = value + kernelDistribution.getRandomDelta(i, value, windowSize); if (newValue < param.getLower() || newValue > param.getUpper()) { return Double.NEGATIVE_INFINITY; @@ -49,12 +58,12 @@ public double proposal() { @Override public double getCoercableParameterValue() { - return scaleFactor; + return windowSize; } @Override public void setCoercableParameterValue(double value) { - scaleFactor = value; + windowSize = value; } /** @@ -70,8 +79,8 @@ public void optimize(double logAlpha) { // must be overridden by operator implementation to have an effect double delta = calcDelta(logAlpha); - delta += Math.log(scaleFactor); - scaleFactor = Math.exp(delta); + delta += Math.log(windowSize); + windowSize = Math.exp(delta); } } @@ -90,11 +99,12 @@ public final String getPerformanceSuggestion() { if (ratio < 0.5) ratio = 0.5; // new scale factor - double newWindowSize = scaleFactor * ratio; + double newWindowSize = windowSize * ratio; DecimalFormat formatter = new DecimalFormat("#.###"); if (prob < 0.10 || prob > 0.40) { return "Try setting scale factor to about " + formatter.format(newWindowSize); } else return ""; } + } // class BactrianRandomWalkOperator \ No newline at end of file