| 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.inference.model.Variable; |
| 31 |
import dr.inferencexml.operators.ScaleOperatorParser; |
| 32 |
import dr.math.MathUtils; |
| 33 |
|
| 34 |
import java.util.logging.Logger; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public class ScaleOperator extends AbstractAdaptableOperator { |
| 47 |
|
| 48 |
private Parameter indicator; |
| 49 |
private double indicatorOnProb; |
| 50 |
|
| 51 |
public ScaleOperator(Variable variable, double scale) { |
| 52 |
|
| 53 |
this(variable, scale, AdaptationMode.ADAPTATION_ON, 1.0); |
| 54 |
} |
| 55 |
|
| 56 |
public ScaleOperator(Variable<Double> variable, double scale, AdaptationMode mode, double weight) { |
| 57 |
|
| 58 |
this(variable, false, 0, scale, mode, null, 1.0, false); |
| 59 |
setWeight(weight); |
| 60 |
} |
| 61 |
|
| 62 |
public ScaleOperator(Variable<Double> variable, boolean scaleAll, int degreesOfFreedom, double scale, |
| 63 |
AdaptationMode mode, Parameter indicator, double indicatorOnProb, boolean scaleAllInd) { |
| 64 |
|
| 65 |
super(mode); |
| 66 |
|
| 67 |
this.variable = variable; |
| 68 |
this.indicator = indicator; |
| 69 |
this.indicatorOnProb = indicatorOnProb; |
| 70 |
this.scaleAll = scaleAll; |
| 71 |
this.scaleAllIndependently = scaleAllInd; |
| 72 |
this.scaleFactor = scale; |
| 73 |
this.degreesOfFreedom = degreesOfFreedom; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public Variable getVariable() { |
| 81 |
return variable; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public final double doOperation() { |
| 88 |
|
| 89 |
final double scale = (scaleFactor + (MathUtils.nextDouble() * ((1.0 / scaleFactor) - scaleFactor))); |
| 90 |
|
| 91 |
double logq; |
| 92 |
|
| 93 |
final Bounds<Double> bounds = variable.getBounds(); |
| 94 |
final int dim = variable.getSize(); |
| 95 |
|
| 96 |
if (scaleAllIndependently) { |
| 97 |
|
| 98 |
logq = 0; |
| 99 |
for (int i = 0; i < dim; i++) { |
| 100 |
|
| 101 |
final double scaleOne = (scaleFactor + (MathUtils.nextDouble() * ((1.0 / scaleFactor) - scaleFactor))); |
| 102 |
final double offset = bounds.getLowerLimit(i); |
| 103 |
|
| 104 |
|
| 105 |
final double value = ((variable.getValue(i) - offset) * scaleOne) + offset; |
| 106 |
|
| 107 |
logq -= Math.log(scaleOne); |
| 108 |
|
| 109 |
if (value > bounds.getUpperLimit(i)) { |
| 110 |
|
| 111 |
throw new RuntimeException("proposed value greater than upper bound"); |
| 112 |
} |
| 113 |
|
| 114 |
variable.setValue(i, value); |
| 115 |
|
| 116 |
} |
| 117 |
} else if (scaleAll) { |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
if (degreesOfFreedom > 0) |
| 122 |
|
| 123 |
logq = -degreesOfFreedom * Math.log(scale); |
| 124 |
else |
| 125 |
logq = (dim - 2) * Math.log(scale); |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
for (int i = 0; i < dim; i++) { |
| 130 |
|
| 131 |
|
| 132 |
variable.setValue(i, variable.getValue(i) * scale); |
| 133 |
} |
| 134 |
|
| 135 |
for (int i = 0; i < dim; i++) { |
| 136 |
if (variable.getValue(i) > variable.getBounds().getUpperLimit(i)) { |
| 137 |
throw new RuntimeException("proposed value greater than upper bound"); |
| 138 |
} |
| 139 |
} |
| 140 |
} else { |
| 141 |
logq = -Math.log(scale); |
| 142 |
|
| 143 |
|
| 144 |
int index; |
| 145 |
if (indicator != null) { |
| 146 |
final int idim = indicator.getDimension(); |
| 147 |
final boolean impliedOne = idim == (dim - 1); |
| 148 |
|
| 149 |
int[] loc = new int[idim + 1]; |
| 150 |
int nLoc = 0; |
| 151 |
|
| 152 |
final boolean takeOne = indicatorOnProb >= 1.0 || MathUtils.nextDouble() < indicatorOnProb; |
| 153 |
|
| 154 |
if (impliedOne && takeOne) { |
| 155 |
loc[nLoc] = 0; |
| 156 |
++nLoc; |
| 157 |
} |
| 158 |
for (int i = 0; i < idim; i++) { |
| 159 |
final double value = indicator.getStatisticValue(i); |
| 160 |
if (takeOne == (value > 0)) { |
| 161 |
loc[nLoc] = i + (impliedOne ? 1 : 0); |
| 162 |
++nLoc; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
if (nLoc > 0) { |
| 167 |
final int rand = MathUtils.nextInt(nLoc); |
| 168 |
index = loc[rand]; |
| 169 |
} else { |
| 170 |
|
| 171 |
return Double.NEGATIVE_INFINITY; |
| 172 |
} |
| 173 |
} else { |
| 174 |
|
| 175 |
index = MathUtils.nextInt(dim); |
| 176 |
} |
| 177 |
|
| 178 |
final double oldValue = variable.getValue(index); |
| 179 |
final double offset = bounds.getLowerLimit(index); |
| 180 |
|
| 181 |
if (oldValue == 0) { |
| 182 |
Logger.getLogger("dr.inference").severe("The " + ScaleOperatorParser.SCALE_OPERATOR + |
| 183 |
" for " + |
| 184 |
variable.getVariableName() |
| 185 |
+ " has failed since the parameter has a value of 0.0." + |
| 186 |
"\nTo fix this problem, initalize the value of " + |
| 187 |
variable.getVariableName() + " to be a positive real number" |
| 188 |
); |
| 189 |
} |
| 190 |
final double newValue = ((oldValue - offset) * scale) + offset; |
| 191 |
|
| 192 |
if (newValue > bounds.getUpperLimit(index)) { |
| 193 |
|
| 194 |
throw new RuntimeException("proposed value greater than upper bound: " + newValue + " (" + variable.getId() + ")"); |
| 195 |
} |
| 196 |
|
| 197 |
variable.setValue(index, newValue); |
| 198 |
|
| 199 |
|
| 200 |
cleanupOperation(newValue, oldValue); |
| 201 |
} |
| 202 |
|
| 203 |
return logq; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
void cleanupOperation(double newValue, double oldValue) { |
| 213 |
|
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
public final String getOperatorName() { |
| 218 |
return "scale(" + variable.getVariableName() + ")"; |
| 219 |
} |
| 220 |
|
| 221 |
public double getAdaptableParameter() { |
| 222 |
return Math.log(1.0 / scaleFactor - 1.0); |
| 223 |
} |
| 224 |
|
| 225 |
public void setAdaptableParameter(double value) { |
| 226 |
scaleFactor = 1.0 / (Math.exp(value) + 1.0); |
| 227 |
} |
| 228 |
|
| 229 |
public double getRawParameter() { |
| 230 |
return scaleFactor; |
| 231 |
} |
| 232 |
|
| 233 |
public double getScaleFactor() { |
| 234 |
return scaleFactor; |
| 235 |
} |
| 236 |
|
| 237 |
public String getAdaptableParameterName() { |
| 238 |
return "scaleFactor"; |
| 239 |
} |
| 240 |
|
| 241 |
public String toString() { |
| 242 |
return ScaleOperatorParser.SCALE_OPERATOR + "(" + variable.getVariableName() + " [" + scaleFactor + ", " + (1.0 / scaleFactor) + "]"; |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
private Variable<Double> variable = null; |
| 248 |
private boolean scaleAll = false; |
| 249 |
private boolean scaleAllIndependently = false; |
| 250 |
private int degreesOfFreedom = 0; |
| 251 |
private double scaleFactor = 0.5; |
| 252 |
} |