Skip to content

Commit

Permalink
towards renaming scaleFactor to windowSize in BactrianRandomWalkOpera…
Browse files Browse the repository at this point in the history
…tor #1169
  • Loading branch information
rbouckaert committed Sep 29, 2024
1 parent edc84fe commit 5eacf16
Showing 1 changed file with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<RealParameter> parameterInput = new Input<>("parameter", "the parameter to operate a random walk on.", Validate.REQUIRED);
public final Input<Double> scaleFactorInput = new Input<>("scaleFactor", "scaling factor: larger means more bold proposals", 1.0);
public final Input<Double> scaleFactorInput = new Input<>("scaleFactor", "deprecated -- use windowSize instead");
public final Input<Double> windowSizeInput = new Input<>("windowSize", "window size: larger means more bold proposals");
final public Input<Boolean> 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
Expand All @@ -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;
Expand All @@ -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;
}

/**
Expand All @@ -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);
}
}

Expand All @@ -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

0 comments on commit 5eacf16

Please sign in to comment.