| 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.evomodel.coalescent.operators; |
| 27 |
|
| 28 |
import dr.evomodel.coalescent.GMRFSkyrideLikelihood; |
| 29 |
import dr.evomodelxml.coalescent.operators.GMRFSkyrideBlockUpdateOperatorParser; |
| 30 |
import dr.inference.model.Parameter; |
| 31 |
import dr.inference.operators.*; |
| 32 |
import dr.math.MathUtils; |
| 33 |
import no.uib.cipr.matrix.*; |
| 34 |
|
| 35 |
import java.util.logging.Logger; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public class GMRFSkyrideBlockUpdateOperator extends AbstractAdaptableOperator { |
| 44 |
|
| 45 |
private static boolean FAIL_SILENTLY = true; |
| 46 |
|
| 47 |
private double scaleFactor; |
| 48 |
private double lambdaScaleFactor; |
| 49 |
private int fieldLength; |
| 50 |
|
| 51 |
private int maxIterations; |
| 52 |
private double stopValue; |
| 53 |
|
| 54 |
private Parameter popSizeParameter; |
| 55 |
private Parameter precisionParameter; |
| 56 |
private Parameter lambdaParameter; |
| 57 |
|
| 58 |
GMRFSkyrideLikelihood gmrfField; |
| 59 |
|
| 60 |
private double[] zeros; |
| 61 |
|
| 62 |
public GMRFSkyrideBlockUpdateOperator(GMRFSkyrideLikelihood gmrfLikelihood, |
| 63 |
double weight, AdaptationMode mode, double scaleFactor, |
| 64 |
int maxIterations, double stopValue) { |
| 65 |
super(mode); |
| 66 |
gmrfField = gmrfLikelihood; |
| 67 |
popSizeParameter = gmrfLikelihood.getPopSizeParameter(); |
| 68 |
precisionParameter = gmrfLikelihood.getPrecisionParameter(); |
| 69 |
lambdaParameter = gmrfLikelihood.getLambdaParameter(); |
| 70 |
|
| 71 |
this.scaleFactor = scaleFactor; |
| 72 |
lambdaScaleFactor = 0.0; |
| 73 |
fieldLength = popSizeParameter.getDimension(); |
| 74 |
|
| 75 |
this.maxIterations = maxIterations; |
| 76 |
this.stopValue = stopValue; |
| 77 |
setWeight(weight); |
| 78 |
|
| 79 |
zeros = new double[fieldLength]; |
| 80 |
} |
| 81 |
|
| 82 |
private double getNewLambda(double currentValue, double lambdaScale) { |
| 83 |
double a = MathUtils.nextDouble() * lambdaScale - lambdaScale / 2; |
| 84 |
double b = currentValue + a; |
| 85 |
if (b > 1) |
| 86 |
b = 2 - b; |
| 87 |
if (b < 0) |
| 88 |
b = -b; |
| 89 |
|
| 90 |
return b; |
| 91 |
} |
| 92 |
|
| 93 |
private double getNewPrecision(double currentValue, double scaleFactor) { |
| 94 |
double length = scaleFactor - 1 / scaleFactor; |
| 95 |
double returnValue; |
| 96 |
|
| 97 |
|
| 98 |
if (scaleFactor == 1) |
| 99 |
return currentValue; |
| 100 |
if (MathUtils.nextDouble() < length / (length + 2 * Math.log(scaleFactor))) { |
| 101 |
returnValue = (1 / scaleFactor + length * MathUtils.nextDouble()) * currentValue; |
| 102 |
} else { |
| 103 |
returnValue = Math.pow(scaleFactor, 2.0 * MathUtils.nextDouble() - 1) * currentValue; |
| 104 |
} |
| 105 |
|
| 106 |
return returnValue; |
| 107 |
} |
| 108 |
|
| 109 |
public DenseVector getMultiNormalMean(DenseVector CanonVector, BandCholesky Cholesky) { |
| 110 |
|
| 111 |
DenseVector tempValue = new DenseVector(zeros); |
| 112 |
DenseVector Mean = new DenseVector(zeros); |
| 113 |
|
| 114 |
UpperTriangBandMatrix CholeskyUpper = Cholesky.getU(); |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
CholeskyUpper.transSolve(CanonVector, tempValue); |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
CholeskyUpper.solve(tempValue, Mean); |
| 125 |
|
| 126 |
return Mean; |
| 127 |
} |
| 128 |
|
| 129 |
public DenseVector getMultiNormal(DenseVector StandNorm, DenseVector Mean, BandCholesky Cholesky) { |
| 130 |
|
| 131 |
DenseVector returnValue = new DenseVector(zeros); |
| 132 |
|
| 133 |
UpperTriangBandMatrix CholeskyUpper = Cholesky.getU(); |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
CholeskyUpper.solve(StandNorm, returnValue); |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
returnValue.add(Mean); |
| 142 |
|
| 143 |
return returnValue; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
public static DenseVector getMultiNormal(DenseVector Mean, UpperSPDDenseMatrix Variance) { |
| 148 |
int length = Mean.size(); |
| 149 |
DenseVector tempValue = new DenseVector(length); |
| 150 |
DenseVector returnValue = new DenseVector(length); |
| 151 |
UpperSPDDenseMatrix ab = Variance.copy(); |
| 152 |
|
| 153 |
for (int i = 0; i < returnValue.size(); i++) |
| 154 |
tempValue.set(i, MathUtils.nextGaussian()); |
| 155 |
|
| 156 |
DenseCholesky chol = new DenseCholesky(length, true); |
| 157 |
chol.factor(ab); |
| 158 |
|
| 159 |
UpperTriangDenseMatrix x = chol.getU(); |
| 160 |
|
| 161 |
x.transMult(tempValue, returnValue); |
| 162 |
returnValue.add(Mean); |
| 163 |
return returnValue; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
public static double logGeneralizedDeterminant(UpperTriangBandMatrix Matrix) { |
| 168 |
double returnValue = 0; |
| 169 |
|
| 170 |
for (int i = 0; i < Matrix.numColumns(); i++) { |
| 171 |
if (Matrix.get(i, i) > 0.0000001) { |
| 172 |
returnValue += Math.log(Matrix.get(i, i)); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
return returnValue; |
| 177 |
} |
| 178 |
|
| 179 |
public DenseVector newtonRaphson(double[] data, DenseVector currentGamma, SymmTridiagMatrix proposedQ) { |
| 180 |
return newNewtonRaphson(data, currentGamma, proposedQ, maxIterations, stopValue); |
| 181 |
} |
| 182 |
|
| 183 |
public static DenseVector newNewtonRaphson(double[] data, DenseVector currentGamma, SymmTridiagMatrix proposedQ, |
| 184 |
int maxIterations, double stopValue) { |
| 185 |
DenseVector iterateGamma = currentGamma.copy(); |
| 186 |
DenseVector tempValue = currentGamma.copy(); |
| 187 |
|
| 188 |
int numberIterations = 0; |
| 189 |
|
| 190 |
|
| 191 |
while (gradient(data, iterateGamma, proposedQ).norm(Vector.Norm.Two) > stopValue) { |
| 192 |
try { |
| 193 |
jacobian(data, iterateGamma, proposedQ).solve(gradient(data, iterateGamma, proposedQ), tempValue); |
| 194 |
} catch (no.uib.cipr.matrix.MatrixNotSPDException e) { |
| 195 |
if (FAIL_SILENTLY) { |
| 196 |
return null; |
| 197 |
} |
| 198 |
Logger.getLogger("dr.evomodel.coalescent.operators.GMRFSkyrideBlockUpdateOperator").fine("Newton-Raphson F"); |
| 199 |
throw new RuntimeException("Newton-Raphson F."); |
| 200 |
} catch (no.uib.cipr.matrix.MatrixSingularException e) { |
| 201 |
if (FAIL_SILENTLY) { |
| 202 |
return null; |
| 203 |
} |
| 204 |
Logger.getLogger("dr.evomodel.coalescent.operators.GMRFSkyrideBlockUpdateOperator").fine("Newton-Raphson F"); |
| 205 |
throw new RuntimeException("Newton-Raphson F."); |
| 206 |
} |
| 207 |
iterateGamma.add(tempValue); |
| 208 |
numberIterations++; |
| 209 |
|
| 210 |
if (numberIterations > maxIterations) { |
| 211 |
if (FAIL_SILENTLY) { |
| 212 |
return null; |
| 213 |
} |
| 214 |
Logger.getLogger("dr.evomodel.coalescent.operators.GMRFSkyrideBlockUpdateOperator").fine("Newton-Raphson F"); |
| 215 |
throw new RuntimeException("Newton Raphson algorithm did not converge within " + maxIterations + " step to a norm less than " + stopValue + "\n" + |
| 216 |
"Try starting BEAST with a more accurate initial tree."); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
Logger.getLogger("dr.evomodel.coalescent.operators.GMRFSkyrideBlockUpdateOperator").fine("Newton-Raphson S"); |
| 221 |
return iterateGamma; |
| 222 |
|
| 223 |
} |
| 224 |
|
| 225 |
private static DenseVector gradient(double[] data, DenseVector value, SymmTridiagMatrix Q) { |
| 226 |
|
| 227 |
DenseVector returnValue = new DenseVector(value.size()); |
| 228 |
Q.mult(value, returnValue); |
| 229 |
for (int i = 0; i < value.size(); i++) { |
| 230 |
returnValue.set(i, -returnValue.get(i) - 1 + data[i] * Math.exp(-value.get(i))); |
| 231 |
} |
| 232 |
return returnValue; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
private static SPDTridiagMatrix jacobian(double[] data, DenseVector value, SymmTridiagMatrix Q) { |
| 237 |
SPDTridiagMatrix jacobian = new SPDTridiagMatrix(Q, true); |
| 238 |
for (int i = 0, n = value.size(); i < n; i++) { |
| 239 |
jacobian.set(i, i, jacobian.get(i, i) + Math.exp(-value.get(i)) * data[i]); |
| 240 |
} |
| 241 |
return jacobian; |
| 242 |
} |
| 243 |
|
| 244 |
public double doOperation() { |
| 245 |
|
| 246 |
double currentPrecision = precisionParameter.getParameterValue(0); |
| 247 |
double proposedPrecision = this.getNewPrecision(currentPrecision, scaleFactor); |
| 248 |
|
| 249 |
double currentLambda = this.lambdaParameter.getParameterValue(0); |
| 250 |
double proposedLambda = this.getNewLambda(currentLambda, lambdaScaleFactor); |
| 251 |
|
| 252 |
precisionParameter.setParameterValue(0, proposedPrecision); |
| 253 |
lambdaParameter.setParameterValue(0, proposedLambda); |
| 254 |
|
| 255 |
DenseVector currentGamma = new DenseVector(gmrfField.getPopSizeParameter().getParameterValues()); |
| 256 |
DenseVector proposedGamma; |
| 257 |
|
| 258 |
SymmTridiagMatrix currentQ = gmrfField.getStoredScaledWeightMatrix(currentPrecision, currentLambda); |
| 259 |
SymmTridiagMatrix proposedQ = gmrfField.getScaledWeightMatrix(proposedPrecision, proposedLambda); |
| 260 |
|
| 261 |
|
| 262 |
double[] wNative = gmrfField.getSufficientStatistics(); |
| 263 |
|
| 264 |
UpperSPDBandMatrix forwardQW = new UpperSPDBandMatrix(proposedQ, 1); |
| 265 |
UpperSPDBandMatrix backwardQW = new UpperSPDBandMatrix(currentQ, 1); |
| 266 |
|
| 267 |
BandCholesky forwardCholesky = new BandCholesky(wNative.length, 1, true); |
| 268 |
BandCholesky backwardCholesky = new BandCholesky(wNative.length, 1, true); |
| 269 |
|
| 270 |
DenseVector diagonal1 = new DenseVector(fieldLength); |
| 271 |
DenseVector diagonal2 = new DenseVector(fieldLength); |
| 272 |
DenseVector diagonal3 = new DenseVector(fieldLength); |
| 273 |
|
| 274 |
DenseVector modeForward = newtonRaphson(wNative, currentGamma, proposedQ.copy()); |
| 275 |
|
| 276 |
if (modeForward == null) { |
| 277 |
|
| 278 |
return Double.NEGATIVE_INFINITY; |
| 279 |
} |
| 280 |
|
| 281 |
for (int i = 0; i < fieldLength; i++) { |
| 282 |
diagonal1.set(i, wNative[i] * Math.exp(-modeForward.get(i))); |
| 283 |
diagonal2.set(i, modeForward.get(i) + 1); |
| 284 |
|
| 285 |
forwardQW.set(i, i, diagonal1.get(i) + forwardQW.get(i, i)); |
| 286 |
diagonal1.set(i, diagonal1.get(i) * diagonal2.get(i) - 1); |
| 287 |
} |
| 288 |
|
| 289 |
forwardCholesky.factor(forwardQW.copy()); |
| 290 |
|
| 291 |
DenseVector forwardMean = getMultiNormalMean(diagonal1, forwardCholesky); |
| 292 |
|
| 293 |
DenseVector stand_norm = new DenseVector(zeros); |
| 294 |
|
| 295 |
for (int i = 0; i < zeros.length; i++) |
| 296 |
stand_norm.set(i, MathUtils.nextGaussian()); |
| 297 |
|
| 298 |
proposedGamma = getMultiNormal(stand_norm, forwardMean, forwardCholesky); |
| 299 |
|
| 300 |
|
| 301 |
for (int i = 0; i < fieldLength; i++) |
| 302 |
popSizeParameter.setParameterValueQuietly(i, proposedGamma.get(i)); |
| 303 |
|
| 304 |
((Parameter.Abstract) popSizeParameter).fireParameterChangedEvent(); |
| 305 |
|
| 306 |
|
| 307 |
double hRatio = 0; |
| 308 |
|
| 309 |
diagonal1.zero(); |
| 310 |
diagonal2.zero(); |
| 311 |
diagonal3.zero(); |
| 312 |
|
| 313 |
DenseVector modeBackward = newtonRaphson(wNative, proposedGamma, currentQ.copy()); |
| 314 |
|
| 315 |
if (modeBackward == null) { |
| 316 |
|
| 317 |
return Double.NEGATIVE_INFINITY; |
| 318 |
} |
| 319 |
|
| 320 |
for (int i = 0; i < fieldLength; i++) { |
| 321 |
diagonal1.set(i, wNative[i] * Math.exp(-modeBackward.get(i))); |
| 322 |
diagonal2.set(i, modeBackward.get(i) + 1); |
| 323 |
|
| 324 |
backwardQW.set(i, i, diagonal1.get(i) + backwardQW.get(i, i)); |
| 325 |
diagonal1.set(i, diagonal1.get(i) * diagonal2.get(i) - 1); |
| 326 |
} |
| 327 |
|
| 328 |
backwardCholesky.factor(backwardQW.copy()); |
| 329 |
|
| 330 |
DenseVector backwardMean = getMultiNormalMean(diagonal1, backwardCholesky); |
| 331 |
|
| 332 |
for (int i = 0; i < fieldLength; i++) { |
| 333 |
diagonal1.set(i, currentGamma.get(i) - backwardMean.get(i)); |
| 334 |
} |
| 335 |
|
| 336 |
backwardQW.mult(diagonal1, diagonal3); |
| 337 |
|
| 338 |
|
| 339 |
hRatio += logGeneralizedDeterminant(backwardCholesky.getU()) - 0.5 * diagonal1.dot(diagonal3); |
| 340 |
hRatio -= logGeneralizedDeterminant(forwardCholesky.getU() ) - 0.5 * stand_norm.dot(stand_norm); |
| 341 |
|
| 342 |
|
| 343 |
return hRatio; |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
public final String getOperatorName() { |
| 349 |
return GMRFSkyrideBlockUpdateOperatorParser.BLOCK_UPDATE_OPERATOR; |
| 350 |
} |
| 351 |
|
| 352 |
public double getAdaptableParameter() { |
| 353 |
|
| 354 |
return Math.sqrt(scaleFactor - 1); |
| 355 |
} |
| 356 |
|
| 357 |
public void setAdaptableParameter(double value) { |
| 358 |
|
| 359 |
scaleFactor = 1 + value * value; |
| 360 |
} |
| 361 |
|
| 362 |
public double getRawParameter() { |
| 363 |
return scaleFactor; |
| 364 |
} |
| 365 |
|
| 366 |
public double getScaleFactor() { |
| 367 |
return scaleFactor; |
| 368 |
} |
| 369 |
|
| 370 |
public String getAdaptableParameterName() { |
| 371 |
return "scaleFactor"; |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
} |