| 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.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 |
|
| 76 |
|
| 77 |
this.mhSteps = mhSteps; |
| 78 |
|
| 79 |
setWeight(weight); |
| 80 |
|
| 81 |
} |
| 82 |
|
| 83 |
public Parameter getParameter() { |
| 84 |
return categoriesParameter; |
| 85 |
} |
| 86 |
|
| 87 |
public Variable getVariable() { |
| 88 |
return categoriesParameter; |
| 89 |
} |
| 90 |
|
| 91 |
@Override |
| 92 |
public double doOperation() { |
| 93 |
|
| 94 |
try { |
| 95 |
|
| 96 |
|
| 97 |
doOp(); |
| 98 |
|
| 99 |
} catch (MathException e) { |
| 100 |
e.printStackTrace(); |
| 101 |
} |
| 102 |
|
| 103 |
return 0.0; |
| 104 |
} |
| 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 |
} |
| 116 |
} |
| 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 |
} |
| 133 |
|
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 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 |
|
| 146 |
if(singletonIndex > -1) { |
| 147 |
|
| 148 |
baseProposals[0] = uniqueParameters.getParameterValue(singletonIndex); |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
double[] logClusterProbs = new double[uniqueRealizationCount]; |
| 153 |
|
| 154 |
|
| 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 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
allParameters.setParameterValue(index, value); |
| 171 |
likelihood.makeDirty(); |
| 172 |
|
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
for(; i < logClusterProbs.length; i++){ |
| 177 |
|
| 178 |
logClusterProbs[i] = Math.log((intensity) / (realizationCount - 1 + intensity)); |
| 179 |
|
| 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 |
|
| 188 |
|
| 189 |
|
| 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 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
int sampledCluster = MathUtils.randomChoicePDF(clusterProbs); |
| 217 |
categoriesParameter.setParameterValue(index, sampledCluster); |
| 218 |
|
| 219 |
|
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
private void doOperate() throws MathException { |
| 227 |
|
| 228 |
|
| 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 |
} |
| 240 |
|
| 241 |
} |
| 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 |
|
| 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) { |
| 260 |
|
| 261 |
|
| 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 { |
| 272 |
|
| 273 |
|
| 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 |
} |
| 285 |
|
| 286 |
clusterProbs[i] = logprob; |
| 287 |
} |
| 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 |
|
| 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 |
} |
| 306 |
|
| 307 |
} |
| 308 |
|
| 309 |
@Override |
| 310 |
public String getOperatorName() { |
| 311 |
return DirichletProcessOperatorParser.DIRICHLET_PROCESS_OPERATOR; |
| 312 |
} |
| 313 |
|
| 314 |
} |