Debian Codegraph @

src/dr/inference/model/PathLikelihood.java

raw symbols SCIP
1 /*
2 * PathLikelihood.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.inference.model;
27
28 import dr.evomodel.continuous.SoftThresholdLikelihood;
29 import dr.xml.*;
30
31 import java.util.ArrayList;
32 import java.util.HashSet;
33 import java.util.Set;
34
35 /**
36 * A likelihood function which is simply the product of a set of likelihood functions.
37 *
38 * @author Andrew Rambaut
39 * @author Alex Alekseyenko
40 * @author Marc Suchard
41 * @version $Id: CompoundLikelihood.java,v 1.19 2005/05/25 09:14:36 rambaut Exp $
42 */
43 public class PathLikelihood implements Likelihood {
44
45 public static final String PATH_LIKELIHOOD = "pathLikelihood";
46 public static final String PATH_PARAMETER = "theta";
47 public static final String DIFFERENCE = "delta";
48 public static final String SOURCE = "source";
49 public static final String DESTINATION = "destination";
50 public static final String PSUEDO_SOURCE = "sourcePseudoPrior";
51 public static final String PSUEDO_DESTINATION = "destinationPseudoPrior";
52
53 public PathLikelihood(Likelihood source, Likelihood destination) {
54 this(source, destination, null, null);
55 }
56
57 public PathLikelihood(Likelihood source, Likelihood destination,
58 Likelihood pseudoSource, Likelihood pseudoDestination) {
59 this.source = source;
60 this.destination = destination;
61 this.pseudoSource = pseudoSource;
62 this.pseudoDestination = pseudoDestination;
63 if(source!=null) {
64 for (int j = 0; j < source.getModel().getModelCount(); j++) {
65 for (int i = 0; i < source.getModel().getModel(j).getModelCount(); i++) {
66 if (source.getModel().getModel(j).getModel(i) instanceof SoftThresholdLikelihood) {
67 thresholdSofteners.add((SoftThresholdLikelihood) source.getModel().getModel(j).getModel(i));
68 }
69 }
70 }
71 }
72
73 if(pseudoSource!=null){
74 for (int i = 0; i < pseudoSource.getModel().getModelCount(); i++) {
75 if(pseudoSource.getModel().getModel(i) instanceof SoftThresholdLikelihood){
76 thresholdSofteners.add((SoftThresholdLikelihood) pseudoSource.getModel().getModel(i));
77 }
78 }
79 }
80
81 compoundModel.addModel(source.getModel());
82 compoundModel.addModel(destination.getModel());
83 }
84
85 public double getPathParameter() {
86 return pathParameter;
87 }
88
89 public void setPathParameter(double pathParameter) {
90 this.pathParameter = pathParameter;
91 if(thresholdSofteners!=null){
92 for(SoftThresholdLikelihood threshold:thresholdSofteners){
93 threshold.setPathParameter(pathParameter);
94 }
95 }
96 }
97
98 // **************************************************************
99 // Likelihood IMPLEMENTATION
100 // **************************************************************
101
102 public Model getModel() {
103 return compoundModel;
104 }
105
106 public double getLogLikelihood() {
107 // Depends on complete model (include pseudo-densities)
108 return (source.getLogLikelihood() * pathParameter) + (destination.getLogLikelihood() * (1.0 - pathParameter));
109 }
110
111 public Likelihood getSourceLikelihood() {
112 return source;
113 }
114
115 public Likelihood getDestinationLikelihood() {
116 return destination;
117 }
118
119 @Override
120 public Set<Likelihood> getLikelihoodSet() {
121 Set<Likelihood> set = new HashSet<Likelihood>();
122 set.add(source);
123 set.add(destination);
124 return set;
125 }
126
127 public void makeDirty() {
128 source.makeDirty();
129 destination.makeDirty();
130 }
131
132 public boolean evaluateEarly() {
133 return false;
134 }
135
136 public String toString() {
137
138 return Double.toString(getLogLikelihood());
139
140 }
141
142 // **************************************************************
143 // Loggable IMPLEMENTATION
144 // **************************************************************
145
146 /**
147 * @return the log columns.
148 */
149 public dr.inference.loggers.LogColumn[] getColumns() {
150 return new dr.inference.loggers.LogColumn[]{
151 new DeltaLogLikelihoodColumn(getId() + "." + DIFFERENCE),
152 new PosteriorColumn(getId() + "." + SOURCE),
153 new PriorColumn(getId() + "." + DESTINATION),
154 new ThetaColumn(getId() + "." + PATH_PARAMETER),
155 new PathLikelihoodColumn(getId() + "." + PATH_LIKELIHOOD)
156 };
157 }
158
159 private class PriorColumn extends dr.inference.loggers.NumberColumn {
160
161 public PriorColumn(String label) {
162 super(label);
163 }
164
165 public double getDoubleValue() {
166 //assume that the prior is the destination in the BEAST xml
167 return destination.getLogLikelihood();
168 }
169
170 }
171
172 private class PosteriorColumn extends dr.inference.loggers.NumberColumn {
173
174 public PosteriorColumn(String label) {
175 super(label);
176 }
177
178 public double getDoubleValue() {
179 //assume that the posterior is the source in the BEAST xml
180 return source.getLogLikelihood();
181 }
182
183 }
184
185 private class DeltaLogLikelihoodColumn extends dr.inference.loggers.NumberColumn {
186
187 public DeltaLogLikelihoodColumn(String label) {
188 super(label);
189 }
190
191 public double getDoubleValue() {
192 // Remove pseudo-densities
193 double totalLikelihoodCorrection = 0;
194 if(thresholdSofteners!=null){
195 for(SoftThresholdLikelihood threshold:thresholdSofteners){
196 totalLikelihoodCorrection += threshold.getLikelihoodCorrection();
197 }
198 }
199
200 double logDensity = source.getLogLikelihood() - destination.getLogLikelihood() + totalLikelihoodCorrection;
201 if (pseudoSource != null) {
202 logDensity -= pseudoSource.getLogLikelihood();
203 }
204 if (pseudoDestination != null) {
205 // System.err.println("value = "+pseudoDestination.getLogLikelihood());
206 // logDensity += pseudoDestination.getLogLikelihood();
207 }
208 return logDensity;
209 }
210 }
211
212 private class ThetaColumn extends dr.inference.loggers.NumberColumn {
213
214 public ThetaColumn(String label) {
215 super(label);
216 }
217
218 public double getDoubleValue() {
219 return pathParameter;
220 }
221
222 }
223
224 private class PathLikelihoodColumn extends dr.inference.loggers.NumberColumn {
225
226 public PathLikelihoodColumn(String label) {
227 super(label);
228 }
229
230 public double getDoubleValue() {
231 return getLogLikelihood();
232 }
233
234 }
235
236 public boolean isUsed() {
237 return isUsed;
238 }
239
240 public void setUsed() {
241 isUsed = true;
242 }
243
244 private boolean isUsed = false;
245
246 // **************************************************************
247 // Identifiable IMPLEMENTATION
248 // **************************************************************
249
250 private String id = null;
251
252 public void setId(String id) {
253 this.id = id;
254 }
255
256 public String getId() {
257 return id;
258 }
259
260 public String prettyName() {
261 return Abstract.getPrettyName(this);
262 }
263
264 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
265
266 public String getParserName() {
267 return PATH_LIKELIHOOD;
268 }
269
270 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
271
272 Likelihood source = (Likelihood) xo.getElementFirstChild(SOURCE);
273 Likelihood destination = (Likelihood) xo.getElementFirstChild(DESTINATION);
274
275 Likelihood pseudoSource = null;
276 if (xo.hasChildNamed(PSUEDO_SOURCE)) {
277 pseudoSource = (Likelihood) xo.getElementFirstChild(PSUEDO_SOURCE);
278 }
279 Likelihood pseudoDestination = null;
280 if (xo.hasChildNamed(PSUEDO_DESTINATION)) {
281 pseudoDestination = (Likelihood) xo.getElementFirstChild(PSUEDO_DESTINATION);
282 }
283
284 return new PathLikelihood(source, destination, pseudoSource, pseudoDestination);
285 }
286
287 //************************************************************************
288 // AbstractXMLObjectParser implementation
289 //************************************************************************
290
291 public String getParserDescription() {
292 return "A likelihood function used for estimating marginal likelihoods and Bayes factors using path sampling.";
293 }
294
295 public XMLSyntaxRule[] getSyntaxRules() {
296 return rules;
297 }
298
299 private final XMLSyntaxRule[] rules = {
300 new ElementRule(SOURCE,
301 new XMLSyntaxRule[]{new ElementRule(Likelihood.class)}),
302 new ElementRule(DESTINATION,
303 new XMLSyntaxRule[]{new ElementRule(Likelihood.class)}),
304 new ElementRule(PSUEDO_SOURCE,
305 new XMLSyntaxRule[]{new ElementRule(Likelihood.class)}, true),
306 new ElementRule(PSUEDO_DESTINATION,
307 new XMLSyntaxRule[]{new ElementRule(Likelihood.class)}, true),
308 };
309
310 public Class getReturnType() {
311 return CompoundLikelihood.class;
312 }
313 };
314
315 private final Likelihood source;
316 private final Likelihood destination;
317 private final Likelihood pseudoSource;
318 private final Likelihood pseudoDestination;
319
320 private double pathParameter;
321
322 private final CompoundModel compoundModel = new CompoundModel("compoundModel");
323
324 private ArrayList<SoftThresholdLikelihood> thresholdSofteners=new ArrayList<SoftThresholdLikelihood>();
325 }