Debian Codegraph @

src/dr/evomodel/treedatalikelihood/continuous/cdi/MultivariateIntegrator.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 package dr.evomodel.treedatalikelihood.continuous.cdi;
2
3 import dr.math.matrixAlgebra.WrappedVector;
4 import dr.math.matrixAlgebra.missingData.InversionResult;
•5 import org.ejml.data.DMatrixRMaj;
6 import org.ejml.dense.row.CommonOps_DDRM;
7
8 import java.util.HashMap;
9 import java.util.Map;
10
11 import static dr.math.matrixAlgebra.missingData.InversionResult.Code.NOT_OBSERVED;
12 import static dr.math.matrixAlgebra.missingData.MissingOps.safeInvert;
13 import static dr.math.matrixAlgebra.missingData.MissingOps.unwrap;
14 import static dr.math.matrixAlgebra.missingData.MissingOps.wrap;
15
16 /**
17 * @author Marc A. Suchard
18 */
19
20 public class MultivariateIntegrator extends ContinuousDiffusionIntegrator.Basic {
21
22 private static boolean DEBUG = false;
23
24 public MultivariateIntegrator(PrecisionType precisionType, int numTraits, int dimTrait, int bufferCount,
25 int diffusionCount) {
26 super(precisionType, numTraits, dimTrait, bufferCount, diffusionCount);
27
28 assert precisionType == PrecisionType.FULL;
29
30 allocateStorage();
31
32 if (TIMING) {
33 times = new HashMap<String, Long>();
34 } else {
35 times = null;
36 }
37 }
38
39 @Override
40 public String getReport() {
41
42 StringBuilder sb = new StringBuilder();
43
44 if (TIMING) {
45 sb.append("\nTIMING:");
46 for (String key : times.keySet()) {
47 String value = String.format("%4.3e", (double) times.get(key));
48 sb.append("\n").append(key).append("\t\t").append(value);
49 }
50 sb.append("\n");
51 }
52 return sb.toString();
53 }
54
55 static final boolean TIMING = false;
56
57 private final Map<String, Long> times;
58
•59 DMatrixRMaj matrix0;
60 DMatrixRMaj matrix1;
61 DMatrixRMaj matrix2;
62 DMatrixRMaj matrix3;
63 DMatrixRMaj matrix4;
64 DMatrixRMaj matrix5;
65 DMatrixRMaj matrix6;
66
67 double[] vector0;
68
69 private void allocateStorage() {
70 inverseDiffusions = new double[dimTrait * dimTrait * diffusionCount];
71
72 vector0 = new double[dimTrait];
•73 matrix0 = new DMatrixRMaj(dimTrait, dimTrait);
74 matrix1 = new DMatrixRMaj(dimTrait, dimTrait);
75 matrix2 = new DMatrixRMaj(dimTrait, dimTrait);
76 matrix3 = new DMatrixRMaj(dimTrait, dimTrait);
77 matrix4 = new DMatrixRMaj(dimTrait, dimTrait);
78 matrix5 = new DMatrixRMaj(dimTrait, dimTrait);
79 matrix6 = new DMatrixRMaj(dimTrait, dimTrait);
80 }
81
82 @Override
83 public void setDiffusionPrecision(int precisionIndex, final double[] matrix, double logDeterminant) {
84 super.setDiffusionPrecision(precisionIndex, matrix, logDeterminant);
85
86 assert (inverseDiffusions != null);
87
88 final int offset = dimTrait * dimTrait * precisionIndex;
•89 DMatrixRMaj precision = wrap(diffusions, offset, dimTrait, dimTrait);
90 DMatrixRMaj variance = new DMatrixRMaj(dimTrait, dimTrait);
91 CommonOps_DDRM.invert(precision, variance);
92 unwrap(variance, inverseDiffusions, offset);
93
94 if (DEBUG) {
95 System.err.println("At precision index: " + precisionIndex);
96 System.err.println("precision: " + precision);
97 System.err.println("variance : " + variance);
98 }
99 }
100
101 // @Override
102 // public boolean requireDataAugmentationForOuterProducts() {
103 // return true;
104 // }
105
106 @Override
107 public void updatePreOrderPartial(
108 final int kBuffer, // parent
109 final int iBuffer, // node
110 final int iMatrix,
111 final int jBuffer, // sibling
112 final int jMatrix) {
113
114 // Determine buffer offsets
115 int kbo = dimPartial * kBuffer;
116 int ibo = dimPartial * iBuffer;
117 int jbo = dimPartial * jBuffer;
118
119 // Determine matrix offsets
120 final int imo = dimMatrix * iMatrix;
121 final int jmo = dimMatrix * jMatrix;
122
123 // Read variance increments along descendent branches of k
124 final double vi = branchLengths[imo];
125 final double vj = branchLengths[jmo];
126
•127 final DMatrixRMaj Vd = wrap(inverseDiffusions, precisionOffset, dimTrait, dimTrait);
128
129 if (DEBUG) {
130 System.err.println("updatePreOrderPartial for node " + iBuffer);
131 // System.err.println("variance diffusion: " + Vd);
132 System.err.println("\tvi: " + vi + " vj: " + vj);
133 // System.err.println("precisionOffset = " + precisionOffset);
134 }
135
136 // For each trait // TODO in parallel
137 for (int trait = 0; trait < numTraits; ++trait) {
138
139 // A. Get current precision of k and j
•140 final DMatrixRMaj Pk = wrap(prePartials, kbo + dimTrait, dimTrait, dimTrait);
141 // final DMatrixRMaj Pj = wrap(partials, jbo + dimTrait, dimTrait, dimTrait);
142
•143 // final DMatrixRMaj Vk = wrap(prePartials, kbo + dimTrait + dimTrait * dimTrait, dimTrait, dimTrait);
144 final DMatrixRMaj Vj = wrap(partials, jbo + dimTrait + dimTrait * dimTrait, dimTrait, dimTrait);
145
146 // B. Inflate variance along sibling branch using matrix inversion
•147 // final DMatrixRMaj Vjp = new DMatrixRMaj(dimTrait, dimTrait);
148 final DMatrixRMaj Vjp = matrix0;
149 CommonOps_DDRM.add(Vj, vj, Vd, Vjp);
150
•151 // final DMatrixRMaj Pjp = new DMatrixRMaj(dimTrait, dimTrait);
152 final DMatrixRMaj Pjp = matrix1;
153 InversionResult cj = safeInvert(Vjp, Pjp, false);
154
•155 // final DMatrixRMaj Pip = new DMatrixRMaj(dimTrait, dimTrait);
156 final DMatrixRMaj Pip = matrix2;
157 CommonOps_DDRM.add(Pk, Pjp, Pip);
158
•159 // final DMatrixRMaj Vip = new DMatrixRMaj(dimTrait, dimTrait);
160 final DMatrixRMaj Vip = matrix3;
161 InversionResult cip = safeInvert(Pip, Vip, false);
162
163 // C. Compute prePartial mean
164 // final double[] tmp = new double[dimTrait];
165 final double[] tmp = vector0;
166 for (int g = 0; g < dimTrait; ++g) {
167 double sum = 0.0;
168 for (int h = 0; h < dimTrait; ++h) {
169 sum += Pk.unsafe_get(g, h) * prePartials[kbo + h]; // Read parent
170 sum += Pjp.unsafe_get(g, h) * partials[jbo + h]; // Read sibling
171 }
172 tmp[g] = sum;
173 }
174 for (int g = 0; g < dimTrait; ++g) {
175 double sum = 0.0;
176 for (int h = 0; h < dimTrait; ++h) {
177 sum += Vip.unsafe_get(g, h) * tmp[h];
178 }
179 prePartials[ibo + g] = sum; // Write node
180 }
181
182 // C. Inflate variance along node branch
•183 final DMatrixRMaj Vi = Vip;
184 CommonOps_DDRM.add(vi, Vd, Vip, Vi);
185
•186 // final DMatrixRMaj Pi = new DMatrixRMaj(dimTrait, dimTrait);
187 final DMatrixRMaj Pi = matrix4;
188 InversionResult ci = safeInvert(Vi, Pi, false);
189
190 // X. Store precision results for node
191 unwrap(Pi, prePartials, ibo + dimTrait);
192 unwrap(Vi, prePartials, ibo + dimTrait + dimTrait * dimTrait);
193
194 if (DEBUG) {
195 System.err.println("trait: " + trait);
196 System.err.println("pM: " + new WrappedVector.Raw(prePartials, kbo, dimTrait));
197 System.err.println("pP: " + Pk);
198 System.err.println("sM: " + new WrappedVector.Raw(partials, jbo, dimTrait));
199 System.err.println("sV: " + Vj);
200 System.err.println("sVp: " + Vjp);
201 System.err.println("sPp: " + Pjp);
202 System.err.println("Pip: " + Pip);
203 System.err.println("cM: " + new WrappedVector.Raw(prePartials, ibo, dimTrait));
204 System.err.println("cV: " + Vi);
205 }
206
207 // Get ready for next trait
208 kbo += dimPartialForTrait;
209 ibo += dimPartialForTrait;
210 jbo += dimPartialForTrait;
211 }
212 }
213
214 @Override
215 protected void updatePartial(
216 final int kBuffer,
217 final int iBuffer,
218 final int iMatrix,
219 final int jBuffer,
220 final int jMatrix,
221 final boolean incrementOuterProducts
222 ) {
223
224 if (incrementOuterProducts) {
225 throw new RuntimeException("Outer-products are not supported.");
226 }
227
228 if (TIMING) {
229 startTime("total");
230 }
231
232 // Determine buffer offsets
233 int kbo = dimPartial * kBuffer;
234 int ibo = dimPartial * iBuffer;
235 int jbo = dimPartial * jBuffer;
236
237 // Determine matrix offsets
238 final int imo = dimMatrix * iMatrix;
239 final int jmo = dimMatrix * jMatrix;
240
241 // Read variance increments along descendent branches of k
242 final double vi = branchLengths[imo];
243 final double vj = branchLengths[jmo];
244
•245 final DMatrixRMaj Vd = wrap(inverseDiffusions, precisionOffset, dimTrait, dimTrait);
246
247 if (DEBUG) {
248 System.err.println("variance diffusion: " + Vd);
249 System.err.println("\tvi: " + vi + " vj: " + vj);
250 System.err.println("precisionOffset = " + precisionOffset);
251 }
252
253 // For each trait // TODO in parallel
254 for (int trait = 0; trait < numTraits; ++trait) {
255
256 // Layout, offset, dim
257 // trait, 0, dT
258 // precision, dT, dT * dT
259 // variance, dT + dT * dT, dT * dT
260 // scalar, dT + 2 * dT * dT, 1
261
262 if (TIMING) {
263 startTime("peel1");
264 }
265
266 // Increase variance along the branches i -> k and j -> k
267
268 // A. Get current precision of i and j
269 final double lpi = partials[ibo + dimTrait + 2 * dimTrait * dimTrait];
270 final double lpj = partials[jbo + dimTrait + 2 * dimTrait * dimTrait];
271
•272 final DMatrixRMaj Pi = wrap(partials, ibo + dimTrait, dimTrait, dimTrait);
273 final DMatrixRMaj Pj = wrap(partials, jbo + dimTrait, dimTrait, dimTrait);
274
•275 final DMatrixRMaj Vi = wrap(partials, ibo + dimTrait + dimTrait * dimTrait, dimTrait, dimTrait);
276 final DMatrixRMaj Vj = wrap(partials, jbo + dimTrait + dimTrait * dimTrait, dimTrait, dimTrait);
277
278 if (TIMING) {
279 endTime("peel1");
280 startTime("peel2");
281 }
282
283 // B. Integrate along branch using two matrix inversions
284 final double lpip = Double.isInfinite(lpi) ?
285 1.0 / vi : lpi / (1.0 + lpi * vi);
286 final double lpjp = Double.isInfinite(lpj) ?
287 1.0 / vj : lpj / (1.0 + lpj * vj);
288
•289 // final DMatrixRMaj Vip = new DMatrixRMaj(dimTrait, dimTrait);
290 // final DMatrixRMaj Vjp = new DMatrixRMaj(dimTrait, dimTrait);
291 final DMatrixRMaj Vip = matrix0;
292 final DMatrixRMaj Vjp = matrix1;
293
•294 CommonOps_DDRM.add(Vi, vi, Vd, Vip);
295 CommonOps_DDRM.add(Vj, vj, Vd, Vjp);
296
297 if (TIMING) {
298 endTime("peel2");
299 startTime("peel2a");
300 }
301
•302 // final DMatrixRMaj Pip = new DMatrixRMaj(dimTrait, dimTrait);
303 // final DMatrixRMaj Pjp = new DMatrixRMaj(dimTrait, dimTrait);
304 final DMatrixRMaj Pip = matrix2;
305 final DMatrixRMaj Pjp = matrix3;
306
307 InversionResult ci = safeInvert(Vip, Pip, true);
308 InversionResult cj = safeInvert(Vjp, Pjp, true);
309
310 if (TIMING) {
311 endTime("peel2a");
312 startTime("peel3");
313 }
314
315 // Compute partial mean and precision at node k
316
317 // A. Partial precision and variance (for later use) using one matrix inversion
318 final double lpk = lpip + lpjp;
319
•320 // final DMatrixRMaj Pk = new DMatrixRMaj(dimTrait, dimTrait);
321 final DMatrixRMaj Pk = matrix4;
322
•323 CommonOps_DDRM.add(Pip, Pjp, Pk);
324
•325 // final DMatrixRMaj Vk = new DMatrixRMaj(dimTrait, dimTrait);
326 final DMatrixRMaj Vk = matrix5;
327 InversionResult ck = safeInvert(Pk, Vk, true);
328
329 // B. Partial mean
330 // for (int g = 0; g < dimTrait; ++g) {
331 // partials[kbo + g] = (pip * partials[ibo + g] + pjp * partials[jbo + g]) / pk;
332 // }
333
334 if (TIMING) {
335 endTime("peel3");
336 startTime("peel4");
337 }
338
339 // final double[] tmp = new double[dimTrait];
340 final double[] tmp = vector0;
341 for (int g = 0; g < dimTrait; ++g) {
342 double sum = 0.0;
343 for (int h = 0; h < dimTrait; ++h) {
344 sum += Pip.unsafe_get(g, h) * partials[ibo + h];
345 sum += Pjp.unsafe_get(g, h) * partials[jbo + h];
346 }
347 tmp[g] = sum;
348 }
349 for (int g = 0; g < dimTrait; ++g) {
350 double sum = 0.0;
351 for (int h = 0; h < dimTrait; ++h) {
352 sum += Vk.unsafe_get(g, h) * tmp[h];
353 }
354 partials[kbo + g] = sum;
355 }
356
357 if (TIMING) {
358 endTime("peel4");
359 startTime("peel5");
360 }
361
362 // C. Store precision
363 partials[kbo + dimTrait + 2 * dimTrait * dimTrait] = lpk;
364
365 unwrap(Pk, partials, kbo + dimTrait);
366 unwrap(Vk, partials, kbo + dimTrait + dimTrait * dimTrait);
367
368 if (TIMING) {
369 endTime("peel5");
370 }
371
372 if (DEBUG) {
373 System.err.println("\ttrait: " + trait);
374 System.err.println("Pi: " + Pi);
375 System.err.println("Pj: " + Pj);
376 System.err.println("Pk: " + Pk);
377 System.err.print("\t\tmean i:");
378 for (int e = 0; e < dimTrait; ++e) {
379 System.err.print(" " + partials[ibo + e]);
380 }
381 System.err.print("\t\tmean j:");
382 for (int e = 0; e < dimTrait; ++e) {
383 System.err.print(" " + partials[jbo + e]);
384 }
385 System.err.print("\t\tmean k:");
386 for (int e = 0; e < dimTrait; ++e) {
387 System.err.print(" " + partials[kbo + e]);
388 }
389 System.err.println("");
390 }
391
392 // Computer remainder at node k
393 double remainder = 0.0;
394
395 if (DEBUG) {
396 System.err.println("i status: " + ci);
397 System.err.println("j status: " + cj);
398 System.err.println("k status: " + ck);
399 System.err.println("Pip: " + Pip);
400 System.err.println("Vip: " + Vip);
401 System.err.println("Pjp: " + Pjp);
402 System.err.println("Vjp: " + Vjp);
403 }
404
405 if (!(ci.getReturnCode() == NOT_OBSERVED || cj.getReturnCode() == NOT_OBSERVED)) {
406 // if (ci == InversionReturnCode.FULLY_OBSERVED && cj == InversionReturnCode.FULLY_OBSERVED) {
407 // TODO Fix for partially observed
408 // if (pi != 0 && pj != 0) {
409 //
410 if (TIMING) {
411 startTime("remain");
412 }
413
414 // Inner products
415 double SSk = 0;
416 double SSj = 0;
417 double SSi = 0;
418
419 // vector-matrix-vector TODO in parallel
420 for (int g = 0; g < dimTrait; ++g) {
421 final double ig = partials[ibo + g];
422 final double jg = partials[jbo + g];
423 final double kg = partials[kbo + g];
424
425 for (int h = 0; h < dimTrait; ++h) {
426 final double ih = partials[ibo + h];
427 final double jh = partials[jbo + h];
428 final double kh = partials[kbo + h];
429
430 SSi += ig * Pip.unsafe_get(g, h) * ih;
431 SSj += jg * Pjp.unsafe_get(g, h) * jh;
432 SSk += kg * Pk .unsafe_get(g, h) * kh;
433 }
434 }
435
•436 // final DMatrixRMaj Vt = new DMatrixRMaj(dimTrait, dimTrait);
437 final DMatrixRMaj Vt = matrix6;
438 CommonOps_DDRM.add(Vip, Vjp, Vt);
439
440 if (DEBUG) {
441 System.err.println("Vt: " + Vt);
442 }
443
444 int dimensionChange = ci.getEffectiveDimension() + cj.getEffectiveDimension()
445 - ck.getEffectiveDimension();
446
447 remainder += -dimensionChange * LOG_SQRT_2_PI - 0.5 *
•448 // (Math.log(CommonOps_DDRM.det(Vip)) + Math.log(CommonOps_DDRM.det(Vjp)) - Math.log(CommonOps_DDRM.det(Vk)))
449 (Math.log(ci.getDeterminant()) + Math.log(cj.getDeterminant()) + Math.log(ck.getDeterminant()))
450 - 0.5 * (SSi + SSj - SSk);
451
452 // TODO Can get SSi + SSj - SSk from inner product w.r.t Pt (see outer-products below)?
453
454 if (DEBUG) {
455 System.err.println("\t\t\tSSi = " + (SSi));
456 System.err.println("\t\t\tSSj = " + (SSj));
457 System.err.println("\t\t\tSSk = " + (SSk));
458 System.err.println("\t\t\tdeti = " + Math.log(ci.getDeterminant()));
459 System.err.println("\t\t\tdetj = " + Math.log(ci.getDeterminant()));
460 System.err.println("\t\t\tdetk = " + Math.log(ci.getDeterminant()));
461 System.err.println("\t\tremainder: " + remainder);
462 }
463
464 if (TIMING) {
465 endTime("remain");
466 }
467
468 if (incrementOuterProducts) {
469
470 assert (false);
471
•472 final DMatrixRMaj Pt = new DMatrixRMaj(dimTrait, dimTrait);
473 InversionResult ct = safeInvert(Vt, Pt, false);
474
475 int opo = dimTrait * dimTrait * trait;
476 int opd = precisionOffset;
477
478 for (int g = 0; g < dimTrait; ++g) {
479 final double ig = partials[ibo + g];
480 final double jg = partials[jbo + g];
481
482 for (int h = 0; h < dimTrait; ++h) {
483 final double ih = partials[ibo + h];
484 final double jh = partials[jbo + h];
485
486 outerProducts[opo] += (ig - jg) * (ih - jh)
487 // * Pt.unsafe_get(g, h)
488 // * Pk.unsafe_get(g, h)
489
490 // / diffusions[opd];
491 // * pip * pjp / (pip + pjp);
492 * lpip * lpjp / (lpip + lpjp);
493 ++opo;
494 ++opd;
495 }
496 }
497
498 if (DEBUG) {
499 System.err.println("Outer-products:" + wrap(outerProducts, dimTrait * dimTrait * trait, dimTrait, dimTrait));
500 }
501
502 degreesOfFreedom[trait] += 1; // incremenent degrees-of-freedom
503 }
504 } // End if remainder
505
506 // Accumulate remainder up tree and store
507
508 remainders[kBuffer * numTraits + trait] = remainder
509 + remainders[iBuffer * numTraits + trait] + remainders[jBuffer * numTraits + trait];
510
511 // Get ready for next trait
512 kbo += dimPartialForTrait;
513 ibo += dimPartialForTrait;
514 jbo += dimPartialForTrait;
515
516 }
517
518 if (TIMING) {
519 endTime("total");
520 }
521 }
522
523 private final Map<String, Long> startTimes = new HashMap<String, Long>();
524
525 void startTime(String key) {
526 startTimes.put(key, System.nanoTime());
527 }
528
529 void endTime(String key) {
530 long start = startTimes.get(key);
531
532 Long total = times.get(key);
533 if (total == null) {
534 total = new Long(0);
535 }
536
537 long run = total + (System.nanoTime() - start);
538 times.put(key, run);
539 }
540
541 @Override
542 public void calculateRootLogLikelihood(int rootBufferIndex, int priorBufferIndex, final double[] logLikelihoods,
543 boolean incrementOuterProducts) {
544 assert(logLikelihoods.length == numTraits);
545
546 if (DEBUG) {
547 System.err.println("Root calculation for " + rootBufferIndex);
548 System.err.println("Prior buffer index is " + priorBufferIndex);
549 }
550
551 int rootOffset = dimPartial * rootBufferIndex;
552 int priorOffset = dimPartial * priorBufferIndex;
553
•554 final DMatrixRMaj Vd = wrap(inverseDiffusions, precisionOffset, dimTrait, dimTrait);
555
556 // TODO For each trait in parallel
557 for (int trait = 0; trait < numTraits; ++trait) {
558
•559 final DMatrixRMaj Proot = wrap(partials, rootOffset + dimTrait, dimTrait, dimTrait);
560 final DMatrixRMaj Pprior = wrap(partials, priorOffset + dimTrait, dimTrait, dimTrait);
561
•562 final DMatrixRMaj Vroot = wrap(partials, rootOffset + dimTrait + dimTrait * dimTrait, dimTrait, dimTrait);
563 final DMatrixRMaj Vprior = wrap(partials, priorOffset + dimTrait + dimTrait * dimTrait, dimTrait, dimTrait);
564
565 // TODO Block below is for the conjugate prior ONLY
566 {
•567 final DMatrixRMaj Vtmp = new DMatrixRMaj(dimTrait, dimTrait);
568 CommonOps_DDRM.mult(Vd, Vprior, Vtmp);
569 Vprior.setTo(Vtmp);
570 }
571
•572 final DMatrixRMaj Vtotal = new DMatrixRMaj(dimTrait, dimTrait);
573 CommonOps_DDRM.add(Vroot, Vprior, Vtotal);
574
•575 final DMatrixRMaj Ptotal = new DMatrixRMaj(dimTrait, dimTrait);
576 CommonOps_DDRM.invert(Vtotal, Ptotal); // TODO Can return determinant at same time to avoid extra QR decomp
577
578 double SS = 0;
579 for (int g = 0; g < dimTrait; ++g) {
580 final double gDifference = partials[rootOffset + g] - partials[priorOffset + g];
581
582 for (int h = 0; h < dimTrait; ++h) {
583 final double hDifference = partials[rootOffset + h] - partials[priorOffset + h];
584
585 SS += gDifference * Ptotal.unsafe_get(g, h) * hDifference;
586 }
587 }
588
•589 final double logLike = -dimTrait * LOG_SQRT_2_PI - 0.5 * Math.log(CommonOps_DDRM.det(Vtotal)) - 0.5 * SS;
590
591 final double remainder = remainders[rootBufferIndex * numTraits + trait];
592 logLikelihoods[trait] = logLike + remainder;
593
594 if (incrementOuterProducts) {
595 int opo = dimTrait * dimTrait * trait;
596 int opd = precisionOffset;
597
598 double rootScalar = partials[rootOffset + dimTrait + 2 * dimTrait * dimTrait];
599 final double priorScalar = partials[priorOffset + dimTrait];
600
601 if (!Double.isInfinite(priorScalar)) {
602 rootScalar = rootScalar * priorScalar / (rootScalar + priorScalar);
603 }
604
605 for (int g = 0; g < dimTrait; ++g) {
606 final double gDifference = partials[rootOffset + g] - partials[priorOffset + g];
607
608 for (int h = 0; h < dimTrait; ++h) {
609 final double hDifference = partials[rootOffset + h] - partials[priorOffset + h];
610
611 outerProducts[opo] += gDifference * hDifference * rootScalar;
612 ++opo;
613 ++opd;
614 }
615 }
616
617 degreesOfFreedom[trait] += 1; // incremenent degrees-of-freedom
618 }
619
620 if (DEBUG) {
621 System.err.print("mean:");
622 for (int g = 0; g < dimTrait; ++g) {
623 System.err.print(" " + partials[rootOffset + g]);
624 }
625 System.err.println("");
626 System.err.println("P root: " + Proot);
627 System.err.println("V root: " + Vroot);
628 System.err.println("P prior: " + Pprior);
629 System.err.println("V prior: " + Vprior);
630 System.err.println("P total: " + Ptotal);
631 System.err.println("V total: " + Vtotal);
632 System.err.println("\t" + logLike + " " + (logLike + remainder));
633 if (incrementOuterProducts) {
634 System.err.println("Outer-products:" + wrap(outerProducts, dimTrait * dimTrait * trait, dimTrait, dimTrait));
635 }
636 }
637
638 rootOffset += dimPartialForTrait;
639 priorOffset += dimPartialForTrait;
640 }
641
642 if (DEBUG) {
643 System.err.println("End");
644 }
645 }
646
647 double[] inverseDiffusions;
648 }