| 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 |
|
| 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 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
@Override |
| 107 |
public void updatePreOrderPartial( |
| 108 |
final int kBuffer, |
| 109 |
final int iBuffer, |
| 110 |
final int iMatrix, |
| 111 |
final int jBuffer, |
| 112 |
final int jMatrix) { |
| 113 |
|
| 114 |
|
| 115 |
int kbo = dimPartial * kBuffer; |
| 116 |
int ibo = dimPartial * iBuffer; |
| 117 |
int jbo = dimPartial * jBuffer; |
| 118 |
|
| 119 |
|
| 120 |
final int imo = dimMatrix * iMatrix; |
| 121 |
final int jmo = dimMatrix * jMatrix; |
| 122 |
|
| 123 |
|
| 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 |
|
| 132 |
System.err.println("\tvi: " + vi + " vj: " + vj); |
| 133 |
|
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
for (int trait = 0; trait < numTraits; ++trait) { |
| 138 |
|
| 139 |
|
| •140 |
final DMatrixRMaj Pk = wrap(prePartials, kbo + dimTrait, dimTrait, dimTrait); |
| 141 |
|
| 142 |
|
| •143 |
|
| 144 |
final DMatrixRMaj Vj = wrap(partials, jbo + dimTrait + dimTrait * dimTrait, dimTrait, dimTrait); |
| 145 |
|
| 146 |
|
| •147 |
|
| 148 |
final DMatrixRMaj Vjp = matrix0; |
| 149 |
CommonOps_DDRM.add(Vj, vj, Vd, Vjp); |
| 150 |
|
| •151 |
|
| 152 |
final DMatrixRMaj Pjp = matrix1; |
| 153 |
InversionResult cj = safeInvert(Vjp, Pjp, false); |
| 154 |
|
| •155 |
|
| 156 |
final DMatrixRMaj Pip = matrix2; |
| 157 |
CommonOps_DDRM.add(Pk, Pjp, Pip); |
| 158 |
|
| •159 |
|
| 160 |
final DMatrixRMaj Vip = matrix3; |
| 161 |
InversionResult cip = safeInvert(Pip, Vip, false); |
| 162 |
|
| 163 |
|
| 164 |
|
| 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]; |
| 170 |
sum += Pjp.unsafe_get(g, h) * partials[jbo + h]; |
| 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; |
| 180 |
} |
| 181 |
|
| 182 |
|
| •183 |
final DMatrixRMaj Vi = Vip; |
| 184 |
CommonOps_DDRM.add(vi, Vd, Vip, Vi); |
| 185 |
|
| •186 |
|
| 187 |
final DMatrixRMaj Pi = matrix4; |
| 188 |
InversionResult ci = safeInvert(Vi, Pi, false); |
| 189 |
|
| 190 |
|
| 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 |
|
| 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 |
|
| 233 |
int kbo = dimPartial * kBuffer; |
| 234 |
int ibo = dimPartial * iBuffer; |
| 235 |
int jbo = dimPartial * jBuffer; |
| 236 |
|
| 237 |
|
| 238 |
final int imo = dimMatrix * iMatrix; |
| 239 |
final int jmo = dimMatrix * jMatrix; |
| 240 |
|
| 241 |
|
| 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 |
|
| 254 |
for (int trait = 0; trait < numTraits; ++trait) { |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
if (TIMING) { |
| 263 |
startTime("peel1"); |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 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 |
|
| 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 |
|
| 290 |
|
| 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 |
|
| 303 |
|
| 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 |
|
| 316 |
|
| 317 |
|
| 318 |
final double lpk = lpip + lpjp; |
| 319 |
|
| •320 |
|
| 321 |
final DMatrixRMaj Pk = matrix4; |
| 322 |
|
| •323 |
CommonOps_DDRM.add(Pip, Pjp, Pk); |
| 324 |
|
| •325 |
|
| 326 |
final DMatrixRMaj Vk = matrix5; |
| 327 |
InversionResult ck = safeInvert(Pk, Vk, true); |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
if (TIMING) { |
| 335 |
endTime("peel3"); |
| 336 |
startTime("peel4"); |
| 337 |
} |
| 338 |
|
| 339 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
if (TIMING) { |
| 411 |
startTime("remain"); |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
double SSk = 0; |
| 416 |
double SSj = 0; |
| 417 |
double SSi = 0; |
| 418 |
|
| 419 |
|
| 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 |
|
| 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 |
|
| 449 |
(Math.log(ci.getDeterminant()) + Math.log(cj.getDeterminant()) + Math.log(ck.getDeterminant())) |
| 450 |
- 0.5 * (SSi + SSj - SSk); |
| 451 |
|
| 452 |
|
| 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 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 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; |
| 503 |
} |
| 504 |
} |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
remainders[kBuffer * numTraits + trait] = remainder |
| 509 |
+ remainders[iBuffer * numTraits + trait] + remainders[jBuffer * numTraits + trait]; |
| 510 |
|
| 511 |
|
| 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 |
|
| 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 |
|
| 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); |
| 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; |
| 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 |
} |