Debian Codegraph @

src/dr/inference/model/WeightedMixtureModel.java

raw symbols SCIP
1 /*
2 * WeightedMixtureModel.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.LogColumn;
29 import dr.math.LogTricks;
30 import dr.util.Citable;
31 import dr.util.Citation;
32 import dr.util.CommonCitations;
33 import dr.xml.*;
34
35 import java.util.*;
36 import java.util.logging.Logger;
37
38 /**
39 * @author Marc A. Suchard
40 * @author Andrew Rambaut
41 */
42 public class WeightedMixtureModel extends AbstractModelLikelihood implements Citable {
43
44 public static final String MIXTURE_MODEL = "mixtureModel";
45 // public static final String MIXTURE_WEIGHTS = "weights";
46 public static final String NORMALIZE = "normalize";
47
48 public WeightedMixtureModel(List<AbstractModelLikelihood> likelihoodList, Parameter mixtureWeights) {
49 super(MIXTURE_MODEL);
50 this.likelihoodList = likelihoodList;
51 this.mixtureWeights = mixtureWeights;
52 for (AbstractModelLikelihood model : likelihoodList) {
53 addModel(model);
54 }
55 addVariable(mixtureWeights);
56
57 StringBuilder sb = new StringBuilder();
58 sb.append("Constructing a finite mixture model\n");
59 sb.append("\tComponents:\n");
60 for (AbstractModelLikelihood model : likelihoodList) {
61 sb.append("\t\t\t").append(model.getId()).append("\n");
62 }
63 sb.append("\tMixing parameter: ").append(mixtureWeights.getId()).append("\n");
64 sb.append("\tPlease cite:\n");
65 sb.append(Citable.Utils.getCitationString((this)));
66
67 Logger.getLogger("dr.inference.model").info(sb.toString());
68 }
69
70 protected void handleModelChangedEvent(Model model, Object object, int index) {
71 }
72
73 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
74 }
75
76 protected void storeState() {
77 }
78
79 protected void restoreState() {
80 }
81
82 protected void acceptState() {
83 }
84
85 public Model getModel() {
86 return this;
87 }
88
89 public double getLogLikelihood() {
90 double logSum = Double.NEGATIVE_INFINITY;
91 for (int i = 0; i < likelihoodList.size(); ++i) {
92 double pi = mixtureWeights.getParameterValue(i);
93 if (pi > 0.0) {
94 logSum = LogTricks.logSum(logSum,
95 Math.log(pi) + likelihoodList.get(i).getLogLikelihood());
96 }
97 }
98 return logSum;
99 }
100
101 public void makeDirty() {
102 }
103
104 public LogColumn[] getColumns() {
105 return new LogColumn[0];
106 }
107
108 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
109
110 public String getParserName() {
111 return MIXTURE_MODEL;
112 }
113
114 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
115
116 Parameter weights = (Parameter) xo.getChild(Parameter.class);
117 List<AbstractModelLikelihood> likelihoodList = new ArrayList<AbstractModelLikelihood>();
118
119 for (int i = 0; i < xo.getChildCount(); i++) {
120 if (xo.getChild(i) instanceof Likelihood)
121 likelihoodList.add((AbstractModelLikelihood) xo.getChild(i));
122 }
123
124 if (weights.getDimension() != likelihoodList.size()) {
125 throw new XMLParseException("Dim of " + weights.getId() + " does not match the number of likelihoods");
126 }
127
128 if (xo.hasAttribute(NORMALIZE)) {
129 if (xo.getBooleanAttribute(NORMALIZE)) {
130 double sum = 0;
131 for (int i = 0; i < weights.getDimension(); i++)
132 sum += weights.getParameterValue(i);
133 for (int i = 0; i < weights.getDimension(); i++)
134 weights.setParameterValue(i, weights.getParameterValue(i) / sum);
135 }
136 }
137
138 if (!normalized(weights))
139 throw new XMLParseException("Parameter +" + weights.getId() + " must lie on the simplex");
140
141 return new WeightedMixtureModel(likelihoodList, weights);
142 }
143
144 private boolean normalized(Parameter p) {
145 double sum = 0;
146 for (int i = 0; i < p.getDimension(); i++)
147 sum += p.getParameterValue(i);
148 return (sum == 1.0);
149 }
150
151 //************************************************************************
152 // AbstractXMLObjectParser implementation
153 //************************************************************************
154
155 public String getParserDescription() {
156 return "This element represents a finite mixture of likelihood models.";
157 }
158
159 public Class getReturnType() {
160 return CompoundModel.class;
161 }
162
163 public XMLSyntaxRule[] getSyntaxRules() {
164 return rules;
165 }
166
167 private final XMLSyntaxRule[] rules = {
168 AttributeRule.newBooleanRule(NORMALIZE, true),
169 new ElementRule(Likelihood.class,2,Integer.MAX_VALUE),
170 new ElementRule(Parameter.class)
171 };
172 };
173
174
175 private final Parameter mixtureWeights;
176 List<AbstractModelLikelihood> likelihoodList;
177
178
179 public static void main(String[] args) {
180
181 final double l1 = -10;
182 final double l2 = -2;
183
184 AbstractModelLikelihood like1 = new AbstractModelLikelihood("dummy") {
185
186
187 public Model getModel() {
188 return null;
189 }
190
191 public double getLogLikelihood() {
192 return l1;
193 }
194
195 public void makeDirty() {
196 }
197
198 public String prettyName() {
199 return null;
200 }
201
202 public boolean isUsed() {
203 return false;
204 }
205
206 @Override
207 protected void handleModelChangedEvent(Model model, Object object, int index) {
208 }
209
210 @Override
211 protected void handleVariableChangedEvent(Variable variable, int index, Variable.ChangeType type) {
212 }
213
214 @Override
215 protected void storeState() {
216 }
217
218 @Override
219 protected void restoreState() {
220 }
221
222 @Override
223 protected void acceptState() {
224 }
225
226 public void setUsed() {
227 }
228
229 public LogColumn[] getColumns() {
230 return new LogColumn[0];
231 }
232
233 public String getId() {
234 return null;
235 }
236
237 public void setId(String id) {
238 }
239 };
240
241 AbstractModelLikelihood like2 = new AbstractModelLikelihood("dummy") {
242
243 public Model getModel() {
244 return null;
245 }
246
247 public double getLogLikelihood() {
248 return l2;
249 }
250
251 public void makeDirty() {
252 }
253
254 public String prettyName() {
255 return null;
256 }
257
258 public boolean isUsed() {
259 return false;
260 }
261
262 @Override
263 protected void handleModelChangedEvent(Model model, Object object, int index) {
264 }
265
266 @Override
267 protected void handleVariableChangedEvent(Variable variable, int index, Variable.ChangeType type) {
268 }
269
270 @Override
271 protected void storeState() {
272 }
273
274 @Override
275 protected void restoreState() {
276 }
277
278 @Override
279 protected void acceptState() {
280 }
281
282 public void setUsed() {
283 }
284
285 public LogColumn[] getColumns() {
286 return new LogColumn[0];
287 }
288
289 public String getId() {
290 return null;
291 }
292
293 public void setId(String id) {
294 }
295 };
296
297 List<AbstractModelLikelihood> likelihoodList = new ArrayList<AbstractModelLikelihood>();
298 likelihoodList.add(like1);
299 likelihoodList.add(like2);
300
301 Parameter weights = new Parameter.Default(2);
302 double p1 = 0.05;
303 weights.setParameterValue(0, p1);
304 weights.setParameterValue(1, 1.0 - p1);
305
306 WeightedMixtureModel mixture = new WeightedMixtureModel(likelihoodList, weights);
307 System.err.println("getLogLikelihood() = " + mixture.getLogLikelihood());
308
309 double test = Math.log(p1 * Math.exp(l1) + (1.0 - p1) * Math.exp(l2));
310 System.err.println("correct = " + test);
311 }
312
313 @Override
314 public Citation.Category getCategory() {
315 return Citation.Category.MISC;
316 }
317
318 @Override
319 public String getDescription() {
320 return "Weighted mixture model";
321 }
322
323 @Override
324 public List<Citation> getCitations() {
325 return Collections.singletonList(CommonCitations.LEMEY_MIXTURE_2012);
326 }
327 }