Debian Codegraph @

src/dr/evomodel/coalescent/operators/GMRFSkyrideBlockUpdateOperator.java

raw symbols SCIP
1 /*
2 * GMRFSkyrideBlockUpdateOperator.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
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 /* A Metropolis-Hastings operator to update the log population sizes and precision parameter jointly under a Gaussian Markov random field prior
38 *
39 * @author Erik Bloomquist
40 * @author Marc Suchard
41 * @version $Id: GMRFSkylineBlockUpdateOperator.java,v 1.5 2007/03/20 11:26:49 msuchard Exp $
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 // Assume Cholesky factorization of the precision matrix Q = LL^T
117
118 // 1. Solve L\omega = b
119
120 CholeskyUpper.transSolve(CanonVector, tempValue);
121
122 // 2. Solve L^T \mu = \omega
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 // 3. Solve L^T v = z
136
137 CholeskyUpper.solve(StandNorm, returnValue);
138
139 // 4. Return x = \mu + v
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 // used to pass on an OperatorFailedException
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 // used to pass on an OperatorFailedException
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 // Removed 0.5 * 2
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 //MCMCOperator INTERFACE
347
348 public final String getOperatorName() {
349 return GMRFSkyrideBlockUpdateOperatorParser.BLOCK_UPDATE_OPERATOR;
350 }
351
352 public double getAdaptableParameter() {
353 // return Math.log(scaleFactor);
354 return Math.sqrt(scaleFactor - 1);
355 }
356
357 public void setAdaptableParameter(double value) {
358 // scaleFactor = Math.exp(value);
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 // public DenseVector oldNewtonRaphson(double[] data, DenseVector currentGamma, SymmTridiagMatrix proposedQ) throws OperatorFailedException{
375 // return newNewtonRaphson(data, currentGamma, proposedQ, maxIterations, stopValue);
376 //
377 //}
378 //
379 //public static DenseVector newtonRaphson(double[] data, DenseVector currentGamma, SymmTridiagMatrix proposedQ,
380 //int maxIterations, double stopValue) {
381 //
382 //DenseVector iterateGamma = currentGamma.copy();
383 //int numberIterations = 0;
384 //while (gradient(data, iterateGamma, proposedQ).norm(Vector.Norm.Two) > stopValue) {
385 //inverseJacobian(data, iterateGamma, proposedQ).multAdd(gradient(data, iterateGamma, proposedQ), iterateGamma);
386 //numberIterations++;
387 //}
388 //
389 //if (numberIterations > maxIterations)
390 //throw new RuntimeException("Newton Raphson algorithm did not converge within " + maxIterations + " step to a norm less than " + stopValue);
391 //
392 //return iterateGamma;
393 //}
394 //
395 //private static DenseMatrix inverseJacobian(double[] data, DenseVector value, SymmTridiagMatrix Q) {
396 //
397 // SPDTridiagMatrix jacobian = new SPDTridiagMatrix(Q, true);
398 // for (int i = 0; i < value.size(); i++) {
399 // jacobian.set(i, i, jacobian.get(i, i) + Math.exp(-value.get(i)) * data[i]);
400 // }
401 //
402 // DenseMatrix inverseJacobian = Matrices.identity(jacobian.numRows());
403 // jacobian.solve(Matrices.identity(value.size()), inverseJacobian);
404 //
405 // return inverseJacobian;
406 // }
407 }