| 1 |
package dr.evomodel.treedatalikelihood.preorder; |
| 2 |
|
| 3 |
import dr.evolution.tree.MutableTreeModel; |
| 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.ContinuousTraitDataModel; |
| 9 |
import dr.math.distributions.MultivariateNormalDistribution; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
public class UnconditionalOnTipsDelegate extends AbstractRealizedContinuousTraitDelegate { |
| 15 |
|
| 16 |
public UnconditionalOnTipsDelegate(String name, |
| 17 |
MutableTreeModel tree, |
| 18 |
MultivariateDiffusionModel diffusionModel, |
| 19 |
ContinuousTraitDataModel dataModel, |
| 20 |
ConjugateRootTraitPrior rootPrior, |
| 21 |
ContinuousRateTransformation rateTransformation, |
| 22 |
ContinuousDataLikelihoodDelegate likelihoodDelegate) { |
| 23 |
super(name, tree, diffusionModel, dataModel, rootPrior, rateTransformation, likelihoodDelegate); |
| 24 |
|
| 25 |
this.rootPrior = rootPrior; |
| 26 |
} |
| 27 |
|
| 28 |
@Override |
| 29 |
protected void simulateRoot(final int nodeIndex) { |
| 30 |
|
| 31 |
final double[] rootMean = rootPrior.getMean(); |
| 32 |
final double sqrtScale = Math.sqrt(1.0 / rootPrior.getPseudoObservations()); |
| 33 |
|
| 34 |
int offsetSample = dimNode * nodeIndex; |
| 35 |
for (int trait = 0; trait < numTraits; ++trait) { |
| 36 |
MultivariateNormalDistribution.nextMultivariateNormalCholesky( |
| 37 |
rootMean, 0, |
| 38 |
cholesky, sqrtScale, |
| 39 |
sample, offsetSample, |
| 40 |
tmpEpsilon |
| 41 |
); |
| 42 |
|
| 43 |
offsetSample += dimTrait; |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
protected void simulateNode(final BranchNodeOperation operation, |
| 48 |
final double branchNormalization) { |
| 49 |
final int nodeIndex = operation.getNodeNumber(); |
| 50 |
int offsetSample = dimNode * nodeIndex; |
| 51 |
int offsetParent = dimNode * operation.getParentNumber(); |
| 52 |
|
| 53 |
final double branchLength = operation.getBranchLength() * branchNormalization; |
| 54 |
|
| 55 |
if (branchLength == 0.0) { |
| 56 |
System.arraycopy(sample, offsetParent, sample, offsetSample, dimTrait * numTraits); |
| 57 |
} else { |
| 58 |
|
| 59 |
final double sqrtScale = Math.sqrt(branchLength); |
| 60 |
for (int trait = 0; trait < numTraits; ++trait) { |
| 61 |
MultivariateNormalDistribution.nextMultivariateNormalCholesky( |
| 62 |
sample, offsetParent, |
| 63 |
cholesky, sqrtScale, |
| 64 |
sample, offsetSample, |
| 65 |
tmpEpsilon |
| 66 |
); |
| 67 |
|
| 68 |
offsetParent += dimTrait; |
| 69 |
offsetSample += dimTrait; |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
protected void simulateNode(final int parentNumber, |
| 76 |
final int nodeNumber, |
| 77 |
final int nodeMatrix, |
| 78 |
final int siblingNumber, |
| 79 |
final int siblingMatrix) { |
| 80 |
throw new RuntimeException("Not yet implemented -- see above"); |
| 81 |
} |
| 82 |
|
| 83 |
private final ConjugateRootTraitPrior rootPrior; |
| 84 |
} |