Debian Codegraph @

src/dr/evomodel/antigenic/ClusterSingleMoveOperator.java

raw symbols SCIP
1 /*
2 * ClusterSingleMoveOperator.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.Parameter;
29 import dr.inference.model.Variable;
30 import dr.inference.operators.MCMCOperator;
31 import dr.inference.operators.SimpleMCMCOperator;
32 import dr.math.MathUtils;
33 import dr.xml.*;
34
35 /**
36 * An operator to take a single element from one cluster and move it to a new or different cluster.
37 * Based on Ed Baskerville's implementation in Spatial Guilds in the Serengeti Food Web Revealed by
38 * a Bayesian Group Model.
39 *
40 * @author Trevor Bedford
41 * @author Andrew Rambaut
42 * @author Marc Suchard
43 * @version $Id$
44 */
45 public class ClusterSingleMoveOperator extends SimpleMCMCOperator {
46 public final static boolean DEBUG = false;
47
48 public final static String CLUSTER_SINGLE_MOVE_OPERATOR = "clusterSingleMoveOperator";
49
50 private final int N; // the number of items
51 private int K; // the number of occupied clusters
52 private final Parameter allocationParameter;
53
54 public ClusterSingleMoveOperator(Parameter allocationParameter, double weight) {
55 this.allocationParameter = allocationParameter;
56 this.N = allocationParameter.getDimension();
57
58 setWeight(weight);
59 }
60
61
62 /**
63 * @return the parameter this operator acts on.
64 */
65 public Parameter getParameter() {
66 return (Parameter) allocationParameter;
67 }
68
69 /**
70 * @return the Variable this operator acts on.
71 */
72 public Variable getVariable() {
73 return allocationParameter;
74 }
75
76 /**
77 * change the parameter and return the hastings ratio.
78 */
79 public final double doOperation() {
80
81 // get a copy of the allocations to work with...
82 // allocations are: element X -> cluster Y
83 int[] allocations = new int[allocationParameter.getDimension()];
84
85 // construct cluster occupancy vector excluding the selected item and count
86 // the unoccupied clusters.
87 // occupancy is: cluster Y -> element count
88 int[] occupancy = new int[N];
89
90 int K = 0; // k = number of unoccupied clusters
91 for (int i = 0; i < allocations.length; i++) {
92 allocations[i] = (int) allocationParameter.getParameterValue(i);
93 occupancy[allocations[i]] += 1;
94 if (occupancy[allocations[i]] == 1) { // first item in cluster
95 K++;
96 }
97 }
98
99 // log hastings ratio
100 double hastings = 0.0;
101
102 // pick element to move
103 int element = MathUtils.nextInt(N);
104 int elementAssignment = allocations[element];
105 int elementClusterSize = occupancy[elementAssignment]; // cluster size before move
106
107 // pick second element as target, must be different from first element
108 int target = MathUtils.nextInt(N);
109 while (element == target) {
110 target = MathUtils.nextInt(N);
111 }
112 int targetAssignment = allocations[target];
113 int targetClusterSize = occupancy[targetAssignment]; // cluster size before move
114
115 // if allocation of element differs from allocation of target
116 // change element allocation to match target allocation
117 if (elementAssignment != targetAssignment) {
118
119 allocations[element] = targetAssignment;
120 allocationParameter.setParameterValue(element, targetAssignment);
121
122 if (elementClusterSize > 1) {
123 // adjusting Hastings's ratio for differences in cluster size
124 hastings = Math.log(elementClusterSize - 1) - Math.log(targetClusterSize);
125 }
126
127 if (DEBUG) {
128 System.err.println("Move element " + element + " from cluster " + elementAssignment + " to cluster " + targetAssignment);
129 }
130
131 }
132
133 // if allocation of element matches allocation of target
134 // move element to a new cluster
135 else {
136
137 // find random unoccupied cluster
138 int clusterIndex = 0;
139 int targetIndex = MathUtils.nextInt(N-K);
140 int newCluster = 0;
141 for (int i = 0; i < N; i++) {
142 if (occupancy[i] == 0){
143 if (clusterIndex == targetIndex) {
144 newCluster = i;
145 }
146 clusterIndex++;
147 }
148 }
149
150 while (occupancy[newCluster] > 0) {
151 newCluster++;
152 }
153
154 // move the element to this cluster
155 allocations[element] = newCluster;
156 allocationParameter.setParameterValue(element, newCluster);
157
158 if (DEBUG) {
159 System.err.println("Move element " + element + " from cluster " + elementAssignment + " to new cluster " + newCluster);
160 }
161
162 }
163
164 // return log Hastings' ratio
165 return hastings;
166 }
167
168
169 //MCMCOperator INTERFACE
170 public final String getOperatorName() {
171 return CLUSTER_SINGLE_MOVE_OPERATOR +"(" + allocationParameter.getId() + ")";
172 }
173
174 public final void optimize(double targetProb) {
175
176 throw new RuntimeException("This operator cannot be optimized!");
177 }
178
179 public boolean isOptimizing() {
180 return false;
181 }
182
183 public void setOptimizing(boolean opt) {
184 throw new RuntimeException("This operator cannot be optimized!");
185 }
186
187 public double getMinimumAcceptanceLevel() {
188 return 0.1;
189 }
190
191 public double getMaximumAcceptanceLevel() {
192 return 0.4;
193 }
194
195 public double getMinimumGoodAcceptanceLevel() {
196 return 0.20;
197 }
198
199 public double getMaximumGoodAcceptanceLevel() {
200 return 0.30;
201 }
202
203 public String getPerformanceSuggestion() {
204 if (Utils.getAcceptanceProbability(this) < getMinimumAcceptanceLevel()) {
205 return "";
206 } else if (Utils.getAcceptanceProbability(this) > getMaximumAcceptanceLevel()) {
207 return "";
208 } else {
209 return "";
210 }
211 }
212
213 public String toString() {
214 return getOperatorName();
215 }
216
217
218 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
219
220 public String getParserName() {
221 return CLUSTER_SINGLE_MOVE_OPERATOR;
222 }
223
224 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
225
226 double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
227
228 Parameter allocationParameter = (Parameter) xo.getChild(Parameter.class);
229
230 return new ClusterSingleMoveOperator(allocationParameter, weight);
231
232 }
233
234 //************************************************************************
235 // AbstractXMLObjectParser implementation
236 //************************************************************************
237
238 public String getParserDescription() {
239 return "An operator that moves single elements between clusters.";
240 }
241
242 public Class getReturnType() {
243 return ClusterSingleMoveOperator.class;
244 }
245
246
247 public XMLSyntaxRule[] getSyntaxRules() {
248 return rules;
249 }
250
251 private final XMLSyntaxRule[] rules = {
252 AttributeRule.newDoubleRule(MCMCOperator.WEIGHT),
253 new ElementRule(Parameter.class)
254 };
255 };
256
257 public int getStepCount() {
258 return 1;
259 }
260
261 }