| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.inference.operators; |
| 27 |
|
| 28 |
import dr.inference.model.Bounds; |
| 29 |
import dr.inference.model.Parameter; |
| 30 |
import dr.inferencexml.operators.RandomWalkOperatorParser; |
| 31 |
import dr.math.MathUtils; |
| 32 |
import dr.util.Transform; |
| 33 |
|
| 34 |
import java.util.ArrayList; |
| 35 |
import java.util.List; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public class RandomWalkOperator extends AbstractAdaptableOperator { |
| 45 |
|
| 46 |
public enum BoundaryCondition { |
| 47 |
rejecting, |
| 48 |
reflecting, |
| 49 |
absorbing, |
| 50 |
log, |
| 51 |
logit |
| 52 |
} |
| 53 |
|
| 54 |
public RandomWalkOperator(Parameter parameter, double windowSize, BoundaryCondition bc, double weight, AdaptationMode mode) { |
| 55 |
this(parameter, null, windowSize, bc, weight, mode); |
| 56 |
} |
| 57 |
|
| 58 |
public RandomWalkOperator(Parameter parameter, Parameter updateIndex, double windowSize, BoundaryCondition boundaryCondition, |
| 59 |
double weight, AdaptationMode mode) { |
| 60 |
super(mode); |
| 61 |
this.parameter = parameter; |
| 62 |
this.windowSize = windowSize; |
| 63 |
this.boundaryCondition = boundaryCondition; |
| 64 |
|
| 65 |
setWeight(weight); |
| 66 |
if (updateIndex != null) { |
| 67 |
updateMap = new ArrayList<Integer>(); |
| 68 |
for (int i = 0; i < updateIndex.getDimension(); i++) { |
| 69 |
if (updateIndex.getParameterValue(i) == 1.0) |
| 70 |
updateMap.add(i); |
| 71 |
} |
| 72 |
updateMapSize=updateMap.size(); |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public Parameter getParameter() { |
| 80 |
return parameter; |
| 81 |
} |
| 82 |
|
| 83 |
public final double getWindowSize() { |
| 84 |
return windowSize; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public final double doOperation() { |
| 91 |
|
| 92 |
|
| 93 |
if (parameter.getDimension() <= 0) { |
| 94 |
throw new RuntimeException("Illegal Dimension"); |
| 95 |
} |
| 96 |
|
| 97 |
int dim; |
| 98 |
if (updateMap == null) { |
| 99 |
dim = MathUtils.nextInt(parameter.getDimension()); |
| 100 |
} else { |
| 101 |
dim = updateMap.get(MathUtils.nextInt(updateMapSize)); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
double draw = (2.0 * MathUtils.nextDouble() - 1.0) * windowSize; |
| 106 |
|
| 107 |
double oldValue = parameter.getParameterValue(dim); |
| 108 |
|
| 109 |
final Bounds<Double> bounds = parameter.getBounds(); |
| 110 |
final double lower = bounds.getLowerLimit(dim); |
| 111 |
final double upper = bounds.getUpperLimit(dim); |
| 112 |
|
| 113 |
if (boundaryCondition == BoundaryCondition.logit) { |
| 114 |
|
| 115 |
double x1 = (oldValue - lower) / (upper - lower); |
| 116 |
|
| 117 |
double x2 = Transform.LOGIT.inverse(Transform.LOGIT.transform(x1) + draw); |
| 118 |
|
| 119 |
|
| 120 |
parameter.setParameterValue(dim, (x2 * (upper - lower)) + lower); |
| 121 |
|
| 122 |
|
| 123 |
return Transform.LOGIT.getLogJacobian(x1) - Transform.LOGIT.getLogJacobian(x2); |
| 124 |
|
| 125 |
} else if (boundaryCondition == BoundaryCondition.log) { |
| 126 |
|
| 127 |
double x1 = oldValue - lower; |
| 128 |
|
| 129 |
double x2 = Transform.LOG.inverse(Transform.LOG.transform(x1) + draw); |
| 130 |
|
| 131 |
|
| 132 |
parameter.setParameterValue(dim, x2 + lower); |
| 133 |
|
| 134 |
|
| 135 |
return Transform.LOG.getLogJacobian(x1) - Transform.LOG.getLogJacobian(x2); |
| 136 |
|
| 137 |
} else { |
| 138 |
double newValue = oldValue + draw; |
| 139 |
if (boundaryCondition == BoundaryCondition.reflecting) { |
| 140 |
newValue = reflectValue(newValue, lower, upper); |
| 141 |
} else if (boundaryCondition == BoundaryCondition.absorbing && (newValue < lower || newValue > upper)) { |
| 142 |
return 0.0; |
| 143 |
} else if (boundaryCondition == BoundaryCondition.rejecting && (newValue < lower || newValue > upper)) { |
| 144 |
return Double.NEGATIVE_INFINITY; |
| 145 |
} |
| 146 |
|
| 147 |
parameter.setParameterValue(dim, newValue); |
| 148 |
|
| 149 |
return 0.0; |
| 150 |
} |
| 151 |
|
| 152 |
} |
| 153 |
|
| 154 |
public static double reflectValue(double value, double lower, double upper) { |
| 155 |
|
| 156 |
double newValue = value; |
| 157 |
|
| 158 |
if (value < lower) { |
| 159 |
if (Double.isInfinite(upper)) { |
| 160 |
|
| 161 |
newValue = lower + (lower - value); |
| 162 |
} else { |
| 163 |
double remainder = lower - value; |
| 164 |
|
| 165 |
double widths = Math.floor(remainder / (upper - lower)); |
| 166 |
remainder -= (upper - lower) * widths; |
| 167 |
|
| 168 |
|
| 169 |
if (widths % 2 == 0) { |
| 170 |
newValue = lower + remainder; |
| 171 |
|
| 172 |
} else { |
| 173 |
newValue = upper - remainder; |
| 174 |
} |
| 175 |
} |
| 176 |
} else if (value > upper) { |
| 177 |
if (Double.isInfinite(lower)) { |
| 178 |
|
| 179 |
newValue = upper - (newValue - upper); |
| 180 |
} else { |
| 181 |
|
| 182 |
double remainder = value - upper; |
| 183 |
|
| 184 |
double widths = Math.floor(remainder / (upper - lower)); |
| 185 |
remainder -= (upper - lower) * widths; |
| 186 |
|
| 187 |
|
| 188 |
if (widths % 2 == 0) { |
| 189 |
newValue = upper - remainder; |
| 190 |
|
| 191 |
} else { |
| 192 |
newValue = lower + remainder; |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
return newValue; |
| 198 |
} |
| 199 |
|
| 200 |
public double reflectValueLoop(double value, double lower, double upper) { |
| 201 |
double newValue = value; |
| 202 |
|
| 203 |
while (newValue < lower || newValue > upper) { |
| 204 |
if (newValue < lower) { |
| 205 |
newValue = lower + (lower - newValue); |
| 206 |
} |
| 207 |
if (newValue > upper) { |
| 208 |
newValue = upper - (newValue - upper); |
| 209 |
|
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
return newValue; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
public final String getOperatorName() { |
| 218 |
return parameter.getParameterName(); |
| 219 |
} |
| 220 |
|
| 221 |
public double getAdaptableParameter() { |
| 222 |
return Math.log(windowSize); |
| 223 |
} |
| 224 |
|
| 225 |
public void setAdaptableParameter(double value) { |
| 226 |
windowSize = Math.exp(value); |
| 227 |
} |
| 228 |
|
| 229 |
public double getRawParameter() { |
| 230 |
return windowSize; |
| 231 |
} |
| 232 |
|
| 233 |
public String getAdaptableParameterName() { |
| 234 |
return "windowSize"; |
| 235 |
} |
| 236 |
|
| 237 |
public String toString() { |
| 238 |
return RandomWalkOperatorParser.RANDOM_WALK_OPERATOR + "(" + parameter.getParameterName() + ", " + windowSize + ", " + getWeight() + ")"; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
private Parameter parameter = null; |
| 244 |
private double windowSize = 0.01; |
| 245 |
private List<Integer> updateMap = null; |
| 246 |
private int updateMapSize; |
| 247 |
private final BoundaryCondition boundaryCondition; |
| 248 |
} |