| 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 |
package dr.evomodel.continuous; |
| 27 |
|
| 28 |
import dr.evolution.tree.MutableTreeModel; |
| 29 |
import dr.evolution.tree.NodeRef; |
| 30 |
import dr.evomodel.branchratemodel.BranchRateModel; |
| 31 |
import dr.inference.model.CompoundParameter; |
| 32 |
import dr.inference.model.Model; |
| 33 |
import dr.math.distributions.MultivariateNormalDistribution; |
| 34 |
import dr.math.matrixAlgebra.IllegalDimension; |
| 35 |
import dr.math.matrixAlgebra.Matrix; |
| 36 |
|
| 37 |
import java.util.List; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public class SemiConjugateMultivariateTraitLikelihood extends IntegratedMultivariateTraitLikelihood { |
| 46 |
|
| 47 |
public SemiConjugateMultivariateTraitLikelihood(String traitName, |
| 48 |
MutableTreeModel treeModel, |
| 49 |
MultivariateDiffusionModel diffusionModel, |
| 50 |
CompoundParameter traitParameter, |
| 51 |
List<Integer> missingIndices, |
| 52 |
boolean cacheBranches, |
| 53 |
boolean scaleByTime, |
| 54 |
boolean useTreeLength, |
| 55 |
BranchRateModel rateModel, |
| 56 |
Model samplingDensity, |
| 57 |
boolean reportAsMultivariate, |
| 58 |
MultivariateNormalDistribution rootPrior, |
| 59 |
boolean reciprocalRates, |
| 60 |
List<RestrictedPartials> partials) { |
| 61 |
|
| 62 |
super(traitName, treeModel, diffusionModel, traitParameter, null, missingIndices, cacheBranches, scaleByTime, |
| 63 |
useTreeLength, rateModel, null, null, null, samplingDensity, partials, reportAsMultivariate, reciprocalRates); |
| 64 |
|
| 65 |
setRootPrior(rootPrior); |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
public boolean getComputeWishartSufficientStatistics() { |
| 70 |
return false; |
| 71 |
} |
| 72 |
|
| 73 |
protected double calculateAscertainmentCorrection(int taxonIndex) { |
| 74 |
throw new RuntimeException("Ascertainment correction not yet implemented for semi-conjugate trait likelihoods"); |
| 75 |
} |
| 76 |
|
| 77 |
public double getRescaledLengthToRoot(NodeRef node) { |
| 78 |
double length = 0; |
| 79 |
final NodeRef root = treeModel.getRoot(); |
| 80 |
while (node != root) { |
| 81 |
length += getRescaledBranchLengthForPrecision(node); |
| 82 |
node = treeModel.getParent(node); |
| 83 |
} |
| 84 |
return length; |
| 85 |
} |
| 86 |
|
| 87 |
protected double integrateLogLikelihoodAtRoot(double[] y, |
| 88 |
double[] Ay, |
| 89 |
double[][] AplusB, |
| 90 |
double[][] treePrecision, double rootPrecision) { |
| 91 |
double detAplusB = 0; |
| 92 |
double square = 0; |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
if (dimTrait > 1) { |
| 97 |
for (int i = 0; i < dimTrait; i++) { |
| 98 |
Ay[i] += Bz[i]; |
| 99 |
for (int j = 0; j < dimTrait; j++) { |
| 100 |
AplusB[i][j] = treePrecision[i][j] * rootPrecision + rootPriorPrecision[i][j]; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
Matrix mat = new Matrix(AplusB); |
| 105 |
|
| 106 |
try { |
| 107 |
detAplusB = mat.determinant(); |
| 108 |
|
| 109 |
} catch (IllegalDimension illegalDimension) { |
| 110 |
illegalDimension.printStackTrace(); |
| 111 |
} |
| 112 |
|
| 113 |
double[][] invAplusB = mat.inverse().toComponents(); |
| 114 |
|
| 115 |
for (int i = 0; i < dimTrait; i++) { |
| 116 |
for (int j = 0; j < dimTrait; j++) |
| 117 |
square += Ay[i] * invAplusB[i][j] * Ay[j]; |
| 118 |
} |
| 119 |
} else { |
| 120 |
|
| 121 |
detAplusB = treePrecision[0][0] * rootPrecision + rootPriorPrecision[0][0]; |
| 122 |
Ay[0] += Bz[0]; |
| 123 |
square = Ay[0] * Ay[0] / detAplusB; |
| 124 |
} |
| 125 |
|
| 126 |
double retValue = 0.5 * (logRootPriorPrecisionDeterminant - Math.log(detAplusB) - zBz + square); |
| 127 |
|
| 128 |
if (DEBUG) { |
| 129 |
System.err.println("(Ay+Bz)(A+B)^{-1}(Ay+Bz) = " + square); |
| 130 |
System.err.println("density = " + retValue); |
| 131 |
System.err.println("zBz = " + zBz); |
| 132 |
} |
| 133 |
|
| 134 |
return retValue; |
| 135 |
} |
| 136 |
|
| 137 |
private void setRootPriorSumOfSquares() { |
| 138 |
if (integrateRoot) { |
| 139 |
Bz = new double[dimTrait]; |
| 140 |
|
| 141 |
zBz = computeWeightedAverageAndSumOfSquares(rootPriorMean, Bz, rootPriorPrecision, dimTrait, 1.0); |
| 142 |
} else { |
| 143 |
zBz = 0; |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
private void setRootPrior(MultivariateNormalDistribution rootPrior) { |
| 148 |
rootPriorMean = rootPrior.getMean(); |
| 149 |
rootPriorPrecision = rootPrior.getScaleMatrix(); |
| 150 |
|
| 151 |
try { |
| 152 |
logRootPriorPrecisionDeterminant = Math.log(new Matrix(rootPriorPrecision).determinant()); |
| 153 |
} catch (IllegalDimension illegalDimension) { |
| 154 |
illegalDimension.printStackTrace(); |
| 155 |
} |
| 156 |
setRootPriorSumOfSquares(); |
| 157 |
} |
| 158 |
|
| 159 |
protected double[][] computeMarginalRootMeanAndVariance(double[] rootMean, double[][] treePrecision, |
| 160 |
double[][] treeVariance, double rootPrecision) { |
| 161 |
|
| 162 |
computeWeightedAverageAndSumOfSquares(rootMean, Ay, treePrecision, dimTrait, rootPrecision); |
| 163 |
|
| 164 |
double[][] AplusB = tmpM; |
| 165 |
|
| 166 |
for (int i = 0; i < dimTrait; i++) { |
| 167 |
Ay[i] += Bz[i]; |
| 168 |
for (int j = 0; j < dimTrait; j++) { |
| 169 |
AplusB[i][j] = treePrecision[i][j] * rootPrecision + rootPriorPrecision[i][j]; |
| 170 |
} |
| 171 |
} |
| 172 |
Matrix mat = new Matrix(AplusB); |
| 173 |
double[][] invAplusB = mat.inverse().toComponents(); |
| 174 |
|
| 175 |
|
| 176 |
for (int i = 0; i < dimTrait; i++) { |
| 177 |
rootMean[i] = 0.0; |
| 178 |
for (int j = 0; j < dimTrait; j++) { |
| 179 |
rootMean[i] += invAplusB[i][j] * Ay[j]; |
| 180 |
} |
| 181 |
} |
| 182 |
return invAplusB; |
| 183 |
} |
| 184 |
|
| 185 |
protected double[] rootPriorMean; |
| 186 |
protected double[][] rootPriorPrecision; |
| 187 |
protected double logRootPriorPrecisionDeterminant; |
| 188 |
protected double[] Bz; |
| 189 |
private double zBz; |
| 190 |
} |