Debian Codegraph @

src/dr/inference/model/MixtureModelLikelihood.java

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