Debian Codegraph @

src/dr/evomodel/continuous/SampledMultivariateTraitLikelihood.java

raw symbols SCIP
1 /*
2 * SampledMultivariateTraitLikelihood.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.Tree;
31 import dr.evomodel.branchratemodel.BranchRateModel;
32 import dr.evomodel.tree.TreeModel;
33 import dr.inference.model.CompoundParameter;
34 import dr.inference.model.CompoundSymmetricMatrix;
35 import dr.inference.model.Model;
36 import dr.math.matrixAlgebra.Matrix;
37 import dr.math.matrixAlgebra.Vector;
38
39 import java.util.List;
40
41 /**
42 * @author Marc A. Suchard
43 */
44 public class SampledMultivariateTraitLikelihood extends AbstractMultivariateTraitLikelihood {
45
46 public SampledMultivariateTraitLikelihood(String traitName,
47 MutableTreeModel treeModel,
48 MultivariateDiffusionModel diffusionModel,
49 CompoundParameter traitParameter,
50 List<Integer> missingIndices,
51 boolean cacheBranches, boolean scaleByTime, boolean useTreeLength,
52 BranchRateModel rateModel, Model samplingDensity,
53 boolean reportAsMultivariate,
54 boolean reciprocalRates) {
55 super(traitName, treeModel, diffusionModel, traitParameter, null, missingIndices, cacheBranches, scaleByTime,
56 useTreeLength, rateModel,
57 null, null, null, samplingDensity, reportAsMultivariate, reciprocalRates);
58 }
59
60 protected String extraInfo() {
61 return "\tSampling internal trait values: true\n";
62 }
63
64 /**
65 * Calculate the log likelihood of the current state.
66 *
67 * @return the log likelihood.
68 */
69 public double calculateLogLikelihood() {
70
71 double logLikelihood;
72
73 if (!cacheBranches)
74 logLikelihood = traitLogLikelihood(null, treeModel.getRoot());
75 else
76 logLikelihood = traitCachedLogLikelihood(null, treeModel.getRoot());
77 if (logLikelihood > maxLogLikelihood) {
78 maxLogLikelihood = logLikelihood;
79 }
80 return logLikelihood;
81 }
82
83 protected double calculateAscertainmentCorrection(int taxonIndex) {
84 throw new RuntimeException("Ascertainment correction not yet implemented for sampled trait likelihoods");
85 }
86
87 public final double getLogDataLikelihood() {
88 double logLikelihood = 0;
89 for (int i = 0; i < treeModel.getExternalNodeCount(); i++) {
90 NodeRef tip = treeModel.getExternalNode(i); // TODO Do not include integrated tips; how to check???
91
92 if (cacheBranches && validLogLikelihoods[tip.getNumber()])
93 logLikelihood += cachedLogLikelihoods[tip.getNumber()];
94 else {
95 NodeRef parent = treeModel.getParent(tip);
96
97 double[] tipTrait = treeModel.getMultivariateNodeTrait(tip, traitName);
98 double[] parentTrait = treeModel.getMultivariateNodeTrait(parent, traitName);
99 double time = getRescaledBranchLengthForPrecision(tip);
100
101 logLikelihood += diffusionModel.getLogLikelihood(parentTrait, tipTrait, time);
102 }
103 }
104 return logLikelihood;
105 }
106
107
108 private double traitCachedLogLikelihood(double[] parentTrait, NodeRef node) {
109
110 double logL = 0.0;
111 double[] childTrait = null;
112 final int nodeNumber = node.getNumber();
113
114 if (!treeModel.isRoot(node)) {
115
116 if (!validLogLikelihoods[nodeNumber]) { // recompute
117
118 childTrait = treeModel.getMultivariateNodeTrait(node, traitName);
119 double time = getRescaledBranchLengthForPrecision(node);
120 if (parentTrait == null)
121 parentTrait = treeModel.getMultivariateNodeTrait(treeModel.getParent(node), traitName);
122 logL = diffusionModel.getLogLikelihood(parentTrait, childTrait, time);
123 cachedLogLikelihoods[nodeNumber] = logL;
124 validLogLikelihoods[nodeNumber] = true;
125 } else
126 logL = cachedLogLikelihoods[nodeNumber];
127 }
128
129 int childCount = treeModel.getChildCount(node);
130 for (int i = 0; i < childCount; i++) {
131 logL += traitCachedLogLikelihood(childTrait, treeModel.getChild(node, i));
132 }
133
134 return logL;
135 }
136
137 private double traitLogLikelihood(double[] parentTrait, NodeRef node) {
138
139 double logL = 0.0;
140 double[] childTrait = treeModel.getMultivariateNodeTrait(node, traitName);
141
142 if (parentTrait != null) {
143
144 double time = getRescaledBranchLengthForPrecision(node);
145 logL = diffusionModel.getLogLikelihood(parentTrait, childTrait, time);
146 if (new Double(logL).isNaN()) {
147 System.err.println("AbstractMultivariateTraitLikelihood: likelihood is undefined");
148 System.err.println("time = " + time);
149 System.err.println("parent trait value = " + new Vector(parentTrait));
150 System.err.println("child trait value = " + new Vector(childTrait));
151
152 double[][] precisionMatrix = diffusionModel.getPrecisionmatrix();
153 if (precisionMatrix != null) {
154 System.err.println("precision matrix = " + new Matrix(diffusionModel.getPrecisionmatrix()));
155 if (diffusionModel.getPrecisionParameter() instanceof CompoundSymmetricMatrix) {
156 CompoundSymmetricMatrix csMatrix = (CompoundSymmetricMatrix) diffusionModel.getPrecisionParameter();
157 System.err.println("diagonals = " + new Vector(csMatrix.getDiagonals()));
158 System.err.println("off diagonal = " + csMatrix.getOffDiagonal());
159 }
160 }
161 }
162 }
163 int childCount = treeModel.getChildCount(node);
164 for (int i = 0; i < childCount; i++) {
165 logL += traitLogLikelihood(childTrait, treeModel.getChild(node, i));
166 }
167
168 if (new Double(logL).isNaN()) {
169 System.err.println("logL = " + logL);
170 // System.err.println(new Matrix(diffusionModel.getPrecisionmatrix()));
171 System.exit(-1);
172 }
173
174 return logL;
175 }
176
177
178 public double[] getTraitForNode(Tree treeModel, NodeRef node, String traitName) {
179 return ((TreeModel) treeModel).getMultivariateNodeTrait(node, traitName);
180 }
181
182 }