Debian Codegraph @

src/dr/oldevomodel/treelikelihood/ArbitraryPrecisionLikelihoodCore.java

raw symbols SCIP
1 /*
2 * ArbitraryPrecisionLikelihoodCore.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.oldevomodel.treelikelihood;
27
28 import java.math.BigDecimal;
29 import java.math.MathContext;
30
31 /**
32 * GeneralLikelihoodCore - An implementation of LikelihoodCore for any data
33 *
34 * @author Andrew Rambaut
35 * @version $Id: GeneralLikelihoodCore.java,v 1.28 2006/08/31 14:57:24 rambaut Exp $
36 */
37
38 @Deprecated // Switching to BEAGLE
39 public class ArbitraryPrecisionLikelihoodCore implements LikelihoodCore {
40
41 private MathContext precision;
42
43 private int stateCount;
44 private int nodeCount;
45 private int patternCount;
46 private int partialsSize;
47 private int matrixSize;
48 private int matrixCount;
49
50 private boolean integrateCategories;
51
52 private BigDecimal[][][] partials;
53
54 private int[][] states;
55
56 private BigDecimal[][][] matrices;
57
58 private int[] currentMatricesIndices;
59 private int[] storedMatricesIndices;
60 private int[] currentPartialsIndices;
61 private int[] storedPartialsIndices;
62
63
64 /**
65 * Constructor
66 *
67 * @param stateCount number of states
68 */
69 public ArbitraryPrecisionLikelihoodCore(int stateCount, int precision) {
70 this.stateCount = stateCount;
71 this.precision = new MathContext(precision);
72 }
73
74 /**
75 * initializes partial likelihood arrays.
76 *
77 * @param nodeCount the number of nodes in the tree
78 * @param patternCount the number of patterns
79 * @param matrixCount the number of matrices (i.e., number of categories)
80 * @param integrateCategories whether sites are being integrated over all matrices
81 */
82 public void initialize(int nodeCount, int patternCount, int matrixCount, boolean integrateCategories) {
83
84 this.nodeCount = nodeCount;
85 this.patternCount = patternCount;
86 this.matrixCount = matrixCount;
87
88 this.integrateCategories = integrateCategories;
89
90 if (integrateCategories) {
91 partialsSize = patternCount * stateCount * matrixCount;
92 } else {
93 partialsSize = patternCount * stateCount;
94 }
95
96 partials = new BigDecimal[2][nodeCount][];
97
98 currentMatricesIndices = new int[nodeCount];
99 storedMatricesIndices = new int[nodeCount];
100
101 currentPartialsIndices = new int[nodeCount];
102 storedPartialsIndices = new int[nodeCount];
103
104 states = new int[nodeCount][];
105
106 for (int i = 0; i < nodeCount; i++) {
107 partials[0][i] = null;
108 partials[1][i] = null;
109
110 states[i] = null;
111 }
112
113 matrixSize = stateCount * stateCount;
114
115 matrices = new BigDecimal[2][nodeCount][matrixCount * matrixSize];
116 }
117
118 /**
119 * cleans up and deallocates arrays.
120 */
121 public void finalize() throws java.lang.Throwable {
122 super.finalize();
123
124 nodeCount = 0;
125 patternCount = 0;
126 matrixCount = 0;
127
128 partials = null;
129 currentPartialsIndices = null;
130 storedPartialsIndices = null;
131 states = null;
132 matrices = null;
133 currentMatricesIndices = null;
134 storedMatricesIndices = null;
135 }
136
137 /**
138 * Allocates partials for a node
139 */
140 public void createNodePartials(int nodeIndex) {
141
142 this.partials[0][nodeIndex] = new BigDecimal[partialsSize];
143 this.partials[1][nodeIndex] = new BigDecimal[partialsSize];
144 }
145
146 /**
147 * Sets partials for a node
148 */
149 public void setNodePartials(int nodeIndex, double[] partials) {
150
151 if (this.partials[0][nodeIndex] == null) {
152 createNodePartials(nodeIndex);
153 }
154 if (partials.length < partialsSize) {
155 int k = 0;
156 for (int i = 0; i < matrixCount; i++) {
157 for (int j = 0; j < partials.length; j++) {
158 this.partials[0][nodeIndex][k] = new BigDecimal(partials[j], precision);
159 k++;
160 }
161 }
162 } else {
163 for (int j = 0; j < partials.length; j++) {
164 this.partials[0][nodeIndex][j] = new BigDecimal(partials[j], precision);
165 }
166 }
167 }
168
169 /**
170 * Allocates states for a node
171 */
172 public void createNodeStates(int nodeIndex) {
173
174 this.states[nodeIndex] = new int[patternCount];
175 }
176
177 /**
178 * Sets states for a node
179 */
180 public void setNodeStates(int nodeIndex, int[] states) {
181
182 if (this.states[nodeIndex] == null) {
183 createNodeStates(nodeIndex);
184 }
185 System.arraycopy(states, 0, this.states[nodeIndex], 0, patternCount);
186 }
187
188 /**
189 * Gets states for a node
190 */
191 public void getNodeStates(int nodeIndex, int[] states) {
192 System.arraycopy(this.states[nodeIndex], 0, states, 0, patternCount);
193 }
194
195 public void setNodeMatrixForUpdate(int nodeIndex) {
196 currentMatricesIndices[nodeIndex] = 1 - currentMatricesIndices[nodeIndex];
197
198 }
199
200 /**
201 * Sets probability matrix for a node
202 */
203 public void setNodeMatrix(int nodeIndex, int matrixIndex, double[] matrix) {
204
205 for (int j = 0; j < matrixSize; j++) {
206 matrices[currentMatricesIndices[nodeIndex]][nodeIndex][(matrixIndex * matrixSize) + j] = new BigDecimal(matrix[j], precision);
207 }
208 }
209
210 /**
211 * Gets probability matrix for a node
212 */
213 public void getNodeMatrix(int nodeIndex, int matrixIndex, double[] matrix) {
214 for (int j = 0; j < matrixSize; j++) {
215 matrix[j] = matrices[currentMatricesIndices[nodeIndex]][nodeIndex][(matrixIndex * matrixSize) + j].doubleValue();
216 }
217 }
218
219 public void setNodePartialsForUpdate(int nodeIndex) {
220 currentPartialsIndices[nodeIndex] = 1 - currentPartialsIndices[nodeIndex];
221 }
222
223 /**
224 * Get the currently updating node partials for direct access
225 *
226 * @param nodeIndex
227 */
228 public void setCurrentNodePartials(int nodeIndex, double[] partials) {
229 throw new UnsupportedOperationException("setCurrentNodePartials is not supported by ArbitraryPrecisionLikelihoodCore");
230 }
231
232 /**
233 * Calculates partial likelihoods at a node.
234 *
235 * @param nodeIndex1 the 'child 1' node
236 * @param nodeIndex2 the 'child 2' node
237 * @param nodeIndex3 the 'parent' node
238 */
239 public void calculatePartials(int nodeIndex1, int nodeIndex2, int nodeIndex3) {
240 if (states[nodeIndex1] != null) {
241 if (states[nodeIndex2] != null) {
242 calculateStatesStatesPruning(
243 states[nodeIndex1], matrices[currentMatricesIndices[nodeIndex1]][nodeIndex1],
244 states[nodeIndex2], matrices[currentMatricesIndices[nodeIndex2]][nodeIndex2],
245 partials[currentPartialsIndices[nodeIndex3]][nodeIndex3]);
246 } else {
247 calculateStatesPartialsPruning(states[nodeIndex1], matrices[currentMatricesIndices[nodeIndex1]][nodeIndex1],
248 partials[currentPartialsIndices[nodeIndex2]][nodeIndex2], matrices[currentMatricesIndices[nodeIndex2]][nodeIndex2],
249 partials[currentPartialsIndices[nodeIndex3]][nodeIndex3]);
250 }
251 } else {
252 if (states[nodeIndex2] != null) {
253 calculateStatesPartialsPruning(states[nodeIndex2], matrices[currentMatricesIndices[nodeIndex2]][nodeIndex2],
254 partials[currentPartialsIndices[nodeIndex1]][nodeIndex1], matrices[currentMatricesIndices[nodeIndex1]][nodeIndex1],
255 partials[currentPartialsIndices[nodeIndex3]][nodeIndex3]);
256 } else {
257 calculatePartialsPartialsPruning(partials[currentPartialsIndices[nodeIndex1]][nodeIndex1], matrices[currentMatricesIndices[nodeIndex1]][nodeIndex1],
258 partials[currentPartialsIndices[nodeIndex2]][nodeIndex2], matrices[currentMatricesIndices[nodeIndex2]][nodeIndex2],
259 partials[currentPartialsIndices[nodeIndex3]][nodeIndex3]);
260 }
261 }
262 }
263
264 /**
265 * Calculates partial likelihoods at a node when both children have states.
266 */
267 private void calculateStatesStatesPruning(int[] states1, BigDecimal[] matrices1,
268 int[] states2, BigDecimal[] matrices2,
269 BigDecimal[] partials3) {
270 int v = 0;
271
272 for (int l = 0; l < matrixCount; l++) {
273
274 for (int k = 0; k < patternCount; k++) {
275
276 int state1 = states1[k];
277 int state2 = states2[k];
278
279 int w = l * matrixSize;
280
281 if (state1 < stateCount && state2 < stateCount) {
282
283 for (int i = 0; i < stateCount; i++) {
284
285 partials3[v] = matrices1[w + state1].multiply(matrices2[w + state2], precision);
286
287 v++;
288 w += stateCount;
289 }
290
291 } else if (state1 < stateCount) {
292 // child 2 has a gap or unknown state so treat it as unknown
293
294 for (int i = 0; i < stateCount; i++) {
295
296 partials3[v] = matrices1[w + state1];
297
298 v++;
299 w += stateCount;
300 }
301 } else if (state2 < stateCount) {
302 // child 2 has a gap or unknown state so treat it as unknown
303
304 for (int i = 0; i < stateCount; i++) {
305
306 partials3[v] = matrices2[w + state2];
307
308 v++;
309 w += stateCount;
310 }
311 } else {
312 // both children have a gap or unknown state so set partials to 1
313
314 for (int j = 0; j < stateCount; j++) {
315 partials3[v] = BigDecimal.ONE;
316 v++;
317 }
318 }
319 }
320 }
321 }
322
323 /**
324 * Calculates partial likelihoods at a node when one child has states and one has partials.
325 */
326 private void calculateStatesPartialsPruning(int[] states1, BigDecimal[] matrices1,
327 BigDecimal[] partials2, BigDecimal[] matrices2,
328 BigDecimal[] partials3) {
329
330 BigDecimal sum, tmp;
331
332 int u = 0;
333 int v = 0;
334
335 for (int l = 0; l < matrixCount; l++) {
336 for (int k = 0; k < patternCount; k++) {
337
338 int state1 = states1[k];
339
340 int w = l * matrixSize;
341
342 if (state1 < stateCount) {
343
344
345 for (int i = 0; i < stateCount; i++) {
346
347 tmp = matrices1[w + state1];
348
349 sum = BigDecimal.ZERO;
350 for (int j = 0; j < stateCount; j++) {
351 sum = sum.add(matrices2[w].multiply(partials2[v + j], precision), precision);
352 w++;
353 }
354
355 partials3[u] = tmp.multiply(sum, precision);
356 u++;
357 }
358
359 v += stateCount;
360 } else {
361 // Child 1 has a gap or unknown state so don't use it
362
363 for (int i = 0; i < stateCount; i++) {
364
365 sum = BigDecimal.ZERO;
366 for (int j = 0; j < stateCount; j++) {
367 sum = sum.add(matrices2[w].multiply(partials2[v + j], precision), precision);
368 w++;
369 }
370
371 partials3[u] = sum;
372 u++;
373 }
374
375 v += stateCount;
376 }
377 }
378 }
379 }
380
381 /**
382 * Calculates partial likelihoods at a node when both children have partials.
383 */
384 private void calculatePartialsPartialsPruning(BigDecimal[] partials1, BigDecimal[] matrices1,
385 BigDecimal[] partials2, BigDecimal[] matrices2,
386 BigDecimal[] partials3) {
387 BigDecimal sum1, sum2;
388
389 int u = 0;
390 int v = 0;
391
392 for (int l = 0; l < matrixCount; l++) {
393
394 for (int k = 0; k < patternCount; k++) {
395
396 int w = l * matrixSize;
397
398 for (int i = 0; i < stateCount; i++) {
399
400 sum1 = sum2 = BigDecimal.ZERO;
401
402 for (int j = 0; j < stateCount; j++) {
403 sum1 = sum1.add(matrices1[w].multiply(partials1[v + j], precision), precision);
404 sum2 = sum2.add(matrices2[w].multiply(partials2[v + j], precision), precision);
405
406 w++;
407 }
408
409 partials3[u] = sum1.multiply(sum2, precision);
410 u++;
411 }
412 v += stateCount;
413 }
414 }
415 }
416
417 /**
418 * Calculates partial likelihoods at a node.
419 *
420 * @param nodeIndex1 the 'child 1' node
421 * @param nodeIndex2 the 'child 2' node
422 * @param nodeIndex3 the 'parent' node
423 * @param matrixMap a map of which matrix to use for each pattern (can be null if integrating over categories)
424 */
425 public void calculatePartials(int nodeIndex1, int nodeIndex2, int nodeIndex3, int[] matrixMap) {
426 throw new UnsupportedOperationException("calculatePartials(int nodeIndex1, int nodeIndex2, int nodeIndex3, int[] matrixMap) is not implemented in this likelihood core");
427 }
428
429
430 public void integratePartials(int nodeIndex, double[] proportions, double[] outPartials) {
431 throw new UnsupportedOperationException("integratePartials(int nodeIndex, double[] proportions, double[] outPartials) is not implemented in this likelihood core");
432 }
433
434 public void integratePartials(int nodeIndex, double[] proportions, BigDecimal[] outPartials) {
435 BigDecimal[] prop = new BigDecimal[proportions.length];
436 for (int i = 0; i < proportions.length; i++) {
437 prop[i] = new BigDecimal(proportions[i], precision);
438 }
439 calculateIntegratePartials(partials[currentPartialsIndices[nodeIndex]][nodeIndex], prop, outPartials);
440 }
441
442 /**
443 * Integrates partials across categories.
444 *
445 * @param inPartials the array of partials to be integrated
446 * @param proportions the proportions of sites in each category
447 * @param outPartials an array into which the partials will go
448 */
449 private void calculateIntegratePartials(BigDecimal[] inPartials, BigDecimal[] proportions, BigDecimal[] outPartials) {
450
451 int u = 0;
452 int v = 0;
453 for (int k = 0; k < patternCount; k++) {
454
455 for (int i = 0; i < stateCount; i++) {
456
457 outPartials[u] = inPartials[v].multiply(proportions[0], precision);
458 u++;
459 v++;
460 }
461 }
462
463
464 for (int l = 1; l < matrixCount; l++) {
465 u = 0;
466
467 for (int k = 0; k < patternCount; k++) {
468
469 for (int i = 0; i < stateCount; i++) {
470
471 outPartials[u] = outPartials[u].add(inPartials[v].multiply(proportions[l], precision), precision);
472 u++;
473 v++;
474 }
475 }
476 }
477 }
478
479 /**
480 * Calculates patten log likelihoods at a node.
481 *
482 * @param partials the partials used to calculate the likelihoods
483 * @param frequencies an array of state frequencies
484 * @param outLogLikelihoods an array into which the likelihoods will go
485 */
486 public void calculateLogLikelihoods(BigDecimal[] partials, double[] frequencies, double[] outLogLikelihoods) {
487 BigDecimal[] freqs = new BigDecimal[frequencies.length];
488 for (int i = 0; i < freqs.length; i++) {
489 freqs[i] = new BigDecimal(frequencies[i], precision);
490 }
491
492 int v = 0;
493 for (int k = 0; k < patternCount; k++) {
494
495 BigDecimal sum = BigDecimal.ZERO;
496 for (int i = 0; i < stateCount; i++) {
497
498 sum = sum.add(freqs[i].multiply(partials[v], precision), precision);
499 v++;
500 }
501
502 double scale = sum.scale();
503 double value = sum.unscaledValue().doubleValue();
504
505 outLogLikelihoods[k] = Math.log(value) - (scale * Math.log(10));
506 }
507 }
508
509 public void calculateLogLikelihoods(double[] partials, double[] frequencies, double[] outLogLikelihoods) {
510 throw new UnsupportedOperationException("calculateLogLikelihoods(double[] partials, double[] frequencies, double[] outLogLikelihoods) is not implemented in this likelihood core");
511 }
512
513 public void setUseScaling(boolean useScaling) {
514 }
515
516 public double getLogScalingFactor(int pattern) {
517 return 0;
518 }
519
520 public boolean arePartialsRescaled() {
521 return false;
522 }
523
524 public void getLogScalingFactors(int nodeIndex, double[] buffer) {
525 throw new RuntimeException("Not yet implemented.");
526 }
527
528 /**
529 * Gets the partials for a particular node.
530 *
531 * @param nodeIndex the node
532 * @param outPartials an array into which the partials will go
533 */
534 public void getPartials(int nodeIndex, double[] outPartials) {
535 for (int i = 0; i < outPartials.length; i++) {
536 outPartials[i] = partials[currentPartialsIndices[nodeIndex]][nodeIndex][i].doubleValue();
537 }
538 }
539
540 /**
541 * Store current state
542 */
543 public void storeState() {
544
545 System.arraycopy(currentMatricesIndices, 0, storedMatricesIndices, 0, nodeCount);
546 System.arraycopy(currentPartialsIndices, 0, storedPartialsIndices, 0, nodeCount);
547 }
548
549 /**
550 * Restore the stored state
551 */
552 public void restoreState() {
553 // Rather than copying the stored stuff back, just swap the pointers...
554 int[] tmp1 = currentMatricesIndices;
555 currentMatricesIndices = storedMatricesIndices;
556 storedMatricesIndices = tmp1;
557
558 int[] tmp2 = currentPartialsIndices;
559 currentPartialsIndices = storedPartialsIndices;
560 storedPartialsIndices = tmp2;
561 }
562
563 public void checkScaling() {
564 // do nothing
565 }
566
567 }