Debian Codegraph @

src/dr/evomodel/continuous/MultivariateTraitUtils.java

raw symbols SCIP
1 /*
2 * MultivariateTraitUtils.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
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 * @author Marc A. Suchard
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 //check if treeModel.getExternalNode(k).getNumber() == k
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 // tempScalar = tempScalar * (1 - treeModel.getBranchLength(tempNode) * trait.getTimeScaledSelection(tempNode));
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 //root = new double[root.length];
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 //check this
234 //multiply linCombMatrix with displacement means
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 // Fill in diagonal
272 double marginalTime = trait.getRescaledLengthToRoot(treeModel.getExternalNode(i));
273 variance[i][i] = marginalTime;
274
275 // Fill in upper right triangle,
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 // Make symmetric
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 // TODO Handle missing values
304 // variance = removeMissingTipsInTreeVariance(variance); // Automatically prune missing tips
305 //
306 // if (DEBUG) {
307 // System.err.println("");
308 // System.err.println("New tree (trimmed) conditional variance:\n" + new Matrix(variance));
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 double tempScalar;
337 NodeRef tempNode;
338 int tempNodeIndex;
339
340 for(int k = 0; k < tipCount; k++){
341 tempNode = treeModel.getExternalNode(k);
342 //check if treeModel.getExternalNode(k).getNumber() == k
343 tempScalar = 1;
344 tempNodeIndex = k;
345
346 for(int r = 0; r < branchCount; r++){
347 if(r == tempNodeIndex){
348 tempMatrix[k][r] = tempScalar;
349 tempScalar = tempScalar*(1-treeModel.getBranchLength(tempNode)*trait.getTimeScaledSelection(tempNode));
350 tempNode = treeModel.getParent(tempNode);
351 tempNodeIndex = tempNode.getNumber();
352 }else{
353 tempMatrix[k][r]= 0;
354 }
355
356 }
357 }
358 */
359
360 tempMatrix = computeLinCombMatrix(trait);
361
362 for (int i = 0; i < branchCount; i++) {
363 // diagMatrix[i][i] = treeModel.getBranchLength(treeModel.getNode(i));
364 // diagMatrix[i][i] = (2*strengthOfSelection.getBranchRate(treeModel, node) ) / (1 - Math.exp(-2*getTimeScaledSelection(node)));
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 }