| 1 |
/* |
| 2 |
* ContinuousDiffusionIntegrator.java |
| 3 |
* |
| 4 |
* Copyright (c) 2002-2016 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.evomodel.treedatalikelihood.continuous.cdi; |
| 27 |
|
| 28 |
import dr.math.matrixAlgebra.WrappedVector; |
| 29 |
import dr.xml.Reportable; |
| 30 |
|
| 31 |
import static dr.math.matrixAlgebra.missingData.MissingOps.*; |
| 32 |
|
| 33 |
/** |
| 34 |
* @author Marc A. Suchard |
| 35 |
*/ |
| 36 |
public interface ContinuousDiffusionIntegrator extends Reportable { |
| 37 |
int OPERATION_TUPLE_SIZE = 5; |
| 38 |
int NONE = -1; |
| 39 |
|
| 40 |
double LOG_SQRT_2_PI = 0.5 * Math.log(2 * Math.PI); |
| 41 |
|
| 42 |
void finalize() throws Throwable; |
| 43 |
|
| 44 |
void setPostOrderPartial(int bufferIndex, final double[] partial); |
| 45 |
|
| 46 |
void getPostOrderPartial(int bufferIndex, final double[] partial); |
| 47 |
|
| 48 |
double getBranchMatrices(int bufferIndex, final double[] precision, final double[] displacement); |
| 49 |
|
| 50 |
void setPreOrderPartial(int bufferIndex, final double[] partial); |
| 51 |
|
| 52 |
void getPreOrderPartial(int bufferIndex, final double[] partial); |
| 53 |
|
| 54 |
void setWishartStatistics(final int[] degreesOfFreedom, final double[] outerProducts); |
| 55 |
|
| 56 |
void getWishartStatistics(final int[] degreesOfFreedom, final double[] outerProducts); |
| 57 |
|
| 58 |
void setDiffusionPrecision(int diffusionIndex, final double[] matrix, double logDeterminant); |
| 59 |
|
| 60 |
void updatePostOrderPartials(final int[] operations, int operationCount, boolean incrementOuterProducts); |
| 61 |
|
| 62 |
void updatePreOrderPartials(final int[] operations, int operationCount); |
| 63 |
|
| 64 |
InstanceDetails getDetails(); |
| 65 |
|
| 66 |
void updateBrownianDiffusionMatrices(int precisionIndex, final int[] probabilityIndices, |
| 67 |
final double[] edgeLengths, final double[] driftRates, |
| 68 |
int updateCount); |
| 69 |
|
| 70 |
// void updateOrnsteinUhlenbeckMatrices(int precisionIndex, final int[] probabilityIndices, |
| 71 |
// final double[] edgeLengths, |
| 72 |
// int updateCount); |
| 73 |
|
| 74 |
void calculateRootLogLikelihood(int rootBufferIndex, int priorBufferIndex, double[] logLike, |
| 75 |
boolean incrementOuterProducts); |
| 76 |
|
| 77 |
// int getPartialBufferCount(); |
| 78 |
|
| 79 |
// int getMatrixBufferCount(); |
| 80 |
|
| 81 |
// boolean requireDataAugmentationForOuterProducts(); |
| 82 |
|
| 83 |
// TODO Only send a list of operations |
| 84 |
void updatePreOrderPartial(int kp, int ip, int im, int jp, int jm); |
| 85 |
|
| 86 |
class Basic implements ContinuousDiffusionIntegrator { |
| 87 |
|
| 88 |
// private int instance = -1; |
| 89 |
private InstanceDetails details = new InstanceDetails(); |
| 90 |
|
| 91 |
// private final PrecisionType precisionType; |
| 92 |
final int numTraits; |
| 93 |
final int dimTrait; |
| 94 |
final int bufferCount; |
| 95 |
final int diffusionCount; |
| 96 |
|
| 97 |
final int dimMatrix; |
| 98 |
final int dimPartialForTrait; |
| 99 |
final int dimPartial; |
| 100 |
|
| 101 |
@Override |
| 102 |
public String getReport() { |
| 103 |
return ""; |
| 104 |
} |
| 105 |
|
| 106 |
public Basic( |
| 107 |
final PrecisionType precisionType, |
| 108 |
final int numTraits, |
| 109 |
final int dimTrait, |
| 110 |
final int bufferCount, |
| 111 |
final int diffusionCount |
| 112 |
) { |
| 113 |
assert(numTraits > 0); |
| 114 |
assert(dimTrait > 0); |
| 115 |
assert(bufferCount > 0); |
| 116 |
assert(diffusionCount > 0); |
| 117 |
|
| 118 |
// assert (precisionType == PrecisionType.SCALAR); |
| 119 |
|
| 120 |
// this.precisionType = precisionType; |
| 121 |
this.numTraits = numTraits; |
| 122 |
this.dimTrait = dimTrait; |
| 123 |
this.bufferCount = bufferCount; |
| 124 |
this.diffusionCount = diffusionCount; |
| 125 |
|
| 126 |
this.dimMatrix = precisionType.getMatrixLength(dimTrait); |
| 127 |
this.dimPartialForTrait = dimTrait + dimMatrix; |
| 128 |
this.dimPartial = numTraits * dimPartialForTrait; |
| 129 |
|
| 130 |
if (DEBUG) { |
| 131 |
System.err.println("numTraits: " + numTraits); |
| 132 |
System.err.println("dimTrait: " + dimTrait); |
| 133 |
System.err.println("dimMatrix: " + dimMatrix); |
| 134 |
System.err.println("dimPartialForTrait: " + dimPartialForTrait); |
| 135 |
System.err.println("dimPartial: " + dimPartial); |
| 136 |
} |
| 137 |
|
| 138 |
allocateStorage(); |
| 139 |
} |
| 140 |
|
| 141 |
// @Override |
| 142 |
// public int getPartialBufferCount() { return bufferCount; } |
| 143 |
// |
| 144 |
// @Override |
| 145 |
// public boolean requireDataAugmentationForOuterProducts() { |
| 146 |
// return false; |
| 147 |
// } |
| 148 |
|
| 149 |
@Override |
| 150 |
public void finalize() throws Throwable { |
| 151 |
super.finalize(); |
| 152 |
} |
| 153 |
|
| 154 |
@Override |
| 155 |
public void setPostOrderPartial(int bufferIndex, final double[] partial) { |
| 156 |
assert(partial.length == dimPartial); |
| 157 |
assert(partials != null); |
| 158 |
|
| 159 |
System.arraycopy(partial, 0, partials, dimPartial * bufferIndex, dimPartial); |
| 160 |
} |
| 161 |
|
| 162 |
@Override |
| 163 |
public void getPostOrderPartial(int bufferIndex, final double[] partial) { |
| 164 |
assert(partials != null); |
| 165 |
assert(partial.length >= dimPartial); |
| 166 |
|
| 167 |
System.arraycopy(partials, dimPartial * bufferIndex, partial, 0, dimPartial); |
| 168 |
} |
| 169 |
|
| 170 |
@Override |
| 171 |
public double getBranchMatrices(int bufferIndex, double[] precision, double[] displacement) { |
| 172 |
return 1.0 / branchLengths[bufferIndex * dimMatrix]; |
| 173 |
} |
| 174 |
|
| 175 |
@Override |
| 176 |
public void setPreOrderPartial(int bufferIndex, final double[] partial) { |
| 177 |
assert(partial.length >= dimPartial); |
| 178 |
assert(prePartials != null); |
| 179 |
|
| 180 |
System.arraycopy(partial, 0, prePartials, dimPartial * bufferIndex, dimPartial); |
| 181 |
} |
| 182 |
|
| 183 |
@Override |
| 184 |
public void getPreOrderPartial(int bufferIndex, final double[] partial) { |
| 185 |
assert(partial.length >= dimPartial); |
| 186 |
assert(prePartials != null); |
| 187 |
|
| 188 |
System.arraycopy(prePartials, dimPartial * bufferIndex, partial, 0, dimPartial); |
| 189 |
} |
| 190 |
|
| 191 |
@Override |
| 192 |
public void setWishartStatistics(final int[] degreesOfFreedom, final double[] outerProducts) { |
| 193 |
assert(degreesOfFreedom.length == numTraits); |
| 194 |
assert(outerProducts.length == dimTrait * dimTrait * numTraits); |
| 195 |
|
| 196 |
System.arraycopy(degreesOfFreedom, 0, this.degreesOfFreedom, 0, numTraits); |
| 197 |
System.arraycopy(outerProducts, 0, this.outerProducts, 0, dimTrait * dimTrait * numTraits); |
| 198 |
} |
| 199 |
|
| 200 |
@Override |
| 201 |
public void getWishartStatistics(final int[] degreesOfFreedom, final double[] outerProducts) { |
| 202 |
assert(degreesOfFreedom.length == numTraits); |
| 203 |
assert(outerProducts.length == dimTrait * dimTrait * numTraits); |
| 204 |
|
| 205 |
System.arraycopy(this.degreesOfFreedom, 0, degreesOfFreedom, 0, numTraits); |
| 206 |
System.arraycopy(this.outerProducts, 0, outerProducts, 0, dimTrait * dimTrait * numTraits); |
| 207 |
} |
| 208 |
|
| 209 |
@Override |
| 210 |
public void setDiffusionPrecision(int precisionIndex, final double[] matrix, double logDeterminant) { |
| 211 |
assert(matrix.length == dimTrait * dimTrait); |
| 212 |
assert(diffusions != null); |
| 213 |
assert(determinants != null); |
| 214 |
|
| 215 |
System.arraycopy(matrix, 0, diffusions, dimTrait * dimTrait * precisionIndex, dimTrait * dimTrait); |
| 216 |
determinants[precisionIndex] = logDeterminant; |
| 217 |
} |
| 218 |
|
| 219 |
@Override |
| 220 |
public void calculateRootLogLikelihood(int rootBufferIndex, int priorBufferIndex, final double[] logLikelihoods, |
| 221 |
boolean incrementOuterProducts) { |
| 222 |
assert(logLikelihoods.length == numTraits); |
| 223 |
|
| 224 |
if (DEBUG) { |
| 225 |
System.err.println("Root calculation for " + rootBufferIndex); |
| 226 |
System.err.println("Prior buffer index is " + priorBufferIndex); |
| 227 |
} |
| 228 |
|
| 229 |
int rootOffset = dimPartial * rootBufferIndex; |
| 230 |
int priorOffset = dimPartial * priorBufferIndex; |
| 231 |
|
| 232 |
// TODO For each trait in parallel |
| 233 |
for (int trait = 0; trait < numTraits; ++trait) { |
| 234 |
|
| 235 |
double SS = 0; |
| 236 |
int pob = precisionOffset; |
| 237 |
|
| 238 |
double rootScalar = partials[rootOffset + dimTrait]; |
| 239 |
final double priorScalar = partials[priorOffset + dimTrait]; |
| 240 |
|
| 241 |
if (!Double.isInfinite(priorScalar)) { |
| 242 |
rootScalar = rootScalar * priorScalar / (rootScalar + priorScalar); |
| 243 |
} |
| 244 |
|
| 245 |
for (int g = 0; g < dimTrait; ++g) { |
| 246 |
final double gDifference = partials[rootOffset + g] - partials[priorOffset + g]; |
| 247 |
|
| 248 |
for (int h = 0; h < dimTrait; ++h) { |
| 249 |
final double hDifference = partials[rootOffset + h] - partials[priorOffset + h]; |
| 250 |
|
| 251 |
SS += gDifference * diffusions[pob] * hDifference; |
| 252 |
++pob; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
final double logLike = -dimTrait * LOG_SQRT_2_PI |
| 257 |
+ 0.5 * (dimTrait * Math.log(rootScalar) + precisionLogDet) |
| 258 |
- 0.5 * rootScalar * SS; |
| 259 |
final double remainder = remainders[rootBufferIndex * numTraits + trait]; |
| 260 |
|
| 261 |
logLikelihoods[trait] = logLike + remainder; |
| 262 |
|
| 263 |
if (incrementOuterProducts) { |
| 264 |
int opo = dimTrait * dimTrait * trait; |
| 265 |
|
| 266 |
for (int g = 0; g < dimTrait; ++g) { |
| 267 |
final double gDifference = partials[rootOffset + g] - partials[priorOffset + g]; |
| 268 |
|
| 269 |
for (int h = 0; h < dimTrait; ++h) { |
| 270 |
final double hDifference = partials[rootOffset + h] - partials[priorOffset + h]; |
| 271 |
|
| 272 |
outerProducts[opo] += gDifference * hDifference * rootScalar; |
| 273 |
++opo; |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
degreesOfFreedom[trait] += 1; // incremenent degrees-of-freedom |
| 278 |
} |
| 279 |
|
| 280 |
if (DEBUG) { |
| 281 |
System.err.print("mean:"); |
| 282 |
for (int g = 0; g < dimTrait; ++g) { |
| 283 |
System.err.print(" " + partials[rootOffset + g]); |
| 284 |
} |
| 285 |
System.err.println(""); |
| 286 |
System.err.println("prec: " + partials[rootOffset + dimTrait]); |
| 287 |
System.err.println("rootScalar: " + rootScalar); |
| 288 |
System.err.println("\t" + logLike + " " + (logLike + remainder)); |
| 289 |
if (incrementOuterProducts) { |
| 290 |
System.err.println("Outer-products:" + wrap(outerProducts, dimTrait * dimTrait * trait, dimTrait, dimTrait)); |
| 291 |
} |
| 292 |
System.err.println(""); |
| 293 |
} |
| 294 |
|
| 295 |
rootOffset += dimPartialForTrait; |
| 296 |
priorOffset += dimPartialForTrait; |
| 297 |
} |
| 298 |
|
| 299 |
if (DEBUG) { |
| 300 |
System.err.println("End"); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
@Override |
| 305 |
public void updatePreOrderPartials(final int[] operations, int operationCount) { |
| 306 |
|
| 307 |
if (DEBUG) { |
| 308 |
System.err.println("Pre-order operations:"); |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
throw new RuntimeException("Not yet implemented"); |
| 313 |
} |
| 314 |
|
| 315 |
@Override |
| 316 |
public void updatePostOrderPartials(final int[] operations, int operationCount, boolean incrementOuterProducts) { |
| 317 |
|
| 318 |
if (DEBUG) { |
| 319 |
System.err.println("Post-order operations:"); |
| 320 |
} |
| 321 |
|
| 322 |
int offset = 0; |
| 323 |
for (int op = 0; op < operationCount; ++op) { |
| 324 |
|
| 325 |
if (DEBUG) { |
| 326 |
System.err.println("\t" + getOperationString(operations, offset)); |
| 327 |
} |
| 328 |
|
| 329 |
updatePartial( |
| 330 |
operations[offset ], |
| 331 |
operations[offset + 1], |
| 332 |
operations[offset + 2], |
| 333 |
operations[offset + 3], |
| 334 |
operations[offset + 4], |
| 335 |
incrementOuterProducts |
| 336 |
); |
| 337 |
|
| 338 |
offset += ContinuousDiffusionIntegrator.OPERATION_TUPLE_SIZE; |
| 339 |
} |
| 340 |
|
| 341 |
if (DEBUG) { |
| 342 |
System.err.println("End"); |
| 343 |
System.err.println(""); |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
@Override |
| 348 |
public void updateBrownianDiffusionMatrices(int precisionIndex, final int[] probabilityIndices, |
| 349 |
final double[] edgeLengths, final double[] driftRates, |
| 350 |
int updateCount) { |
| 351 |
|
| 352 |
if (DEBUG) { |
| 353 |
System.err.println("Matrices (basic):"); |
| 354 |
} |
| 355 |
|
| 356 |
for (int up = 0; up < updateCount; ++up) { |
| 357 |
|
| 358 |
if (DEBUG) { |
| 359 |
System.err.println("\t" + probabilityIndices[up] + " <- " + edgeLengths[up]); |
| 360 |
} |
| 361 |
|
| 362 |
// TODO Currently only written for SCALAR model |
| 363 |
branchLengths[dimMatrix * probabilityIndices[up]] = edgeLengths[up]; // TODO Remove dimMatrix |
| 364 |
} |
| 365 |
|
| 366 |
precisionOffset = dimTrait * dimTrait * precisionIndex; |
| 367 |
precisionLogDet = determinants[precisionIndex]; |
| 368 |
} |
| 369 |
|
| 370 |
@Override |
| 371 |
public InstanceDetails getDetails() { |
| 372 |
return details; |
| 373 |
} |
| 374 |
|
| 375 |
// Internal storage |
| 376 |
double[] partials; |
| 377 |
|
| 378 |
double[] branchLengths; |
| 379 |
double[] variances; |
| 380 |
double[] precisions; |
| 381 |
|
| 382 |
double[] remainders; |
| 383 |
double[] diffusions; |
| 384 |
double[] determinants; |
| 385 |
int[] degreesOfFreedom; |
| 386 |
double[] outerProducts; |
| 387 |
double[] prePartials; |
| 388 |
|
| 389 |
// Set during updateDiffusionMatrices() and used in updatePartials() |
| 390 |
int precisionOffset; |
| 391 |
double precisionLogDet; |
| 392 |
|
| 393 |
static final boolean INLINE = true; |
| 394 |
|
| 395 |
@Override |
| 396 |
public void updatePreOrderPartial( |
| 397 |
final int kBuffer, // parent |
| 398 |
final int iBuffer, // node |
| 399 |
final int iMatrix, |
| 400 |
final int jBuffer, // sibling |
| 401 |
final int jMatrix) { |
| 402 |
|
| 403 |
// Determine buffer offsets |
| 404 |
int kbo = dimPartial * kBuffer; |
| 405 |
int ibo = dimPartial * iBuffer; |
| 406 |
int jbo = dimPartial * jBuffer; |
| 407 |
|
| 408 |
// Determine matrix offsets |
| 409 |
final int imo = dimMatrix * iMatrix; |
| 410 |
final int jmo = dimMatrix * jMatrix; |
| 411 |
|
| 412 |
// Read variance increments along descendent branches of k |
| 413 |
final double vi = branchLengths[imo]; |
| 414 |
final double vj = branchLengths[jmo]; |
| 415 |
// |
| 416 |
// final DenseMatrix64F Vd = wrap(inverseDiffusions, precisionOffset, dimTrait, dimTrait); |
| 417 |
// |
| 418 |
if (DEBUG) { |
| 419 |
System.err.println("updatePreOrderPartial for node " + iBuffer); |
| 420 |
System.err.println("\tvi: " + vi + " vj: " + vj); |
| 421 |
} |
| 422 |
|
| 423 |
// For each trait // TODO in parallel |
| 424 |
for (int trait = 0; trait < numTraits; ++trait) { |
| 425 |
|
| 426 |
// A. Get current precision of k and j |
| 427 |
final double pk = prePartials[kbo + dimTrait]; |
| 428 |
final double pj = partials[jbo + dimTrait]; |
| 429 |
|
| 430 |
// final DenseMatrix64F Pk = wrap(prePartials, kbo + dimTrait, dimTrait, dimTrait); |
| 431 |
// // final DenseMatrix64F Pj = wrap(partials, jbo + dimTrait, dimTrait, dimTrait); |
| 432 |
// |
| 433 |
// // final DenseMatrix64F Vk = wrap(prePartials, kbo + dimTrait + dimTrait * dimTrait, dimTrait, dimTrait); |
| 434 |
// final DenseMatrix64F Vj = wrap(partials, jbo + dimTrait + dimTrait * dimTrait, dimTrait, dimTrait); |
| 435 |
// |
| 436 |
|
| 437 |
// B. Inflate variance along sibling branch using matrix inversion |
| 438 |
final double pjp = Double.isInfinite(pj) ? |
| 439 |
1.0 / vj : pj / (1.0 + pj * vj); |
| 440 |
|
| 441 |
// // final DenseMatrix64F Vjp = new DenseMatrix64F(dimTrait, dimTrait); |
| 442 |
// final DenseMatrix64F Vjp = matrix0; |
| 443 |
// CommonOps.add(Vj, vj, Vd, Vjp); |
| 444 |
// |
| 445 |
// // final DenseMatrix64F Pjp = new DenseMatrix64F(dimTrait, dimTrait); |
| 446 |
// final DenseMatrix64F Pjp = matrix1; |
| 447 |
// InversionResult cj = safeInvert(Vjp, Pjp, false); |
| 448 |
// |
| 449 |
// // final DenseMatrix64F Pip = new DenseMatrix64F(dimTrait, dimTrait); |
| 450 |
// final DenseMatrix64F Pip = matrix2; |
| 451 |
// CommonOps.add(Pk, Pjp, Pip); |
| 452 |
|
| 453 |
final double pip = pjp + pk; |
| 454 |
// |
| 455 |
// // final DenseMatrix64F Vip = new DenseMatrix64F(dimTrait, dimTrait); |
| 456 |
// final DenseMatrix64F Vip = matrix3; |
| 457 |
// InversionResult cip = safeInvert(Pip, Vip, false); |
| 458 |
// |
| 459 |
// // C. Compute prePartial mean |
| 460 |
// // final double[] tmp = new double[dimTrait]; |
| 461 |
// final double[] tmp = vector0; |
| 462 |
// for (int g = 0; g < dimTrait; ++g) { |
| 463 |
// double sum = 0.0; |
| 464 |
// for (int h = 0; h < dimTrait; ++h) { |
| 465 |
// sum += Pk.unsafe_get(g, h) * prePartials[kbo + h]; // Read parent |
| 466 |
// sum += Pjp.unsafe_get(g, h) * partials[jbo + h]; // Read sibling |
| 467 |
// } |
| 468 |
// tmp[g] = sum; |
| 469 |
// } |
| 470 |
// for (int g = 0; g < dimTrait; ++g) { |
| 471 |
// double sum = 0.0; |
| 472 |
// for (int h = 0; h < dimTrait; ++h) { |
| 473 |
// sum += Vip.unsafe_get(g, h) * tmp[h]; |
| 474 |
// } |
| 475 |
// prePartials[ibo + g] = sum; // Write node |
| 476 |
// } |
| 477 |
|
| 478 |
for (int g = 0; g < dimTrait; ++g) { |
| 479 |
prePartials[ibo + g] = (pk * prePartials[kbo + g] + pjp * partials[jbo + g]) / pip; |
| 480 |
} |
| 481 |
|
| 482 |
// C. Inflate variance along node branch |
| 483 |
|
| 484 |
final double pi = Double.isInfinite(pip) ? |
| 485 |
1.0 / vi : pip / (1.0 + pip * vi); |
| 486 |
|
| 487 |
// final DenseMatrix64F Vi = Vip; |
| 488 |
// CommonOps.add(vi, Vd, Vip, Vi); |
| 489 |
// |
| 490 |
// // final DenseMatrix64F Pi = new DenseMatrix64F(dimTrait, dimTrait); |
| 491 |
// final DenseMatrix64F Pi = matrix4; |
| 492 |
// InversionResult ci = safeInvert(Vi, Pi, false); |
| 493 |
// |
| 494 |
// // X. Store precision results for node |
| 495 |
|
| 496 |
prePartials[ibo + dimTrait] = pi; |
| 497 |
|
| 498 |
// unwrap(Pi, prePartials, ibo + dimTrait); |
| 499 |
// unwrap(Vi, prePartials, ibo + dimTrait + dimTrait * dimTrait); |
| 500 |
// |
| 501 |
if (DEBUG) { |
| 502 |
System.err.println("trait: " + trait); |
| 503 |
System.err.println("pM: " + new WrappedVector.Raw(prePartials, kbo, dimTrait)); |
| 504 |
System.err.println("pk: " + pk); |
| 505 |
System.err.println("sM: " + new WrappedVector.Raw(partials, jbo, dimTrait)); |
| 506 |
System.err.println("sV: " + vj); |
| 507 |
// System.err.println("sVp: " + Vjp); |
| 508 |
System.err.println("sPp: " + pjp); |
| 509 |
System.err.println("Pip: " + pip); |
| 510 |
System.err.println("cM: " + new WrappedVector.Raw(prePartials, ibo, dimTrait)); |
| 511 |
System.err.println("cV: " + pi); |
| 512 |
} |
| 513 |
// |
| 514 |
// Get ready for next trait |
| 515 |
kbo += dimPartialForTrait; |
| 516 |
ibo += dimPartialForTrait; |
| 517 |
jbo += dimPartialForTrait; |
| 518 |
} |
| 519 |
// throw new RuntimeException("Not yet implemented"); |
| 520 |
} |
| 521 |
|
| 522 |
protected void updatePartial( |
| 523 |
final int kBuffer, |
| 524 |
final int iBuffer, |
| 525 |
final int iMatrix, |
| 526 |
final int jBuffer, |
| 527 |
final int jMatrix, |
| 528 |
final boolean incrementOuterProducts |
| 529 |
) { |
| 530 |
// Determine buffer offsets |
| 531 |
int kbo = dimPartial * kBuffer; |
| 532 |
int ibo = dimPartial * iBuffer; |
| 533 |
int jbo = dimPartial * jBuffer; |
| 534 |
|
| 535 |
// Determine matrix offsets |
| 536 |
final int imo = dimMatrix * iMatrix; |
| 537 |
final int jmo = dimMatrix * jMatrix; |
| 538 |
|
| 539 |
// Read variance increments along descendent branches of k |
| 540 |
final double vi = branchLengths[imo]; |
| 541 |
final double vj = branchLengths[jmo]; |
| 542 |
|
| 543 |
if (DEBUG) { |
| 544 |
System.err.println("i:"); |
| 545 |
System.err.println("\tvar : " + branchLengths[imo]); |
| 546 |
} |
| 547 |
|
| 548 |
// For each trait // TODO in parallel |
| 549 |
for (int trait = 0; trait < numTraits; ++trait) { |
| 550 |
|
| 551 |
// Increase variance along the branches i -> k and j -> k |
| 552 |
|
| 553 |
// A. Get current precision of i and j |
| 554 |
final double pi = partials[ibo + dimTrait]; |
| 555 |
final double pj = partials[jbo + dimTrait]; |
| 556 |
|
| 557 |
// B. Integrate along branch using two matrix inversions |
| 558 |
final double pip = Double.isInfinite(pi) ? |
| 559 |
1.0 / vi : pi / (1.0 + pi * vi); |
| 560 |
final double pjp = Double.isInfinite(pj) ? |
| 561 |
1.0 / vj : pj / (1.0 + pj * vj); |
| 562 |
|
| 563 |
// Compute partial mean and precision at node k |
| 564 |
|
| 565 |
// A. Partial precision scalar |
| 566 |
final double pk = pip + pjp; |
| 567 |
|
| 568 |
// B. Partial mean |
| 569 |
if (INLINE) { |
| 570 |
// For each dimension // TODO in parallel |
| 571 |
for (int g = 0; g < dimTrait; ++g) { |
| 572 |
partials[kbo + g] = (pip * partials[ibo + g] + pjp * partials[jbo + g]) / pk; |
| 573 |
} |
| 574 |
} else { |
| 575 |
updateMean(partials, kbo, ibo, jbo, pip, pjp, pk, dimTrait); |
| 576 |
} |
| 577 |
|
| 578 |
// C. Store precision |
| 579 |
partials[kbo + dimTrait] = pk; |
| 580 |
|
| 581 |
if (DEBUG) { |
| 582 |
System.err.println("\ttrait: " + trait); |
| 583 |
//System.err.println("\t\tprec: " + pi); |
| 584 |
System.err.print("\t\tmean i:"); |
| 585 |
for (int e = 0; e < dimTrait; ++e) { |
| 586 |
System.err.print(" " + partials[ibo + e]); |
| 587 |
} |
| 588 |
System.err.println(" prec i: " + pi); |
| 589 |
System.err.print("\t\tmean j:"); |
| 590 |
for (int e = 0; e < dimTrait; ++e) { |
| 591 |
System.err.print(" " + partials[jbo + e]); |
| 592 |
} |
| 593 |
System.err.println(" prec j: " + pj); |
| 594 |
|
| 595 |
if (pj == 0.0) { System.exit(-1); } |
| 596 |
System.err.print("\t\tmean k:"); |
| 597 |
for (int e = 0; e < dimTrait; ++e) { |
| 598 |
System.err.print(" " + partials[kbo + e]); |
| 599 |
} |
| 600 |
System.err.println(" prec k: " + pk); |
| 601 |
System.err.println(""); |
| 602 |
} |
| 603 |
|
| 604 |
// Computer remainder at node k |
| 605 |
double remainder = 0.0; |
| 606 |
if (pi != 0 && pj != 0) { |
| 607 |
|
| 608 |
// TODO Suspect this is very inefficient, since SSi and SSj were already computed when k = i or j |
| 609 |
|
| 610 |
final double remainderPrecision = pip * pjp / pk; |
| 611 |
|
| 612 |
// Inner products |
| 613 |
double SSk = 0; |
| 614 |
double SSj = 0; |
| 615 |
double SSi = 0; |
| 616 |
|
| 617 |
int pob = precisionOffset; |
| 618 |
|
| 619 |
// vector-matrix-vector TODO in parallel |
| 620 |
for (int g = 0; g < dimTrait; ++g) { |
| 621 |
final double ig = partials[ibo + g]; |
| 622 |
final double jg = partials[jbo + g]; |
| 623 |
final double kg = partials[kbo + g]; |
| 624 |
|
| 625 |
for (int h = 0; h < dimTrait; ++h) { |
| 626 |
final double ih = partials[ibo + h]; |
| 627 |
final double jh = partials[jbo + h]; |
| 628 |
final double kh = partials[kbo + h]; |
| 629 |
|
| 630 |
final double diffusion = diffusions[pob]; // element [g][h] |
| 631 |
|
| 632 |
SSi += ig * diffusion * ih; |
| 633 |
SSj += jg * diffusion * jh; |
| 634 |
SSk += kg * diffusion * kh; |
| 635 |
|
| 636 |
++pob; |
| 637 |
} |
| 638 |
} |
| 639 |
|
| 640 |
remainder += -dimTrait * LOG_SQRT_2_PI // TODO Can move some calculation outside the loop |
| 641 |
+ 0.5 * (dimTrait * Math.log(remainderPrecision) + precisionLogDet) |
| 642 |
- 0.5 * (pip * SSi + pjp * SSj - pk * SSk); |
| 643 |
|
| 644 |
if (DEBUG) { |
| 645 |
System.err.println("\t\t\tpk: " + pk); |
| 646 |
System.err.println("\t\t\tSSi = " + (pip * SSi)); |
| 647 |
System.err.println("\t\t\tSSj = " + (pjp * SSj)); |
| 648 |
System.err.println("\t\t\tSSk = " + (pk * SSk)); |
| 649 |
} |
| 650 |
|
| 651 |
|
| 652 |
if (DEBUG) { |
| 653 |
System.err.println("\t\tremainder: " + remainder); |
| 654 |
} |
| 655 |
|
| 656 |
if (incrementOuterProducts) { |
| 657 |
int opo = dimTrait * dimTrait * trait; |
| 658 |
|
| 659 |
// final double remainderPrecision = pip * pjp / (pip + pjp); |
| 660 |
|
| 661 |
if (DEBUG) { |
| 662 |
System.err.println("pip: " + pip); |
| 663 |
System.err.println("pjp: " + pjp); |
| 664 |
System.err.println("sum: " + (pip + pjp)); |
| 665 |
System.err.println("op prec: " + remainderPrecision); |
| 666 |
} |
| 667 |
|
| 668 |
for (int g = 0; g < dimTrait; ++g) { |
| 669 |
final double ig = partials[ibo + g]; |
| 670 |
final double jg = partials[jbo + g]; |
| 671 |
|
| 672 |
for (int h = 0; h < dimTrait; ++h) { |
| 673 |
final double ih = partials[ibo + h]; |
| 674 |
final double jh = partials[jbo + h]; |
| 675 |
|
| 676 |
outerProducts[opo] += (ig - jg) * (ih - jh) * remainderPrecision; |
| 677 |
++opo; |
| 678 |
} |
| 679 |
} |
| 680 |
|
| 681 |
if (DEBUG) { |
| 682 |
System.err.println("Outer-products:" + wrap(outerProducts, dimTrait * dimTrait * trait, dimTrait, dimTrait)); |
| 683 |
} |
| 684 |
|
| 685 |
degreesOfFreedom[trait] += 1; // incremenent degrees-of-freedom |
| 686 |
} |
| 687 |
} // End if remainder |
| 688 |
|
| 689 |
// Accumulate remainder up tree and store |
| 690 |
|
| 691 |
remainders[kBuffer * numTraits + trait] = remainder |
| 692 |
+ remainders[iBuffer * numTraits + trait] + remainders[jBuffer * numTraits + trait]; |
| 693 |
|
| 694 |
// Get ready for next trait |
| 695 |
kbo += dimPartialForTrait; |
| 696 |
ibo += dimPartialForTrait; |
| 697 |
jbo += dimPartialForTrait; |
| 698 |
|
| 699 |
} |
| 700 |
} |
| 701 |
|
| 702 |
private static void updateMean(final double[] partials, |
| 703 |
final int kob, |
| 704 |
final int iob, |
| 705 |
final int job, |
| 706 |
final double pip, |
| 707 |
final double pjp, |
| 708 |
final double pk, |
| 709 |
final int dimTrait) { |
| 710 |
for (int g = 0; g < dimTrait; ++g) { |
| 711 |
partials[kob + g] = (pip * partials[iob + g] + pjp * partials[job + g]) / pk; |
| 712 |
} |
| 713 |
} |
| 714 |
|
| 715 |
private void allocateStorage() { |
| 716 |
partials = new double[dimPartial * bufferCount]; |
| 717 |
branchLengths = new double[dimMatrix * bufferCount]; // TODO Should be just bufferCount |
| 718 |
// variances = new double[dimMatrix * bufferCount]; // TODO Should be dimTrait * dimTrait |
| 719 |
remainders = new double[numTraits * bufferCount]; |
| 720 |
|
| 721 |
diffusions = new double[dimTrait * dimTrait * diffusionCount]; |
| 722 |
determinants = new double[diffusionCount]; |
| 723 |
|
| 724 |
degreesOfFreedom = new int[numTraits]; |
| 725 |
outerProducts = new double[dimTrait * dimTrait * numTraits]; |
| 726 |
|
| 727 |
prePartials = new double[dimPartial * bufferCount]; |
| 728 |
} |
| 729 |
|
| 730 |
private String getOperationString(final int[] operations, final int offset) { |
| 731 |
StringBuilder sb = new StringBuilder("op:"); |
| 732 |
for (int i = 0; i < ContinuousDiffusionIntegrator.OPERATION_TUPLE_SIZE; ++i) { |
| 733 |
sb.append(" ").append(operations[offset + i]); |
| 734 |
} |
| 735 |
return sb.toString(); |
| 736 |
} |
| 737 |
|
| 738 |
private static boolean DEBUG = false; |
| 739 |
} |
| 740 |
|
| 741 |
} |