| 1 |
package dr.evomodel.treedatalikelihood.preorder; |
| 2 |
|
| 3 |
import dr.evolution.tree.Tree; |
| 4 |
import dr.evomodel.continuous.MultivariateDiffusionModel; |
| 5 |
import dr.evomodel.treedatalikelihood.continuous.ConjugateRootTraitPrior; |
| 6 |
import dr.evomodel.treedatalikelihood.continuous.ContinuousDataLikelihoodDelegate; |
| 7 |
import dr.evomodel.treedatalikelihood.continuous.ContinuousRateTransformation; |
| 8 |
import dr.evomodel.treedatalikelihood.continuous.ContinuousTraitPartialsProvider; |
| 9 |
import dr.evomodel.treedatalikelihood.continuous.cdi.ContinuousDiffusionIntegrator; |
| 10 |
import dr.evomodel.treedatalikelihood.continuous.cdi.PrecisionType; |
| 11 |
import dr.math.distributions.MultivariateNormalDistribution; |
| 12 |
import dr.math.matrixAlgebra.WrappedVector; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
public class ConditionalOnTipsRealizedDelegate extends AbstractRealizedContinuousTraitDelegate { |
| 18 |
|
| 19 |
static final private boolean DEBUG = false; |
| 20 |
|
| 21 |
final protected int dimPartial; |
| 22 |
final boolean hasNoDrift; |
| 23 |
|
| 24 |
public ConditionalOnTipsRealizedDelegate(String name, |
| 25 |
Tree tree, |
| 26 |
MultivariateDiffusionModel diffusionModel, |
| 27 |
ContinuousTraitPartialsProvider dataModel, |
| 28 |
ConjugateRootTraitPrior rootPrior, |
| 29 |
ContinuousRateTransformation rateTransformation, |
| 30 |
ContinuousDataLikelihoodDelegate likelihoodDelegate) { |
| 31 |
super(name, tree, diffusionModel, dataModel, rootPrior, rateTransformation, likelihoodDelegate); |
| 32 |
|
| 33 |
this.likelihoodDelegate = likelihoodDelegate; |
| 34 |
this.cdi = likelihoodDelegate.getIntegrator(); |
| 35 |
this.dimPartial = dimTrait + likelihoodDelegate.getPrecisionType().getMatrixLength(dimTrait); |
| 36 |
partialNodeBuffer = new double[numTraits * dimPartial]; |
| 37 |
partialPriorBuffer = new double[numTraits * dimPartial]; |
| 38 |
|
| 39 |
tmpMean = new double[dimTrait]; |
| 40 |
|
| 41 |
this.hasNoDrift = ! likelihoodDelegate.getDiffusionProcessDelegate().hasDrift(); |
| 42 |
|
| 43 |
if (hasNoDrift) { |
| 44 |
this.precisionBuffer = null; |
| 45 |
this.displacementBuffer = null; |
| 46 |
} else { |
| 47 |
this.precisionBuffer = new double[dimTrait * dimTrait]; |
| 48 |
this.displacementBuffer = new double[dimTrait]; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
@Override |
| 53 |
protected void simulateRoot(final int nodeIndex) { |
| 54 |
|
| 55 |
cdi.getPostOrderPartial(rootProcessDelegate.getPriorBufferIndex(), partialPriorBuffer); |
| 56 |
cdi.getPostOrderPartial(likelihoodDelegate.getActiveNodeIndex(nodeIndex), partialNodeBuffer); |
| 57 |
|
| 58 |
if (DEBUG) { |
| 59 |
System.err.println("Simulate root node " + nodeIndex); |
| 60 |
} |
| 61 |
|
| 62 |
int offsetPartial = 0; |
| 63 |
int offsetSample = dimNode * nodeIndex; |
| 64 |
for (int trait = 0; trait < numTraits; ++trait) { |
| 65 |
|
| 66 |
simulateTraitForRoot(offsetSample, offsetPartial); |
| 67 |
|
| 68 |
offsetSample += dimTrait; |
| 69 |
offsetPartial += dimPartial; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
protected void simulateTraitForRoot(final int offsetSample, final int offsetPartial) { |
| 74 |
|
| 75 |
assert (likelihoodDelegate.getPrecisionType() == PrecisionType.SCALAR); |
| 76 |
|
| 77 |
final double rootPrec = partialNodeBuffer[offsetPartial + dimTrait]; |
| 78 |
|
| 79 |
if (DEBUG) { |
| 80 |
System.err.println("\trootPrec: " + rootPrec); |
| 81 |
} |
| 82 |
|
| 83 |
if (Double.isInfinite(rootPrec)) { |
| 84 |
|
| 85 |
System.arraycopy(partialNodeBuffer, offsetPartial, sample, offsetSample, dimTrait); |
| 86 |
|
| 87 |
} else { |
| 88 |
|
| 89 |
final double priorPrec = partialPriorBuffer[offsetPartial + dimTrait]; |
| 90 |
final double totalPrec = priorPrec + rootPrec; |
| 91 |
|
| 92 |
for (int i = 0; i < dimTrait; ++i) { |
| 93 |
tmpMean[i] = (rootPrec * partialNodeBuffer[offsetPartial + i] |
| 94 |
+ priorPrec * partialPriorBuffer[offsetPartial + i]) |
| 95 |
/ totalPrec; |
| 96 |
} |
| 97 |
|
| 98 |
if (DEBUG) { |
| 99 |
System.err.println("\troot mean: " + new WrappedVector.Raw(partialNodeBuffer, offsetPartial, dimTrait)); |
| 100 |
System.err.println("\tprior prec: " + priorPrec); |
| 101 |
System.err.println("\tprior mean: " + new WrappedVector.Raw(partialPriorBuffer, offsetPartial, dimTrait)); |
| 102 |
System.err.println("\tweight mean: " + new WrappedVector.Raw(tmpMean, 0, dimTrait)); |
| 103 |
} |
| 104 |
|
| 105 |
final double sqrtScale = Math.sqrt(1.0 / totalPrec); |
| 106 |
|
| 107 |
MultivariateNormalDistribution.nextMultivariateNormalCholesky( |
| 108 |
tmpMean, 0, |
| 109 |
cholesky, sqrtScale, |
| 110 |
sample, offsetSample, |
| 111 |
tmpEpsilon); |
| 112 |
|
| 113 |
if (DEBUG) { |
| 114 |
System.err.println("\tsample: " + new WrappedVector.Raw(sample, offsetSample, dimTrait)); |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
@Override |
| 120 |
protected void simulateNode(final int parentNumber, |
| 121 |
final int nodeNumber, |
| 122 |
final int nodePartial, |
| 123 |
final int nodeMatrix, |
| 124 |
final int external) { |
| 125 |
|
| 126 |
cdi.getPostOrderPartial(nodePartial, partialNodeBuffer); |
| 127 |
final double branchPrecision = cdi.getBranchMatrices(nodeMatrix, precisionBuffer, displacementBuffer); |
| 128 |
|
| 129 |
int offsetPartial = 0; |
| 130 |
int offsetSample = dimNode * nodeNumber; |
| 131 |
int offsetParent = dimNode * parentNumber; |
| 132 |
|
| 133 |
if (DEBUG) { |
| 134 |
System.err.println("Simulate for node " + nodeNumber); |
| 135 |
} |
| 136 |
for (int trait = 0; trait < numTraits; ++trait) { |
| 137 |
|
| 138 |
simulateTraitForNode(nodeNumber, trait, offsetSample, offsetParent, offsetPartial, external, branchPrecision); |
| 139 |
|
| 140 |
offsetSample += dimTrait; |
| 141 |
offsetParent += dimTrait; |
| 142 |
offsetPartial += dimPartial; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
protected void simulateTraitForNode(final int nodeIndex, |
| 147 |
final int traitIndex, |
| 148 |
final int offsetSample, |
| 149 |
final int offsetParent, |
| 150 |
final int offsetPartial, |
| 151 |
final int external, |
| 152 |
final double branchPrecision) { |
| 153 |
|
| 154 |
final double nodePrecision = partialNodeBuffer[offsetPartial + dimTrait]; |
| 155 |
|
| 156 |
if (Double.isInfinite(nodePrecision)) { |
| 157 |
|
| 158 |
if (DEBUG) { |
| 159 |
System.err.println("\tCopy from node partial"); |
| 160 |
} |
| 161 |
|
| 162 |
System.arraycopy(partialNodeBuffer, offsetPartial, sample, offsetSample, dimTrait); |
| 163 |
|
| 164 |
} else if (Double.isInfinite(branchPrecision)) { |
| 165 |
|
| 166 |
if (DEBUG) { |
| 167 |
System.err.println("\tCopy from parent sample"); |
| 168 |
} |
| 169 |
|
| 170 |
System.arraycopy(sample, offsetParent, sample, offsetSample, dimTrait); |
| 171 |
|
| 172 |
} else { |
| 173 |
|
| 174 |
final double totalPrecision = nodePrecision + branchPrecision; |
| 175 |
|
| 176 |
for (int i = 0; i < dimTrait; ++i) { |
| 177 |
tmpMean[i] = (nodePrecision * partialNodeBuffer[offsetPartial + i] |
| 178 |
+ branchPrecision * sample[offsetParent + i]) / totalPrecision; |
| 179 |
} |
| 180 |
|
| 181 |
final double sqrtScale = Math.sqrt(1.0 / totalPrecision); |
| 182 |
|
| 183 |
MultivariateNormalDistribution.nextMultivariateNormalCholesky( |
| 184 |
tmpMean, 0, |
| 185 |
cholesky, sqrtScale, |
| 186 |
sample, offsetSample, |
| 187 |
tmpEpsilon); |
| 188 |
} |
| 189 |
|
| 190 |
if (DEBUG) { |
| 191 |
System.err.println("\tSample value: " + new WrappedVector.Raw(sample, offsetSample, dimTrait)); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
final ContinuousDataLikelihoodDelegate likelihoodDelegate; |
| 196 |
final private ContinuousDiffusionIntegrator cdi; |
| 197 |
final double[] partialNodeBuffer; |
| 198 |
final double[] partialPriorBuffer; |
| 199 |
final double[] precisionBuffer; |
| 200 |
final double[] displacementBuffer; |
| 201 |
final double[] tmpMean; |
| 202 |
} |