| 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.treelikelihood; |
| 27 |
|
| 28 |
import dr.evolution.alignment.PatternList; |
| 29 |
import dr.evolution.datatype.DataType; |
| 30 |
import dr.evolution.tree.NodeRef; |
| 31 |
import dr.evomodel.tree.TreeModel; |
| 32 |
import dr.inference.model.*; |
| 33 |
import dr.xml.Reportable; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
@Deprecated |
| 44 |
public abstract class AbstractTreeLikelihood extends AbstractModelLikelihood implements Reportable { |
| 45 |
|
| 46 |
protected static final boolean COUNT_TOTAL_OPERATIONS = true; |
| 47 |
|
| 48 |
public AbstractTreeLikelihood(String name, TreeModel treeModel) { |
| 49 |
|
| 50 |
super(name); |
| 51 |
|
| 52 |
this.treeModel = treeModel; |
| 53 |
addModel(treeModel); |
| 54 |
|
| 55 |
nodeCount = treeModel.getNodeCount(); |
| 56 |
|
| 57 |
updateNode = new boolean[nodeCount]; |
| 58 |
for (int i = 0; i < nodeCount; i++) { |
| 59 |
updateNode[i] = true; |
| 60 |
} |
| 61 |
|
| 62 |
likelihoodKnown = false; |
| 63 |
|
| 64 |
hasInitialized = true; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
protected void updateNode(NodeRef node) { |
| 72 |
if (COUNT_TOTAL_OPERATIONS) |
| 73 |
totalRateUpdateSingleCount++; |
| 74 |
|
| 75 |
updateNode[node.getNumber()] = true; |
| 76 |
likelihoodKnown = false; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
protected void updateNodeAndChildren(NodeRef node) { |
| 83 |
if (COUNT_TOTAL_OPERATIONS) |
| 84 |
totalRateUpdateSingleCount++; |
| 85 |
|
| 86 |
updateNode[node.getNumber()] = true; |
| 87 |
|
| 88 |
for (int i = 0; i < treeModel.getChildCount(node); i++) { |
| 89 |
if (COUNT_TOTAL_OPERATIONS) |
| 90 |
totalRateUpdateSingleCount++; |
| 91 |
|
| 92 |
NodeRef child = treeModel.getChild(node, i); |
| 93 |
updateNode[child.getNumber()] = true; |
| 94 |
} |
| 95 |
likelihoodKnown = false; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
protected void updateNodeAndDescendents(NodeRef node) { |
| 102 |
if (COUNT_TOTAL_OPERATIONS) |
| 103 |
totalRateUpdateSingleCount++; |
| 104 |
|
| 105 |
updateNode[node.getNumber()] = true; |
| 106 |
|
| 107 |
for (int i = 0; i < treeModel.getChildCount(node); i++) { |
| 108 |
NodeRef child = treeModel.getChild(node, i); |
| 109 |
updateNodeAndDescendents(child); |
| 110 |
} |
| 111 |
|
| 112 |
likelihoodKnown = false; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
protected void updateAllNodes() { |
| 119 |
if (COUNT_TOTAL_OPERATIONS) |
| 120 |
totalRateUpdateAllCount++; |
| 121 |
|
| 122 |
for (int i = 0; i < nodeCount; i++) { |
| 123 |
updateNode[i] = true; |
| 124 |
} |
| 125 |
likelihoodKnown = false; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 133 |
|
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 141 |
if (COUNT_TOTAL_OPERATIONS) |
| 142 |
totalModelChangedCount++; |
| 143 |
likelihoodKnown = false; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
protected void storeState() { |
| 150 |
|
| 151 |
storedLikelihoodKnown = likelihoodKnown; |
| 152 |
storedLogLikelihood = logLikelihood; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
protected void restoreState() { |
| 159 |
|
| 160 |
likelihoodKnown = storedLikelihoodKnown; |
| 161 |
logLikelihood = storedLogLikelihood; |
| 162 |
} |
| 163 |
|
| 164 |
protected void acceptState() { |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public final Model getModel() { |
| 172 |
return this; |
| 173 |
} |
| 174 |
|
| 175 |
public final double getLogLikelihood() { |
| 176 |
if (COUNT_TOTAL_OPERATIONS) |
| 177 |
totalGetLogLikelihoodCount++; |
| 178 |
if (CompoundLikelihood.DEBUG_PARALLEL_EVALUATION) { |
| 179 |
System.err.println((likelihoodKnown ? "lazy" : "evaluate")); |
| 180 |
} |
| 181 |
if (!likelihoodKnown) { |
| 182 |
if (COUNT_TOTAL_OPERATIONS) |
| 183 |
totalCalculateLikelihoodCount++; |
| 184 |
logLikelihood = calculateLogLikelihood(); |
| 185 |
likelihoodKnown = true; |
| 186 |
} |
| 187 |
return logLikelihood; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
public void makeDirty() { |
| 194 |
if (COUNT_TOTAL_OPERATIONS) |
| 195 |
totalMakeDirtyCount++; |
| 196 |
likelihoodKnown = false; |
| 197 |
updateAllNodes(); |
| 198 |
} |
| 199 |
|
| 200 |
public boolean isLikelihoodKnown() { |
| 201 |
return likelihoodKnown; |
| 202 |
} |
| 203 |
|
| 204 |
protected abstract double calculateLogLikelihood(); |
| 205 |
|
| 206 |
public String getReport() { |
| 207 |
if (hasInitialized) { |
| 208 |
String rtnValue = getClass().getName() + "(" + getLogLikelihood() + ")"; |
| 209 |
if (COUNT_TOTAL_OPERATIONS) |
| 210 |
rtnValue += "\n total operations = " + totalOperationCount + |
| 211 |
"\n matrix updates = " + totalMatrixUpdateCount + |
| 212 |
"\n model changes = " + totalModelChangedCount + |
| 213 |
"\n make dirties = " + totalMakeDirtyCount + |
| 214 |
"\n calculate likelihoods = " + totalCalculateLikelihoodCount + |
| 215 |
"\n get likelihoods = " + totalGetLogLikelihoodCount + |
| 216 |
"\n all rate updates = " + totalRateUpdateAllCount + |
| 217 |
"\n partial rate updates = " + totalRateUpdateSingleCount; |
| 218 |
return rtnValue; |
| 219 |
} else { |
| 220 |
return getClass().getName() + "(uninitialized)"; |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
protected TreeModel treeModel = null; |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
protected int nodeCount; |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
protected boolean[] updateNode; |
| 242 |
|
| 243 |
private double logLikelihood; |
| 244 |
private double storedLogLikelihood; |
| 245 |
protected boolean likelihoodKnown = false; |
| 246 |
private boolean storedLikelihoodKnown = false; |
| 247 |
|
| 248 |
protected boolean hasInitialized = false; |
| 249 |
|
| 250 |
protected int totalOperationCount = 0; |
| 251 |
protected int totalMatrixUpdateCount = 0; |
| 252 |
protected int totalGetLogLikelihoodCount = 0; |
| 253 |
protected int totalModelChangedCount = 0; |
| 254 |
protected int totalMakeDirtyCount = 0; |
| 255 |
protected int totalCalculateLikelihoodCount = 0; |
| 256 |
protected int totalRateUpdateAllCount = 0; |
| 257 |
protected int totalRateUpdateSingleCount = 0; |
| 258 |
|
| 259 |
} |