Debian Codegraph @

src/dr/evomodel/continuous/MultinomialLatentLiabilityLikelihood.java

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