package dr.evomodel.treedatalikelihood.continuous.cdi; import dr.math.matrixAlgebra.WrappedVector; import dr.math.matrixAlgebra.missingData.InversionResult; import org.ejml.data.DMatrixRMaj; import org.ejml.dense.row.CommonOps_DDRM; import static dr.math.matrixAlgebra.missingData.InversionResult.Code.NOT_OBSERVED; import static dr.math.matrixAlgebra.missingData.MissingOps.*; /** * @author Marc A. Suchard */ public class SafeMultivariateIntegrator extends MultivariateIntegrator { private static boolean DEBUG = false; public SafeMultivariateIntegrator(PrecisionType precisionType, int numTraits, int dimTrait, int bufferCount, int diffusionCount) { super(precisionType, numTraits, dimTrait, bufferCount, diffusionCount); // assert precisionType == PrecisionType.FULL; // // allocateStorage(); // // if (TIMING) { // times = new HashMap(); // } else { // times = null; // } System.err.println("Trying SafeMultivariateIntegrator"); } // @Override // public String getReport() { // // StringBuilder sb = new StringBuilder(); // // if (TIMING) { // sb.append("\nTIMING:"); // for (String key : times.keySet()) { // String value = String.format("%4.3e", (double) times.get(key)); // sb.append("\n" + key + "\t\t" + value); // } // sb.append("\n"); // } // return sb.toString(); // } private static final boolean TIMING = false; // private final Map times; // // private DMatrixRMaj matrix0; // private DMatrixRMaj matrix1; // private DMatrixRMaj matrix2; // private DMatrixRMaj matrix3; // private DMatrixRMaj matrix4; // private DMatrixRMaj matrix5; // private DMatrixRMaj matrix6; // // private double[] vector0; // private void allocateStorage() { // inverseDiffusions = new double[dimTrait * dimTrait * diffusionCount]; // // vector0 = new double[dimTrait]; // matrix0 = new DMatrixRMaj(dimTrait, dimTrait); // matrix1 = new DMatrixRMaj(dimTrait, dimTrait); // matrix2 = new DMatrixRMaj(dimTrait, dimTrait); // matrix3 = new DMatrixRMaj(dimTrait, dimTrait); // matrix4 = new DMatrixRMaj(dimTrait, dimTrait); // matrix5 = new DMatrixRMaj(dimTrait, dimTrait); // matrix6 = new DMatrixRMaj(dimTrait, dimTrait); // } // @Override // public void setDiffusionPrecision(int precisionIndex, final double[] matrix, double logDeterminant) { // super.setDiffusionPrecision(precisionIndex, matrix, logDeterminant); // // assert (inverseDiffusions != null); // // final int offset = dimTrait * dimTrait * precisionIndex; // DMatrixRMaj precision = wrap(diffusions, offset, dimTrait, dimTrait); // DMatrixRMaj variance = new DMatrixRMaj(dimTrait, dimTrait); // CommonOps_DDRM.invert(precision, variance); // unwrap(variance, inverseDiffusions, offset); // // if (DEBUG) { // System.err.println("At precision index: " + precisionIndex); // System.err.println("precision: " + precision); // System.err.println("variance : " + variance); // } // } // @Override // public boolean requireDataAugmentationForOuterProducts() { // return true; // } // @Override // public void updatePreOrderPartial( // final int kBuffer, // parent // final int iBuffer, // node // final int iMatrix, // final int jBuffer, // sibling // final int jMatrix) { // // // Determine buffer offsets // int kbo = dimPartial * kBuffer; // int ibo = dimPartial * iBuffer; // int jbo = dimPartial * jBuffer; // // // Determine matrix offsets // final int imo = dimMatrix * iMatrix; // final int jmo = dimMatrix * jMatrix; // // // Read variance increments along descendent branches of k // final double vi = branchLengths[imo]; // final double vj = branchLengths[jmo]; // // final DMatrixRMaj Vd = wrap(inverseDiffusions, precisionOffset, dimTrait, dimTrait); // // if (DEBUG) { // System.err.println("updatePreOrderPartial for node " + iBuffer); //// System.err.println("variance diffusion: " + Vd); // System.err.println("\tvi: " + vi + " vj: " + vj); //// System.err.println("precisionOffset = " + precisionOffset); // } // // // For each trait // TODO in parallel // for (int trait = 0; trait < numTraits; ++trait) { // // // A. Get current precision of k and j // final DMatrixRMaj Pk = wrap(prePartials, kbo + dimTrait, dimTrait, dimTrait); //// final DMatrixRMaj Pj = wrap(partials, jbo + dimTrait, dimTrait, dimTrait); // //// final DMatrixRMaj Vk = wrap(prePartials, kbo + dimTrait + dimTrait * dimTrait, dimTrait, dimTrait); // final DMatrixRMaj Vj = wrap(partials, jbo + dimTrait + dimTrait * dimTrait, dimTrait, dimTrait); // // // B. Inflate variance along sibling branch using matrix inversion //// final DMatrixRMaj Vjp = new DMatrixRMaj(dimTrait, dimTrait); // final DMatrixRMaj Vjp = matrix0; // CommonOps_DDRM.add(Vj, vj, Vd, Vjp); // //// final DMatrixRMaj Pjp = new DMatrixRMaj(dimTrait, dimTrait); // final DMatrixRMaj Pjp = matrix1; // InversionResult cj = safeInvert(Vjp, Pjp, false); // //// final DMatrixRMaj Pip = new DMatrixRMaj(dimTrait, dimTrait); // final DMatrixRMaj Pip = matrix2; // CommonOps_DDRM.add(Pk, Pjp, Pip); // //// final DMatrixRMaj Vip = new DMatrixRMaj(dimTrait, dimTrait); // final DMatrixRMaj Vip = matrix3; // InversionResult cip = safeInvert(Pip, Vip, false); // // // C. Compute prePartial mean //// final double[] tmp = new double[dimTrait]; // final double[] tmp = vector0; // for (int g = 0; g < dimTrait; ++g) { // double sum = 0.0; // for (int h = 0; h < dimTrait; ++h) { // sum += Pk.unsafe_get(g, h) * prePartials[kbo + h]; // Read parent // sum += Pjp.unsafe_get(g, h) * partials[jbo + h]; // Read sibling // } // tmp[g] = sum; // } // for (int g = 0; g < dimTrait; ++g) { // double sum = 0.0; // for (int h = 0; h < dimTrait; ++h) { // sum += Vip.unsafe_get(g, h) * tmp[h]; // } // prePartials[ibo + g] = sum; // Write node // } // // // C. Inflate variance along node branch // final DMatrixRMaj Vi = Vip; // CommonOps_DDRM.add(vi, Vd, Vip, Vi); // //// final DMatrixRMaj Pi = new DMatrixRMaj(dimTrait, dimTrait); // final DMatrixRMaj Pi = matrix4; // InversionResult ci = safeInvert(Vi, Pi, false); // // // X. Store precision results for node // unwrap(Pi, prePartials, ibo + dimTrait); // unwrap(Vi, prePartials, ibo + dimTrait + dimTrait * dimTrait); // // if (DEBUG) { // System.err.println("trait: " + trait); // System.err.println("pM: " + new WrappedVector.Raw(prePartials, kbo, dimTrait)); // System.err.println("pP: " + Pk); // System.err.println("sM: " + new WrappedVector.Raw(partials, jbo, dimTrait)); // System.err.println("sV: " + Vj); // System.err.println("sVp: " + Vjp); // System.err.println("sPp: " + Pjp); // System.err.println("Pip: " + Pip); // System.err.println("cM: " + new WrappedVector.Raw(prePartials, ibo, dimTrait)); // System.err.println("cV: " + Vi); // } // // // Get ready for next trait // kbo += dimPartialForTrait; // ibo += dimPartialForTrait; // jbo += dimPartialForTrait; // } // } @Override protected void updatePartial( final int kBuffer, final int iBuffer, final int iMatrix, final int jBuffer, final int jMatrix, final boolean incrementOuterProducts ) { if (incrementOuterProducts) { throw new RuntimeException("Outer-products are not supported."); } if (TIMING) { startTime("total"); } // Determine buffer offsets int kbo = dimPartial * kBuffer; int ibo = dimPartial * iBuffer; int jbo = dimPartial * jBuffer; // Determine matrix offsets final int imo = dimMatrix * iMatrix; final int jmo = dimMatrix * jMatrix; // Read variance increments along descendent branches of k final double vi = branchLengths[imo]; final double vj = branchLengths[jmo]; final DMatrixRMaj Vd = wrap(inverseDiffusions, precisionOffset, dimTrait, dimTrait); final DMatrixRMaj Pd = wrap(diffusions, precisionOffset, dimTrait, dimTrait); if (DEBUG) { System.err.println("variance diffusion: " + Vd); System.err.println("\tvi: " + vi + " vj: " + vj); System.err.println("precisionOffset = " + precisionOffset); } // For each trait // TODO in parallel for (int trait = 0; trait < numTraits; ++trait) { // Layout, offset, dim // trait, 0, dT // precision, dT, dT * dT // variance, dT + dT * dT, dT * dT // scalar, dT + 2 * dT * dT, 1 if (TIMING) { startTime("peel1"); } // Increase variance along the branches i -> k and j -> k // A. Get current precision of i and j final double lpi = partials[ibo + dimTrait + 2 * dimTrait * dimTrait]; final double lpj = partials[jbo + dimTrait + 2 * dimTrait * dimTrait]; final DMatrixRMaj Pi = wrap(partials, ibo + dimTrait, dimTrait, dimTrait); final DMatrixRMaj Pj = wrap(partials, jbo + dimTrait, dimTrait, dimTrait); if (TIMING) { endTime("peel1"); startTime("peel2"); } // B. Integrate along branch using two matrix inversions final double lpip = Double.isInfinite(lpi) ? 1.0 / vi : lpi / (1.0 + lpi * vi); final double lpjp = Double.isInfinite(lpj) ? 1.0 / vj : lpj / (1.0 + lpj * vj); InversionResult ci; InversionResult cj; final DMatrixRMaj Pip = matrix2; final DMatrixRMaj Pjp = matrix3; // boolean useVariance = anyDiagonalInfinities(Pi) || anyDiagonalInfinities(Pj); final boolean useVariancei = anyDiagonalInfinities(Pi); final boolean useVariancej = anyDiagonalInfinities(Pj); if (useVariancei) { final DMatrixRMaj Vip = matrix0; final DMatrixRMaj Vi = wrap(partials, ibo + dimTrait + dimTrait * dimTrait, dimTrait, dimTrait); CommonOps_DDRM.add(Vi, vi, Vd, Vip); ci = safeInvert(Vip, Pip, true); } else { final DMatrixRMaj PiPlusPd = matrix0; CommonOps_DDRM.add(Pi, 1.0 / vi, Pd, PiPlusPd); final DMatrixRMaj PiPlusPdInv = new DMatrixRMaj(dimTrait, dimTrait); safeInvert(PiPlusPd, PiPlusPdInv, false); CommonOps_DDRM.mult(PiPlusPdInv, Pi, Pip); CommonOps_DDRM.mult(Pi, Pip, PiPlusPdInv); CommonOps_DDRM.add(Pi, -1, PiPlusPdInv, Pip); ci = safeDeterminant(Pip, false); } if (useVariancej) { final DMatrixRMaj Vjp = matrix1; final DMatrixRMaj Vj = wrap(partials, jbo + dimTrait + dimTrait * dimTrait, dimTrait, dimTrait); CommonOps_DDRM.add(Vj, vj, Vd, Vjp); cj = safeInvert(Vjp, Pjp, true); } else { final DMatrixRMaj PjPlusPd = matrix1; CommonOps_DDRM.add(Pj, 1.0 / vj, Pd, PjPlusPd); final DMatrixRMaj PjPlusPdInv = new DMatrixRMaj(dimTrait, dimTrait); safeInvert(PjPlusPd, PjPlusPdInv, false); CommonOps_DDRM.mult(PjPlusPdInv, Pj, Pjp); CommonOps_DDRM.mult(Pj, Pjp, PjPlusPdInv); CommonOps_DDRM.add(Pj, -1, PjPlusPdInv, Pjp); cj = safeDeterminant(Pjp, false); } // if (useVariance) { // // final DMatrixRMaj Vip = matrix0; // final DMatrixRMaj Vjp = matrix1; // // final DMatrixRMaj Vi = wrap(partials, ibo + dimTrait + dimTrait * dimTrait, dimTrait, dimTrait); // final DMatrixRMaj Vj = wrap(partials, jbo + dimTrait + dimTrait * dimTrait, dimTrait, dimTrait); // // CommonOps_DDRM.add(Vi, vi, Vd, Vip); // CommonOps_DDRM.add(Vj, vj, Vd, Vjp); // // ci = safeInvert(Vip, Pip, true); // cj = safeInvert(Vjp, Pjp, true); // } else { // // final DMatrixRMaj PiPlusPd = matrix0; // final DMatrixRMaj PjPlusPd = matrix1; // // CommonOps_DDRM.add(Pi, 1.0 / vi, Pd, PiPlusPd); // CommonOps_DDRM.add(Pj, 1.0 / vj, Pd, PjPlusPd); // // final DMatrixRMaj PiPlusPdInv = new DMatrixRMaj(dimTrait, dimTrait); // final DMatrixRMaj PjPlusPdInv = new DMatrixRMaj(dimTrait, dimTrait); // // safeInvert(PiPlusPd, PiPlusPdInv, false); // safeInvert(PjPlusPd, PjPlusPdInv, false); // // CommonOps_DDRM.mult(PiPlusPdInv, Pi, Pip); // CommonOps_DDRM.mult(PjPlusPdInv, Pj, Pjp); // // CommonOps_DDRM.mult(Pi, Pip, PiPlusPdInv); // CommonOps_DDRM.mult(Pj, Pjp, PjPlusPdInv); // // CommonOps_DDRM.add(Pi, -1, PiPlusPdInv, Pip); // CommonOps_DDRM.add(Pj, -1, PjPlusPdInv, Pjp); // // ci = safeDeterminant(Pip, false); // cj = safeDeterminant(Pjp, false); // } if (TIMING) { endTime("peel2"); startTime("peel2a"); } if (TIMING) { endTime("peel2a"); startTime("peel3"); } // Compute partial mean and precision at node k // A. Partial precision and variance (for later use) using one matrix inversion final double lpk = lpip + lpjp; // final DMatrixRMaj Pk = new DMatrixRMaj(dimTrait, dimTrait); final DMatrixRMaj Pk = matrix4; CommonOps_DDRM.add(Pip, Pjp, Pk); // final DMatrixRMaj Vk = new DMatrixRMaj(dimTrait, dimTrait); // final DMatrixRMaj Vk = matrix5; // if (useVariance) { //// InversionResult ck = // safeInvert(Pk, Vk, true); // } // InversionResult ck = safeDeterminant(Pk, true); // System.err.println(ck); // System.err.println(ck2); // System.exit(-1); // B. Partial mean if (TIMING) { endTime("peel3"); startTime("peel4"); } // final double[] tmp = new double[dimTrait]; final double[] tmp = vector0; for (int g = 0; g < dimTrait; ++g) { double sum = 0.0; for (int h = 0; h < dimTrait; ++h) { sum += Pip.unsafe_get(g, h) * partials[ibo + h]; sum += Pjp.unsafe_get(g, h) * partials[jbo + h]; } tmp[g] = sum; } // for (int g = 0; g < dimTrait; ++g) { // double sum = 0.0; // for (int h = 0; h < dimTrait; ++h) { // sum += Vk.unsafe_get(g, h) * tmp[h]; // } // partials[kbo + g] = sum; // } final WrappedVector kPartials = new WrappedVector.Raw(partials, kbo, dimTrait); final WrappedVector wrapTmp = new WrappedVector.Raw(tmp, 0, dimTrait); // System.err.println(kPartials); // System.err.println(ck.getDeterminant()); InversionResult ck = safeSolve(Pk, wrapTmp, kPartials, true); // System.err.println(kPartials); // System.err.println(ck.getDeterminant()); // System.exit(-1); // TODO Use safeSolve just above if (TIMING) { endTime("peel4"); startTime("peel5"); } // C. Store precision partials[kbo + dimTrait + 2 * dimTrait * dimTrait] = lpk; unwrap(Pk, partials, kbo + dimTrait); // if (useVariance) { // unwrap(Vk, partials, kbo + dimTrait + dimTrait * dimTrait); // } if (TIMING) { endTime("peel5"); } if (DEBUG) { System.err.println("\ttrait: " + trait); System.err.println("Pi: " + Pi); System.err.println("Pj: " + Pj); System.err.println("Pk: " + Pk); System.err.print("\t\tmean i:"); for (int e = 0; e < dimTrait; ++e) { System.err.print(" " + partials[ibo + e]); } System.err.print("\t\tmean j:"); for (int e = 0; e < dimTrait; ++e) { System.err.print(" " + partials[jbo + e]); } System.err.print("\t\tmean k:"); for (int e = 0; e < dimTrait; ++e) { System.err.print(" " + partials[kbo + e]); } System.err.println(""); } // Computer remainder at node k double remainder = 0.0; if (DEBUG) { System.err.println("i status: " + ci); System.err.println("j status: " + cj); System.err.println("k status: " + ck); System.err.println("Pip: " + Pip); // System.err.println("Vip: " + Vip); System.err.println("Pjp: " + Pjp); // System.err.println("Vjp: " + Vjp); } if (!(ci.getReturnCode() == NOT_OBSERVED || cj.getReturnCode() == NOT_OBSERVED)) { // if (ci == InversionReturnCode.FULLY_OBSERVED && cj == InversionReturnCode.FULLY_OBSERVED) { // TODO Fix for partially observed // if (pi != 0 && pj != 0) { // if (TIMING) { startTime("remain"); } // Inner products double SSk = 0; double SSj = 0; double SSi = 0; // vector-matrix-vector TODO in parallel for (int g = 0; g < dimTrait; ++g) { final double ig = partials[ibo + g]; final double jg = partials[jbo + g]; final double kg = partials[kbo + g]; for (int h = 0; h < dimTrait; ++h) { final double ih = partials[ibo + h]; final double jh = partials[jbo + h]; final double kh = partials[kbo + h]; SSi += ig * Pip.unsafe_get(g, h) * ih; SSj += jg * Pjp.unsafe_get(g, h) * jh; SSk += kg * Pk .unsafe_get(g, h) * kh; } } // final DMatrixRMaj Vt = new DMatrixRMaj(dimTrait, dimTrait); // final DMatrixRMaj Vt = matrix6; // CommonOps_DDRM.add(Vip, Vjp, Vt); // if (DEBUG) { // System.err.println("Vt: " + Vt); // } int dimensionChange = ci.getEffectiveDimension() + cj.getEffectiveDimension() - ck.getEffectiveDimension(); // System.err.println(ci.getDeterminant()); // System.err.println(CommonOps_DDRM.det(Vip)); // // System.err.println(cj.getDeterminant()); // System.err.println(CommonOps_DDRM.det(Vjp)); // // System.err.println(1.0 / ck.getDeterminant()); // System.err.println(CommonOps_DDRM.det(Vk)); remainder += -dimensionChange * LOG_SQRT_2_PI - 0.5 * // (Math.log(CommonOps_DDRM.det(Vip)) + Math.log(CommonOps_DDRM.det(Vjp)) - Math.log(CommonOps_DDRM.det(Vk))) (Math.log(ci.getDeterminant()) + Math.log(cj.getDeterminant()) + Math.log(ck.getDeterminant())) - 0.5 * (SSi + SSj - SSk); // TODO Can get SSi + SSj - SSk from inner product w.r.t Pt (see outer-products below)? if (DEBUG) { System.err.println("\t\t\tSSi = " + (SSi)); System.err.println("\t\t\tSSj = " + (SSj)); System.err.println("\t\t\tSSk = " + (SSk)); System.err.println("\t\t\tdeti = " + Math.log(ci.getDeterminant())); System.err.println("\t\t\tdetj = " + Math.log(ci.getDeterminant())); System.err.println("\t\t\tdetk = " + Math.log(ci.getDeterminant())); System.err.println("\t\tremainder: " + remainder); // System.exit(-1); } if (TIMING) { endTime("remain"); } } // End if remainder // Accumulate remainder up tree and store remainders[kBuffer * numTraits + trait] = remainder + remainders[iBuffer * numTraits + trait] + remainders[jBuffer * numTraits + trait]; // Get ready for next trait kbo += dimPartialForTrait; ibo += dimPartialForTrait; jbo += dimPartialForTrait; } if (TIMING) { endTime("total"); } } // private final Map startTimes = new HashMap(); // // private void startTime(String key) { // startTimes.put(key, System.nanoTime()); // } // // private void endTime(String key) { // long start = startTimes.get(key); // // Long total = times.get(key); // if (total == null) { // total = new Long(0); // } // // long run = total + (System.nanoTime() - start); // times.put(key, run); // //// System.err.println("run = " + run); //// System.exit(-1); // } // private void incrementTiming(long start, long end, String key) { // Long total = times.get(key); // // System.err.println(start + " " + end + " " + key); // System.exit(-1); // if (total == null) { // total = new Long(0); // times.put(key, total); // } // total += (end - start); //// times.put(key, total); // } @Override public void calculateRootLogLikelihood(int rootBufferIndex, int priorBufferIndex, final double[] logLikelihoods, boolean incrementOuterProducts) { assert(logLikelihoods.length == numTraits); if (DEBUG) { System.err.println("Root calculation for " + rootBufferIndex); System.err.println("Prior buffer index is " + priorBufferIndex); } int rootOffset = dimPartial * rootBufferIndex; int priorOffset = dimPartial * priorBufferIndex; final DMatrixRMaj Pd = wrap(diffusions, precisionOffset, dimTrait, dimTrait); // final DMatrixRMaj Vd = wrap(inverseDiffusions, precisionOffset, dimTrait, dimTrait); // TODO For each trait in parallel for (int trait = 0; trait < numTraits; ++trait) { final DMatrixRMaj Proot = wrap(partials, rootOffset + dimTrait, dimTrait, dimTrait); final DMatrixRMaj Pprior = wrap(partials, priorOffset + dimTrait, dimTrait, dimTrait); // final DMatrixRMaj Vroot = wrap(partials, rootOffset + dimTrait + dimTrait * dimTrait, dimTrait, dimTrait); // final DMatrixRMaj Vprior = wrap(partials, priorOffset + dimTrait + dimTrait * dimTrait, dimTrait, dimTrait); // TODO Block below is for the conjugate prior ONLY { // final DMatrixRMaj Vtmp = new DMatrixRMaj(dimTrait, dimTrait); // CommonOps_DDRM.mult(Vd, Vprior, Vtmp); // Vprior.set(Vtmp); final DMatrixRMaj Ptmp = new DMatrixRMaj(dimTrait, dimTrait); CommonOps_DDRM.mult(Pd, Pprior, Ptmp); Pprior.setTo(Ptmp); // TODO What does this do? } final DMatrixRMaj Vtotal = new DMatrixRMaj(dimTrait, dimTrait); // CommonOps_DDRM.add(Vroot, Vprior, Vtotal); final DMatrixRMaj Ptotal = new DMatrixRMaj(dimTrait, dimTrait); CommonOps_DDRM.invert(Vtotal, Ptotal); // TODO Can return determinant at same time to avoid extra QR decomp final DMatrixRMaj tmp1 = new DMatrixRMaj(dimTrait, dimTrait); final DMatrixRMaj tmp2 = new DMatrixRMaj(dimTrait, dimTrait); CommonOps_DDRM.add(Proot, Pprior, Ptotal); CommonOps_DDRM.invert(Ptotal, Vtotal); CommonOps_DDRM.mult(Vtotal, Proot, tmp1); CommonOps_DDRM.mult(Proot, tmp1, tmp2); CommonOps_DDRM.add(Proot, -1.0, tmp2, Ptotal); double SS = 0; for (int g = 0; g < dimTrait; ++g) { final double gDifference = partials[rootOffset + g] - partials[priorOffset + g]; for (int h = 0; h < dimTrait; ++h) { final double hDifference = partials[rootOffset + h] - partials[priorOffset + h]; SS += gDifference * Ptotal.unsafe_get(g, h) * hDifference; } } final double logLike = -dimTrait * LOG_SQRT_2_PI // - 0.5 * Math.log(CommonOps_DDRM.det(Vtotal)) + 0.5 * Math.log(CommonOps_DDRM.det(Ptotal)) - 0.5 * SS; final double remainder = remainders[rootBufferIndex * numTraits + trait]; logLikelihoods[trait] = logLike + remainder; if (incrementOuterProducts) { assert false : "Should not get here"; // int opo = dimTrait * dimTrait * trait; // int opd = precisionOffset; // // double rootScalar = partials[rootOffset + dimTrait + 2 * dimTrait * dimTrait]; // final double priorScalar = partials[priorOffset + dimTrait]; // // if (!Double.isInfinite(priorScalar)) { // rootScalar = rootScalar * priorScalar / (rootScalar + priorScalar); // } // // for (int g = 0; g < dimTrait; ++g) { // final double gDifference = partials[rootOffset + g] - partials[priorOffset + g]; // // for (int h = 0; h < dimTrait; ++h) { // final double hDifference = partials[rootOffset + h] - partials[priorOffset + h]; // // outerProducts[opo] += gDifference * hDifference //// * Ptotal.unsafe_get(g, h) / diffusions[opd]; // * rootScalar; // ++opo; // ++opd; // } // } // // degreesOfFreedom[trait] += 1; // incremenent degrees-of-freedom } if (DEBUG) { System.err.print("mean:"); for (int g = 0; g < dimTrait; ++g) { System.err.print(" " + partials[rootOffset + g]); } System.err.println(""); System.err.println("Proot: " + Proot); // System.err.println("Vroot: " + Vroot); System.err.println("Pprior: " + Pprior); // System.err.println("Vprior: " + Vprior); System.err.println("Ptotal: " + Ptotal); // System.err.println("Vtotal: " + Vtotal); // System.err.println("prec: " + partials[rootOffset + dimTrait]); System.err.println("\t" + logLike + " " + (logLike + remainder)); if (incrementOuterProducts) { System.err.println("Outer-products:" + wrap(outerProducts, dimTrait * dimTrait * trait, dimTrait, dimTrait)); } } rootOffset += dimPartialForTrait; priorOffset += dimPartialForTrait; } if (DEBUG) { System.err.println("End"); } } }