| 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.continuous; |
| 27 |
|
| 28 |
import dr.evolution.alignment.PatternList; |
| 29 |
import dr.evolution.datatype.TwoStates; |
| 30 |
import dr.evolution.tree.NodeRef; |
| 31 |
import dr.evolution.util.Taxon; |
| 32 |
import dr.evomodel.tree.TreeModel; |
| 33 |
import dr.inference.model.*; |
| 34 |
import dr.math.distributions.Distribution; |
| 35 |
import dr.util.Citable; |
| 36 |
import dr.util.Citation; |
| 37 |
import dr.util.CommonCitations; |
| 38 |
import dr.xml.*; |
| 39 |
|
| 40 |
import java.util.ArrayList; |
| 41 |
import java.util.LinkedHashMap; |
| 42 |
import java.util.List; |
| 43 |
import java.util.Map; |
| 44 |
import java.util.logging.Logger; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public class BinaryLatentLiabilityLikelihood extends AbstractModelLikelihood implements LatentTruncation, Citable, SoftThresholdLikelihood { |
| 56 |
|
| 57 |
public final static String LATENT_LIABILITY_LIKELIHOOD = "latentLiabilityLikelihood"; |
| 58 |
|
| 59 |
public BinaryLatentLiabilityLikelihood(TreeModel treeModel, PatternList patternList, CompoundParameter tipTraitParameter) { |
| 60 |
super(LATENT_LIABILITY_LIKELIHOOD); |
| 61 |
this.treeModel = treeModel; |
| 62 |
this.patternList = patternList; |
| 63 |
this.tipTraitParameter = tipTraitParameter; |
| 64 |
|
| 65 |
addVariable(tipTraitParameter); |
| 66 |
|
| 67 |
setTipDataValuesForAllNodes(); |
| 68 |
|
| 69 |
StringBuilder sb = new StringBuilder(); |
| 70 |
sb.append("Constructing a latent liability likelihood model:\n"); |
| 71 |
sb.append("\tBinary patterns: ").append(patternList.getId()).append("\n"); |
| 72 |
sb.append("\tPlease cite:\n").append(Citable.Utils.getCitationString(this)); |
| 73 |
Logger.getLogger("dr.evomodel.continous").info(sb.toString()); |
| 74 |
} |
| 75 |
|
| 76 |
private void setTipDataValuesForAllNodes() { |
| 77 |
if (tipData == null) { |
| 78 |
tipData = new boolean[treeModel.getExternalNodeCount()][patternList.getPatternCount()]; |
| 79 |
} |
| 80 |
for (int i = 0; i < treeModel.getExternalNodeCount(); i++) { |
| 81 |
NodeRef node = treeModel.getExternalNode(i); |
| 82 |
String id = treeModel.getTaxonId(i); |
| 83 |
int index = patternList.getTaxonIndex(id); |
| 84 |
System.err.println("\t For node: " + i + " with ID " + id + " you get taxon " + index + " with ID " + patternList.getTaxonId(index)); |
| 85 |
System.err.println("taxon: "+id+", index: "+index); |
| 86 |
setTipDataValuesForNode(node, index); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
private void setTipDataValuesForNode(NodeRef node, int indexFromPatternList) { |
| 91 |
|
| 92 |
int index = node.getNumber(); |
| 93 |
|
| 94 |
if (index != indexFromPatternList) { |
| 95 |
throw new RuntimeException("Need to figure out the indexing"); |
| 96 |
} |
| 97 |
|
| 98 |
for (int datum = 0; datum < patternList.getPatternCount(); ++datum) { |
| 99 |
tipData[index][datum] = patternList.getPattern(datum)[index] == 1; |
| 100 |
if (DEBUG) { |
| 101 |
Parameter oneTipTraitParameter = tipTraitParameter.getParameter(index); |
| 102 |
System.err.println("Data = " + tipData[index][datum] + " : " + oneTipTraitParameter.getParameterValue(datum)); |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
@Override |
| 108 |
protected void handleModelChangedEvent(Model model, Object object, int index) { |
| 109 |
} |
| 110 |
|
| 111 |
@Override |
| 112 |
protected void handleVariableChangedEvent(Variable variable, int index, Variable.ChangeType type) { |
| 113 |
likelihoodKnown = false; |
| 114 |
} |
| 115 |
|
| 116 |
@Override |
| 117 |
protected void storeState() { |
| 118 |
storedLogLikelihood = logLikelihood; |
| 119 |
} |
| 120 |
|
| 121 |
@Override |
| 122 |
protected void restoreState() { |
| 123 |
logLikelihood = storedLogLikelihood; |
| 124 |
likelihoodKnown = true; |
| 125 |
} |
| 126 |
|
| 127 |
@Override |
| 128 |
protected void acceptState() { |
| 129 |
|
| 130 |
} |
| 131 |
|
| 132 |
public void makeDirty() { |
| 133 |
likelihoodKnown = false; |
| 134 |
} |
| 135 |
|
| 136 |
public Model getModel() { |
| 137 |
return this; |
| 138 |
} |
| 139 |
|
| 140 |
public double getLogLikelihood() { |
| 141 |
if (!likelihoodKnown) { |
| 142 |
logLikelihood = computeLogLikelihood(); |
| 143 |
likelihoodKnown = true; |
| 144 |
} |
| 145 |
return logLikelihood; |
| 146 |
} |
| 147 |
|
| 148 |
public String toString() { |
| 149 |
return getClass().getName() + "(" + getLogLikelihood() + ")"; |
| 150 |
} |
| 151 |
|
| 152 |
protected double computeLogLikelihood() { |
| 153 |
boolean valid = true; |
| 154 |
for (int tip = 0; tip < tipData.length && valid; ++tip) { |
| 155 |
valid = validTraitForTip(tip); |
| 156 |
} |
| 157 |
if (valid) { |
| 158 |
return 0.0; |
| 159 |
} else { |
| 160 |
return Double.NEGATIVE_INFINITY; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
public boolean validTraitForTip(int tip) { |
| 165 |
boolean valid = true; |
| 166 |
Parameter oneTipTraitParameter = tipTraitParameter.getParameter(tip); |
| 167 |
boolean[] data = tipData[tip]; |
| 168 |
for (int index = 0; index < data.length && valid; ++index) { |
| 169 |
boolean datum = data[index]; |
| 170 |
double trait = oneTipTraitParameter.getParameterValue(index); |
| 171 |
boolean positive = trait > 0.0; |
| 172 |
valid = datum == positive; |
| 173 |
} |
| 174 |
return valid; |
| 175 |
} |
| 176 |
|
| 177 |
public double getNormalizationConstant(Distribution working) { |
| 178 |
return normalizationDelegate.getNormalizationConstant(working); |
| 179 |
} |
| 180 |
|
| 181 |
public void setPathParameter(double beta){ |
| 182 |
pathParameter=beta; |
| 183 |
} |
| 184 |
|
| 185 |
@Override |
| 186 |
public double getLikelihoodCorrection() { |
| 187 |
return 0; |
| 188 |
} |
| 189 |
|
| 190 |
private final LatentTruncation.Delegate normalizationDelegate = new LatentTruncation.Delegate() { |
| 191 |
|
| 192 |
protected double computeNormalizationConstant(Distribution working) { |
| 193 |
|
| 194 |
double constant = 0.0; |
| 195 |
return constant; |
| 196 |
} |
| 197 |
}; |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 204 |
|
| 205 |
public final static String TIP_TRAIT = "tipTrait"; |
| 206 |
|
| 207 |
public String getParserName() { |
| 208 |
return LATENT_LIABILITY_LIKELIHOOD; |
| 209 |
} |
| 210 |
|
| 211 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 212 |
|
| 213 |
AbstractMultivariateTraitLikelihood traitLikelihood = (AbstractMultivariateTraitLikelihood) |
| 214 |
xo.getChild(AbstractMultivariateTraitLikelihood.class); |
| 215 |
PatternList patternList = (PatternList) xo.getChild(PatternList.class); |
| 216 |
TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class); |
| 217 |
CompoundParameter tipTraitParameter = (CompoundParameter) xo.getElementFirstChild(TIP_TRAIT); |
| 218 |
|
| 219 |
int numTaxa = treeModel.getTaxonCount(); |
| 220 |
int numData = traitLikelihood.getNumData(); |
| 221 |
int dimTrait = traitLikelihood.getDimTrait(); |
| 222 |
|
| 223 |
if (tipTraitParameter.getDimension() != numTaxa * numData * dimTrait) { |
| 224 |
throw new XMLParseException("Tip trait parameter is wrong dimension in latent liability model"); |
| 225 |
} |
| 226 |
|
| 227 |
if (!(patternList.getDataType() instanceof TwoStates)) { |
| 228 |
throw new XMLParseException("Latent liability model currently only works for binary data"); |
| 229 |
} |
| 230 |
|
| 231 |
if (patternList.getPatternCount() != numData * dimTrait) { |
| 232 |
throw new XMLParseException("Binary data is wrong dimension in latent liability model"); |
| 233 |
} |
| 234 |
|
| 235 |
return new BinaryLatentLiabilityLikelihood(treeModel, patternList, tipTraitParameter); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
public String getParserDescription() { |
| 243 |
return "Provides the likelihood of a latent liability model on multivariate-binary trait data"; |
| 244 |
} |
| 245 |
|
| 246 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 247 |
return rules; |
| 248 |
} |
| 249 |
|
| 250 |
private final XMLSyntaxRule[] rules = { |
| 251 |
new ElementRule(AbstractMultivariateTraitLikelihood.class, "The model for the latent random variables"), |
| 252 |
new ElementRule(TIP_TRAIT, CompoundParameter.class, "The parameter of tip locations from the tree"), |
| 253 |
new ElementRule(PatternList.class, "The binary tip data"), |
| 254 |
new ElementRule(TreeModel.class, "The tree model"), |
| 255 |
}; |
| 256 |
|
| 257 |
public Class getReturnType() { |
| 258 |
return BinaryLatentLiabilityLikelihood.class; |
| 259 |
} |
| 260 |
}; |
| 261 |
|
| 262 |
@Override |
| 263 |
public Citation.Category getCategory() { |
| 264 |
return Citation.Category.TRAIT_MODELS; |
| 265 |
} |
| 266 |
|
| 267 |
@Override |
| 268 |
public String getDescription() { |
| 269 |
return "Latent Liability model"; |
| 270 |
} |
| 271 |
|
| 272 |
@Override |
| 273 |
public List<Citation> getCitations() { |
| 274 |
List<Citation> citations = new ArrayList<Citation>(); |
| 275 |
citations.add(CommonCitations.CYBIS_2015_ASSESSING); |
| 276 |
return citations; |
| 277 |
} |
| 278 |
|
| 279 |
private TreeModel treeModel; |
| 280 |
private PatternList patternList; |
| 281 |
private CompoundParameter tipTraitParameter; |
| 282 |
|
| 283 |
private boolean[][] tipData; |
| 284 |
|
| 285 |
private boolean likelihoodKnown = false; |
| 286 |
private double logLikelihood; |
| 287 |
private double storedLogLikelihood; |
| 288 |
|
| 289 |
private static final boolean DEBUG = false; |
| 290 |
|
| 291 |
private double pathParameter=1; |
| 292 |
|
| 293 |
} |