| 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 java.util.ArrayList; |
| 29 |
import java.util.List; |
| 30 |
|
| 31 |
import dr.inference.model.Bounds; |
| 32 |
import dr.inference.model.Parameter; |
| 33 |
import dr.inferencexml.operators.RandomWalkOperatorParser; |
| 34 |
import dr.math.MathUtils; |
| 35 |
import dr.util.Transform; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class TransformedRandomWalkOperator extends AbstractAdaptableOperator { |
| 43 |
|
| 44 |
private Parameter parameter = null; |
| 45 |
private final Transform[] transformations; |
| 46 |
private double windowSize = 0.01; |
| 47 |
private List<Integer> updateMap = null; |
| 48 |
private int updateMapSize; |
| 49 |
private final BoundaryCondition condition; |
| 50 |
|
| 51 |
private final Double lowerOperatorBound; |
| 52 |
private final Double upperOperatorBound; |
| 53 |
|
| 54 |
public enum BoundaryCondition { |
| 55 |
reflecting, |
| 56 |
absorbing |
| 57 |
} |
| 58 |
|
| 59 |
public TransformedRandomWalkOperator(Parameter parameter, Transform[] transformations, double windowSize, BoundaryCondition bc, double weight, AdaptationMode mode) { |
| 60 |
this(parameter, transformations, null, windowSize, bc, weight, mode); |
| 61 |
} |
| 62 |
|
| 63 |
public TransformedRandomWalkOperator(Parameter parameter, Transform[] transformations, Parameter updateIndex, double windowSize, BoundaryCondition bc, |
| 64 |
double weight, AdaptationMode mode) { |
| 65 |
this(parameter, transformations, updateIndex, windowSize, bc, weight, mode, null, null); |
| 66 |
} |
| 67 |
|
| 68 |
public TransformedRandomWalkOperator(Parameter parameter, Transform[] transformations, Parameter updateIndex, double windowSize, BoundaryCondition bc, |
| 69 |
double weight, AdaptationMode mode, Double lowerOperatorBound, Double upperOperatorBound) { |
| 70 |
super(mode); |
| 71 |
this.parameter = parameter; |
| 72 |
this.transformations = transformations; |
| 73 |
this.windowSize = windowSize; |
| 74 |
this.condition = bc; |
| 75 |
|
| 76 |
setWeight(weight); |
| 77 |
if (updateIndex != null) { |
| 78 |
updateMap = new ArrayList<Integer>(); |
| 79 |
for (int i = 0; i < updateIndex.getDimension(); i++) { |
| 80 |
if (updateIndex.getParameterValue(i) == 1.0) |
| 81 |
updateMap.add(i); |
| 82 |
} |
| 83 |
updateMapSize=updateMap.size(); |
| 84 |
} |
| 85 |
|
| 86 |
this.lowerOperatorBound = lowerOperatorBound; |
| 87 |
this.upperOperatorBound = upperOperatorBound; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public Parameter getParameter() { |
| 94 |
return parameter; |
| 95 |
} |
| 96 |
|
| 97 |
public final double getWindowSize() { |
| 98 |
return windowSize; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public final double doOperation() { |
| 105 |
|
| 106 |
|
| 107 |
double logJacobian = 0.0; |
| 108 |
|
| 109 |
|
| 110 |
int index; |
| 111 |
if (updateMap == null) { |
| 112 |
index = MathUtils.nextInt(parameter.getDimension()); |
| 113 |
} else { |
| 114 |
index = updateMap.get(MathUtils.nextInt(updateMapSize)); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
double draw = (2.0 * MathUtils.nextDouble() - 1.0) * windowSize; |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
double[] x = parameter.getParameterValues(); |
| 126 |
int dim = parameter.getDimension(); |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
double[] transformedX = new double[dim]; |
| 131 |
for (int i = 0; i < dim; i++) { |
| 132 |
transformedX[i] = transformations[i].transform(x[i]); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
double newValue = transformedX[index] + draw; |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
final Bounds<Double> bounds = parameter.getBounds(); |
| 143 |
final double lower = (lowerOperatorBound == null ? bounds.getLowerLimit(index) : Math.max(bounds.getLowerLimit(index), lowerOperatorBound)); |
| 144 |
final double upper = (upperOperatorBound == null ? bounds.getUpperLimit(index) : Math.min(bounds.getUpperLimit(index), upperOperatorBound)); |
| 145 |
|
| 146 |
if (condition == BoundaryCondition.reflecting) { |
| 147 |
newValue = RandomWalkOperator.reflectValue(newValue, lower, upper); |
| 148 |
} else if (newValue < lower || newValue > upper) { |
| 149 |
|
| 150 |
return Double.NEGATIVE_INFINITY; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
parameter.setParameterValue(index, transformations[index].inverse(newValue)); |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
logJacobian += transformations[index].getLogJacobian(x[index]) - transformations[index].getLogJacobian(parameter.getParameterValue(index)); |
| 162 |
|
| 163 |
|
| 164 |
return logJacobian; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
public final String getOperatorName() { |
| 185 |
return parameter.getParameterName(); |
| 186 |
} |
| 187 |
|
| 188 |
public double getAdaptableParameter() { |
| 189 |
return Math.log(windowSize); |
| 190 |
} |
| 191 |
|
| 192 |
public void setAdaptableParameter(double value) { |
| 193 |
windowSize = Math.exp(value); |
| 194 |
} |
| 195 |
|
| 196 |
public double getRawParameter() { |
| 197 |
return windowSize; |
| 198 |
} |
| 199 |
|
| 200 |
public String getAdaptableParameterName() { |
| 201 |
return "windowSize"; |
| 202 |
} |
| 203 |
|
| 204 |
public String toString() { |
| 205 |
return RandomWalkOperatorParser.RANDOM_WALK_OPERATOR + "(" + parameter.getParameterName() + ", " + windowSize + ", " + getWeight() + ")"; |
| 206 |
} |
| 207 |
|
| 208 |
} |