| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 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; |
| 62 |
|
| 63 |
setWeight(weight); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public Parameter getParameter() { |
| 71 |
return (Parameter) clusteringParameter; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public Variable getVariable() { |
| 78 |
return clusteringParameter; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public final double doOperation() { |
| 85 |
|
| 86 |
int index = MathUtils.nextInt(clusteringParameter.getDimension()); |
| 87 |
|
| 88 |
int[] occupancy = new int[N]; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
int X = K; |
| 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) { |
| 99 |
X -= 1; |
| 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); |
| 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); |
| 127 |
this.exp(P); |
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
} |