| 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.inference.mcmc; |
| 27 |
|
| 28 |
import dr.inference.loggers.Logger; |
| 29 |
import dr.inference.loggers.MCLogger; |
| 30 |
import dr.inference.markovchain.MarkovChain; |
| 31 |
import dr.inference.markovchain.MarkovChainListener; |
| 32 |
import dr.inference.model.Model; |
| 33 |
import dr.inference.model.PathLikelihood; |
| 34 |
import dr.inference.operators.*; |
| 35 |
import dr.util.Author; |
| 36 |
import dr.util.Citable; |
| 37 |
import dr.util.Citation; |
| 38 |
import dr.util.Identifiable; |
| 39 |
import dr.xml.*; |
| 40 |
import org.apache.commons.math.MathException; |
| 41 |
import org.apache.commons.math.distribution.BetaDistributionImpl; |
| 42 |
|
| 43 |
import java.util.ArrayList; |
| 44 |
import java.util.Arrays; |
| 45 |
import java.util.List; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public class MarginalLikelihoodEstimator implements Runnable, Identifiable, Citable { |
| 54 |
|
| 55 |
public MarginalLikelihoodEstimator(String id, int chainLength, int burninLength, int pathSteps, double[] fixedRunValues, |
| 56 |
|
| 57 |
PathScheme scheme, |
| 58 |
PathLikelihood pathLikelihood, |
| 59 |
OperatorSchedule schedule, |
| 60 |
List<MCLogger> loggers) { |
| 61 |
|
| 62 |
this.id = id; |
| 63 |
this.chainLength = chainLength; |
| 64 |
this.pathSteps = pathSteps; |
| 65 |
this.scheme = scheme; |
| 66 |
this.schedule = schedule; |
| 67 |
this.fixedRunValues = fixedRunValues; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
this.burninLength = burninLength; |
| 73 |
|
| 74 |
MCMCCriterion criterion = new MCMCCriterion(); |
| 75 |
|
| 76 |
pathDelta = 1.0 / pathSteps; |
| 77 |
pathParameter = 1.0; |
| 78 |
|
| 79 |
this.pathLikelihood = pathLikelihood; |
| 80 |
pathLikelihood.setPathParameter(pathParameter); |
| 81 |
|
| 82 |
mc = new MarkovChain(pathLikelihood, schedule, criterion, 0, 0, 0.0, true); |
| 83 |
|
| 84 |
this.loggers = loggers; |
| 85 |
} |
| 86 |
|
| 87 |
private void setDefaultBurnin() { |
| 88 |
if (burninLength == -1) { |
| 89 |
burnin = (int) (0.1 * chainLength); |
| 90 |
} else { |
| 91 |
burnin = burninLength; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
public void integrate(Integrator scheme) { |
| 96 |
setDefaultBurnin(); |
| 97 |
mc.setCurrentLength(burnin); |
| 98 |
scheme.init(); |
| 99 |
((CombinedOperatorSchedule) schedule).reset(); |
| 100 |
for (pathParameter = scheme.nextPathParameter(); pathParameter >= 0; pathParameter = scheme.nextPathParameter()) { |
| 101 |
pathLikelihood.setPathParameter(pathParameter); |
| 102 |
reportIteration(pathParameter, chainLength, burnin, scheme.pathSteps, scheme.step); |
| 103 |
|
| 104 |
for (int i = 0; i < schedule.getOperatorCount(); ++i) { |
| 105 |
MCMCOperator operator = schedule.getOperator(i); |
| 106 |
if (operator instanceof PathDependentOperator) { |
| 107 |
((PathDependentOperator)operator).setPathParameter(pathParameter); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
long cl = mc.getCurrentLength(); |
| 112 |
mc.setCurrentLength(0); |
| 113 |
mc.runChain(burnin, false); |
| 114 |
mc.setCurrentLength(cl); |
| 115 |
mc.runChain(chainLength, false); |
| 116 |
|
| 117 |
if (SHOW_OPERATOR_ANALYSIS) { |
| 118 |
OperatorAnalysisPrinter.showOperatorAnalysis(System.out, schedule, false); |
| 119 |
} |
| 120 |
((CombinedOperatorSchedule) schedule).reset(); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
public abstract class Integrator { |
| 125 |
protected int step; |
| 126 |
protected int pathSteps; |
| 127 |
|
| 128 |
protected Integrator(int pathSteps) { |
| 129 |
this.pathSteps = pathSteps; |
| 130 |
} |
| 131 |
|
| 132 |
public void init() { |
| 133 |
step = 0; |
| 134 |
} |
| 135 |
|
| 136 |
abstract double nextPathParameter(); |
| 137 |
} |
| 138 |
|
| 139 |
public class FixedThetaRun extends Integrator { |
| 140 |
private double[] value; |
| 141 |
|
| 142 |
public FixedThetaRun(double[] value) { |
| 143 |
super(value.length); |
| 144 |
this.value = value; |
| 145 |
} |
| 146 |
|
| 147 |
double nextPathParameter() { |
| 148 |
if (step < value.length) { |
| 149 |
step++; |
| 150 |
return value[step-1]; |
| 151 |
} else { |
| 152 |
return -1.0; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
} |
| 157 |
|
| 158 |
public class LinearIntegrator extends Integrator { |
| 159 |
public LinearIntegrator(int pathSteps) { |
| 160 |
super(pathSteps); |
| 161 |
} |
| 162 |
|
| 163 |
double nextPathParameter() { |
| 164 |
if (step > pathSteps) { |
| 165 |
return -1; |
| 166 |
} |
| 167 |
double pathParameter = 1.0 - (double)step / (double)(pathSteps); |
| 168 |
step = step + 1; |
| 169 |
return pathParameter; |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
public class SigmoidIntegrator extends Integrator { |
| 174 |
private double alpha; |
| 175 |
|
| 176 |
public SigmoidIntegrator(double alpha, int pathSteps) { |
| 177 |
super(pathSteps); |
| 178 |
this.alpha = alpha; |
| 179 |
} |
| 180 |
|
| 181 |
double nextPathParameter() { |
| 182 |
if (step == 0) { |
| 183 |
step++; |
| 184 |
return 1.0; |
| 185 |
} else if (step == pathSteps) { |
| 186 |
step++; |
| 187 |
return 0.0; |
| 188 |
} else if (step > pathSteps) { |
| 189 |
return -1.0; |
| 190 |
} else { |
| 191 |
double xvalue = ((pathSteps - step)/((double)pathSteps)) - 0.5; |
| 192 |
step++; |
| 193 |
return Math.exp(alpha*xvalue)/(Math.exp(alpha*xvalue) + Math.exp(-alpha*xvalue)); |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
public class BetaQuantileIntegrator extends Integrator { |
| 199 |
private double alpha; |
| 200 |
|
| 201 |
public BetaQuantileIntegrator(double alpha, int pathSteps) { |
| 202 |
super(pathSteps); |
| 203 |
this.alpha = alpha; |
| 204 |
} |
| 205 |
|
| 206 |
double nextPathParameter() { |
| 207 |
if (step > pathSteps) |
| 208 |
return -1; |
| 209 |
double result = Math.pow((pathSteps - step)/((double)pathSteps), 1.0/alpha); |
| 210 |
step++; |
| 211 |
return result; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
public class BetaIntegrator extends Integrator { |
| 216 |
private BetaDistributionImpl betaDistribution; |
| 217 |
|
| 218 |
public BetaIntegrator(double alpha, double beta, int pathSteps) { |
| 219 |
super(pathSteps); |
| 220 |
this.betaDistribution = new BetaDistributionImpl(alpha, beta); |
| 221 |
} |
| 222 |
|
| 223 |
double nextPathParameter() { |
| 224 |
if (step > pathSteps) |
| 225 |
return -1; |
| 226 |
if (step == 0) { |
| 227 |
step += 1; |
| 228 |
return 1.0; |
| 229 |
} else if (step + 1 < pathSteps) { |
| 230 |
double ratio = (double) step / (double) (pathSteps - 1); |
| 231 |
try { |
| 232 |
step += 1; |
| 233 |
return 1.0 - betaDistribution.inverseCumulativeProbability(ratio); |
| 234 |
} catch (MathException e) { |
| 235 |
e.printStackTrace(); |
| 236 |
} |
| 237 |
} |
| 238 |
step += 1; |
| 239 |
return 0.0; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
public class GeometricIntegrator extends Integrator { |
| 244 |
|
| 245 |
public GeometricIntegrator(int pathSteps) { |
| 246 |
super(pathSteps); |
| 247 |
} |
| 248 |
|
| 249 |
double nextPathParameter() { |
| 250 |
if (step > pathSteps) { |
| 251 |
return -1; |
| 252 |
} |
| 253 |
if (step == pathSteps) { |
| 254 |
step += 1; |
| 255 |
return 0; |
| 256 |
} |
| 257 |
|
| 258 |
step += 1; |
| 259 |
return Math.pow(2, -(step - 1)); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
private void reportIteration(double pathParameter, long chainLength, long burnin, long totalSteps, long steps) { |
| 308 |
if (scheme == PathScheme.FIXED) { |
| 309 |
System.out.println("Attempting fixed theta ("+steps+"/" + (totalSteps) +") = " + pathParameter + " for " + chainLength + " iterations + " + burnin + " burnin."); |
| 310 |
} else { |
| 311 |
System.out.println("Attempting theta ("+steps+"/" + (totalSteps+1) +") = " + pathParameter + " for " + chainLength + " iterations + " + burnin + " burnin."); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
public void run() { |
| 316 |
|
| 317 |
for (MCLogger logger : loggers) { |
| 318 |
logger.startLogging(); |
| 319 |
} |
| 320 |
mc.addMarkovChainListener(chainListener); |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
switch (scheme) { |
| 340 |
case FIXED: |
| 341 |
integrate(new FixedThetaRun(fixedRunValues)); |
| 342 |
break; |
| 343 |
case LINEAR: |
| 344 |
integrate(new LinearIntegrator(pathSteps)); |
| 345 |
break; |
| 346 |
case GEOMETRIC: |
| 347 |
integrate(new GeometricIntegrator(pathSteps)); |
| 348 |
break; |
| 349 |
case ONE_SIDED_BETA: |
| 350 |
integrate(new BetaIntegrator(1.0, betaFactor, pathSteps)); |
| 351 |
break; |
| 352 |
case BETA: |
| 353 |
integrate(new BetaIntegrator(alphaFactor, betaFactor, pathSteps)); |
| 354 |
break; |
| 355 |
case BETA_QUANTILE: |
| 356 |
integrate(new BetaQuantileIntegrator(alphaFactor, pathSteps)); |
| 357 |
break; |
| 358 |
case SIGMOID: |
| 359 |
integrate(new SigmoidIntegrator(alphaFactor, pathSteps)); |
| 360 |
break; |
| 361 |
default: |
| 362 |
throw new RuntimeException("Illegal path scheme"); |
| 363 |
} |
| 364 |
|
| 365 |
mc.removeMarkovChainListener(chainListener); |
| 366 |
} |
| 367 |
|
| 368 |
private final MarkovChainListener chainListener = new MarkovChainListener() { |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
@Override |
| 377 |
public void currentState(long state, MarkovChain markovChain, Model currentModel) { |
| 378 |
|
| 379 |
currentState = state; |
| 380 |
|
| 381 |
if (currentState >= burnin) { |
| 382 |
for (MCLogger logger : loggers) { |
| 383 |
logger.log(state); |
| 384 |
} |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
@Override |
| 392 |
public void bestState(long state, MarkovChain markovChain, Model bestModel) { |
| 393 |
currentState = state; |
| 394 |
} |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
@Override |
| 400 |
public void finished(long chainLength, MarkovChain markovChain) { |
| 401 |
currentState = chainLength; |
| 402 |
OperatorAnalysisPrinter.showOperatorAnalysis(System.out, schedule, false); |
| 403 |
|
| 404 |
for (MCLogger logger : loggers) { |
| 405 |
logger.stopLogging(); |
| 406 |
} |
| 407 |
} |
| 408 |
}; |
| 409 |
|
| 410 |
@Override |
| 411 |
public Citation.Category getCategory() { |
| 412 |
return Citation.Category.FRAMEWORK; |
| 413 |
} |
| 414 |
|
| 415 |
@Override |
| 416 |
public String getDescription() { |
| 417 |
return "Marginal likelihood estimation using path sampling / stepping-stone sampling (first 2 citations) and generalized stepping-stone sampling (3rd citation)"; |
| 418 |
} |
| 419 |
|
| 420 |
@Override |
| 421 |
public List<Citation> getCitations() { |
| 422 |
return Arrays.asList(new Citation( |
| 423 |
new Author[]{ |
| 424 |
new Author("G", "Baele"), |
| 425 |
new Author("P", "Lemey"), |
| 426 |
new Author("T", "Bedford"), |
| 427 |
new Author("A", "Rambaut"), |
| 428 |
new Author("MA", "Suchard"), |
| 429 |
new Author("AV", "Alekseyenko") |
| 430 |
}, |
| 431 |
"Improving the accuracy of demographic and molecular clock model comparison while accommodating phylogenetic uncertainty", |
| 432 |
2012, |
| 433 |
"Mol. Biol. Evol.", |
| 434 |
29, |
| 435 |
2157, 2167, |
| 436 |
Citation.Status.PUBLISHED |
| 437 |
), |
| 438 |
new Citation( |
| 439 |
new Author[]{ |
| 440 |
new Author("G", "Baele"), |
| 441 |
new Author("WLS", "Li"), |
| 442 |
new Author("AJ", "Drummond"), |
| 443 |
new Author("MA", "Suchard"), |
| 444 |
new Author("P", "Lemey") |
| 445 |
}, |
| 446 |
"Accurate model selection of relaxed molecular clocks in Bayesian phylogenetics", |
| 447 |
2013, |
| 448 |
"Mol. Biol. Evol.", |
| 449 |
30, |
| 450 |
239, 243, |
| 451 |
Citation.Status.PUBLISHED |
| 452 |
), |
| 453 |
new Citation( |
| 454 |
new Author[]{ |
| 455 |
new Author("G", "Baele"), |
| 456 |
new Author("P", "Lemey"), |
| 457 |
new Author("MA", "Suchard") |
| 458 |
}, |
| 459 |
"Genealogical working distributions for Bayesian model testing with phylogenetic uncertainty", |
| 460 |
2016, |
| 461 |
"Syst. Biol.", |
| 462 |
65, |
| 463 |
250, 264, |
| 464 |
Citation.Status.PUBLISHED |
| 465 |
) |
| 466 |
); |
| 467 |
} |
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
public boolean getSpawnable() { |
| 475 |
return spawnable; |
| 476 |
} |
| 477 |
|
| 478 |
private boolean spawnable = true; |
| 479 |
|
| 480 |
public void setSpawnable(boolean spawnable) { |
| 481 |
this.spawnable = spawnable; |
| 482 |
} |
| 483 |
|
| 484 |
public void setAlphaFactor(double alpha) { |
| 485 |
alphaFactor = alpha; |
| 486 |
} |
| 487 |
|
| 488 |
public void setBetaFactor(double beta) { |
| 489 |
betaFactor = beta; |
| 490 |
} |
| 491 |
|
| 492 |
public double getAlphaFactor() { |
| 493 |
return alphaFactor; |
| 494 |
} |
| 495 |
|
| 496 |
public double getBetaFactor() { |
| 497 |
return betaFactor; |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 502 |
|
| 503 |
public String getParserName() { |
| 504 |
return MARGINAL_LIKELIHOOD_ESTIMATOR; |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 511 |
|
| 512 |
PathLikelihood pathLikelihood = (PathLikelihood) xo.getChild(PathLikelihood.class); |
| 513 |
|
| 514 |
List<MCLogger> loggerList = new ArrayList<MCLogger>(); |
| 515 |
for (int i = 0; i < xo.getChildCount(); i++) { |
| 516 |
if (xo.getChild(i) instanceof MCLogger) { |
| 517 |
loggerList.add((MCLogger)xo.getChild(i)); |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
int chainLength = xo.getIntegerAttribute(CHAIN_LENGTH); |
| 524 |
int pathSteps = -1; |
| 525 |
double[] fixedRunValues = null; |
| 526 |
if (xo.hasAttribute(PATH_STEPS)) { |
| 527 |
pathSteps = xo.getIntegerAttribute(PATH_STEPS); |
| 528 |
} else if (xo.hasAttribute(FIXED_VALUE)) { |
| 529 |
fixedRunValues = xo.getDoubleArrayAttribute(FIXED_VALUE); |
| 530 |
} else { |
| 531 |
throw new RuntimeException("Either a number of path steps or predefined beta values need to be provided."); |
| 532 |
} |
| 533 |
|
| 534 |
if (xo.hasAttribute(PRINT_OPERATOR_ANALYSIS)) { |
| 535 |
SHOW_OPERATOR_ANALYSIS = xo.getBooleanAttribute(PRINT_OPERATOR_ANALYSIS); |
| 536 |
} |
| 537 |
|
| 538 |
int burninLength = -1; |
| 539 |
if (xo.hasAttribute(BURNIN)) { |
| 540 |
burninLength = xo.getIntegerAttribute(BURNIN); |
| 541 |
} |
| 542 |
|
| 543 |
int prerunLength = -1; |
| 544 |
if (xo.hasAttribute(PRERUN)) { |
| 545 |
prerunLength = xo.getIntegerAttribute(PRERUN); |
| 546 |
} |
| 547 |
|
| 548 |
|
| 549 |
boolean linear = xo.getAttribute(LINEAR, true); |
| 550 |
|
| 551 |
PathScheme scheme; |
| 552 |
if (linear) { |
| 553 |
scheme = PathScheme.LINEAR; |
| 554 |
} else { |
| 555 |
scheme = PathScheme.GEOMETRIC; |
| 556 |
} |
| 557 |
|
| 558 |
|
| 559 |
if (xo.hasAttribute(PATH_SCHEME)) { |
| 560 |
scheme = PathScheme.parseFromString(xo.getAttribute(PATH_SCHEME, PathScheme.LINEAR.getText())); |
| 561 |
} |
| 562 |
|
| 563 |
for (int i = 0; i < xo.getChildCount(); i++) { |
| 564 |
Object child = xo.getChild(i); |
| 565 |
if (child instanceof Logger) { |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
CombinedOperatorSchedule os = new CombinedOperatorSchedule(); |
| 570 |
|
| 571 |
XMLObject mcmcXML = xo.getChild(MCMC); |
| 572 |
for (int i = 0; i < mcmcXML.getChildCount(); ++i) { |
| 573 |
if (mcmcXML.getChild(i) instanceof MCMC) { |
| 574 |
MCMC mcmc = (MCMC) mcmcXML.getChild(i); |
| 575 |
if (prerunLength > 0) { |
| 576 |
java.util.logging.Logger.getLogger("dr.inference").info("Path Sampling Marginal Likelihood Estimator:\n\tEquilibrating chain " + mcmc.getId() + " for " + prerunLength + " iterations."); |
| 577 |
for (Logger log : mcmc.getLoggers()) { |
| 578 |
log.stopLogging(); |
| 579 |
} |
| 580 |
mcmc.getMarkovChain().runChain(prerunLength, false); |
| 581 |
} |
| 582 |
if (xo.getChild(OperatorSchedule.class) != null) { |
| 583 |
os.addOperatorSchedule((OperatorSchedule)xo.getChild(OperatorSchedule.class)); |
| 584 |
} else { |
| 585 |
os.addOperatorSchedule(mcmc.getOperatorSchedule()); |
| 586 |
} |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
if (os.getScheduleCount() == 0) { |
| 591 |
System.err.println("Error: no mcmc objects provided in construction. Bayes Factor estimation will likely fail."); |
| 592 |
} |
| 593 |
|
| 594 |
MarginalLikelihoodEstimator mle = new MarginalLikelihoodEstimator(MARGINAL_LIKELIHOOD_ESTIMATOR, chainLength, |
| 595 |
burninLength, pathSteps, fixedRunValues, scheme, pathLikelihood, os, loggerList); |
| 596 |
|
| 597 |
if (!xo.getAttribute(SPAWN, true)) |
| 598 |
mle.setSpawnable(false); |
| 599 |
|
| 600 |
if (xo.hasAttribute(ALPHA)) { |
| 601 |
mle.setAlphaFactor(xo.getAttribute(ALPHA, 0.5)); |
| 602 |
} |
| 603 |
|
| 604 |
if (xo.hasAttribute(BETA)) { |
| 605 |
mle.setBetaFactor(xo.getAttribute(BETA, 0.5)); |
| 606 |
} |
| 607 |
|
| 608 |
String alphaBetaText = ""; |
| 609 |
if (scheme == PathScheme.ONE_SIDED_BETA) { |
| 610 |
alphaBetaText += "(1," + mle.getBetaFactor() + ")"; |
| 611 |
} else if (scheme == PathScheme.BETA) { |
| 612 |
alphaBetaText += "(" + mle.getAlphaFactor() + "," + mle.getBetaFactor() + ")"; |
| 613 |
} else if (scheme == PathScheme.BETA_QUANTILE) { |
| 614 |
alphaBetaText += "(" + mle.getAlphaFactor() + ")"; |
| 615 |
} else if (scheme == PathScheme.SIGMOID) { |
| 616 |
alphaBetaText += "(" + mle.getAlphaFactor() + ")"; |
| 617 |
} |
| 618 |
java.util.logging.Logger.getLogger("dr.inference").info("\nCreating the Marginal Likelihood Estimator chain:" + |
| 619 |
"\n chainLength=" + chainLength + |
| 620 |
"\n pathSteps=" + pathSteps + |
| 621 |
"\n pathScheme=" + scheme.getText() + alphaBetaText); |
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
return mle; |
| 630 |
} |
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
public String getParserDescription() { |
| 637 |
return "This element returns an MCMC chain and runs the chain as a side effect."; |
| 638 |
} |
| 639 |
|
| 640 |
public Class getReturnType() { |
| 641 |
return MCMC.class; |
| 642 |
} |
| 643 |
|
| 644 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 645 |
return rules; |
| 646 |
} |
| 647 |
|
| 648 |
private final XMLSyntaxRule[] rules = { |
| 649 |
AttributeRule.newIntegerRule(CHAIN_LENGTH), |
| 650 |
AttributeRule.newIntegerRule(PATH_STEPS, true), |
| 651 |
AttributeRule.newIntegerRule(BURNIN, true), |
| 652 |
AttributeRule.newIntegerRule(PRERUN, true), |
| 653 |
AttributeRule.newBooleanRule(LINEAR, true), |
| 654 |
AttributeRule.newBooleanRule(LACING, true), |
| 655 |
AttributeRule.newBooleanRule(SPAWN, true), |
| 656 |
AttributeRule.newBooleanRule(PRINT_OPERATOR_ANALYSIS, true), |
| 657 |
AttributeRule.newStringRule(PATH_SCHEME, true), |
| 658 |
AttributeRule.newDoubleArrayRule(FIXED_VALUE, true), |
| 659 |
AttributeRule.newDoubleRule(ALPHA, true), |
| 660 |
AttributeRule.newDoubleRule(BETA, true), |
| 661 |
new ElementRule(MCMC, |
| 662 |
new XMLSyntaxRule[]{new ElementRule(MCMC.class, 1, Integer.MAX_VALUE)}, false), |
| 663 |
|
| 664 |
new ElementRule(PathLikelihood.class), |
| 665 |
new ElementRule(MCLogger.class, 1, Integer.MAX_VALUE) |
| 666 |
}; |
| 667 |
|
| 668 |
}; |
| 669 |
|
| 670 |
public String getId() { |
| 671 |
return id; |
| 672 |
} |
| 673 |
|
| 674 |
public void setId(String id) { |
| 675 |
this.id = id; |
| 676 |
} |
| 677 |
|
| 678 |
enum PathScheme { |
| 679 |
FIXED("fixed"), |
| 680 |
LINEAR("linear"), |
| 681 |
GEOMETRIC("geometric"), |
| 682 |
BETA("beta"), |
| 683 |
ONE_SIDED_BETA("oneSidedBeta"), |
| 684 |
BETA_QUANTILE("betaQuantile"), |
| 685 |
SIGMOID("sigmoid"); |
| 686 |
|
| 687 |
PathScheme(String text) { |
| 688 |
this.text = text; |
| 689 |
} |
| 690 |
|
| 691 |
public String getText() { |
| 692 |
return text; |
| 693 |
} |
| 694 |
|
| 695 |
private final String text; |
| 696 |
|
| 697 |
public static PathScheme parseFromString(String text) { |
| 698 |
for (PathScheme scheme : PathScheme.values()) { |
| 699 |
if (scheme.getText().compareToIgnoreCase(text) == 0) |
| 700 |
return scheme; |
| 701 |
} |
| 702 |
return null; |
| 703 |
} |
| 704 |
} |
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
private final MarkovChain mc; |
| 713 |
private OperatorSchedule schedule; |
| 714 |
|
| 715 |
private String id = null; |
| 716 |
|
| 717 |
private long currentState; |
| 718 |
private final long chainLength; |
| 719 |
|
| 720 |
private long burnin; |
| 721 |
private final long burninLength; |
| 722 |
private int pathSteps; |
| 723 |
|
| 724 |
|
| 725 |
private final PathScheme scheme; |
| 726 |
private double alphaFactor = 0.5; |
| 727 |
private double betaFactor = 0.5; |
| 728 |
private double[] fixedRunValues; |
| 729 |
private final double pathDelta; |
| 730 |
private double pathParameter; |
| 731 |
|
| 732 |
private final List<MCLogger> loggers; |
| 733 |
|
| 734 |
private final PathLikelihood pathLikelihood; |
| 735 |
|
| 736 |
public static final String MARGINAL_LIKELIHOOD_ESTIMATOR = "marginalLikelihoodEstimator"; |
| 737 |
public static final String CHAIN_LENGTH = "chainLength"; |
| 738 |
public static final String PATH_STEPS = "pathSteps"; |
| 739 |
public static final String FIXED = "fixed"; |
| 740 |
public static final String LINEAR = "linear"; |
| 741 |
public static final String LACING = "lacing"; |
| 742 |
public static final String SPAWN = "spawn"; |
| 743 |
public static final String BURNIN = "burnin"; |
| 744 |
public static final String MCMC = "samplers"; |
| 745 |
public static final String PATH_SCHEME = "pathScheme"; |
| 746 |
public static final String FIXED_VALUE = "fixedValues"; |
| 747 |
public static final String ALPHA = "alpha"; |
| 748 |
public static final String BETA = "beta"; |
| 749 |
public static final String PRERUN = "prerun"; |
| 750 |
public static final String PRINT_OPERATOR_ANALYSIS = "printOperatorAnalysis"; |
| 751 |
|
| 752 |
private static boolean SHOW_OPERATOR_ANALYSIS = false; |
| 753 |
} |