| 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.hmc; |
| 27 |
|
| 28 |
import dr.inference.hmc.GradientWrtParameterProvider; |
| 29 |
import dr.inference.model.Likelihood; |
| 30 |
import dr.inference.model.Parameter; |
| 31 |
import dr.inference.operators.AdaptationMode; |
| 32 |
import dr.inference.operators.GeneralOperator; |
| 33 |
import dr.inference.operators.GibbsOperator; |
| 34 |
import dr.math.MathUtils; |
| 35 |
import dr.util.Transform; |
| 36 |
|
| 37 |
import java.util.Arrays; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public class NoUTurnOperator extends HamiltonianMonteCarloOperator implements GeneralOperator, GibbsOperator { |
| 45 |
|
| 46 |
private final int dim = gradientProvider.getDimension(); |
| 47 |
|
| 48 |
private class Options { |
| 49 |
private double kappa = 0.75; |
| 50 |
private double t0 = 10.0; |
| 51 |
private double gamma = 0.05; |
| 52 |
private double delta = 0.2; |
| 53 |
private double deltaMax = 1000.0; |
| 54 |
private double muFactor = 10.0; |
| 55 |
|
| 56 |
private int findMax = 100; |
| 57 |
private int maxDepth = 100; |
| 58 |
private int adaptLength = 1000; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
private final Options options = new Options(); |
| 63 |
|
| 64 |
public NoUTurnOperator(AdaptationMode mode, double weight, GradientWrtParameterProvider gradientProvider, |
| 65 |
Parameter parameter, Transform transform, double stepSize, int nSteps, double drawVariance) { |
| 66 |
super(mode, weight, gradientProvider, parameter, transform, stepSize, nSteps, drawVariance); |
| 67 |
} |
| 68 |
|
| 69 |
private class StepSize { |
| 70 |
final double initialStepSize; |
| 71 |
double stepSize; |
| 72 |
double logStepSize; |
| 73 |
double averageLogStepSize; |
| 74 |
double h; |
| 75 |
double mu; |
| 76 |
|
| 77 |
private StepSize(double initialStepSize) { |
| 78 |
this.initialStepSize = initialStepSize; |
| 79 |
this.stepSize = initialStepSize; |
| 80 |
this.logStepSize = Math.log(stepSize); |
| 81 |
this.averageLogStepSize = 0; |
| 82 |
this.h = 0; |
| 83 |
this.mu = Math.log(options.muFactor * initialStepSize); |
| 84 |
} |
| 85 |
|
| 86 |
private void update(long m, double alpha, double nAlpha, Options options) { |
| 87 |
|
| 88 |
if (m <= options.adaptLength) { |
| 89 |
|
| 90 |
h = (1 - 1 / (m + options.t0)) * h + 1 / (m + options.t0) * (options.delta - (alpha / nAlpha)); |
| 91 |
logStepSize = mu - Math.sqrt(m) / options.gamma * h; |
| 92 |
averageLogStepSize = Math.pow(m, -options.kappa) * logStepSize + |
| 93 |
(1 - Math.pow(m, -options.kappa)) * averageLogStepSize; |
| 94 |
stepSize = Math.exp(logStepSize); |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
private StepSize stepSizeInformation; |
| 100 |
|
| 101 |
@Override |
| 102 |
public String getOperatorName() { |
| 103 |
return "No-UTurn-Sampler operator"; |
| 104 |
} |
| 105 |
|
| 106 |
@Override |
| 107 |
public double doOperation(Likelihood likelihood) { |
| 108 |
|
| 109 |
final double[] initialPosition = leapFrogEngine.getInitialPosition(); |
| 110 |
final double initialLogLikelihood = gradientProvider.getLikelihood().getLogLikelihood(); |
| 111 |
|
| 112 |
if (stepSizeInformation == null) { |
| 113 |
stepSizeInformation = findReasonableStepSize(initialPosition); |
| 114 |
|
| 115 |
final double testLogLikelihood = gradientProvider.getLikelihood().getLogLikelihood(); |
| 116 |
assert (testLogLikelihood == initialLogLikelihood); |
| 117 |
assert (Arrays.equals(leapFrogEngine.getInitialPosition(), initialPosition)); |
| 118 |
} |
| 119 |
|
| 120 |
double[] position = takeOneStep(getCount() + 1, initialPosition); |
| 121 |
leapFrogEngine.setParameter(position); |
| 122 |
|
| 123 |
return 0.0; |
| 124 |
} |
| 125 |
|
| 126 |
private double[] takeOneStep(long m, double[] initialPosition) { |
| 127 |
|
| 128 |
double[] endPosition = Arrays.copyOf(initialPosition, initialPosition.length); |
| 129 |
final double[] initialMomentum = drawInitialMomentum(drawDistribution, dim); |
| 130 |
|
| 131 |
final double initialJointDensity = getJointProbability(gradientProvider, initialMomentum); |
| 132 |
|
| 133 |
double logSliceU = Math.log(MathUtils.nextDouble()) + initialJointDensity; |
| 134 |
|
| 135 |
TreeState root = new TreeState(initialPosition, initialMomentum, 1, true); |
| 136 |
|
| 137 |
int j = 0; |
| 138 |
|
| 139 |
while (root.flagContinue) { |
| 140 |
|
| 141 |
double[] tmp = updateRoot(root, j, logSliceU, initialJointDensity); |
| 142 |
if (tmp != null) { |
| 143 |
endPosition = tmp; |
| 144 |
} |
| 145 |
|
| 146 |
j++; |
| 147 |
|
| 148 |
if (j > options.maxDepth) { |
| 149 |
throw new RuntimeException("Reach maximum tree depth"); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
stepSizeInformation.update(m, root.alpha, root.nAlpha, options); |
| 154 |
|
| 155 |
return endPosition; |
| 156 |
} |
| 157 |
|
| 158 |
private double[] updateRoot(TreeState root, int j, double logSliceU, double initialJointDensity) { |
| 159 |
|
| 160 |
double[] endPosition = null; |
| 161 |
|
| 162 |
final double uniform1 = MathUtils.nextDouble(); |
| 163 |
int direction = (uniform1 < 0.5) ? -1 : 1; |
| 164 |
|
| 165 |
TreeState node = buildTree(root.getPosition(direction), root.getMomentum(direction), direction, |
| 166 |
logSliceU, j, stepSizeInformation.stepSize, initialJointDensity); |
| 167 |
|
| 168 |
root.setPosition(direction, node.getPosition(direction)); |
| 169 |
root.setMomentum(direction, node.getMomentum(direction)); |
| 170 |
|
| 171 |
if (node.flagContinue) { |
| 172 |
|
| 173 |
final double uniform = MathUtils.nextDouble(); |
| 174 |
final double p = (double) node.numNodes / (double)root.numNodes; |
| 175 |
|
| 176 |
if (uniform < p) { |
| 177 |
endPosition = node.getPosition(0); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
root.numNodes += node.numNodes; |
| 183 |
root.flagContinue = computeStopCriterion(node.flagContinue, root); |
| 184 |
|
| 185 |
|
| 186 |
root.alpha += node.alpha; |
| 187 |
root.nAlpha += node.nAlpha; |
| 188 |
|
| 189 |
return endPosition; |
| 190 |
} |
| 191 |
|
| 192 |
private TreeState buildTree(double[] position, double[] momentum, int direction, |
| 193 |
double logSliceU, int j, double stepSize, double initialJointDensity) { |
| 194 |
|
| 195 |
if (j == 0) { |
| 196 |
return buildBaseCase(position, momentum, direction, logSliceU, stepSize, initialJointDensity); |
| 197 |
} else { |
| 198 |
return buildRecursiveCase(position, momentum, direction, logSliceU, j, stepSize, initialJointDensity); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
private TreeState buildBaseCase(double[] inPosition, double[] inMomentum, int direction, |
| 203 |
double logSliceU, double stepSize, double initialJointDensity) { |
| 204 |
|
| 205 |
|
| 206 |
double[] position = Arrays.copyOf(inPosition, inPosition.length); |
| 207 |
double[] momentum = Arrays.copyOf(inMomentum, inMomentum.length); |
| 208 |
|
| 209 |
leapFrogEngine.setParameter(position); |
| 210 |
|
| 211 |
|
| 212 |
doLeap(position, momentum, direction * stepSize); |
| 213 |
|
| 214 |
double logJointProbAfter = getJointProbability(gradientProvider, momentum); |
| 215 |
|
| 216 |
final int numNodes = (logSliceU <= logJointProbAfter ? 1 : 0); |
| 217 |
|
| 218 |
final boolean flagContinue = (logSliceU < options.deltaMax + logJointProbAfter); |
| 219 |
|
| 220 |
|
| 221 |
final double alpha = Math.min(1.0, Math.exp(logJointProbAfter - initialJointDensity)); |
| 222 |
final int nAlpha = 1; |
| 223 |
|
| 224 |
leapFrogEngine.setParameter(inPosition); |
| 225 |
|
| 226 |
return new TreeState(position, momentum, numNodes, flagContinue, alpha, nAlpha); |
| 227 |
} |
| 228 |
|
| 229 |
private TreeState buildRecursiveCase(double[] inPosition, double[] inMomentum, int direction, |
| 230 |
double logSliceU, int j, double stepSize, double initialJointDensity) { |
| 231 |
|
| 232 |
TreeState node = buildTree(inPosition, inMomentum, direction, logSliceU, |
| 233 |
j - 1, |
| 234 |
stepSize, initialJointDensity); |
| 235 |
|
| 236 |
if (node.flagContinue) { |
| 237 |
|
| 238 |
TreeState child = buildTree(node.getPosition(direction), node.getMomentum(direction), direction, |
| 239 |
logSliceU, j - 1, stepSizeInformation.stepSize, initialJointDensity); |
| 240 |
|
| 241 |
node.setPosition(direction, child.getPosition(direction)); |
| 242 |
node.setMomentum(direction, child.getMomentum(direction)); |
| 243 |
|
| 244 |
double uniform = MathUtils.nextDouble(); |
| 245 |
if (child.numNodes > 0 |
| 246 |
&& uniform < ((double) child.numNodes / (double) (node.numNodes + child.numNodes))) { |
| 247 |
|
| 248 |
node.setPosition(0, child.getPosition(0)); |
| 249 |
} |
| 250 |
|
| 251 |
node.numNodes += child.numNodes; |
| 252 |
node.flagContinue = computeStopCriterion(child.flagContinue, node); |
| 253 |
|
| 254 |
node.alpha += child.alpha; |
| 255 |
node.nAlpha += child.nAlpha; |
| 256 |
|
| 257 |
} |
| 258 |
return node; |
| 259 |
} |
| 260 |
|
| 261 |
private void doLeap(final double[] position, |
| 262 |
final double[] momentum, |
| 263 |
final double stepSize) { |
| 264 |
leapFrogEngine.updateMomentum(position, momentum, gradientProvider.getGradientLogDensity(), stepSize / 2); |
| 265 |
leapFrogEngine.updatePosition(position, momentum, stepSize, 1.0); |
| 266 |
leapFrogEngine.updateMomentum(position, momentum, gradientProvider.getGradientLogDensity(), stepSize / 2); |
| 267 |
} |
| 268 |
|
| 269 |
private StepSize findReasonableStepSize(double[] initialPosition) { |
| 270 |
|
| 271 |
double stepSize = 1; |
| 272 |
double[] momentum = drawInitialMomentum(drawDistribution, dim); |
| 273 |
int count = 1; |
| 274 |
|
| 275 |
double[] position = Arrays.copyOf(initialPosition, dim); |
| 276 |
|
| 277 |
double probBefore = getJointProbability(gradientProvider, momentum); |
| 278 |
|
| 279 |
doLeap(position, momentum, stepSize); |
| 280 |
|
| 281 |
double probAfter = getJointProbability(gradientProvider, momentum); |
| 282 |
|
| 283 |
double a = ((probAfter - probBefore) > Math.log(0.5) ? 1 : -1); |
| 284 |
|
| 285 |
double probRatio = Math.exp(probAfter - probBefore); |
| 286 |
|
| 287 |
while (Math.pow(probRatio, a) > Math.pow(2, -a)) { |
| 288 |
|
| 289 |
probBefore = probAfter; |
| 290 |
|
| 291 |
|
| 292 |
doLeap(position, momentum, stepSize); |
| 293 |
|
| 294 |
probAfter = getJointProbability(gradientProvider, momentum); |
| 295 |
probRatio = Math.exp(probAfter - probBefore); |
| 296 |
|
| 297 |
stepSize = Math.pow(2, a) * stepSize; |
| 298 |
count++; |
| 299 |
|
| 300 |
|
| 301 |
if (count > options.findMax) { |
| 302 |
throw new RuntimeException("Cannot find a reasonable step size in " + options.findMax + " iterations"); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
leapFrogEngine.setParameter(initialPosition); |
| 307 |
|
| 308 |
return new StepSize(stepSize); |
| 309 |
} |
| 310 |
|
| 311 |
private static boolean computeStopCriterion(boolean flagContinue, TreeState state) { |
| 312 |
return computeStopCriterion(flagContinue, |
| 313 |
state.getPosition(1), state.getPosition(-1), |
| 314 |
state.getMomentum(1), state.getMomentum(-1)); |
| 315 |
} |
| 316 |
|
| 317 |
private static boolean computeStopCriterion(boolean flagContinue, |
| 318 |
double[] positionPlus, double[] positionMinus, |
| 319 |
double[] momentumPlus, double[] momentumMinus) { |
| 320 |
|
| 321 |
double[] positionDifference = subtractArray(positionPlus, positionMinus); |
| 322 |
|
| 323 |
return flagContinue && |
| 324 |
getDotProduct(positionDifference, momentumMinus) >= 0 && |
| 325 |
getDotProduct(positionDifference, momentumPlus) >= 0; |
| 326 |
} |
| 327 |
|
| 328 |
private static double getDotProduct(double[] x, double[] y) { |
| 329 |
|
| 330 |
assert (x.length == y.length); |
| 331 |
final int dim = x.length; |
| 332 |
|
| 333 |
double total = 0.0; |
| 334 |
for (int i = 0; i < dim; i++) { |
| 335 |
total += x[i] * y[i]; |
| 336 |
} |
| 337 |
return total; |
| 338 |
} |
| 339 |
|
| 340 |
private static double[] subtractArray(double[] a, double[] b) { |
| 341 |
|
| 342 |
assert (a.length == b.length); |
| 343 |
final int dim = a.length; |
| 344 |
|
| 345 |
double result[] = new double[dim]; |
| 346 |
for (int i = 0; i < dim; i++) { |
| 347 |
result[i] = a[i] - b[i]; |
| 348 |
} |
| 349 |
|
| 350 |
return result; |
| 351 |
} |
| 352 |
|
| 353 |
private double getJointProbability(GradientWrtParameterProvider gradientProvider, double[] momentum) { |
| 354 |
|
| 355 |
assert (gradientProvider != null); |
| 356 |
assert (momentum != null); |
| 357 |
|
| 358 |
return gradientProvider.getLikelihood().getLogLikelihood() - getScaledDotProduct(momentum, 1.0) |
| 359 |
- leapFrogEngine.getParameterLogJacobian(); |
| 360 |
} |
| 361 |
|
| 362 |
private class TreeState { |
| 363 |
|
| 364 |
private TreeState(double[] position, double[] moment, |
| 365 |
int numNodes, boolean flagContinue) { |
| 366 |
this(position, moment, numNodes, flagContinue, 0.0, 0); |
| 367 |
} |
| 368 |
|
| 369 |
private TreeState(double[] position, double[] moment, |
| 370 |
int numNodes, boolean flagContinue, |
| 371 |
double alpha, int nAlpha) { |
| 372 |
this.position = new double[3][]; |
| 373 |
this.momentum = new double[3][]; |
| 374 |
|
| 375 |
for (int i = 0; i < 3; ++i) { |
| 376 |
this.position[i] = position; |
| 377 |
this.momentum[i] = moment; |
| 378 |
} |
| 379 |
|
| 380 |
|
| 381 |
this.numNodes = numNodes; |
| 382 |
this.flagContinue = flagContinue; |
| 383 |
|
| 384 |
|
| 385 |
this.alpha = alpha; |
| 386 |
this.nAlpha = nAlpha; |
| 387 |
} |
| 388 |
|
| 389 |
private double[] getPosition(int direction) { |
| 390 |
return position[getIndex(direction)]; |
| 391 |
} |
| 392 |
|
| 393 |
private double[] getMomentum(int direction) { |
| 394 |
return momentum[getIndex(direction)]; |
| 395 |
} |
| 396 |
|
| 397 |
private void setPosition(int direction, double[] position) { |
| 398 |
this.position[getIndex(direction)] = position; |
| 399 |
} |
| 400 |
|
| 401 |
private void setMomentum(int direction, double[] momentum) { |
| 402 |
this.momentum[getIndex(direction)] = momentum; |
| 403 |
} |
| 404 |
|
| 405 |
private int getIndex(int direction) { |
| 406 |
assert (direction >= -1 && direction <= 1); |
| 407 |
return direction + 1; |
| 408 |
} |
| 409 |
|
| 410 |
final private double[][] position; |
| 411 |
final private double[][] momentum; |
| 412 |
|
| 413 |
private int numNodes; |
| 414 |
private boolean flagContinue; |
| 415 |
|
| 416 |
private double alpha; |
| 417 |
private int nAlpha; |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
|