| 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.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 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 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 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public abstract double logTreeProbability(int taxonCount); |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 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 |
|
| 75 |
|
| 76 |
public abstract boolean includeExternalNodesInLikelihoodCalculation(); |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 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 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 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 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 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 |
|
| 137 |
return 1; |
| 138 |
} |
| 139 |
|
| 140 |
|
| 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 |
|
| 151 |
lnL[0] += logNodeProbability(tree, node); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 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 |
} |