| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
package dr.evomodel.operators; |
| 37 |
|
| 38 |
import dr.evolution.tree.MutableTreeModel; |
| 39 |
import dr.evolution.tree.NodeRef; |
| 40 |
import dr.evomodel.continuous.FullyConjugateMultivariateTraitLikelihood; |
| 41 |
import dr.evomodel.continuous.LatentTruncation; |
| 42 |
import dr.inference.model.CompoundParameter; |
| 43 |
import dr.inference.model.MatrixParameter; |
| 44 |
import dr.inference.model.Parameter; |
| 45 |
import dr.inference.operators.MCMCOperator; |
| 46 |
import dr.inference.operators.SimpleMCMCOperator; |
| 47 |
import dr.math.MathUtils; |
| 48 |
import dr.math.distributions.MultivariateNormalDistribution; |
| 49 |
import dr.math.distributions.NormalDistribution; |
| 50 |
import dr.math.matrixAlgebra.IllegalDimension; |
| 51 |
import dr.math.matrixAlgebra.SymmetricMatrix; |
| 52 |
import dr.math.matrixAlgebra.Vector; |
| 53 |
import dr.xml.*; |
| 54 |
import dr.math.matrixAlgebra.Matrix; |
| 55 |
|
| 56 |
|
| 57 |
import java.util.logging.Logger; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public class LatentLiabilityGibbs extends SimpleMCMCOperator { |
| 64 |
|
| 65 |
|
| 66 |
public static final String LATENT_LIABILITY_GIBBS_OPERATOR = "latentLiabilityGibbsOperator"; |
| 67 |
public static final String TREE_MODEL = "treeModel"; |
| 68 |
|
| 69 |
|
| 70 |
private final LatentTruncation latentLiability; |
| 71 |
|
| 72 |
|
| 73 |
private final FullyConjugateMultivariateTraitLikelihood traitModel; |
| 74 |
private final CompoundParameter tipTraitParameter; |
| 75 |
|
| 76 |
|
| 77 |
protected double[] rootPriorMean; |
| 78 |
protected double rootPriorSampleSize; |
| 79 |
|
| 80 |
private final MatrixParameter precisionParam; |
| 81 |
private final MutableTreeModel treeModel; |
| 82 |
private final int dim; |
| 83 |
|
| 84 |
public double[][] postMeans; |
| 85 |
public double[][] preMeans; |
| 86 |
public double[] preP; |
| 87 |
public double[] postP; |
| 88 |
private Parameter mask; |
| 89 |
private boolean hasMask=false; |
| 90 |
|
| 91 |
|
| 92 |
private int numFixed = 0; |
| 93 |
private int numUpdate = 0; |
| 94 |
private int[] doUpdate; |
| 95 |
private int[] dontUpdate; |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public LatentLiabilityGibbs( |
| 100 |
FullyConjugateMultivariateTraitLikelihood traitModel, |
| 101 |
LatentTruncation LatentLiability, CompoundParameter tipTraitParameter, Parameter mask, |
| 102 |
double weight) { |
| 103 |
super(); |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
this.latentLiability = LatentLiability; |
| 110 |
this.traitModel = traitModel; |
| 111 |
this.tipTraitParameter = tipTraitParameter; |
| 112 |
|
| 113 |
|
| 114 |
this.rootPriorMean = traitModel.getPriorMean(); |
| 115 |
this.rootPriorSampleSize = traitModel.getPriorSampleSize(); |
| 116 |
|
| 117 |
this.precisionParam = (MatrixParameter) traitModel.getDiffusionModel().getPrecisionParameter(); |
| 118 |
this.treeModel = traitModel.getTreeModel(); |
| 119 |
dim = precisionParam.getRowDimension(); |
| 120 |
|
| 121 |
this.mask=mask; |
| 122 |
if(mask!=null) { |
| 123 |
hasMask=true; |
| 124 |
|
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
postMeans = new double[treeModel.getNodeCount()][dim]; |
| 129 |
preMeans = new double[treeModel.getNodeCount()][dim]; |
| 130 |
preP = new double[treeModel.getNodeCount()]; |
| 131 |
postP = new double[treeModel.getNodeCount()]; |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
dontUpdate=new int[dim]; |
| 137 |
doUpdate=new int[dim]; |
| 138 |
|
| 139 |
if (hasMask) { |
| 140 |
for (int i = 0; i < dim; i++) { |
| 141 |
if (mask.getParameterValue(i) == 0.0) { |
| 142 |
dontUpdate[numFixed] = i; |
| 143 |
numFixed++; |
| 144 |
} else { |
| 145 |
doUpdate[numUpdate] = i; |
| 146 |
numUpdate++; |
| 147 |
} |
| 148 |
|
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
setWeight(weight); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
public int getStepCount() { |
| 157 |
return 1; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
private void printInformation(MatrixParameter par) { |
| 162 |
StringBuffer sb = new StringBuffer("\n \n parameter \n"); |
| 163 |
for (int j = 0; j < dim; j++) { |
| 164 |
sb.append(par.getParameterValue(0, j)); |
| 165 |
} |
| 166 |
|
| 167 |
Logger.getLogger("dr.evomodel").info(sb.toString()); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
private void printInformation(double[] par) { |
| 174 |
StringBuffer sb = new StringBuffer("\n \n double vector \n"); |
| 175 |
for (int j = 0; j < treeModel.getNodeCount(); j++) { |
| 176 |
sb.append(par[j]); |
| 177 |
} |
| 178 |
Logger.getLogger("dr.evomodel").info(sb.toString()); |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
private void printInformation(double[][] par) { |
| 186 |
StringBuffer sb = new StringBuffer("\n \n double matrix \n"); |
| 187 |
for (int i = 0; i < 1; i++) { |
| 188 |
for (int j = 0; j < treeModel.getNodeCount(); j++) { |
| 189 |
|
| 190 |
sb.append(par[j][i]); |
| 191 |
} |
| 192 |
} |
| 193 |
Logger.getLogger("dr.evomodel").info(sb.toString()); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
private void printInformation(double par) { |
| 198 |
StringBuffer sb = new StringBuffer("\n \n double \n"); |
| 199 |
|
| 200 |
sb.append(par); |
| 201 |
|
| 202 |
Logger.getLogger("dr.evomodel").info(sb.toString()); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
private void printInformation(double par, String lala) { |
| 207 |
StringBuffer sb = new StringBuffer("\n"); |
| 208 |
|
| 209 |
sb.append(lala); |
| 210 |
sb.append("\t\t"); |
| 211 |
sb.append(par); |
| 212 |
|
| 213 |
Logger.getLogger("dr.evomodel").info(sb.toString()); |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
public double doOperation() { |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
final int pos = MathUtils.nextInt(treeModel.getExternalNodeCount()); |
| 229 |
|
| 230 |
|
| 231 |
NodeRef node = treeModel.getExternalNode(pos); |
| 232 |
|
| 233 |
|
| 234 |
double logq = sampleNode2(node); |
| 235 |
|
| 236 |
|
| 237 |
tipTraitParameter.fireParameterChangedEvent(); |
| 238 |
|
| 239 |
return logq; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
public void doPostOrderTraversal(NodeRef node) { |
| 245 |
|
| 246 |
final int thisNumber = node.getNumber(); |
| 247 |
|
| 248 |
if (treeModel.isExternal(node)) { |
| 249 |
|
| 250 |
|
| 251 |
double[] traitValue = getNodeTrait(node); |
| 252 |
|
| 253 |
for (int j = 0; j < dim; j++) { |
| 254 |
postMeans[thisNumber][j] = traitValue[j]; |
| 255 |
} |
| 256 |
|
| 257 |
postP[thisNumber] = 1 / traitModel.getRescaledBranchLengthForPrecision(node); |
| 258 |
|
| 259 |
|
| 260 |
return; |
| 261 |
|
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
final NodeRef childNode0 = treeModel.getChild(node, 0); |
| 266 |
final NodeRef childNode1 = treeModel.getChild(node, 1); |
| 267 |
|
| 268 |
doPostOrderTraversal(childNode0); |
| 269 |
doPostOrderTraversal(childNode1); |
| 270 |
|
| 271 |
|
| 272 |
if (!treeModel.isRoot(node)) { |
| 273 |
|
| 274 |
final int childNumber0 = childNode0.getNumber(); |
| 275 |
final int childNumber1 = childNode1.getNumber(); |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
final double precision0 = postP[childNumber0]; |
| 280 |
final double precision1 = postP[childNumber1]; |
| 281 |
final double thisPrecision = 1 / traitModel.getRescaledBranchLengthForPrecision(node); |
| 282 |
double tp = precision0 + precision1; |
| 283 |
postP[thisNumber] = tp * thisPrecision / (tp + thisPrecision); |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
for (int j = 0; j < dim; j++) { |
| 288 |
postMeans[thisNumber][j] = (precision0 * postMeans[childNumber0][j] + precision1 * postMeans[childNumber1][j]) / (precision0 + precision1); |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
public double[] getNodeTrait(NodeRef node) { |
| 296 |
int index = node.getNumber(); |
| 297 |
double[] traitValue = tipTraitParameter.getParameter(index).getParameterValues(); |
| 298 |
return traitValue; |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
public double getNodeTrait(NodeRef node, int entry) { |
| 303 |
int index = node.getNumber(); |
| 304 |
double traitValue = tipTraitParameter.getParameter(index).getParameterValue(entry); |
| 305 |
return traitValue; |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
public void setNodeTrait(NodeRef node, double[] traitValue) { |
| 310 |
int index = node.getNumber(); |
| 311 |
for (int i = 0; i < dim; i++) { |
| 312 |
|
| 313 |
tipTraitParameter.getParameter(index).setParameterValue(i, traitValue[i]); |
| 314 |
} |
| 315 |
traitModel.getTraitParameter().getParameter(index).fireParameterChangedEvent(); |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
public void setNodeTrait(NodeRef node, int entry, double traitValue) { |
| 320 |
int index = node.getNumber(); |
| 321 |
|
| 322 |
tipTraitParameter.getParameter(index).setParameterValue(entry, traitValue); |
| 323 |
|
| 324 |
|
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
public void doPreOrderTraversal(NodeRef node) { |
| 332 |
|
| 333 |
|
| 334 |
final int thisNumber = node.getNumber(); |
| 335 |
|
| 336 |
|
| 337 |
if (treeModel.isRoot(node)) { |
| 338 |
preP[thisNumber] = rootPriorSampleSize; |
| 339 |
for (int j = 0; j < dim; j++) { |
| 340 |
preMeans[thisNumber][j] = rootPriorMean[j]; |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
} else { |
| 345 |
|
| 346 |
final NodeRef parentNode = treeModel.getParent(node); |
| 347 |
final NodeRef sibNode = getSisterNode(node); |
| 348 |
|
| 349 |
final int parentNumber = parentNode.getNumber(); |
| 350 |
final int sibNumber = sibNode.getNumber(); |
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
final double precisionParent = preP[parentNumber]; |
| 374 |
final double precisionSib = postP[sibNumber]; |
| 375 |
final double thisPrecision = 1 / traitModel.getRescaledBranchLengthForPrecision(node); |
| 376 |
double tp = precisionParent + precisionSib; |
| 377 |
preP[thisNumber] = tp * thisPrecision / (tp + thisPrecision); |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
for (int j = 0; j < dim; j++) { |
| 382 |
preMeans[thisNumber][j] = (precisionParent * preMeans[parentNumber][j] + precisionSib * postMeans[sibNumber][j]) / (precisionParent + precisionSib); |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
if (treeModel.isExternal(node)) { |
| 387 |
return; |
| 388 |
} else { |
| 389 |
doPreOrderTraversal(treeModel.getChild(node, 0)); |
| 390 |
doPreOrderTraversal(treeModel.getChild(node, 1)); |
| 391 |
|
| 392 |
} |
| 393 |
|
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
public NodeRef getSisterNode(NodeRef node) { |
| 398 |
NodeRef sib0 = treeModel.getChild(treeModel.getParent(node), 0); |
| 399 |
NodeRef sib1 = treeModel.getChild(treeModel.getParent(node), 1); |
| 400 |
|
| 401 |
|
| 402 |
if (sib0 == node) { |
| 403 |
return sib1; |
| 404 |
} else return sib0; |
| 405 |
|
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
public double sampleNode(NodeRef node) { |
| 410 |
|
| 411 |
final int thisNumber = node.getNumber(); |
| 412 |
double[] traitValue = getNodeTrait(node); |
| 413 |
|
| 414 |
|
| 415 |
double[] mean = new double[dim]; |
| 416 |
for (int i = 0; i < dim; i++) { |
| 417 |
mean[i] = preMeans[thisNumber][i]; |
| 418 |
} |
| 419 |
|
| 420 |
double p = preP[thisNumber]; |
| 421 |
|
| 422 |
double[][] thisP = new double[dim][dim]; |
| 423 |
|
| 424 |
for (int i = 0; i < dim; i++) { |
| 425 |
for (int j = 0; j < dim; j++) { |
| 426 |
|
| 427 |
thisP[i][j] = p * precisionParam.getParameterValue(i, j); |
| 428 |
|
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
int entry = MathUtils.nextInt(dim); |
| 469 |
|
| 470 |
double thisMean = getConditionalMean(entry, thisP, traitValue, mean); |
| 471 |
|
| 472 |
double SD = Math.sqrt(1 / thisP[entry][entry]); |
| 473 |
|
| 474 |
|
| 475 |
double oldValue = getNodeTrait(node, entry); |
| 476 |
|
| 477 |
|
| 478 |
double value = MathUtils.nextGaussian(); |
| 479 |
value *= SD; |
| 480 |
value += thisMean; |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
NormalDistribution distribution = new NormalDistribution(thisMean, SD); |
| 488 |
|
| 489 |
|
| 490 |
double pOld = distribution.logPdf(oldValue); |
| 491 |
|
| 492 |
|
| 493 |
double pNew = distribution.logPdf(value); |
| 494 |
|
| 495 |
|
| 496 |
setNodeTrait(node, entry, value); |
| 497 |
|
| 498 |
|
| 499 |
double logq = pOld - pNew; |
| 500 |
|
| 501 |
|
| 502 |
traitModel.getTraitParameter().getParameter(thisNumber).fireParameterChangedEvent(); |
| 503 |
|
| 504 |
|
| 505 |
return logq; |
| 506 |
|
| 507 |
|
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
public double sampleNode2(NodeRef node) { |
| 513 |
|
| 514 |
final int thisNumber = node.getNumber(); |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
double[] mean=traitModel.getConditionalMean(thisNumber); |
| 536 |
double[][] thisP=traitModel.getConditionalPrecision(thisNumber); |
| 537 |
|
| 538 |
|
| 539 |
double[] oldValue = getNodeTrait(node); |
| 540 |
double[] value = oldValue; |
| 541 |
|
| 542 |
int attempt = 0; |
| 543 |
boolean validTip = false; |
| 544 |
|
| 545 |
if(hasMask) { |
| 546 |
|
| 547 |
double[] oldCompVal =new double[numUpdate]; |
| 548 |
double[] newValue =new double[numUpdate]; |
| 549 |
double[][] condP=new double[numUpdate][numUpdate]; |
| 550 |
|
| 551 |
for (int i=0; i<numUpdate;i++){ |
| 552 |
oldCompVal[i]= value[doUpdate[i]]; |
| 553 |
for (int j=0;j<numUpdate;j++){ |
| 554 |
condP[i][j]= thisP[doUpdate[i]][doUpdate[j]]; |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
double[] condMean = getComponentConditionalMean(thisP, oldValue, mean, condP); |
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
MultivariateNormalDistribution distribution = new MultivariateNormalDistribution(condMean, condP); |
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
while (!validTip & attempt < 10000) { |
| 576 |
newValue = distribution.nextMultivariateNormal(); |
| 577 |
|
| 578 |
for(int i=0; i<numUpdate; i++){ |
| 579 |
value[doUpdate[i]]=newValue[i]; |
| 580 |
} |
| 581 |
|
| 582 |
setNodeTrait(node, value); |
| 583 |
|
| 584 |
if (latentLiability.validTraitForTip(thisNumber)) { |
| 585 |
validTip = true; |
| 586 |
|
| 587 |
} |
| 588 |
attempt++; |
| 589 |
} |
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
double pOld = distribution.logPdf(oldCompVal); |
| 603 |
|
| 604 |
double pNew = distribution.logPdf(newValue); |
| 605 |
|
| 606 |
double logq= pOld -pNew; |
| 607 |
|
| 608 |
|
| 609 |
traitModel.getTraitParameter().getParameter(thisNumber).fireParameterChangedEvent(); |
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
return logq; |
| 615 |
|
| 616 |
}else { |
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
MultivariateNormalDistribution distribution = new MultivariateNormalDistribution(mean, thisP); |
| 622 |
|
| 623 |
|
| 624 |
while (!validTip & attempt < 10000) { |
| 625 |
|
| 626 |
value = distribution.nextMultivariateNormal(); |
| 627 |
|
| 628 |
setNodeTrait(node, value); |
| 629 |
|
| 630 |
if (latentLiability.validTraitForTip(thisNumber)) { |
| 631 |
validTip = true; |
| 632 |
} |
| 633 |
attempt++; |
| 634 |
} |
| 635 |
|
| 636 |
|
| 637 |
double pOld = distribution.logPdf(oldValue); |
| 638 |
|
| 639 |
double pNew = distribution.logPdf(value); |
| 640 |
|
| 641 |
|
| 642 |
double logq = pOld - pNew; |
| 643 |
|
| 644 |
|
| 645 |
traitModel.getTraitParameter().getParameter(thisNumber).fireParameterChangedEvent(); |
| 646 |
|
| 647 |
|
| 648 |
return logq; |
| 649 |
|
| 650 |
} |
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
} |
| 657 |
|
| 658 |
|
| 659 |
private double[] getComponentConditionalMean(double[][] thisP, double[] oldValue, double[] mean, double[][] condP){ |
| 660 |
|
| 661 |
double[] condMean =new double[numUpdate]; |
| 662 |
|
| 663 |
double[][] H =new double[numUpdate][numFixed]; |
| 664 |
Matrix prod =new Matrix(numUpdate,numFixed); |
| 665 |
Vector dif =new Vector(numUpdate); |
| 666 |
double[] contMeans = new double[numFixed] ; |
| 667 |
|
| 668 |
|
| 669 |
for (int i=0; i<numUpdate;i++){ |
| 670 |
for (int j=0;j<numFixed;j++){ |
| 671 |
H[i][j]= thisP[doUpdate[i]][dontUpdate[j]]; |
| 672 |
} |
| 673 |
} |
| 674 |
|
| 675 |
for (int i=0; i<numFixed;i++){ |
| 676 |
contMeans[i]= oldValue[dontUpdate[i]]-mean[dontUpdate[i]]; |
| 677 |
} |
| 678 |
|
| 679 |
|
| 680 |
Matrix invK= new SymmetricMatrix(condP).inverse() ; |
| 681 |
Matrix HH= new Matrix(H); |
| 682 |
|
| 683 |
try { |
| 684 |
prod = invK.product(HH); |
| 685 |
dif= prod.product(new Vector(contMeans)) ; |
| 686 |
|
| 687 |
} catch (IllegalDimension illegalDimension) { |
| 688 |
illegalDimension.printStackTrace(); |
| 689 |
} |
| 690 |
|
| 691 |
for(int i=0; i<numUpdate;i++){ |
| 692 |
condMean[i]= mean[doUpdate[i]] - dif.component(i); |
| 693 |
|
| 694 |
} |
| 695 |
|
| 696 |
|
| 697 |
return condMean; |
| 698 |
} |
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
private double getConditionalMean(int entry, double[][] thisP, double[] traitValue, double[] mean) { |
| 706 |
double sumProd = 0; |
| 707 |
for (int i = 0; i < dim; i++) { |
| 708 |
if (i != entry) sumProd += thisP[entry][i] * (traitValue[i] - mean[i]); |
| 709 |
} |
| 710 |
|
| 711 |
double condMean = mean[entry] - sumProd / thisP[entry][entry]; |
| 712 |
return condMean; |
| 713 |
} |
| 714 |
|
| 715 |
|
| 716 |
public String getPerformanceSuggestion() { |
| 717 |
return null; |
| 718 |
} |
| 719 |
|
| 720 |
|
| 721 |
public String getOperatorName() { |
| 722 |
return LATENT_LIABILITY_GIBBS_OPERATOR; |
| 723 |
} |
| 724 |
|
| 725 |
|
| 726 |
public static dr.xml.XMLObjectParser PARSER = new dr.xml.AbstractXMLObjectParser() { |
| 727 |
|
| 728 |
public final static String MASK = "mask"; |
| 729 |
|
| 730 |
|
| 731 |
public String getParserName() { |
| 732 |
return LATENT_LIABILITY_GIBBS_OPERATOR; |
| 733 |
} |
| 734 |
|
| 735 |
|
| 736 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 737 |
|
| 738 |
|
| 739 |
if (xo.getChildCount() < 3) { |
| 740 |
throw new XMLParseException( |
| 741 |
"Element with id = '" + xo.getName() + "' should contain:\n" + |
| 742 |
"\t 1 conjugate multivariateTraitLikelihood, 1 latentLiabilityLikelihood and one parameter \n" |
| 743 |
); |
| 744 |
} |
| 745 |
|
| 746 |
double weight = xo.getDoubleAttribute(WEIGHT); |
| 747 |
|
| 748 |
FullyConjugateMultivariateTraitLikelihood traitModel = (FullyConjugateMultivariateTraitLikelihood) xo.getChild(FullyConjugateMultivariateTraitLikelihood.class); |
| 749 |
LatentTruncation LLModel = (LatentTruncation) xo.getChild(LatentTruncation.class); |
| 750 |
|
| 751 |
|
| 752 |
CompoundParameter tipTraitParameter = (CompoundParameter) xo.getChild(CompoundParameter.class); |
| 753 |
|
| 754 |
|
| 755 |
Parameter mask =null; |
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
if (xo.hasChildNamed(MASK)) { |
| 760 |
mask = (Parameter) xo.getElementFirstChild(MASK); |
| 761 |
} |
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
return new LatentLiabilityGibbs(traitModel, LLModel, tipTraitParameter, mask, weight); |
| 769 |
} |
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
public String getParserDescription() { |
| 776 |
return "This element returns a gibbs sampler on tip latent trais for latent liability model."; |
| 777 |
} |
| 778 |
|
| 779 |
public Class getReturnType() { |
| 780 |
return MCMCOperator.class; |
| 781 |
} |
| 782 |
|
| 783 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 784 |
return rules; |
| 785 |
} |
| 786 |
|
| 787 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 788 |
AttributeRule.newDoubleRule(WEIGHT), |
| 789 |
new ElementRule(FullyConjugateMultivariateTraitLikelihood.class, "The model for the latent random variables"), |
| 790 |
new ElementRule(LatentTruncation.class, "The model that links latent and observed variables"), |
| 791 |
new ElementRule(MASK, dr.inference.model.Parameter.class, "Mask: 1 for latent variables that should be sampled",true), |
| 792 |
|
| 793 |
new ElementRule(CompoundParameter.class, "The parameter of tip locations from the tree") |
| 794 |
|
| 795 |
|
| 796 |
}; |
| 797 |
}; |
| 798 |
} |
| 799 |
|
| 800 |
|
| 801 |
|