Search in sources :

Example 1 with MarkovChain

use of dr.inference.markovchain.MarkovChain in project beast-mcmc by beast-dev.

the class MCMC method init.

/**
     * Must be called before calling chain.
     *
     * @param options    the options for this MCMC analysis
     * @param schedule   operator schedule to be used in chain.
     * @param likelihood the likelihood for this MCMC
     * @param loggers    an array of loggers to record output of this MCMC run
     */
public void init(MCMCOptions options, Likelihood likelihood, OperatorSchedule schedule, Logger[] loggers) {
    MCMCCriterion criterion = new MCMCCriterion();
    criterion.setTemperature(options.getTemperature());
    mc = new MarkovChain(likelihood, schedule, criterion, options.getFullEvaluationCount(), options.minOperatorCountForFullEvaluation(), options.getEvaluationTestThreshold(), options.useCoercion());
    this.options = options;
    this.loggers = loggers;
    this.schedule = schedule;
    //initialize transients
    currentState = 0;
    if (Factory.INSTANCE != null) {
        for (MarkovChainListener listener : Factory.INSTANCE.getStateSaverChainListeners()) {
            mc.addMarkovChainListener(listener);
        }
    }
}
Also used : MarkovChain(dr.inference.markovchain.MarkovChain) MarkovChainListener(dr.inference.markovchain.MarkovChainListener)

Example 2 with MarkovChain

use of dr.inference.markovchain.MarkovChain in project beast-mcmc by beast-dev.

the class MCMCParser method parseXMLObject.

/**
     * @return an mcmc object based on the XML element it was passed.
     */
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    MCMC mcmc = new MCMC(xo.getAttribute(NAME, "mcmc1"));
    long chainLength = xo.getLongIntegerAttribute(CHAIN_LENGTH);
    boolean useCoercion = xo.getAttribute(COERCION, true);
    long coercionDelay = chainLength / 100;
    if (xo.hasAttribute(PRE_BURNIN)) {
        coercionDelay = xo.getIntegerAttribute(PRE_BURNIN);
    }
    coercionDelay = xo.getAttribute(COERCION_DELAY, coercionDelay);
    double temperature = xo.getAttribute(TEMPERATURE, 1.0);
    long fullEvaluationCount = xo.getAttribute(FULL_EVALUATION, 2000);
    double evaluationTestThreshold = MarkovChain.EVALUATION_TEST_THRESHOLD;
    if (System.getProperty("mcmc.evaluation.threshold") != null) {
        evaluationTestThreshold = Double.parseDouble(System.getProperty("mcmc.evaluation.threshold"));
    }
    evaluationTestThreshold = xo.getAttribute(EVALUATION_THRESHOLD, evaluationTestThreshold);
    int minOperatorCountForFullEvaluation = xo.getAttribute(MIN_OPS_EVALUATIONS, 1);
    MCMCOptions options = new MCMCOptions(chainLength, fullEvaluationCount, minOperatorCountForFullEvaluation, evaluationTestThreshold, useCoercion, coercionDelay, temperature);
    OperatorSchedule opsched = (OperatorSchedule) xo.getChild(OperatorSchedule.class);
    Likelihood likelihood = (Likelihood) xo.getChild(Likelihood.class);
    likelihood.setUsed();
    if (Boolean.valueOf(System.getProperty("show_warnings", "false"))) {
        // check that all models, parameters and likelihoods are being used
        for (Likelihood l : Likelihood.FULL_LIKELIHOOD_SET) {
            if (!l.isUsed()) {
                java.util.logging.Logger.getLogger("dr.inference").warning("Likelihood, " + l.getId() + ", of class " + l.getClass().getName() + " is not being handled by the MCMC.");
            }
        }
        for (Model m : Model.FULL_MODEL_SET) {
            if (!m.isUsed()) {
                java.util.logging.Logger.getLogger("dr.inference").warning("Model, " + m.getId() + ", of class " + m.getClass().getName() + " is not being handled by the MCMC.");
            }
        }
        for (Parameter p : Parameter.FULL_PARAMETER_SET) {
            if (!p.isUsed()) {
                java.util.logging.Logger.getLogger("dr.inference").warning("Parameter, " + p.getId() + ", of class " + p.getClass().getName() + " is not being handled by the MCMC.");
            }
        }
    }
    ArrayList<Logger> loggers = new ArrayList<Logger>();
    for (int i = 0; i < xo.getChildCount(); i++) {
        Object child = xo.getChild(i);
        if (child instanceof Logger) {
            loggers.add((Logger) child);
        }
    }
    mcmc.setShowOperatorAnalysis(true);
    if (xo.hasAttribute(OPERATOR_ANALYSIS)) {
        mcmc.setOperatorAnalysisFile(XMLParser.getLogFile(xo, OPERATOR_ANALYSIS));
    }
    Logger[] loggerArray = new Logger[loggers.size()];
    loggers.toArray(loggerArray);
    java.util.logging.Logger.getLogger("dr.inference").info("\nCreating the MCMC chain:" + "\n  chainLength=" + options.getChainLength() + "\n  autoOptimize=" + options.useCoercion() + (options.useCoercion() ? "\n  autoOptimize delayed for " + options.getCoercionDelay() + " steps" : "") + (options.getFullEvaluationCount() == 0 ? "\n  full evaluation test off" : ""));
    mcmc.init(options, likelihood, opsched, loggerArray);
    MarkovChain mc = mcmc.getMarkovChain();
    double initialScore = mc.getCurrentScore();
    if (initialScore == Double.NEGATIVE_INFINITY) {
        String message = "The initial posterior is zero";
        if (likelihood instanceof CompoundLikelihood) {
            message += ": " + ((CompoundLikelihood) likelihood).getDiagnosis(2);
        } else {
            message += "!";
        }
        throw new IllegalArgumentException(message);
    }
    if (!xo.getAttribute(SPAWN, true))
        mcmc.setSpawnable(false);
    return mcmc;
}
Also used : OperatorSchedule(dr.inference.operators.OperatorSchedule) CompoundLikelihood(dr.inference.model.CompoundLikelihood) Likelihood(dr.inference.model.Likelihood) CompoundLikelihood(dr.inference.model.CompoundLikelihood) MCMC(dr.inference.mcmc.MCMC) ArrayList(java.util.ArrayList) MarkovChain(dr.inference.markovchain.MarkovChain) Logger(dr.inference.loggers.Logger) MCMCOptions(dr.inference.mcmc.MCMCOptions) Model(dr.inference.model.Model) Parameter(dr.inference.model.Parameter)

Aggregations

MarkovChain (dr.inference.markovchain.MarkovChain)2 Logger (dr.inference.loggers.Logger)1 MarkovChainListener (dr.inference.markovchain.MarkovChainListener)1 MCMC (dr.inference.mcmc.MCMC)1 MCMCOptions (dr.inference.mcmc.MCMCOptions)1 CompoundLikelihood (dr.inference.model.CompoundLikelihood)1 Likelihood (dr.inference.model.Likelihood)1 Model (dr.inference.model.Model)1 Parameter (dr.inference.model.Parameter)1 OperatorSchedule (dr.inference.operators.OperatorSchedule)1 ArrayList (java.util.ArrayList)1