Debian Codegraph @

src/dr/inference/operators/LFMSplitMergeOperator.java

raw symbols SCIP
1 package dr.inference.operators;
2
3 import dr.evomodel.continuous.FullyConjugateMultivariateTraitLikelihood;
4 import dr.evomodel.continuous.GaussianProcessFromTree;
5 import dr.inference.model.AdaptableSizeFastMatrixParameter;
6 import dr.math.MathUtils;
7 import dr.math.distributions.GammaDistribution;
8 import dr.math.distributions.NormalDistribution;
9
10 /**
11 * @author Max Tolkoff
12 */
13 public class LFMSplitMergeOperator extends AbstractAdaptableOperator {
14
15 NormalDistribution drawDistribution;
16 AdaptableSizeFastMatrixParameter factors;
17 AdaptableSizeFastMatrixParameter sparseLoadings;
18 AdaptableSizeFastMatrixParameter denseLoadings;
19 AdaptableSizeFastMatrixParameter cutoffs;
20 FullyConjugateMultivariateTraitLikelihood tree;
21 GaussianProcessFromTree treeDraw;
22 NormalDistribution standardNormal = new NormalDistribution(0, 1);
23 GammaDistribution gamma = new GammaDistribution(1, 1);
24
25 private String lastCall;
26
27 //TODO implment flag for not this case
28 final boolean upperTriangular = true;
29
30 public LFMSplitMergeOperator(double weight, double initialVariance, AdaptableSizeFastMatrixParameter factors,
31 AdaptableSizeFastMatrixParameter denseLoadings, AdaptableSizeFastMatrixParameter sparseLoadings,
32 AdaptableSizeFastMatrixParameter cutoffs, FullyConjugateMultivariateTraitLikelihood tree){
33
34 super(AdaptationMode.DEFAULT);
35 setWeight(weight);
36
37 drawDistribution = new NormalDistribution(0, Math.sqrt(initialVariance));
38 this.factors = factors;
39 this.sparseLoadings = sparseLoadings;
40 this.denseLoadings = denseLoadings;
41 this.cutoffs = cutoffs;
42 this.tree = tree;
43 GaussianProcessFromTree treeDraw = new GaussianProcessFromTree(tree);
44 this.treeDraw = treeDraw;
45 }
46
47 @Override
48 public String getOperatorName() {
49 return "Latent Factor Model Split-Merge Operator";
50 }
51
52 @Override
53 public double doOperation() {
54 double randNum = MathUtils.nextDouble();
55 double from1 = 0;
56 double hastingsRatio;
57 if((randNum < .5 ||
58 denseLoadings.getColumnDimension() == denseLoadings.getMaxColumnDimension()) && denseLoadings.getColumnDimension() != 1){
59 lastCall = "decrement";
60 if(
61 denseLoadings.getColumnDimension() == 2
62 )
63 from1 += Math.log(2);
64 if(
65 denseLoadings.getColumnDimension() == denseLoadings.getMaxColumnDimension()
66 )
67 from1 += -Math.log(2);
68 hastingsRatio = decrement();
69 // hastingsRatio = Double.POSITIVE_INFINITY;
70 }
71 else{
72 lastCall = "increment";
73 if(
74 denseLoadings.getColumnDimension() == 1
75 )
76 from1 += -Math.log(2);
77 if(
78 denseLoadings.getColumnDimension() == denseLoadings.getMaxColumnDimension() - 1
79 )
80 from1 += Math.log(2);
81 hastingsRatio = increment();
82 // hastingsRatio = Double.POSITIVE_INFINITY;
83 }
84
85 denseLoadings.fireParameterChangedEvent();
86 sparseLoadings.fireParameterChangedEvent();
87 cutoffs.fireParameterChangedEvent();
88 factors.fireParameterChangedEvent();
89 // factors.fireParameterChangedEvent(-1, Variable.ChangeType.ADDED);
90
91 // System.out.println(drawDistribution.getSD());
92 // System.out.println(hastingsRatio);
93 return hastingsRatio + from1;
94 }
95
96 @Override
97 public double getAdaptableParameter() {
98 // return Math.log(drawDistribution.getSD());
99 return 0;
100 }
101
102 @Override
103 public void setAdaptableParameter(double value) {
104 // drawDistribution.setSD(Math.exp(value));
105
106 }
107
108 @Override
109 public double getRawParameter() {
110 // return drawDistribution.getSD();
111 return 0;
112 }
113
114 @Override
115 public String getAdaptableParameterName() {
116 return "";
117 }
118
119 private double increment(){
120 double hastings = 0;
121 int splitFac = MathUtils.nextInt(denseLoadings.getColumnDimension());
122 int lastColumn = denseLoadings.getColumnDimension();
123 denseLoadings.setColumnDimension(denseLoadings.getColumnDimension() + 1);
124 sparseLoadings.setColumnDimension(sparseLoadings.getColumnDimension() + 1);
125 cutoffs.setColumnDimension(cutoffs.getColumnDimension() + 1);
126 // factors.setRowDimension(factors.getRowDimension() + 1);
127 // System.out.println(factors.getColumnDimension());
128 // NormalDistribution standardNormal = new NormalDistribution(0, 1);
129 // double draw = 0;
130 // double cutoffDraw = 0;
131
132 for (int i = 0; i < denseLoadings.getRowDimension(); i++) {
133 if(i >= lastColumn || !upperTriangular) {
134 // hastings += Math.log(3);
135
136 if (sparseLoadings.getParameterValue(i, splitFac) == 0) {
137 hastings += sparseIncrement(i, lastColumn);
138 } else {
139 //discrete split
140 int intDraw = MathUtils.nextInt(3);
141 intDraw = 2;
142 if (intDraw == 0) {
143 hastings += sparseIncrement(i, lastColumn);
144 } else if (intDraw == 1) {
145 //move to last column
146 sparseLoadings.setParameterValueQuietly(i, lastColumn, sparseLoadings.getParameterValue(i, splitFac));
147 cutoffs.setParameterValueQuietly(i, lastColumn, cutoffs.getParameterValue(i, splitFac));
148 denseLoadings.setParameterValueQuietly(i, lastColumn, denseLoadings.getParameterValue(i, splitFac));
149
150 //split
151 hastings += sparseIncrement(i, splitFac);
152 } else {
153
154 //backwards cutoff draw hastings ratio
155 hastings += -Math.log(Math.pow(denseLoadings.getParameterValue(i, splitFac), 2));
156 //both are 1
157 sparseLoadings.setParameterValueQuietly(i, lastColumn, 1);
158
159 //dense split
160 double draw = (Double) drawDistribution.nextRandom();
161 double reset1 = denseLoadings.getParameterValue(i, splitFac) + draw;
162 double reset2 = denseLoadings.getParameterValue(i, splitFac) - draw;
163 denseLoadings.setParameterValueQuietly(i, lastColumn, reset1);
164 denseLoadings.setParameterValueQuietly(i, splitFac, reset2);
165 // System.out.println(draw);
166 hastings += -drawDistribution.logPdf(draw);
167 //jacobian
168 hastings += Math.log(2);
169
170 //new cutoffs
171 double cutoffDraw1 = MathUtils.nextDouble() * Math.pow(reset1, 2);
172 double cutoffDraw2 = MathUtils.nextDouble() * Math.pow(reset2, 2);
173 hastings += Math.log(Math.pow(reset1, 2));
174 hastings += Math.log(Math.pow(reset2, 2));
175 cutoffs.setParameterValueQuietly(i, lastColumn, cutoffDraw1);
176 cutoffs.setParameterValueQuietly(i, splitFac, cutoffDraw2);
177 }
178 }
179 }
180 else{
181 // hastings += -standardNormal.logPdf(0);
182 double cutoffDraw = gamma.nextGamma();
183 hastings += -gamma.logPdf(cutoffDraw);
184 cutoffs.setParameterValueQuietly(i, lastColumn, cutoffDraw);
185 // sparseLoadings.setParameterValueQuietly(i, lastColumn, MathUtils.nextInt(2));
186 // hastings += Math.log(2);
187 }
188
189 }
190 //setup tree draw
191 // double[] drawnFactors = treeDraw.nextRandomFast();
192 //// System.out.println(factors.getColumnDimension());
193 //// System.out.println(drawnFactors.length);
194 // for (int i = 0; i < factors.getColumnDimension() ; i++) {
195 //// System.out.println(i);
196 // factors.setParameterValueQuietly(lastColumn, i, drawnFactors[i]);
197 // }
198 // factors.fireParameterChangedEvent();
199 // double logFull = tree.getLogLikelihood();
200 // factors.setRowDimension(factors.getRowDimension() - 1);
201 // double logEmpty = tree.getLogLikelihood();
202 // hastings += logEmpty - logFull;
203 //// System.out.println(logEmpty - logFull);
204 // factors.setRowDimension(factors.getRowDimension() + 1);
205 //
206 // for (int i = 0; i < factors.getColumnDimension(); i++) {
207 //// double draw = (Double) drawDistribution.nextRandom();
208 // factors.setParameterValueQuietly(lastColumn, i, factors.getParameterValue(splitFac, i) + drawnFactors[i] * drawDistribution.getSD());
209 // factors.setParameterValueQuietly(splitFac, i, factors.getParameterValue(splitFac, i) - drawnFactors[i] * drawDistribution.getSD());
210 //// hastings += -drawDistribution.logPdf(draw);
211 // //Jacobian
212 // hastings += Math.log(2);
213 // }
214
215 // System.out.println(factors.getRowDimension());
216 return hastings;
217 }
218
219 double sparseIncrement(int i, int column){
220
221 double draw = 0;
222 double cutoffDraw = 0;
223 double hastings = 0;
224
225 //discrete split
226 sparseLoadings.setParameterValueQuietly(i, column, 0);
227 // hastings += Math.log(3);
228
229 //dense denovo draw
230 draw = (Double) standardNormal.nextRandom();
231 denseLoadings.setParameterValueQuietly(i, column, draw);
232 hastings += -standardNormal.logPdf(draw);
233
234 //cutoff draw
235 cutoffDraw = MathUtils.nextDouble() * Math.pow(draw, 2);
236 hastings += Math.log(Math.pow(draw, 2));
237 cutoffs.setParameterValueQuietly(i, column, cutoffDraw);
238 // System.out.println(draw);
239 return hastings;
240 }
241
242
243 private double decrement(){
244 double hastings = 0;
245 int combineFac = MathUtils.nextInt(denseLoadings.getColumnDimension() - 1);
246 int lastColumn = denseLoadings.getColumnDimension() - 1;
247 for (int i = 0; i < denseLoadings.getRowDimension(); i++) {
248 if(i >= lastColumn || !upperTriangular){
249 // System.out.println(i);
250 //Backwards probabilities
251 // hastings += -Math.log(3);
252 if(sparseLoadings.getParameterValue(i, combineFac) == 1 && sparseLoadings.getParameterValue(i, lastColumn) == 1){
253 hastings += -Math.log(Math.pow(denseLoadings.getParameterValue(i, combineFac), 2));
254 hastings += -Math.log(Math.pow(denseLoadings.getParameterValue(i, lastColumn), 2));
255 hastings += drawDistribution.logPdf(( denseLoadings.getParameterValue(i, lastColumn) - denseLoadings.getParameterValue(i, combineFac)) / 2);
256 //jacobian
257 hastings += -Math.log(2);
258 double sum = (denseLoadings.getParameterValue(i, combineFac) + denseLoadings.getParameterValue(i, lastColumn)) / 2;
259 denseLoadings.setParameterValueQuietly(i, combineFac, sum);
260 double draw = MathUtils.nextDouble() * Math.pow(sum, 2);
261 cutoffs.setParameterValueQuietly(i, combineFac, draw);
262 }
263 else if(sparseLoadings.getParameterValue(i, combineFac) == 1 && sparseLoadings.getParameterValue(i, lastColumn) == 0){
264 hastings += standardNormal.logPdf(denseLoadings.getParameterValue(i, lastColumn));
265 hastings += -Math.log(Math.pow(denseLoadings.getParameterValue(i, lastColumn), 2));
266 }
267 else if(sparseLoadings.getParameterValue(i, combineFac) == 0 && sparseLoadings.getParameterValue(i, lastColumn) == 1){
268 hastings += standardNormal.logPdf(denseLoadings.getParameterValue(i, combineFac));
269 hastings += -Math.log(Math.pow(denseLoadings.getParameterValue(i, combineFac), 2));
270 denseLoadings.setParameterValueQuietly(i, combineFac, denseLoadings.getParameterValue(i, lastColumn));
271 cutoffs.setParameterValueQuietly(i, combineFac, cutoffs.getParameterValue(i, lastColumn));
272 sparseLoadings.setParameterValueQuietly(i, combineFac, 1);
273 }
274 else{
275 hastings += standardNormal.logPdf(denseLoadings.getParameterValue(i, lastColumn));
276 hastings += -Math.log(Math.pow(denseLoadings.getParameterValue(i, lastColumn), 2));
277 }
278 }
279 else{
280 // hastings += standardNormal.logPdf(0);
281 hastings += gamma.logPdf(cutoffs.getParameterValue(i, lastColumn));
282 // hastings += -Math.log(2);
283 }
284 }
285
286
287
288 // for (int i = 0; i < factors.getColumnDimension(); i++) {
289 //// hastings += drawDistribution.logPdf((factors.getParameterValue(combineFac, i) - factors.getParameterValue(lastColumn, i)) / 2 );
290 // hastings += -Math.log(2);
291 //// System.out.println(drawDistribution.logPdf((factors.getParameterValue(combineFac, i) - factors.getParameterValue(lastColumn, i)) / 2 ));
292 // double temp1 = (factors.getParameterValue(combineFac, i) + factors.getParameterValue(lastColumn, i)) / 2 ;
293 // double temp2 = (factors.getParameterValue(combineFac, i) - factors.getParameterValue(lastColumn, i)) / (2 * drawDistribution.getSD());
294 // factors.setParameterValueQuietly(combineFac, i, temp1 );
295 // factors.setParameterValueQuietly(lastColumn, i, temp2 );
296 // }
297 // factors.fireParameterChangedEvent();
298 // double fullDraw = tree.getLogLikelihood();
299 //
300 //
301 denseLoadings.setColumnDimension(denseLoadings.getColumnDimension() - 1);
302 sparseLoadings.setColumnDimension(sparseLoadings.getColumnDimension() - 1);
303 cutoffs.setColumnDimension(cutoffs.getColumnDimension() - 1);
304 //// System.out.println(factors.getRowDimension());
305 // factors.setRowDimension(factors.getRowDimension() - 1);
306 //////
307 // double truncDraw = tree.getLogLikelihood();
308 // hastings += fullDraw - truncDraw;
309
310
311 // System.out.println(fullDraw - truncDraw);
312 // System.out.println(hastings);
313 return hastings;
314 }
315
316
317 // @Override
318 // public void accept(double deviation) {
319 // super.accept(deviation);
320 // System.out.println(lastCall);
321 // System.out.println(deviation);
322 // }
323
324 // public void reject(){
325 // super.reject();
326 // System.out.println(lastCall);
327 // }
328
329 // private void storeDimensions(){
330 // if(factors != null){
331 // storedFactors.setRowDimension(factors.getRowDimension());
332 // storedFactors.setColumnDimension(factors.getColumnDimension());
333 // }
334 // storedLoadings.setRowDimension(loadings.getRowDimension());
335 // storedLoadings.setColumnDimension(loadings.getColumnDimension());
336 // if(loadingsSparsity != null){
337 // storedLoadingsSparsity.setRowDimension(loadingsSparsity.getRowDimension());
338 // storedLoadingsSparsity.setColumnDimension(loadingsSparsity.getColumnDimension());
339 // }
340 // if(cutoffs != null){
341 // storedCutoffs.setRowDimension(cutoffs.getRowDimension());
342 // storedCutoffs.setColumnDimension(cutoffs.getColumnDimension());
343 // }
344 // }
345 //
346 // private void restoreDimensions(){
347 // if(factors != null){
348 // factors.setRowDimension(storedFactors.getRowDimension());
349 // factors.setColumnDimension(storedFactors.getColumnDimension());
350 // }
351 // loadings.setRowDimension(storedLoadings.getRowDimension());
352 // loadings.setColumnDimension(storedLoadings.getColumnDimension());
353 // if(loadingsSparsity != null){
354 // loadingsSparsity.setRowDimension(storedLoadingsSparsity.getRowDimension());
355 // loadingsSparsity.setColumnDimension(storedLoadingsSparsity.getColumnDimension());
356 // }
357 // if(cutoffs != null){
358 // cutoffs.setRowDimension(storedCutoffs.getRowDimension());
359 // cutoffs.setColumnDimension(storedCutoffs.getColumnDimension());
360 // }
361 // }
362 //
363 //
364 // private void storeValues(){
365 // if(factors != null){
366 // for (int i = 0; i < factors.getDimension(); i++) {
367 // storedFactors.setParameterValue(i, factors.getParameterValue(i));}
368 // }
369 //
370 // for (int i = 0; i < loadings.getDimension(); i++) {
371 // storedLoadings.setParameterValue(i, loadings.getParameterValue(i));
372 // if(loadingsSparsity != null)
373 // storedLoadingsSparsity.setParameterValue(i, loadingsSparsity.getParameterValue(i));
374 // if(storedCutoffs != null)
375 // storedCutoffs.setParameterValue(i, cutoffs.getParameterValue(i));
376 // }
377 // }
378 //
379 // private void restoreValues(){
380 // if(factors != null){
381 // for (int i = 0; i < factors.getDimension(); i++) {
382 // factors.setParameterValue(i, storedFactors.getParameterValue(i));}
383 // }
384 // for (int i = 0; i < loadings.getDimension(); i++) {
385 // loadings.setParameterValue(i, storedLoadings.getParameterValue(i));
386 // if(loadingsSparsity != null)
387 // loadingsSparsity.setParameterValue(i, storedLoadingsSparsity.getParameterValue(i));
388 // if(cutoffs != null)
389 // cutoffs.setParameterValue(i, storedCutoffs.getParameterValue(i));
390 // }
391 // }
392
393
394 }