Debian Codegraph @

src/dr/evomodel/antigenic/DirichletProcessGibbsOperator.java

raw symbols SCIP
1 /*
2 * DirichletProcessGibbsOperator.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.antigenic;
27
28 import dr.inference.model.Likelihood;
29 import dr.inference.model.Parameter;
30 import dr.inference.model.Variable;
31 import dr.inference.operators.GibbsOperator;
32 import dr.inference.operators.MCMCOperator;
33 import dr.inference.operators.SimpleMCMCOperator;
34 import dr.math.MathUtils;
35 import dr.xml.*;
36
37 /**
38 * A Gibbs operator for allocation of items to clusters under a Dirichlet process.
39 *
40 * @author Andrew Rambaut
41 * @author Marc Suchard
42 * @version $Id: DirichletProcessGibbsOperator.java,v 1.16 2005/06/14 10:40:34 rambaut Exp $
43 */
44 public class DirichletProcessGibbsOperator extends SimpleMCMCOperator implements GibbsOperator {
45 public final static String DIRICHLET_PROCESS_OPERATOR = "dirichletProcessOperator";
46
47 private final int N;
48 private final int K;
49 private final Parameter chiParameter;
50
51 private final Likelihood modelLikelihood;
52
53 public DirichletProcessGibbsOperator(Parameter clusteringParameter,
54 Parameter chiParameter,
55 Likelihood modelLikelihood,
56 double weight) {
57 this.clusteringParameter = clusteringParameter;
58 this.N = clusteringParameter.getDimension();
59 this.chiParameter = chiParameter;
60 this.modelLikelihood = modelLikelihood;
61 this.K = this.N; // TODO number of potential clusters should be much less than N
62
63 setWeight(weight);
64 }
65
66
67 /**
68 * @return the parameter this operator acts on.
69 */
70 public Parameter getParameter() {
71 return (Parameter) clusteringParameter;
72 }
73
74 /**
75 * @return the Variable this operator acts on.
76 */
77 public Variable getVariable() {
78 return clusteringParameter;
79 }
80
81 /**
82 * change the parameter and return the hastings ratio.
83 */
84 public final double doOperation() {
85
86 int index = MathUtils.nextInt(clusteringParameter.getDimension());
87
88 int[] occupancy = new int[N];
89
90 // construct cluster occupancy vector excluding the selected item and count
91 // the unoccupied clusters.
92
93 int X = K; // X = number of unoccupied clusters
94 for (int i = 0; i < clusteringParameter.getDimension(); i++) {
95 int j = (int) clusteringParameter.getParameterValue(i);
96 if (i != index) {
97 occupancy[j] += 1;
98 if (occupancy[j] == 1) { // first item in cluster
99 X -= 1; // one fewer unoccupied
100 }
101 }
102 }
103
104 double chi = chiParameter.getParameterValue(0);
105
106 double p1 = chi / ((N - 1 + chi) * X);
107 double[] P = new double[K];
108 for (int i = 0; i < K; i++) {
109 double p;
110 if (occupancy[i] == 0) {
111 p = p1;
112 } else {
113 p = occupancy[i] / (N - 1 + chi);
114 }
115
116 P[i] = Math.log(p); // Store in log-scale for addition with conditionalLogLikelihood
117 }
118
119 if (modelLikelihood != null) {
120 for (int k = 0; k < K; ++k) {
121 clusteringParameter.setParameterValue(index, k);
122 P[k] += modelLikelihood.getLogLikelihood();;
123 }
124 }
125
126 this.rescale(P); // Improve numerical stability
127 this.exp(P); // Transform back to probability-scale
128
129 int k = MathUtils.randomChoicePDF(P);
130
131 ((Parameter) clusteringParameter).setParameterValue(index, k);
132
133 return 0.0;
134 }
135
136
137 private void exp(double[] logX) {
138 for (int i = 0; i < logX.length; ++i) {
139 logX[i] = Math.exp(logX[i]);
140 }
141 }
142
143 private void rescale(double[] logX) {
144 double max = this.max(logX);
145 for (int i = 0; i < logX.length; ++i) {
146 logX[i] -= max;
147 }
148 }
149
150 private double max(double[] x) {
151 double max = x[0];
152 for (double xi : x) {
153 if (xi > max) {
154 max = xi;
155 }
156 }
157 return max;
158 }
159
160 //MCMCOperator INTERFACE
161 public final String getOperatorName() {
162 return DIRICHLET_PROCESS_OPERATOR+"(" + clusteringParameter.getId() + "|" + chiParameter.getId() + ")";
163 }
164
165 public final void optimize(double targetProb) {
166
167 throw new RuntimeException("This operator cannot be optimized!");
168 }
169
170 public boolean isOptimizing() {
171 return false;
172 }
173
174 public void setOptimizing(boolean opt) {
175 throw new RuntimeException("This operator cannot be optimized!");
176 }
177
178 public double getMinimumAcceptanceLevel() {
179 return 0.1;
180 }
181
182 public double getMaximumAcceptanceLevel() {
183 return 0.4;
184 }
185
186 public double getMinimumGoodAcceptanceLevel() {
187 return 0.20;
188 }
189
190 public double getMaximumGoodAcceptanceLevel() {
191 return 0.30;
192 }
193
194 public String getPerformanceSuggestion() {
195 if (Utils.getAcceptanceProbability(this) < getMinimumAcceptanceLevel()) {
196 return "";
197 } else if (Utils.getAcceptanceProbability(this) > getMaximumAcceptanceLevel()) {
198 return "";
199 } else {
200 return "";
201 }
202 }
203
204 public String toString() {
205 return DIRICHLET_PROCESS_OPERATOR + "(" + clusteringParameter.getId() + ")";
206 }
207
208 //PRIVATE STUFF
209
210 private Parameter clusteringParameter = null;
211
212 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
213 public final static String CHI = "chi";
214 public final static String LIKELIHOOD = "likelihood";
215
216 public String getParserName() {
217 return DIRICHLET_PROCESS_OPERATOR;
218 }
219
220 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
221
222 double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
223
224 Parameter clusteringParameter = (Parameter) xo.getChild(Parameter.class);
225
226 Parameter chiParameter = (Parameter)xo.getElementFirstChild(CHI);
227
228 Likelihood likelihood = null;
229
230 if (xo.hasChildNamed(LIKELIHOOD)) {
231 likelihood = (Likelihood)xo.getElementFirstChild(LIKELIHOOD);
232 }
233
234 return new DirichletProcessGibbsOperator(clusteringParameter, chiParameter, likelihood, weight);
235
236 }
237
238 //************************************************************************
239 // AbstractXMLObjectParser implementation
240 //************************************************************************
241
242 public String getParserDescription() {
243 return "An operator that picks a new allocation of an item to a cluster under the Dirichlet process.";
244 }
245
246 public Class getReturnType() {
247 return DirichletProcessGibbsOperator.class;
248 }
249
250
251 public XMLSyntaxRule[] getSyntaxRules() {
252 return rules;
253 }
254
255 private final XMLSyntaxRule[] rules = {
256 AttributeRule.newDoubleRule(MCMCOperator.WEIGHT),
257 new ElementRule(CHI, new XMLSyntaxRule[] {
258 new ElementRule(Parameter.class),
259 }),
260 new ElementRule(LIKELIHOOD, new XMLSyntaxRule[] {
261 new ElementRule(Likelihood.class),
262 }, true),
263 new ElementRule(Parameter.class)
264 };
265 };
266
267 public int getStepCount() {
268 return 1;
269 }
270
271 }