Debian Codegraph @

src/dr/evomodel/antigenic/ClusterSplitMergeOperator.java

raw symbols SCIP
1 /*
2 * ClusterSplitMergeOperator.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.MatrixParameter;
29 import dr.inference.model.Parameter;
30 import dr.inference.model.Variable;
31 import dr.inference.operators.MCMCOperator;
32 import dr.inference.operators.SimpleMCMCOperator;
33 import dr.math.MathUtils;
34 import dr.xml.*;
35
36 /**
37 * An operator to split or merge clusters.
38 *
39 * @author Andrew Rambaut
40 * @author Marc Suchard
41 * @version $Id: DirichletProcessGibbsOperator.java,v 1.16 2005/06/14 10:40:34 rambaut Exp $
42 */
43 public class ClusterSplitMergeOperator extends SimpleMCMCOperator {
44 public final static boolean DEBUG = false;
45
46 public final static String CLUSTER_SPLIT_MERGE_OPERATOR = "clusterSplitMergeOperator";
47
48 private final int N; // the number of items
49 private int K; // the number of occupied clusters
50 private final Parameter allocationParameter;
51 private final MatrixParameter clusterLocations;
52
53 public ClusterSplitMergeOperator(Parameter allocationParameter,
54 MatrixParameter clusterLocations,
55 double weight) {
56 this.allocationParameter = allocationParameter;
57 this.clusterLocations = clusterLocations;
58 this.N = allocationParameter.getDimension();
59
60 setWeight(weight);
61 }
62
63
64 /**
65 * @return the parameter this operator acts on.
66 */
67 public Parameter getParameter() {
68 return (Parameter) allocationParameter;
69 }
70
71 /**
72 * @return the Variable this operator acts on.
73 */
74 public Variable getVariable() {
75 return allocationParameter;
76 }
77
78 /**
79 * change the parameter and return the hastings ratio.
80 */
81 public final double doOperation() {
82
83 // get a copy of the allocations to work with...
84 int[] allocations = new int[allocationParameter.getDimension()];
85
86 // construct cluster occupancy vector excluding the selected item and count
87 // the unoccupied clusters.
88 int[] occupancy = new int[N];
89
90 int[] occupiedIndices = new int[N];
91
92 // for testing, set these to -1 to force out of bounds exception if
93 // used but not set
94 for (int i = 0; i < occupiedIndices.length; i++) {
95 occupiedIndices[i] = -1;
96 }
97
98 int K = 0; // k = number of unoccupied clusters
99 for (int i = 0; i < allocations.length; i++) {
100 allocations[i] = (int) allocationParameter.getParameterValue(i);
101 occupancy[allocations[i]] += 1;
102 if (occupancy[allocations[i]] == 1) { // first item in cluster
103 occupiedIndices[K] = allocations[i];
104 K++;
105 }
106 }
107
108 // Container for split/merge random variable (only 2 draws in 2D)
109 int paramDim = clusterLocations.getParameter(0).getDimension();
110 double[] splitDraw = new double[paramDim]; // Need to keep these for computing MHG ratio
111 double scale = 1.0; // TODO make tunable
112 double newClusterProb = 0.5; // TODO Make tunable
113
114 // always split when K = 1, always merge when K = N, otherwise 50:50
115 boolean doSplit = K == 1 || (K != N && MathUtils.nextBoolean());
116
117 if (doSplit) {
118 // Split operation
119
120 int cluster1;
121 do {
122 // pick an occupied cluster
123 cluster1 = occupiedIndices[MathUtils.nextInt(K)];
124
125 // For reversibility, merge step requires that both resulting clusters are occupied,
126 // so we should resample until condition is true
127 } while (occupancy[cluster1] == 0);
128
129 // find the first unoccupied cluster
130 int cluster2 = 0;
131 while (occupancy[cluster2] > 0) {
132 cluster2 ++;
133 }
134
135 int oldCount = occupancy[cluster1];
136 do {
137 occupancy[cluster1] = 0;
138 occupancy[cluster2] = 0;
139 for (int i = 0; i < allocations.length; i++) {
140 if (allocations[i] == cluster1 || allocations[i] == cluster2) {
141 boolean putInNewCluster = MathUtils.nextDouble() < newClusterProb;
142 if (putInNewCluster) {
143 allocations[i] = cluster2;
144 occupancy[cluster2]++;
145 } else {
146 allocations[i] = cluster1;
147 occupancy[cluster1]++;
148 }
149 }
150 }
151 } while (occupancy[cluster1] != 0 && occupancy[cluster2] != 0);
152
153 K++;
154
155 // set both clusters to a location based on the first cluster with some random jitter...
156 Parameter param1 = clusterLocations.getParameter(cluster1);
157 Parameter param2 = clusterLocations.getParameter(cluster2);
158
159 double[] loc = param1.getParameterValues();
160 for (int dim = 0; dim < param1.getDimension(); dim++) {
161 splitDraw[dim] = MathUtils.nextGaussian();
162 param1.setParameterValue(dim, loc[dim] + (splitDraw[dim] * scale));
163 param2.setParameterValue(dim, loc[dim] - (splitDraw[dim] * scale)); // Move in opposite direction
164 }
165
166 if (DEBUG) {
167 System.err.println("Split: " + (oldCount - occupancy[cluster1]) + " items from cluster " + cluster1 + " to create cluster " + cluster2);
168 }
169 } else {
170 // Merge operation
171
172 // pick 2 occupied clusters
173 int cluster1 = occupiedIndices[MathUtils.nextInt(K)];
174 int cluster2;
175
176 do {
177 cluster2 = occupiedIndices[MathUtils.nextInt(K)];
178
179 // resample until cluster1 != cluster2 to maintain reversibility, because split assumes they are different
180 } while (cluster1 == cluster2);
181
182 for (int i = 0; i < allocations.length; i++) {
183 if (allocations[i] == cluster2) {
184 allocations[i] = cluster1;
185
186 // keep occupancy up to date (remove if not need)
187 occupancy[cluster1] ++;
188 occupancy[cluster2] --;
189 }
190 }
191
192 K--;
193
194 // set the merged cluster to the mean location of the two original clusters
195 Parameter loc1 = clusterLocations.getParameter(cluster1);
196 Parameter loc2 = clusterLocations.getParameter(cluster2);
197 for (int dim = 0; dim < loc1.getDimension(); dim++) {
198 double average = (loc1.getParameterValue(dim) + loc2.getParameterValue(dim)) / 2.0;
199 splitDraw[dim] = (loc1.getParameterValue(dim) - average) / scale; // Record that the reverse step would need to draw
200 loc1.setParameterValue(dim, average);
201
202 // Consider loc2 as the extra dimensions for dimension-matching
203 // On second thought, maybe not a good idea
204 // loc2.setParameterValue(dim, splitDraw[dim]);
205 }
206 if (DEBUG) {
207 System.err.println("Merge: " + occupancy[cluster1] + "items into cluster " + cluster1 + " from " + cluster2);
208 }
209
210 }
211
212 // set the final allocations (only for those that have changed)
213 for (int i = 0; i < allocations.length; i++) {
214 int k = (int) allocationParameter.getParameterValue(i);
215 if (allocations[i] != k) {
216 allocationParameter.setParameterValue(i, allocations[i]);
217 }
218 }
219
220 // todo the Hastings ratio
221 return 0.0;
222 }
223
224
225 //MCMCOperator INTERFACE
226 public final String getOperatorName() {
227 return CLUSTER_SPLIT_MERGE_OPERATOR +"(" + allocationParameter.getId() + ")";
228 }
229
230 public final void optimize(double targetProb) {
231
232 throw new RuntimeException("This operator cannot be optimized!");
233 }
234
235 public boolean isOptimizing() {
236 return false;
237 }
238
239 public void setOptimizing(boolean opt) {
240 throw new RuntimeException("This operator cannot be optimized!");
241 }
242
243 public double getMinimumAcceptanceLevel() {
244 return 0.1;
245 }
246
247 public double getMaximumAcceptanceLevel() {
248 return 0.4;
249 }
250
251 public double getMinimumGoodAcceptanceLevel() {
252 return 0.20;
253 }
254
255 public double getMaximumGoodAcceptanceLevel() {
256 return 0.30;
257 }
258
259 public String getPerformanceSuggestion() {
260 if (Utils.getAcceptanceProbability(this) < getMinimumAcceptanceLevel()) {
261 return "";
262 } else if (Utils.getAcceptanceProbability(this) > getMaximumAcceptanceLevel()) {
263 return "";
264 } else {
265 return "";
266 }
267 }
268
269 public String toString() {
270 return getOperatorName();
271 }
272
273
274 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
275 public final static String CHI = "chi";
276 public final static String LIKELIHOOD = "likelihood";
277
278 public String getParserName() {
279 return CLUSTER_SPLIT_MERGE_OPERATOR;
280 }
281
282 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
283
284 double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
285
286 Parameter allocationParameter = (Parameter) xo.getChild(Parameter.class);
287
288 MatrixParameter locationsParameter = (MatrixParameter) xo.getElementFirstChild("locations");
289
290 return new ClusterSplitMergeOperator(allocationParameter, locationsParameter, weight);
291
292 }
293
294 //************************************************************************
295 // AbstractXMLObjectParser implementation
296 //************************************************************************
297
298 public String getParserDescription() {
299 return "An operator that splits and merges clusters.";
300 }
301
302 public Class getReturnType() {
303 return ClusterSplitMergeOperator.class;
304 }
305
306
307 public XMLSyntaxRule[] getSyntaxRules() {
308 return rules;
309 }
310
311 private final XMLSyntaxRule[] rules = {
312 AttributeRule.newDoubleRule(MCMCOperator.WEIGHT),
313 new ElementRule(Parameter.class),
314 new ElementRule("locations", new XMLSyntaxRule[] {
315 new ElementRule(MatrixParameter.class)
316 })
317 };
318 };
319
320 public int getStepCount() {
321 return 1;
322 }
323
324 }