Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * DirichletProcessOperator.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 org.apache.commons.math.MathException;
29
30 import dr.inference.model.CompoundLikelihood;
31 import dr.inference.model.Likelihood;
32 import dr.inference.model.Parameter;
33 import dr.inference.model.Variable;
34 import dr.inference.operators.GibbsOperator;
35 import dr.inference.operators.SimpleMCMCOperator;
36 import dr.math.MathUtils;
37
38 @SuppressWarnings("serial")
39 public class DirichletProcessOperator extends SimpleMCMCOperator implements
40 GibbsOperator {
41
42 private static final boolean DEBUG = false;
43
44 private DirichletProcessPrior dpp;
45
46 private int realizationCount;
47 private int uniqueRealizationCount;
48 private double intensity;
49 private int mhSteps;
50
51 private Parameter categoriesParameter;
52 private CountableRealizationsParameter allParameters;
53 private Parameter uniqueParameters;
54
55 private CompoundLikelihood likelihood;
56
57 public DirichletProcessOperator(DirichletProcessPrior dpp, //
58 Parameter categoriesParameter, //
59 Parameter uniqueParameters, //
60 CountableRealizationsParameter allParameters,
61 Likelihood likelihood, //
62 int mhSteps, //
63 double weight//
64 ) {
65
66 this.dpp = dpp;
67 this.intensity = dpp.getGamma();
68 this.uniqueRealizationCount = dpp.getCategoryCount();
69 this.realizationCount = categoriesParameter.getDimension();
70
71 this.categoriesParameter = categoriesParameter;
72 this.allParameters = allParameters;
73 this.uniqueParameters = uniqueParameters;
74 this.likelihood = (CompoundLikelihood) likelihood;
75 // this.likelihood = likelihood;
76
77 this.mhSteps = mhSteps;
78
79 setWeight(weight);
80
81 }// END: Constructor
82
83 public Parameter getParameter() {
84 return categoriesParameter;
85 }// END: getParameter
86
87 public Variable getVariable() {
88 return categoriesParameter;
89 }// END: getVariable
90
91 @Override
92 public double doOperation() {
93
94 try {
95
96 // doOperate();
97 doOp();
98
99 } catch (MathException e) {
100 e.printStackTrace();
101 }// END: try-catch block
102
103 return 0.0;
104 }// END: doOperation
105
106 private void doOp() throws MathException {
107
108 for (int index = 0; index < realizationCount; index++) {
109
110 int[] occupancy = new int[uniqueRealizationCount];
111 for (int i = 0; i < realizationCount; i++) {
112 if (i != index) {
113 int j = (int) categoriesParameter.getParameterValue(i);
114 occupancy[j]++;
115 }// END: i check
116 }// END: i loop
117
118
119 double[] existingValues = new double[uniqueRealizationCount];
120 int counter = 0;
121 int singletonIndex = -1;
122 for(int i = 0; i < uniqueRealizationCount;i++){
123 if(occupancy[i] > 0) {
124
125 occupancy[counter] = occupancy[i];
126 existingValues[counter++] = dpp.getUniqueParameter(i) .getParameterValue(0);
127
128 } else {
129
130 singletonIndex = i;
131
132 }//END: occupancy check
133
134 }//END: i loop
135
136
137 // Propose new value(s)
138 double[] baseProposals = new double[realizationCount];
139 for (int i = 0; i < baseProposals.length; i++) {
140
141 baseProposals[i] = dpp.baseModel.nextRandom()[0];
142
143 }
144
145 // If a singleton
146 if(singletonIndex > -1) {
147
148 baseProposals[0] = uniqueParameters.getParameterValue(singletonIndex);
149
150 }
151
152 double[] logClusterProbs = new double[uniqueRealizationCount];
153
154 // draw existing
155 int i;
156 for(i = 0; i < counter; i++) {
157
158 logClusterProbs[i] = Math.log(occupancy[i] / (realizationCount - 1 + intensity));
159
160 double value = allParameters.getParameterValue(index);
161 double candidate = existingValues[i];
162 allParameters.setParameterValue(index, candidate);
163 likelihood.makeDirty();
164
165 logClusterProbs[i] = logClusterProbs[i] + likelihood.getLikelihood(index) .getLogLikelihood();
166 // logClusterProbs[i] = logClusterProbs[i] + likelihood .getLogLikelihood();
167
168 // System.out.println(likelihood.getLikelihood(index) .getLogLikelihood() + " " + likelihood .getLogLikelihood());
169
170 allParameters.setParameterValue(index, value);
171 likelihood.makeDirty();
172
173 }
174
175 // draw new
176 for(; i < logClusterProbs.length; i++){
177
178 logClusterProbs[i] = Math.log((intensity) / (realizationCount - 1 + intensity));
179 // logClusterProbs[i] = Math.log(intensity / uniqueRealizationCount / (realizationCount - 1 + intensity));
180
181 double value = allParameters.getParameterValue(index);
182 double candidate = baseProposals[i - counter];
183 allParameters.setParameterValue(index, candidate);
184 likelihood.makeDirty();
185
186 logClusterProbs[i] = logClusterProbs[i] + likelihood.getLikelihood(index).getLogLikelihood();
187 // logClusterProbs[i] = logClusterProbs[i] + likelihood.getLogLikelihood();
188
189 // System.out.println(likelihood.getLikelihood(index) .getLogLikelihood() + " " + likelihood .getLogLikelihood());
190
191 allParameters.setParameterValue(index, value);
192 likelihood.makeDirty();
193
194 }
195
196 double smallestVal = logClusterProbs[0];
197 for(i = 1; i < uniqueRealizationCount; i++){
198
199 if(smallestVal > logClusterProbs[i]) {
200 smallestVal = logClusterProbs[i];
201 }
202
203 }
204
205
206 double[] clusterProbs = new double[uniqueRealizationCount];
207 for(i = 0; i < clusterProbs.length;i++) {
208 clusterProbs[i] = Math.exp(logClusterProbs[i]-smallestVal);
209
210 }
211
212 // dr.app.bss.Utils.printArray(clusterProbs);
213 // System.exit(-1);
214
215 // sample
216 int sampledCluster = MathUtils.randomChoicePDF(clusterProbs);
217 categoriesParameter.setParameterValue(index, sampledCluster);
218
219
220 }//END: index loop
221
222
223 }//END: doOp
224
225
226 private void doOperate() throws MathException {
227
228 // int index = 0;
229 for (int index = 0; index < realizationCount; index++) {
230
231 int[] occupancy = new int[uniqueRealizationCount];
232 for (int i = 0; i < realizationCount; i++) {
233
234 if (i != index) {
235
236 int j = (int) categoriesParameter.getParameterValue(i);
237 occupancy[j]++;
238
239 }// END: i check
240
241 }// END: i loop
242
243 if (DEBUG) {
244 System.out.println("N[-index]: ");
245 dr.app.bss.Utils.printArray(occupancy);
246 }
247
248 Likelihood clusterLikelihood = (Likelihood) likelihood.getLikelihood(index);
249 // Likelihood clusterLikelihood = likelihood;
250
251 int category = (int) categoriesParameter.getParameterValue(index);
252 double value = uniqueParameters.getParameterValue(category);
253
254 double[] clusterProbs = new double[uniqueRealizationCount];
255
256 for (int i = 0; i < uniqueRealizationCount; i++) {
257
258 double logprob = 0;
259 if (occupancy[i] == 0) {// draw new
260
261 // draw from base model, evaluate at likelihood
262
263 double candidate = dpp.baseModel.nextRandom()[0];
264
265 uniqueParameters.setParameterValue(category, candidate);
266 double loglike = clusterLikelihood.getLogLikelihood();
267 uniqueParameters.setParameterValue(category, value);
268
269 logprob = Math.log((intensity) / (realizationCount - 1 + intensity)) + loglike;
270
271 } else {// draw existing
272
273 // likelihood for component x_index
274
275 double candidate = dpp.getUniqueParameter(i)
276 .getParameterValue(0);
277
278 uniqueParameters.setParameterValue(category, candidate);
279 double loglike = clusterLikelihood.getLogLikelihood();
280 uniqueParameters.setParameterValue(category, value);
281
282 logprob = Math.log(occupancy[i]) / (realizationCount - 1 + intensity) + loglike;
283
284 }// END: occupancy check
285
286 clusterProbs[i] = logprob;
287 }// END: i loop
288
289 dr.app.bss.Utils.exponentiate(clusterProbs);
290
291 if (DEBUG) {
292 System.out.println("P(z[index] | z[-index]): ");
293 dr.app.bss.Utils.printArray(clusterProbs);
294 }
295
296 // sample
297 int sampledCluster = MathUtils.randomChoicePDF(clusterProbs);
298 categoriesParameter.setParameterValue(index, sampledCluster);
299
300 if (DEBUG) {
301 System.out
302 .println("sampled category: " + sampledCluster + "\n");
303 }
304
305 }// END: index loop
306
307 }// END: doOperate
308
309 @Override
310 public String getOperatorName() {
311 return DirichletProcessOperatorParser.DIRICHLET_PROCESS_OPERATOR;
312 }
313
314 }// END: class