| 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.evolution.tree.TreeUtils; |
| 31 |
import dr.math.KroneckerOperation; |
| 32 |
import dr.math.matrixAlgebra.Matrix; |
| 33 |
import dr.math.matrixAlgebra.SymmetricMatrix; |
| 34 |
|
| 35 |
import java.util.HashSet; |
| 36 |
import java.util.Set; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class MultivariateTraitUtils { |
| 42 |
|
| 43 |
public static NodeRef findMRCA(FullyConjugateMultivariateTraitLikelihood trait, int iTip, int jTip) { |
| 44 |
MutableTreeModel treeModel = trait.getTreeModel(); |
| 45 |
Set<String> leafNames = new HashSet<String>(); |
| 46 |
leafNames.add(treeModel.getTaxonId(iTip)); |
| 47 |
leafNames.add(treeModel.getTaxonId(jTip)); |
| 48 |
return TreeUtils.getCommonAncestorNode(treeModel, leafNames); |
| 49 |
} |
| 50 |
|
| 51 |
public static double[][] computeTreePrecision(FullyConjugateMultivariateTraitLikelihood trait, boolean conditionOnRoot) { |
| 52 |
if (trait.strengthOfSelection != null) { |
| 53 |
return new SymmetricMatrix(computeTreeVarianceOU(trait, conditionOnRoot)).inverse().toComponents(); |
| 54 |
} else { |
| 55 |
return new SymmetricMatrix(computeTreeVariance(trait, conditionOnRoot)).inverse().toComponents(); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
public static double[][] computeTreeTraitPrecision(FullyConjugateMultivariateTraitLikelihood trait, boolean conditionOnRoot) { |
| 60 |
double[][] treePrecision = computeTreePrecision(trait, conditionOnRoot); |
| 61 |
double[][] traitPrecision = trait.getDiffusionModel().getPrecisionmatrix(); |
| 62 |
return productKronecker(treePrecision, traitPrecision); |
| 63 |
} |
| 64 |
|
| 65 |
private static double[][] productKronecker(double[][] A, double[][] B) { |
| 66 |
if (B.length > 1) { |
| 67 |
A = KroneckerOperation.product(A, B); |
| 68 |
} else { |
| 69 |
final double b = B[0][0]; |
| 70 |
for (int i = 0; i < A.length; ++i) { |
| 71 |
for (int j = 0; j < A[i].length; ++j) { |
| 72 |
A[i][j] *= b; |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
return A; |
| 77 |
} |
| 78 |
|
| 79 |
private static double[][] productMatrices(double[][] A, double[][] B) { |
| 80 |
double[][] C = new double[A.length][B[0].length]; |
| 81 |
|
| 82 |
|
| 83 |
for (int i = 0; i < A.length; i++) { |
| 84 |
for (int k = 0; k < B[0].length; k++) { |
| 85 |
for (int j = 0; j < A[0].length; j++) { |
| 86 |
C[i][k] = C[i][k] + A[i][j] * B[j][k]; |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
return C; |
| 92 |
} |
| 93 |
|
| 94 |
private static double[][] transposeMatrix(double[][] A) { |
| 95 |
double[][] B = new double[A[0].length][A.length]; |
| 96 |
|
| 97 |
for (int i = 0; i < A.length; i++) { |
| 98 |
for (int j = 0; j < A[0].length; j++) { |
| 99 |
B[j][i] = A[i][j]; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
return B; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
private static double[][] computeLinCombMatrix(FullyConjugateMultivariateTraitLikelihood trait) { |
| 108 |
|
| 109 |
MutableTreeModel treeModel = trait.getTreeModel(); |
| 110 |
final int tipCount = treeModel.getExternalNodeCount(); |
| 111 |
final int branchCount = 2 * tipCount - 2; |
| 112 |
double[][] linCombMatrix = new double[tipCount][branchCount]; |
| 113 |
|
| 114 |
double tempScalar; |
| 115 |
NodeRef tempNode; |
| 116 |
int tempNodeIndex; |
| 117 |
|
| 118 |
for (int k = 0; k < tipCount; k++) { |
| 119 |
tempNode = treeModel.getExternalNode(k); |
| 120 |
|
| 121 |
tempScalar = 1; |
| 122 |
tempNodeIndex = k; |
| 123 |
|
| 124 |
for (int r = 0; r < branchCount; r++) { |
| 125 |
if (r == tempNodeIndex) { |
| 126 |
linCombMatrix[k][r] = tempScalar; |
| 127 |
|
| 128 |
tempScalar = tempScalar * Math.exp(-trait.getTimeScaledSelection(tempNode)); |
| 129 |
tempNode = treeModel.getParent(tempNode); |
| 130 |
tempNodeIndex = tempNode.getNumber(); |
| 131 |
} else { |
| 132 |
linCombMatrix[k][r] = 0; |
| 133 |
} |
| 134 |
|
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
return linCombMatrix; |
| 139 |
|
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
private static double[] computeRootMultipliers(FullyConjugateMultivariateTraitLikelihood trait) { |
| 144 |
MutableTreeModel myTreeModel = trait.getTreeModel(); |
| 145 |
final int tipCount = myTreeModel.getExternalNodeCount(); |
| 146 |
double[] multiplierVect = new double[tipCount]; |
| 147 |
NodeRef tempNode; |
| 148 |
|
| 149 |
for (int i = 0; i < tipCount; i++) { |
| 150 |
tempNode = myTreeModel.getExternalNode(i); |
| 151 |
multiplierVect[i] = Math.exp(-trait.getTimeScaledSelection(tempNode)); |
| 152 |
tempNode = myTreeModel.getParent(tempNode); |
| 153 |
while (!myTreeModel.isRoot(tempNode)) { |
| 154 |
multiplierVect[i] = multiplierVect[i] * Math.exp(-trait.getTimeScaledSelection(tempNode)); |
| 155 |
tempNode = myTreeModel.getParent(tempNode); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
return multiplierVect; |
| 161 |
|
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
private static double[] getShiftContributionToMean(NodeRef node, FullyConjugateMultivariateTraitLikelihood trait) { |
| 166 |
|
| 167 |
MutableTreeModel treeModel = trait.getTreeModel(); |
| 168 |
double shiftContribution[] = new double[trait.dimTrait]; |
| 169 |
|
| 170 |
if (!treeModel.isRoot(node)) { |
| 171 |
NodeRef parent = treeModel.getParent(node); |
| 172 |
double shiftContributionParent[] = getShiftContributionToMean(parent, trait); |
| 173 |
for (int i = 0; i < shiftContribution.length; ++i) { |
| 174 |
shiftContribution[i] = trait.getShiftForBranchLength(node)[i] + shiftContributionParent[i]; |
| 175 |
} |
| 176 |
} |
| 177 |
return shiftContribution; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
public static double[] computeTreeTraitMean(FullyConjugateMultivariateTraitLikelihood trait, double[] rootValue, boolean conditionOnRoot) { |
| 182 |
double[] root = trait.getPriorMean(); |
| 183 |
if (conditionOnRoot) { |
| 184 |
System.err.println("WARNING: Not yet fully implemented (conditioning on root in simulator)"); |
| 185 |
|
| 186 |
root = rootValue; |
| 187 |
} |
| 188 |
final int nTaxa = trait.getTreeModel().getExternalNodeCount(); |
| 189 |
double[] mean = new double[root.length * nTaxa]; |
| 190 |
for (int i = 0; i < nTaxa; ++i) { |
| 191 |
System.arraycopy(root, 0, mean, i * root.length, root.length); |
| 192 |
} |
| 193 |
|
| 194 |
if (trait.driftModels != null) { |
| 195 |
MutableTreeModel myTreeModel = trait.getTreeModel(); |
| 196 |
for (int i = 0; i < nTaxa; ++i) { |
| 197 |
double[] shiftContribution = getShiftContributionToMean(myTreeModel.getExternalNode(i), trait); |
| 198 |
for (int j = 0; j < trait.dimTrait; ++j) { |
| 199 |
mean[i * trait.dimTrait + j] = mean[i * trait.dimTrait + j] + shiftContribution[j]; |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
return mean; |
| 205 |
} |
| 206 |
|
| 207 |
public static double[] computeTreeTraitMeanOU(FullyConjugateMultivariateTraitLikelihood trait, double[] rootValue, boolean conditionOnRoot) { |
| 208 |
|
| 209 |
double[] root = trait.getPriorMean(); |
| 210 |
MutableTreeModel myTreeModel = trait.getTreeModel(); |
| 211 |
double[][] linCombMatrix = computeLinCombMatrix(trait); |
| 212 |
double[] rootMultiplierVect = computeRootMultipliers(trait); |
| 213 |
|
| 214 |
if (conditionOnRoot) { |
| 215 |
root = rootValue; |
| 216 |
} |
| 217 |
|
| 218 |
final int nTaxa = myTreeModel.getExternalNodeCount(); |
| 219 |
final int branchCount = 2 * nTaxa - 2; |
| 220 |
final int traitDim = trait.dimTrait; |
| 221 |
double[] mean = new double[root.length * nTaxa]; |
| 222 |
double[] displacementMeans = new double[branchCount * traitDim]; |
| 223 |
double[] tempVect = new double[nTaxa * traitDim]; |
| 224 |
NodeRef tempNode; |
| 225 |
|
| 226 |
for (int k = 0; k < branchCount; k++) { |
| 227 |
tempNode = myTreeModel.getNode(k); |
| 228 |
for (int t = 0; t < traitDim; t++) { |
| 229 |
displacementMeans[k * traitDim + t] = (1 - Math.exp(-trait.getTimeScaledSelection(tempNode))) * trait.getOptimalValue(tempNode)[t]; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
for (int i = 0; i < nTaxa; i++) { |
| 236 |
for (int j = 0; j < branchCount; j++) { |
| 237 |
for (int k = 0; k < traitDim; k++) { |
| 238 |
tempVect[i * traitDim + k] = tempVect[i * traitDim + k] + linCombMatrix[i][j] * displacementMeans[j * traitDim + k]; |
| 239 |
} |
| 240 |
} |
| 241 |
|
| 242 |
} |
| 243 |
|
| 244 |
for (int i = 0; i < nTaxa; ++i) { |
| 245 |
System.arraycopy(root, 0, mean, i * root.length, root.length); |
| 246 |
|
| 247 |
for (int j = 0; j < traitDim; ++j) { |
| 248 |
mean[i * traitDim + j] = mean[i * traitDim + j] * rootMultiplierVect[i] + tempVect[i * traitDim + j]; |
| 249 |
} |
| 250 |
|
| 251 |
} |
| 252 |
|
| 253 |
return mean; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
public static double[][] computeTreeTraitVariance(FullyConjugateMultivariateTraitLikelihood trait, boolean conditionOnRoot) { |
| 258 |
double[][] treeVariance = computeTreeVariance(trait, conditionOnRoot); |
| 259 |
double[][] traitVariance = |
| 260 |
new SymmetricMatrix(trait.getDiffusionModel().getPrecisionmatrix()).inverse().toComponents(); |
| 261 |
return productKronecker(treeVariance, traitVariance); |
| 262 |
} |
| 263 |
|
| 264 |
public static double[][] computeTreeVariance(FullyConjugateMultivariateTraitLikelihood trait, boolean conditionOnRoot) { |
| 265 |
MutableTreeModel treeModel = trait.getTreeModel(); |
| 266 |
final int tipCount = treeModel.getExternalNodeCount(); |
| 267 |
double[][] variance = new double[tipCount][tipCount]; |
| 268 |
|
| 269 |
for (int i = 0; i < tipCount; i++) { |
| 270 |
|
| 271 |
|
| 272 |
double marginalTime = trait.getRescaledLengthToRoot(treeModel.getExternalNode(i)); |
| 273 |
variance[i][i] = marginalTime; |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
for (int j = i + 1; j < tipCount; j++) { |
| 278 |
NodeRef mrca = findMRCA(trait, i, j); |
| 279 |
if (DEBUG) { |
| 280 |
System.err.println(trait.getTreeModel().getRoot().getNumber()); |
| 281 |
System.err.print("Taxa pair: " + i + " : " + j + " (" + mrca.getNumber() + ") = "); |
| 282 |
} |
| 283 |
double length = trait.getRescaledLengthToRoot(mrca); |
| 284 |
if (DEBUG) { |
| 285 |
System.err.println(length); |
| 286 |
} |
| 287 |
variance[i][j] = length; |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
for (int i = 0; i < tipCount; i++) { |
| 293 |
for (int j = i + 1; j < tipCount; j++) { |
| 294 |
variance[j][i] = variance[i][j]; |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
if (DEBUG) { |
| 299 |
System.err.println(""); |
| 300 |
System.err.println("New tree conditional variance:\n" + new Matrix(variance)); |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
if (!conditionOnRoot) { |
| 312 |
double priorSampleSize = trait.getPriorSampleSize(); |
| 313 |
for (int i = 0; i < tipCount; ++i) { |
| 314 |
for (int j = 0; j < tipCount; ++j) { |
| 315 |
variance[i][j] += 1.0 / priorSampleSize; |
| 316 |
} |
| 317 |
} |
| 318 |
if (DEBUG) { |
| 319 |
System.err.println(""); |
| 320 |
System.err.println("New tree unconditional variance:\n" + new Matrix(variance)); |
| 321 |
} |
| 322 |
} |
| 323 |
return variance; |
| 324 |
} |
| 325 |
|
| 326 |
public static double[][] computeTreeVarianceOU(FullyConjugateMultivariateTraitLikelihood trait, boolean conditionOnRoot) { |
| 327 |
MutableTreeModel treeModel = trait.getTreeModel(); |
| 328 |
final int tipCount = treeModel.getExternalNodeCount(); |
| 329 |
final int branchCount = 2 * tipCount - 2; |
| 330 |
double[][] variance = new double[tipCount][tipCount]; |
| 331 |
double[][] tempMatrix = new double[tipCount][branchCount]; |
| 332 |
double[][] diagMatrix = new double[branchCount][branchCount]; |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
tempMatrix = computeLinCombMatrix(trait); |
| 361 |
|
| 362 |
for (int i = 0; i < branchCount; i++) { |
| 363 |
|
| 364 |
|
| 365 |
diagMatrix[i][i] = (1 - Math.exp(-2 * trait.getTimeScaledSelection(treeModel.getNode(i)))) |
| 366 |
/ (2 * trait.strengthOfSelection.getBranchRate(treeModel, treeModel.getNode(i))); |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
variance = productMatrices(productMatrices(tempMatrix, diagMatrix), transposeMatrix(tempMatrix)); |
| 371 |
|
| 372 |
return variance; |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
private static final boolean DEBUG = false; |
| 377 |
|
| 378 |
} |