Debian Codegraph @

src/dr/inference/operators/MVOUCovarianceOperator.java

raw symbols SCIP
1 /*
2 * MVOUCovarianceOperator.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.inference.operators;
27
28 import dr.inference.model.MatrixParameter;
29 import dr.inference.model.Parameter;
30 import dr.inferencexml.operators.MVOUCovarianceOperatorParser;
31 import dr.math.distributions.WishartDistribution;
32 import dr.math.matrixAlgebra.Matrix;
33
34 /**
35 * @author Marc Suchard
36 */
37 public class MVOUCovarianceOperator extends AbstractAdaptableOperator {
38
39 private double mixingFactor;
40 private MatrixParameter varMatrix;
41 private int dim;
42
43 private MatrixParameter precisionParam;
44 private WishartDistribution priorDistribution;
45 private int priorDf;
46 private double[][] I;
47 private Matrix Iinv;
48
49
50 public MVOUCovarianceOperator(double mixingFactor,
51 MatrixParameter varMatrix,
52 int priorDf,
53 double weight, AdaptationMode mode) {
54 super(mode);
55 this.mixingFactor = mixingFactor;
56 this.varMatrix = varMatrix;
57 this.priorDf = priorDf;
58 setWeight(weight);
59 dim = varMatrix.getColumnDimension();
60 I = new double[dim][dim];
61 for (int i = 0; i < dim; i++)
62 I[i][i] = 1.0;
63 // I[i][i] = i;
64 Iinv = new Matrix(I).inverse();
65 }
66
67 public double doOperation() {
68
69 double[][] draw = WishartDistribution.nextWishart(priorDf, I);
70 // double[][] good = varMatrix.getParameterAsMatrix();
71 // double[][] saveOld = varMatrix.getParameterAsMatrix();
72
73 // System.err.println("draw:\n"+new Matrix(draw));
74 double[][] oldValue = varMatrix.getParameterAsMatrix();
75 for (int i = 0; i < dim; i++) {
76 Parameter column = varMatrix.getParameter(i);
77 for (int j = 0; j < dim; j++)
78 column.setParameterValue(j,
79 mixingFactor * oldValue[j][i] + (1.0 - mixingFactor) * draw[j][i]
80 );
81
82 }
83 // varMatrix.fireParameterChangedEvent();
84 // calculate Hastings ratio
85
86 // System.err.println("oldValue:\n"+new Matrix(oldValue).toString());
87 // System.err.println("newValue:\n"+new Matrix(varMatrix.getParameterAsMatrix()).toString());
88
89 Matrix forwardDrawMatrix = new Matrix(draw);
90 for (int i = 0; i < dim; i++) {
91 for (int j = 0; j < dim; j++) {
92 // saveOld[i][j] *= - mixingFactor;
93 // saveOld[i][j] += varMatrix.getParameterValue(i,j);
94 // saveOld[i][j] /= 1.0 - mixingFactor;
95 oldValue[i][j] -= mixingFactor * varMatrix.getParameterValue(i, j);
96 oldValue[i][j] /= 1.0 - mixingFactor;
97 }
98 }
99
100 // double[][] saveNew = varMatrix.getParameterAsMatrix();
101
102 Matrix backwardDrawMatrix = new Matrix(oldValue);
103
104 // System.err.println("forward:\n"+forwardDrawMatrix);
105 // System.err.println("backward:\n"+backwardDrawMatrix);
106
107 // System.err.println("calc start");
108
109 // if( Math.abs(backwardDrawMatrix.component(0,0) + 0.251) < 0.001 ) {
110 // System.err.println("found:\n"+backwardDrawMatrix);
111 //
112 // System.err.println("original:\n"+new Matrix(good));
113 // System.err.println("draw:\n"+new Matrix(draw));
114 // System.err.println("proposed:\n"+new Matrix(varMatrix.getParameterAsMatrix()));
115 // System.err.println("mixing = "+mixingFactor);
116 // System.err.println("back[0][0] = "+backwardDrawMatrix.component(0,0));
117 // System.err.println("saveOld[0][0] = "+saveOld[0][0]);
118 //
119 //
120 // }
121
122 double bProb = WishartDistribution.logPdf(backwardDrawMatrix, Iinv, priorDf, dim,
123 // WishartDistribution.computeNormalizationConstant(Iinv,priorDf,dim));
124 0);
125
126 if (bProb == Double.NEGATIVE_INFINITY) {
127 // throw new OperatorFailedException("Not reversible");
128 // not clear if this means a HR of -Inf or a RuntimeException
129 return Double.NEGATIVE_INFINITY;
130 }
131
132 double fProb = WishartDistribution.logPdf(forwardDrawMatrix, Iinv, priorDf, dim,
133 // WishartDistribution.computeNormalizationConstant(Iinv,priorDf,dim));
134 0);
135
136 // System.err.println("calc end");
137
138 // if( fProb == Double.NEGATIVE_INFINITY ) {
139 // System.err.println("forwards is problem");
140 // System.exit(-1);
141 // }
142
143 // if( bProb == Double.NEGATIVE_INFINITY ) {
144 // System.err.println("backwards is problem");
145 // System.exit(-1);
146 // }
147
148 // System.err.println("fProb = "+fProb);
149 // System.err.println("bProb = "+bProb);
150
151 // System.exit(-1);
152
153 return bProb - fProb;
154 }
155
156 //MCMCOperator INTERFACE
157 public final String getOperatorName() {
158 return MVOUCovarianceOperatorParser.MVOU_OPERATOR + "(" +
159 varMatrix.getId() + ")";
160 }
161
162 public double getAdaptableParameter() {
163 return Math.log(mixingFactor / (1.0 - mixingFactor));
164 // return Math.log((1.0 - mixingFactor) / mixingFactor);
165 }
166
167 public void setAdaptableParameter(double value) {
168 mixingFactor = Math.exp(value) / (1.0 + Math.exp(value));
169 // mixingFactor = Math.exp(-value) / (1.0 + Math.exp(-value));
170 }
171
172 public double getRawParameter() {
173 return mixingFactor;
174 }
175
176 public String getAdaptableParameterName() {
177 return "mixingFactor";
178 }
179
180 }