Debian Codegraph @

src/dr/evomodel/speciation/UltrametricSpeciationModel.java

raw symbols SCIP
1 /*
2 * UltrametricSpeciationModel.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.speciation;
27
28 import dr.evolution.tree.NodeRef;
29 import dr.evolution.tree.Tree;
30 import dr.evolution.util.Taxon;
31 import dr.evolution.util.Units;
32
33 import java.util.Set;
34
35 /**
36 * This interface provides methods that describe a speciation model.
37 *
38 * @author Andrew Rambaut
39 * @author Alexei Drummond
40 */
41 public abstract class UltrametricSpeciationModel extends SpeciationModel implements Units {
42
43 public UltrametricSpeciationModel(String modelName, Type units) {
44 super(modelName, units);
45 }
46
47 /**
48 * "fixed" part of likelihood. Guaranteed to be called before any logNodeProbability
49 * calls for one evaluation of the tree.
50 *
51 * @param taxonCount Number of taxa in tree
52 * @return density factor which is not node dependent
53 */
54 public abstract double logTreeProbability(int taxonCount);
55
56 /**
57 * Per node part of likelihood.
58 *
59 * @param tree
60 * @param node
61 * @return node contribution to density
62 */
63 public abstract double logNodeProbability(Tree tree, NodeRef node);
64
65 public boolean analyticalMarginalOK() {
66 return false;
67 }
68
69 public double getMarginal(Tree tree, CalibrationPoints calibration) {
70 return calibration.getCorrection(tree, -1);
71 }
72
73 /**
74 * @return true if calls to logNodeProbability for terminal nodes (tips) are required
75 */
76 public abstract boolean includeExternalNodesInLikelihoodCalculation();
77
78 /**
79 * Generic likelihood calculation
80 *
81 * @param tree
82 * @return log-likelihood of density
83 */
84 public final double calculateTreeLogLikelihood(Tree tree) {
85 final int taxonCount = tree.getExternalNodeCount();
86 double logL = logTreeProbability(taxonCount);
87
88 for (int j = 0; j < tree.getInternalNodeCount(); j++) {
89 logL += logNodeProbability(tree, tree.getInternalNode(j));
90 }
91
92 if (includeExternalNodesInLikelihoodCalculation()) {
93 for (int j = 0; j < taxonCount; j++) {
94 logL += logNodeProbability(tree, tree.getExternalNode(j));
95 }
96 }
97
98 return logL;
99 }
100
101 /**
102 * Alternative likelihood calculation that uses recursion over the tree and allows
103 * a list of taxa to exclude
104 *
105 * @param tree the tree
106 * @param exclude a list of taxa to exclude
107 * @return log-likelihood of density
108 */
109 public double calculateTreeLogLikelihood(Tree tree, Set<Taxon> exclude) {
110 final int taxonCount = tree.getExternalNodeCount() - exclude.size();
111
112 double[] lnL = {logTreeProbability(taxonCount)};
113
114 calculateNodeLogLikelihood(tree, tree.getRoot(), exclude, lnL);
115
116 return lnL[0];
117 }
118
119 /**
120 * Alternative likelihood calculation that uses recursion over the tree and allows
121 * a list of taxa to exclude
122 *
123 * @param tree the tree
124 * @param node
125 * @param exclude a list of taxa to exclude
126 * @param lnL a reference to the lnL sum
127 * @return the number of included daughter nodes
128 */
129 private int calculateNodeLogLikelihood(Tree tree, NodeRef node, Set<Taxon> exclude, double[] lnL) {
130 if (tree.isExternal(node)) {
131 if (!exclude.contains(tree.getNodeTaxon(node))) {
132 if (includeExternalNodesInLikelihoodCalculation()) {
133 lnL[0] += logNodeProbability(tree, node);
134 }
135
136 // this tip is included in the subtree...
137 return 1;
138 }
139
140 // this tip is excluded from the subtree...
141 return 0;
142 } else {
143 int count = 0;
144 for (int i = 0; i < tree.getChildCount(node); i++) {
145 NodeRef child = tree.getChild(node, i);
146 count += calculateNodeLogLikelihood(tree, child, exclude, lnL);
147 }
148
149 if (count == 2) {
150 // this node is included in the subtree...
151 lnL[0] += logNodeProbability(tree, node);
152 }
153
154 // if at least one of the children has included tips then return 1 otherwise 0
155 return count > 0 ? 1 : 0;
156 }
157 }
158
159 @Override
160 public double calculateTreeLogLikelihood(Tree tree, CalibrationPoints calibration) {
161 double logL = calculateTreeLogLikelihood(tree);
162 double mar = getMarginal(tree, calibration);
163 logL += mar;
164 return logL;
165 }
166 }