Debian Codegraph @

src/dr/evomodel/branchmodel/lineagespecific/DirichletProcessPrior.java

raw symbols SCIP
1 /*
2 * DirichletProcessPrior.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.branchmodel.lineagespecific;
27
28 import java.util.ArrayList;
29 import java.util.List;
30
31 import dr.app.bss.Utils;
32 import dr.inference.distribution.ParametricMultivariateDistributionModel;
33 import dr.inference.model.AbstractModelLikelihood;
34 import dr.inference.model.CompoundParameter;
35 import dr.inference.model.Model;
36 import dr.inference.model.Parameter;
37 import dr.inference.model.Variable;
38 import dr.inference.model.Variable.ChangeType;
39
40 @SuppressWarnings("serial")
41 public class DirichletProcessPrior extends AbstractModelLikelihood {
42
43 private static boolean VERBOSE = false;
44
45 private Parameter categoriesParameter;
46 private CompoundParameter uniquelyRealizedParameters;
47 public ParametricMultivariateDistributionModel baseModel;
48 private Parameter gamma;
49
50 private int categoryCount;
51 private int N;
52
53 private boolean likelihoodKnown = false;
54 private double logLikelihood;
55 private final List<Double> cachedLogFactorials;
56
57 public DirichletProcessPrior(Parameter categoriesParameter, //
58 CompoundParameter uniquelyRealizedParameters, //
59 ParametricMultivariateDistributionModel baseModel, //
60 Parameter gamma //
61 ) {
62
63 super("");
64
65 // vector z of cluster assignments
66 this.categoriesParameter = categoriesParameter;
67 this.baseModel = baseModel;
68 this.uniquelyRealizedParameters = uniquelyRealizedParameters;
69 this.gamma = gamma;
70
71 // K clusters
72 this.categoryCount = uniquelyRealizedParameters.getDimension();
73 // this.categoryCount=Utils.findMaximum(categoriesParameter.getParameterValues()) + 1;
74 this.N = categoriesParameter.getDimension();
75
76 cachedLogFactorials = new ArrayList<Double>();
77 cachedLogFactorials.add(0, 0.0);
78
79 // add all
80 this.addVariable(this.categoriesParameter);
81 this.addVariable(this.gamma);
82
83 this.addVariable(this.uniquelyRealizedParameters);
84
85 if(baseModel != null) {
86 this.addModel(baseModel);
87 }
88
89 this.likelihoodKnown = false;
90 }// END: Constructor
91
92 private double getLogFactorial(int i) {
93
94 if ( cachedLogFactorials.size() <= i) {
95
96 for (int j = cachedLogFactorials.size() - 1; j <= i; j++) {
97
98 double logfactorial = cachedLogFactorials.get(j) + Math.log(j + 1);
99 cachedLogFactorials.add(logfactorial);
100 }
101
102 }
103
104 return cachedLogFactorials.get(i);
105 }
106
107 /**
108 * Assumes mappings start from index 0
109 * */
110 private int[] getCounts() {
111
112 // eta_k parameters (number of assignments to each category)
113 int[] counts = new int[categoryCount];
114 for (int i = 0; i < N; i++) {
115
116 int category = getMapping(i);
117 counts[category]++;
118
119 }// END: i loop
120
121 return counts;
122 }// END: getCounts
123
124 public double getGamma() {
125 return gamma.getParameterValue(0);
126 }
127
128 private int getMapping(int i) {
129 return (int) categoriesParameter.getParameterValue(i);
130 }
131
132 public double getLogDensity(Parameter parameter) {
133 double value[] = parameter.getAttributeValue();
134 return baseModel.logPdf(value);
135 }
136
137 public double getRealizedValuesLogDensity() {
138
139 double total = 0.0;
140
141 for (int i = 0; i < categoryCount; i++) {
142
143 Parameter param = uniquelyRealizedParameters.getParameter(i);
144 total += getLogDensity(param);
145
146 }
147
148 return total;
149 }//END: getRealizedValuesLogDensity
150
151 public double getCategoriesLogDensity() {
152
153 int[] counts = getCounts();
154
155 if (VERBOSE) {
156 Utils.printArray(counts);
157 }
158
159 double loglike = categoryCount * Math.log(getGamma());
160
161 for (int k = 0; k < categoryCount; k++) {
162
163 int eta = counts[k];
164
165 if (eta > 0) {
166 loglike += getLogFactorial(eta - 1);
167 }
168
169 }// END: k loop
170
171 for (int i = 1; i <= N; i++) {
172 loglike -= Math.log(getGamma() + i - 1);
173 }// END: i loop
174
175 return loglike;
176 }// END: getPriorLoglike
177
178 @Override
179 public Model getModel() {
180 return this;
181 }
182
183 @Override
184 public double getLogLikelihood() {
185
186 this.fireModelChanged();
187 likelihoodKnown = false;
188 if (!likelihoodKnown) {
189
190 logLikelihood = calculateLogLikelihood();
191 likelihoodKnown = true;
192 }
193
194 return logLikelihood;
195 }
196
197 private double calculateLogLikelihood() {
198 // getCounts();
199 double loglike = getCategoriesLogDensity() + getRealizedValuesLogDensity();
200
201 //TODOs
202 // System.out.println(loglike);
203
204 return loglike;
205 }//END: calculateLogLikelihood
206
207 @Override
208 public void makeDirty() {
209 // likelihoodKnown = false;
210 }
211
212 @Override
213 protected void handleModelChangedEvent(Model model, Object object, int index) {
214 likelihoodKnown = false;
215 }
216
217 public int getCategoryCount() {
218 return categoryCount;
219 }
220
221 public Parameter getUniqueParameters() {
222 return uniquelyRealizedParameters;
223 }
224
225 public Parameter getUniqueParameter(int index) {
226 return uniquelyRealizedParameters.getParameter(index);
227 }
228
229 @Override
230 protected void handleVariableChangedEvent(Variable variable, int index,
231 ChangeType type) {
232
233 if (variable == categoriesParameter) {
234
235 this.fireModelChanged();
236
237 } else if (variable == gamma) {
238
239 // do nothing
240
241 this.fireModelChanged();
242
243 } else if (variable == uniquelyRealizedParameters) {
244
245 likelihoodKnown = false;
246 this.fireModelChanged();
247
248 } else {
249
250 throw new IllegalArgumentException("Unknown parameter");
251
252 }
253
254 }// END: handleVariableChangedEvent
255
256 public void setVerbose() {
257 VERBOSE = true;
258 }
259
260 @Override
261 protected void storeState() {
262
263 }
264
265 @Override
266 protected void restoreState() {
267 likelihoodKnown = false;
268 }
269
270 @Override
271 protected void acceptState() {
272
273 }
274
275 public static void main(String args[]) {
276
277 testDirichletProcess(new double[] { 0, 1, 2 }, 3, 1.0, -Math.log(6.0));
278
279 testDirichletProcess(new double[] { 0, 0, 1 }, 3, 1.0, -Math.log(6.0));
280
281 testDirichletProcess(new double[] { 0, 1, 2, 3, 4 }, 5, 0.5, -6.851184927493743);
282
283 }// END: main
284
285 private static void testDirichletProcess(double[] mapping, int categoryCount,double gamma,
286 double expectedLogL) {
287
288 Parameter categoriesParameter = new Parameter.Default(mapping);
289 Parameter gammaParameter = new Parameter.Default(gamma);
290
291 CompoundParameter dummy = new CompoundParameter("dummy");
292
293 for (int i = 0; i < categoryCount; i++) {
294 dummy.addParameter(new Parameter.Default(1.0));
295 }
296
297 DirichletProcessPrior dpp = new DirichletProcessPrior(
298 categoriesParameter, dummy, null, gammaParameter);
299 dpp.setVerbose();
300
301 // int[] counts = dpp.getCounts();
302 System.out.println("lnL: " + dpp.getCategoriesLogDensity());
303 System.out.println("expected lnL: " + expectedLogL);
304 }// END: testDirichletProcess
305
306 }// END: class
307