Debian Codegraph @

src/dr/inference/model/Likelihood.java

raw symbols SCIP
1 /*
2 * Likelihood.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.inference.loggers.Loggable;
29 import dr.util.Identifiable;
30
31 import java.util.Arrays;
32 import java.util.HashSet;
33 import java.util.Set;
34
35 /**
36 * classes that calculate likelihoods should implement this interface.
37 *
38 * @author Alexei Drummond
39 * @author Andrew Rambaut
40 * @version $Id: Likelihood.java,v 1.16 2005/05/24 20:26:00 rambaut Exp $
41 */
42
43 public interface Likelihood extends Loggable, Identifiable {
44
45 /**
46 * Get the model.
47 *
48 * @return the model.
49 */
50 Model getModel();
51
52 /**
53 * Get the log likelihood.
54 *
55 * @return the log likelihood.
56 */
57 double getLogLikelihood();
58
59 /**
60 * Forces a complete recalculation of the likelihood next time getLikelihood is called
61 */
62 void makeDirty();
63
64 /**
65 * @return A detailed name of likelihood for debugging.
66 */
67 String prettyName();
68
69 /**
70 * Get the set of sub-component likelihoods that this likelihood uses
71 *
72 * @return
73 */
74 Set<Likelihood> getLikelihoodSet();
75
76 /**
77 * @return is the likelihood used in the MCMC?
78 */
79 boolean isUsed();
80
81 void setUsed();
82
83 /**
84 * @return true if this likelihood should be evaluated early (if for example it may return a zero likelihood
85 * and could terminate the evaluation early or is a required conditioning for another likelihood.
86 */
87 boolean evaluateEarly();
88
89 /**
90 * A simple abstract base class for likelihood functions
91 */
92
93 public abstract class Abstract implements Likelihood, ModelListener {
94
95 public Abstract(Model model) {
96
97 this.model = model;
98 if (model != null) model.addModelListener(this);
99 }
100
101 public void modelChangedEvent(Model model, Object object, int index) {
102 makeDirty();
103 }
104
105 // by default restore is the same as changed
106 public void modelRestored(Model model) {
107 makeDirty();
108 }
109
110 // **************************************************************
111 // Likelihood IMPLEMENTATION
112 // **************************************************************
113
114 /**
115 * Get the model.
116 *
117 * @return the model.
118 */
119 public Model getModel() {
120 return model;
121 }
122
123 public final double getLogLikelihood() {
124 if (!getLikelihoodKnown()) {
125 logLikelihood = calculateLogLikelihood();
126 likelihoodKnown = true;
127 }
128 return logLikelihood;
129 }
130
131 public void makeDirty() {
132 likelihoodKnown = false;
133 }
134
135 /**
136 * Called to decide if the likelihood must be calculated. Can be overridden
137 * (for example, to always return false).
138 *
139 * @return true if no need to recompute likelihood
140 */
141 protected boolean getLikelihoodKnown() {
142 return likelihoodKnown;
143 }
144
145 protected abstract double calculateLogLikelihood();
146
147 public Set<Likelihood> getLikelihoodSet() {
148 return new HashSet<Likelihood>(Arrays.asList(this));
149 }
150
151 public String toString() {
152 // don't call any "recalculating" stuff like getLogLikelihood() in toString -
153 // this interferes with the debugger.
154
155 //return getClass().getName() + "(" + getLogLikelihood() + ")";
156 return getClass().getName() + "(" + (getLikelihoodKnown() ? logLikelihood : "??") + ")";
157 }
158
159 static public String getPrettyName(Likelihood l) {
160 final Model m = l.getModel();
161 String s = l.getClass().getName();
162 String[] parts = s.split("\\.");
163 s = parts[parts.length - 1];
164 if (m != null) {
165 final String modelName = m.getModelName();
166 final String i = m.getId();
167 s = s + "(" + modelName;
168 if (i != null && !i.equals(modelName)) {
169 s = s + '[' + i + ']';
170 }
171 s = s + ")";
172 }
173 return s;
174 }
175
176 public String prettyName() {
177 return getPrettyName(this);
178 }
179
180 public boolean isUsed() {
181 return used;
182 }
183
184 public void setUsed() {
185 this.used = true;
186 }
187
188 public boolean evaluateEarly() {
189 return false;
190 }
191
192 // **************************************************************
193 // Loggable IMPLEMENTATION
194 // **************************************************************
195
196 /**
197 * @return the log columns.
198 */
199 public dr.inference.loggers.LogColumn[] getColumns() {
200 return new dr.inference.loggers.LogColumn[]{
201 new LikelihoodColumn(getId())
202 };
203 }
204
205 private class LikelihoodColumn extends dr.inference.loggers.NumberColumn {
206 public LikelihoodColumn(String label) {
207 super(label);
208 }
209
210 public double getDoubleValue() {
211 return getLogLikelihood();
212 }
213 }
214
215 // **************************************************************
216 // Identifiable IMPLEMENTATION
217 // **************************************************************
218
219 private String id = null;
220
221 public void setId(String id) {
222 this.id = id;
223 }
224
225 public String getId() {
226 return id;
227 }
228
229 private final Model model;
230 private double logLikelihood;
231 private boolean likelihoodKnown = false;
232
233 private boolean used = false;
234 }
235
236
237 // set to store all created likelihoods
238 final static Set<Likelihood> FULL_LIKELIHOOD_SET = new HashSet<Likelihood>();
239 final static Set<Likelihood> CONNECTED_LIKELIHOOD_SET = new HashSet<Likelihood>();
240
241 }