| 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.Tree; |
| 29 |
import dr.evolution.util.Units; |
| 30 |
import dr.evomodelxml.speciation.SpeciationLikelihoodParser; |
| 31 |
import dr.inference.model.*; |
| 32 |
|
| 33 |
import java.util.List; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class ModelAveragingSpeciationLikelihood extends AbstractModelLikelihood implements Units { |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public ModelAveragingSpeciationLikelihood(List<Tree> trees, List<MaskableSpeciationModel> speciationModels, |
| 47 |
Variable<Integer> indexVariable, Variable<Double> maxIndexVariable, String id) { |
| 48 |
this(SpeciationLikelihoodParser.SPECIATION_LIKELIHOOD, trees, speciationModels, indexVariable, maxIndexVariable); |
| 49 |
setId(id); |
| 50 |
} |
| 51 |
|
| 52 |
public ModelAveragingSpeciationLikelihood(String name, List<Tree> trees, List<MaskableSpeciationModel> speciationModels, |
| 53 |
Variable<Integer> indexVariable, Variable<Double> maxIndexVariable) { |
| 54 |
|
| 55 |
super(name); |
| 56 |
|
| 57 |
this.trees = trees; |
| 58 |
this.speciationModels = speciationModels; |
| 59 |
|
| 60 |
if (trees.size() != speciationModels.size()) { |
| 61 |
throw new IllegalArgumentException("The number of trees and the number of speciation models should be equal."); |
| 62 |
} |
| 63 |
|
| 64 |
for (Tree tree : trees) { |
| 65 |
if (tree instanceof Model) { |
| 66 |
addModel((Model) tree); |
| 67 |
} |
| 68 |
} |
| 69 |
for (SpeciationModel speciationModel : speciationModels) { |
| 70 |
if (speciationModel != null) { |
| 71 |
addModel(speciationModel); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
if ( (indexVariable.getSize() + 1) != trees.size()) { |
| 76 |
throw new IllegalArgumentException("Index parameter must be same size as the number of trees."); |
| 77 |
} |
| 78 |
this.indexVariable = indexVariable; |
| 79 |
for (int i = 0; i < indexVariable.getSize(); i++) { |
| 80 |
indexVariable.setValue(i, i+1); |
| 81 |
} |
| 82 |
indexVariable.addBounds(new Bounds.Staircase(indexVariable)); |
| 83 |
addVariable(indexVariable); |
| 84 |
|
| 85 |
for (int i = 0; i < maxIndexVariable.getSize(); i++) { |
| 86 |
maxIndexVariable.setValue(i, 0.0); |
| 87 |
} |
| 88 |
this.maxIndexVariable = maxIndexVariable; |
| 89 |
addVariable(maxIndexVariable); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
protected final void handleModelChangedEvent(Model model, Object object, int index) { |
| 97 |
likelihoodKnown = false; |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 105 |
likelihoodKnown = false; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
protected final void storeState() { |
| 116 |
storedLikelihoodKnown = likelihoodKnown; |
| 117 |
storedLogLikelihood = logLikelihood; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
protected final void restoreState() { |
| 124 |
likelihoodKnown = storedLikelihoodKnown; |
| 125 |
logLikelihood = storedLogLikelihood; |
| 126 |
} |
| 127 |
|
| 128 |
protected final void acceptState() { |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public final Model getModel() { |
| 136 |
return this; |
| 137 |
} |
| 138 |
|
| 139 |
public final double getLogLikelihood() { |
| 140 |
if (!likelihoodKnown) { |
| 141 |
logLikelihood = calculateLogLikelihood(); |
| 142 |
likelihoodKnown = true; |
| 143 |
} |
| 144 |
return logLikelihood; |
| 145 |
} |
| 146 |
|
| 147 |
public final void makeDirty() { |
| 148 |
likelihoodKnown = false; |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
private double calculateLogLikelihood() { |
| 158 |
|
| 159 |
double logL = 0; |
| 160 |
|
| 161 |
|
| 162 |
if (!isValidate(indexVariable.getValues())) { |
| 163 |
|
| 164 |
return Double.NEGATIVE_INFINITY; |
| 165 |
} |
| 166 |
|
| 167 |
for (int i = 0; i < trees.size(); i++) { |
| 168 |
MaskableSpeciationModel model = speciationModels.get(i); |
| 169 |
if (i > 0) { |
| 170 |
SpeciationModel mask = speciationModels.get(indexVariable.getValue(i-1)); |
| 171 |
if (model != mask) { |
| 172 |
model.mask(mask); |
| 173 |
} else { |
| 174 |
model.unmask(); |
| 175 |
} |
| 176 |
} |
| 177 |
logL += model.calculateTreeLogLikelihood(trees.get(i)); |
| 178 |
} |
| 179 |
|
| 180 |
Double maxI = (double) (int) getMaxIndex(indexVariable.getValues()); |
| 181 |
maxIndexVariable.setValue(0, maxI); |
| 182 |
|
| 183 |
return logL; |
| 184 |
} |
| 185 |
|
| 186 |
private boolean isValidate(Integer[] pattern) { |
| 187 |
|
| 188 |
int[] indexFreq = new int[pattern.length]; |
| 189 |
for (int i = 0; i < pattern.length; i++) { |
| 190 |
if (pattern[i] > 0) |
| 191 |
indexFreq[pattern[i] - 1] += 1; |
| 192 |
|
| 193 |
if (i > 0 && (pattern[i] - pattern[i - 1] > 1)) { |
| 194 |
for (int f = 0; f < i; f++) { |
| 195 |
if (indexFreq[f] < 1) return false; |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
return true; |
| 201 |
} |
| 202 |
|
| 203 |
private int getMaxIndex(Integer[] pattern) { |
| 204 |
int max = 0; |
| 205 |
|
| 206 |
for (int p : pattern) { |
| 207 |
if (p > max) { |
| 208 |
max = p; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
return max; |
| 213 |
} |
| 214 |
|
| 215 |
private void output(String message, Variable<Integer> indexVariable) { |
| 216 |
System.out.print(message + ": "); |
| 217 |
for (int i = 0; i < indexVariable.getSize(); i++) { |
| 218 |
System.out.print(indexVariable.getValue(i) + "\t"); |
| 219 |
} |
| 220 |
System.out.println(); |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
public final dr.inference.loggers.LogColumn[] getColumns() { |
| 231 |
|
| 232 |
String columnName = getId(); |
| 233 |
if (columnName == null) columnName = getModelName() + ".likelihood"; |
| 234 |
|
| 235 |
return new dr.inference.loggers.LogColumn[]{ |
| 236 |
new LikelihoodColumn(columnName) |
| 237 |
}; |
| 238 |
} |
| 239 |
|
| 240 |
private final class LikelihoodColumn extends dr.inference.loggers.NumberColumn { |
| 241 |
public LikelihoodColumn(String label) { |
| 242 |
super(label); |
| 243 |
} |
| 244 |
|
| 245 |
public double getDoubleValue() { |
| 246 |
return getLogLikelihood(); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
public final void setUnits(Type u) { |
| 259 |
for (SpeciationModel speciationModel : speciationModels) { |
| 260 |
speciationModel.setUnits(u); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
public final Type getUnits() { |
| 269 |
return speciationModels.get(0).getUnits(); |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
List<MaskableSpeciationModel> speciationModels = null; |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
List<Tree> trees = null; |
| 285 |
|
| 286 |
Variable<Integer> indexVariable = null; |
| 287 |
Variable<Double> maxIndexVariable = null; |
| 288 |
|
| 289 |
private double logLikelihood; |
| 290 |
private double storedLogLikelihood; |
| 291 |
private boolean likelihoodKnown = false; |
| 292 |
private boolean storedLikelihoodKnown = false; |
| 293 |
} |