Search in sources :

Example 6 with Logger

use of dr.inference.loggers.Logger in project beast-mcmc by beast-dev.

the class SMC method chain.

/**
 * This method actually initiates the MCMC analysis.
 */
public void chain() {
    currentState = 0;
    timer.start();
    if (loggers != null) {
        for (Logger logger : loggers) {
            logger.startLogging();
        }
    }
    mc.addMarkovChainListener(chainListener);
    for (StateLoaderSaver particleState : particleStates) {
        // Don't need the savedLnL - it won't be there
        particleState.loadState(mc, new double[1]);
        // reset the current chain length to 0
        mc.setCurrentLength(0);
        mc.runChain(options.getChainLength(), true);
        // Save state to file...
        particleState.saveState(mc, mc.getCurrentLength(), mc.getCurrentScore());
    }
    mc.terminateChain();
    mc.removeMarkovChainListener(chainListener);
    timer.stop();
}
Also used : StateLoaderSaver(dr.inference.state.StateLoaderSaver) Logger(dr.inference.loggers.Logger)

Example 7 with Logger

use of dr.inference.loggers.Logger 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

Logger (dr.inference.loggers.Logger)7 Likelihood (dr.inference.model.Likelihood)4 OperatorSchedule (dr.inference.operators.OperatorSchedule)4 ArrayList (java.util.ArrayList)4 CompoundLikelihood (dr.inference.model.CompoundLikelihood)3 MarkovChain (dr.inference.markovchain.MarkovChain)2 MCMC (dr.inference.mcmc.MCMC)2 MCMCOptions (dr.inference.mcmc.MCMCOptions)2 Model (dr.inference.model.Model)2 Parameter (dr.inference.model.Parameter)2 StateLoaderSaver (dr.inference.state.StateLoaderSaver)2 MLOptimizer (dr.inference.ml.MLOptimizer)1 SMC (dr.inference.smc.SMC)1 SMCOptions (dr.inference.smc.SMCOptions)1 StateLoader (dr.inference.state.StateLoader)1 File (java.io.File)1