Search in sources :

Example 11 with AdaptationMode

use of dr.inference.operators.AdaptationMode in project beast-mcmc by beast-dev.

the class SubtreeLeapOperatorParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    AdaptationMode mode = AdaptationMode.parseMode(xo);
    TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
    final double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
    // size attribute is mandatory
    final double size = xo.getAttribute(SIZE, Double.NaN);
    final double targetAcceptance = xo.getAttribute(TARGET_ACCEPTANCE, 0.234);
    SubtreeLeapOperator.DistanceKernelType distanceKernel = SubtreeLeapOperator.DistanceKernelType.NORMAL;
    if (xo.hasAttribute(DISTANCE_KERNEL)) {
        try {
            distanceKernel = SubtreeLeapOperator.DistanceKernelType.valueOf(xo.getStringAttribute(DISTANCE_KERNEL).trim().toUpperCase());
        } catch (IllegalArgumentException iae) {
            throw new XMLParseException("Unrecognised distanceKernel attribute: " + xo.getStringAttribute(DISTANCE_KERNEL));
        }
    }
    if (size <= 0.0) {
        throw new XMLParseException("The SubTreeLeap size attribute must be positive and non-zero.");
    }
    if (targetAcceptance <= 0.0 || targetAcceptance >= 1.0) {
        throw new XMLParseException("Target acceptance probability has to lie in (0, 1)");
    }
    final boolean slideOnly = xo.getAttribute(SLIDE_ONLY, false);
    return new SubtreeLeapOperator(treeModel, weight, size, distanceKernel, slideOnly, mode, targetAcceptance);
}
Also used : TreeModel(dr.evomodel.tree.TreeModel) AdaptationMode(dr.inference.operators.AdaptationMode) SubtreeLeapOperator(dr.evomodel.operators.SubtreeLeapOperator)

Example 12 with AdaptationMode

use of dr.inference.operators.AdaptationMode in project beast-mcmc by beast-dev.

the class TipLeapOperatorParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    AdaptationMode mode = AdaptationMode.parseMode(xo);
    TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
    final double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
    Taxa taxa = (Taxa) xo.getChild(Taxa.class);
    List<NodeRef> tips = new ArrayList<NodeRef>();
    for (Taxon taxon : taxa) {
        boolean found = false;
        for (int i = 0; i < treeModel.getExternalNodeCount(); i++) {
            NodeRef tip = treeModel.getExternalNode(i);
            if (treeModel.getNodeTaxon(tip).equals(taxon)) {
                tips.add(tip);
                found = true;
                break;
            }
        }
        if (!found) {
            throw new XMLParseException("Error constructing " + TIP_LEAP + ": " + taxon.getId() + ", not found in tree with id " + treeModel.getId());
        }
    }
    // size attribute is mandatory
    final double size = xo.getAttribute(SIZE, Double.NaN);
    final double targetAcceptance = xo.getAttribute(TARGET_ACCEPTANCE, 0.234);
    final SubtreeLeapOperator.DistanceKernelType distanceKernel = SubtreeLeapOperator.DistanceKernelType.NORMAL;
    if (xo.hasAttribute(DISTANCE_KERNEL)) {
        try {
            SubtreeLeapOperator.DistanceKernelType.valueOf(xo.getStringAttribute(DISTANCE_KERNEL).trim().toUpperCase());
        } catch (IllegalArgumentException iae) {
            throw new XMLParseException("Unrecognised distanceKernel attribute: " + xo.getStringAttribute(DISTANCE_KERNEL));
        }
    }
    if (size <= 0.0) {
        throw new XMLParseException("The TipLeap size attribute must be positive and non-zero.");
    }
    if (targetAcceptance <= 0.0 || targetAcceptance >= 1.0) {
        throw new XMLParseException("Target acceptance probability has to lie in (0, 1)");
    }
    return new SubtreeLeapOperator(treeModel, taxa, weight, size, distanceKernel, mode, targetAcceptance);
}
Also used : Taxon(dr.evolution.util.Taxon) ArrayList(java.util.ArrayList) TreeModel(dr.evomodel.tree.TreeModel) Taxa(dr.evolution.util.Taxa) NodeRef(dr.evolution.tree.NodeRef) AdaptationMode(dr.inference.operators.AdaptationMode) SubtreeLeapOperator(dr.evomodel.operators.SubtreeLeapOperator)

Example 13 with AdaptationMode

use of dr.inference.operators.AdaptationMode in project beast-mcmc by beast-dev.

the class FunkyPriorMixerOperatorParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    AdaptationMode mode = AdaptationMode.parseMode(xo);
    double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
    double windowSize = xo.getDoubleAttribute(WINDOW_SIZE);
    Parameter parameter = (Parameter) xo.getChild(Parameter.class);
    TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
    RandomWalkOperator.BoundaryCondition condition = RandomWalkOperator.BoundaryCondition.valueOf(xo.getAttribute(BOUNDARY_CONDITION, RandomWalkOperator.BoundaryCondition.reflecting.name()));
    return new FunkyPriorMixerOperator(treeModel, parameter, windowSize, condition, weight, mode);
}
Also used : RandomWalkOperator(dr.inference.operators.RandomWalkOperator) TreeModel(dr.evomodel.tree.TreeModel) AdaptationMode(dr.inference.operators.AdaptationMode) Parameter(dr.inference.model.Parameter) FunkyPriorMixerOperator(dr.evomodel.operators.FunkyPriorMixerOperator)

Example 14 with AdaptationMode

use of dr.inference.operators.AdaptationMode in project beast-mcmc by beast-dev.

the class GMRFSkyrideBlockUpdateOperatorParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    boolean logRecord = xo.getAttribute(KEEP_LOG_RECORD, false);
    Handler gmrfHandler;
    Logger gmrfLogger = Logger.getLogger("dr.evomodel.coalescent.operators.GMRFSkyrideBlockUpdateOperator");
    gmrfLogger.setUseParentHandlers(false);
    if (logRecord) {
        gmrfLogger.setLevel(Level.FINE);
        try {
            gmrfHandler = new FileHandler("GMRFBlockUpdate.log." + MathUtils.getSeed());
        } catch (IOException e) {
            throw new RuntimeException(e.getMessage());
        }
        gmrfHandler.setLevel(Level.FINE);
        gmrfHandler.setFormatter(new XMLFormatter() {

            public String format(LogRecord record) {
                return "<record>\n \t<message>\n\t" + record.getMessage() + "\n\t</message>\n<record>\n";
            }
        });
        gmrfLogger.addHandler(gmrfHandler);
    }
    AdaptationMode mode = AdaptationMode.parseMode(xo);
    if (mode == AdaptationMode.DEFAULT)
        mode = AdaptationMode.ADAPTATION_ON;
    double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
    double scaleFactor = xo.getDoubleAttribute(SCALE_FACTOR);
    if (scaleFactor == 1.0) {
        mode = AdaptationMode.ADAPTATION_OFF;
    }
    // throw new XMLParseException("scaleFactor must be greater than 0.0");
    if (scaleFactor < 1.0) {
        throw new XMLParseException("scaleFactor must be greater than or equal to 1.0");
    }
    int maxIterations = xo.getAttribute(MAX_ITERATIONS, 200);
    double stopValue = xo.getAttribute(STOP_VALUE, 0.01);
    if (xo.getAttribute(OLD_SKYRIDE, true) && !(xo.getName().compareTo(GRID_BLOCK_UPDATE_OPERATOR) == 0)) {
        OldGMRFSkyrideLikelihood gmrfLikelihood = (OldGMRFSkyrideLikelihood) xo.getChild(OldGMRFSkyrideLikelihood.class);
        return new GMRFSkyrideBlockUpdateOperator(gmrfLikelihood, weight, mode, scaleFactor, maxIterations, stopValue);
    } else {
        GMRFMultilocusSkyrideLikelihood gmrfMultilocusLikelihood = (GMRFMultilocusSkyrideLikelihood) xo.getChild(GMRFMultilocusSkyrideLikelihood.class);
        return new GMRFMultilocusSkyrideBlockUpdateOperator(gmrfMultilocusLikelihood, weight, mode, scaleFactor, maxIterations, stopValue);
    }
}
Also used : OldGMRFSkyrideLikelihood(dr.evomodel.coalescent.OldGMRFSkyrideLikelihood) IOException(java.io.IOException) AdaptationMode(dr.inference.operators.AdaptationMode) GMRFSkyrideBlockUpdateOperator(dr.evomodel.coalescent.operators.GMRFSkyrideBlockUpdateOperator) GMRFMultilocusSkyrideLikelihood(dr.evomodel.coalescent.GMRFMultilocusSkyrideLikelihood) GMRFMultilocusSkyrideBlockUpdateOperator(dr.evomodel.coalescent.operators.GMRFMultilocusSkyrideBlockUpdateOperator)

Example 15 with AdaptationMode

use of dr.inference.operators.AdaptationMode in project beast-mcmc by beast-dev.

the class NodeHeightOperatorParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
    final double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
    OperatorType operatorType = OperatorType.UNIFORM;
    if (xo.hasAttribute(OPERATOR_TYPE)) {
        try {
            operatorType = OperatorType.valueOf(xo.getStringAttribute(OPERATOR_TYPE).trim().toUpperCase());
        } catch (IllegalArgumentException iae) {
            throw new XMLParseException("Unrecognised operator type attribute: " + xo.getStringAttribute(OPERATOR_TYPE));
        }
    }
    if (operatorType != OperatorType.UNIFORM) {
        AdaptationMode mode = AdaptationMode.parseMode(xo);
        double tuningParameter = 0.75;
        if (xo.hasAttribute(SIZE)) {
            tuningParameter = xo.getDoubleAttribute(SIZE);
            if (tuningParameter <= 0.0) {
                throw new XMLParseException("The UniformNodeHeightOperator size attribute must be positive and non-zero.");
            }
        }
        if (xo.hasAttribute(SCALE_FACTOR)) {
            tuningParameter = xo.getDoubleAttribute(SCALE_FACTOR);
            if (tuningParameter <= 0.0 || tuningParameter >= 1.0) {
                throw new XMLParseException("The UniformNodeHeightOperator scaleFactor attribute must be between 0 and 1.");
            }
        }
        final double targetAcceptance = xo.getAttribute(TARGET_ACCEPTANCE, 0.234);
        if (targetAcceptance <= 0.0 || targetAcceptance >= 1.0) {
            throw new XMLParseException("Target acceptance probability has to lie in (0, 1)");
        }
        switch(operatorType) {
            case RANDOMWALK:
                return new RandomWalkNodeHeightOperator(treeModel, weight, tuningParameter, mode, targetAcceptance);
            case SCALEROOT:
            case SCALEALL:
                return new ScaleNodeHeightOperator(treeModel, weight, tuningParameter, operatorType, mode, targetAcceptance);
            default:
                throw new IllegalArgumentException("Unknown operator type");
        }
    }
    if (xo.hasAttribute(SIZE) || xo.hasAttribute(SCALE_FACTOR) || xo.hasAttribute(TARGET_ACCEPTANCE) || xo.hasAttribute(AdaptableMCMCOperator.AUTO_OPTIMIZE)) {
        throw new XMLParseException("Uniform node height operator does not take adaptable operator arguments");
    }
    return new UniformNodeHeightOperator(treeModel, weight);
}
Also used : TreeModel(dr.evomodel.tree.TreeModel) ScaleNodeHeightOperator(dr.evomodel.operators.ScaleNodeHeightOperator) AdaptationMode(dr.inference.operators.AdaptationMode) RandomWalkNodeHeightOperator(dr.evomodel.operators.RandomWalkNodeHeightOperator) UniformNodeHeightOperator(dr.evomodel.operators.UniformNodeHeightOperator)

Aggregations

AdaptationMode (dr.inference.operators.AdaptationMode)25 TreeModel (dr.evomodel.tree.TreeModel)8 Parameter (dr.inference.model.Parameter)8 LatentFactorModel (dr.inference.model.LatentFactorModel)5 GaussianProcessSkytrackLikelihood (dr.evomodel.coalescent.GaussianProcessSkytrackLikelihood)2 SubtreeLeapOperator (dr.evomodel.operators.SubtreeLeapOperator)2 DiagonalMatrix (dr.inference.model.DiagonalMatrix)2 MatrixParameter (dr.inference.model.MatrixParameter)2 Transform (dr.util.Transform)2 IOException (java.io.IOException)2 NodeRef (dr.evolution.tree.NodeRef)1 Taxa (dr.evolution.util.Taxa)1 Taxon (dr.evolution.util.Taxon)1 CladeAwareSubtreeLeap (dr.evomodel.bigfasttree.constrainedtree.CladeAwareSubtreeLeap)1 CladeNodeModel (dr.evomodel.bigfasttree.constrainedtree.CladeNodeModel)1 GMRFMultilocusSkyrideLikelihood (dr.evomodel.coalescent.GMRFMultilocusSkyrideLikelihood)1 GMRFSkygridLikelihood (dr.evomodel.coalescent.GMRFSkygridLikelihood)1 OldGMRFSkyrideLikelihood (dr.evomodel.coalescent.OldGMRFSkyrideLikelihood)1 GMRFMultilocusSkyrideBlockUpdateOperator (dr.evomodel.coalescent.operators.GMRFMultilocusSkyrideBlockUpdateOperator)1 GMRFSkygridBlockUpdateOperator (dr.evomodel.coalescent.operators.GMRFSkygridBlockUpdateOperator)1