Debian Codegraph @

src/dr/evomodel/continuous/IntervalLatentLiabilityLikelihood.java

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