Search in sources :

Example 21 with AdaptationMode

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

the class DeltaExchangeOperatorParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    AdaptationMode mode = AdaptationMode.parseMode(xo);
    final boolean isIntegerOperator = xo.getAttribute(INTEGER_OPERATOR, false);
    double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
    double delta = xo.getDoubleAttribute(DELTA);
    if (delta <= 0.0) {
        throw new XMLParseException("delta must be greater than 0.0");
    }
    Parameter parameter = (Parameter) xo.getChild(Parameter.class);
    int[] parameterWeights;
    if (xo.hasAttribute(PARAMETER_WEIGHTS)) {
        parameterWeights = xo.getIntegerArrayAttribute(PARAMETER_WEIGHTS);
        System.out.print("Parameter weights for delta exchange are: ");
        for (int parameterWeight : parameterWeights) {
            System.out.print(parameterWeight + "\t");
        }
        System.out.println();
    } else {
        parameterWeights = new int[parameter.getDimension()];
        for (int i = 0; i < parameterWeights.length; i++) {
            parameterWeights[i] = 1;
        }
    }
    if (parameterWeights.length != parameter.getDimension()) {
        throw new XMLParseException("parameter weights have the same length as parameter");
    }
    return new DeltaExchangeOperator(parameter, parameterWeights, delta, weight, isIntegerOperator, mode);
}
Also used : AdaptationMode(dr.inference.operators.AdaptationMode) Parameter(dr.inference.model.Parameter) DeltaExchangeOperator(dr.inference.operators.DeltaExchangeOperator)

Example 22 with AdaptationMode

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

the class ScaleOperatorParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    final boolean scaleAll = xo.getAttribute(SCALE_ALL, false);
    final boolean scaleAllInd = xo.getAttribute(SCALE_ALL_IND, false);
    final int degreesOfFreedom = xo.getAttribute(DEGREES_OF_FREEDOM, 0);
    final boolean ignoreBounds = xo.getAttribute(IGNORE_BOUNDS, false);
    final AdaptationMode mode = AdaptationMode.parseMode(xo);
    final double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
    final double scaleFactor = xo.getDoubleAttribute(SCALE_FACTOR);
    if (scaleFactor <= 0.0 || scaleFactor >= 1.0) {
        throw new XMLParseException("scaleFactor must be between 0.0 and 1.0");
    }
    final Parameter parameter = (Parameter) xo.getChild(Parameter.class);
    Bounds<Double> bounds = parameter.getBounds();
    Boolean negativeSpace = null;
    for (int dim = 0; dim < parameter.getDimension(); dim++) {
        if (parameter.getParameterValue(dim) < 0.0) {
            if (negativeSpace != null && !negativeSpace) {
                throw new XMLParseException("Scale operator can only be used on parameters where all elements are strictly positive or negative (" + parameter.getId() + ")");
            }
            negativeSpace = true;
            if (bounds.getUpperLimit(dim) > 0.0) {
                throw new XMLParseException("Scale operator can only be used on parameters constrained to be strictly positive or negative (" + parameter.getId() + ")");
            }
            if (!ignoreBounds && !Double.isInfinite(bounds.getLowerLimit(dim))) {
                throw new XMLParseException("Scale operator can only be used on parameters with an infinite upper or lower bound (use a RandomWalk) (" + parameter.getId() + ")");
            }
        } else if (parameter.getParameterValue(dim) > 0.0) {
            if (negativeSpace != null && negativeSpace) {
                throw new XMLParseException("Scale operator can only be used on parameters where all elements are strictly positive or negative (" + parameter.getId() + ")");
            }
            negativeSpace = false;
            if (bounds.getLowerLimit(dim) < 0.0) {
                throw new XMLParseException("Scale operator can only be used on parameters constrained to be strictly positive or negative (" + parameter.getId() + ")");
            }
            if (!ignoreBounds && !Double.isInfinite(bounds.getUpperLimit(dim))) {
                throw new XMLParseException("Scale operator can only be used on parameters with an infinite upper or lower bound (use a RandomWalk) (" + parameter.getId() + ")");
            }
        } else {
            // disallow zero starting values
            throw new XMLParseException("Scale operator can only be used on parameters where all elements are strictly positive or negative (" + parameter.getId() + ")");
        }
    }
    Parameter indicator = null;
    double indicatorOnProb = 1.0;
    final XMLObject inds = xo.getChild(INDICATORS);
    if (inds != null) {
        indicator = (Parameter) inds.getChild(Parameter.class);
        if (inds.hasAttribute(PICKONEPROB)) {
            indicatorOnProb = inds.getDoubleAttribute(PICKONEPROB);
            if (!(0 <= indicatorOnProb && indicatorOnProb <= 1)) {
                throw new XMLParseException("pickoneprob must be between 0.0 and 1.0");
            }
        }
    }
    ScaleOperator operator = new ScaleOperator(parameter, scaleAll, degreesOfFreedom, scaleFactor, mode, indicator, indicatorOnProb, scaleAllInd);
    operator.setWeight(weight);
    return operator;
}
Also used : AdaptationMode(dr.inference.operators.AdaptationMode) Parameter(dr.inference.model.Parameter) ScaleOperator(dr.inference.operators.ScaleOperator)

Example 23 with AdaptationMode

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

the class TransformedRandomWalkOperatorParser 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);
    int dim = parameter.getDimension();
    Transform[] transformations = new Transform[dim];
    for (int i = 0; i < dim; i++) {
        transformations[i] = Transform.NONE;
    }
    for (int i = 0; i < xo.getChildCount(); i++) {
        Object child = xo.getChild(i);
        if (child instanceof Transform.ParsedTransform) {
            Transform.ParsedTransform thisObject = (Transform.ParsedTransform) child;
            System.err.println("Transformations:");
            for (int j = thisObject.start; j < thisObject.end; ++j) {
                transformations[j] = thisObject.transform;
                System.err.print(transformations[j].getTransformName() + " ");
            }
            System.err.println();
        }
    }
    Double lower = null;
    Double upper = null;
    if (xo.hasAttribute(LOWER)) {
        lower = xo.getDoubleAttribute(LOWER);
    }
    if (xo.hasAttribute(UPPER)) {
        upper = xo.getDoubleAttribute(UPPER);
    }
    TransformedRandomWalkOperator.BoundaryCondition condition = TransformedRandomWalkOperator.BoundaryCondition.valueOf(xo.getAttribute(BOUNDARY_CONDITION, TransformedRandomWalkOperator.BoundaryCondition.reflecting.name()));
    if (xo.hasChildNamed(UPDATE_INDEX)) {
        XMLObject cxo = xo.getChild(UPDATE_INDEX);
        Parameter updateIndex = (Parameter) cxo.getChild(Parameter.class);
        if (updateIndex.getDimension() != parameter.getDimension())
            throw new RuntimeException("Parameter to update and missing indices must have the same dimension");
        return new TransformedRandomWalkOperator(parameter, transformations, updateIndex, windowSize, condition, weight, mode, lower, upper);
    }
    return new TransformedRandomWalkOperator(parameter, transformations, null, windowSize, condition, weight, mode, lower, upper);
}
Also used : TransformedRandomWalkOperator(dr.inference.operators.TransformedRandomWalkOperator) XMLObject(dr.xml.XMLObject) AdaptationMode(dr.inference.operators.AdaptationMode) Parameter(dr.inference.model.Parameter) XMLObject(dr.xml.XMLObject) Transform(dr.util.Transform)

Example 24 with AdaptationMode

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

the class MVOUCovarianceOperatorParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    AdaptationMode mode = AdaptationMode.parseMode(xo);
    double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
    double mixingFactor = xo.getDoubleAttribute(MIXING_FACTOR);
    int priorDf = xo.getIntegerAttribute(PRIOR_DF);
    if (mixingFactor <= 0.0 || mixingFactor >= 1.0) {
        throw new XMLParseException("mixingFactor must be greater than 0.0 and less thatn 1.0");
    }
    // Parameter parameter = (Parameter) xo.getChild(Parameter.class);
    // XMLObject cxo = (XMLObject) xo.getChild(VARIANCE_MATRIX);
    MatrixParameter varMatrix = (MatrixParameter) xo.getChild(MatrixParameter.class);
    if (varMatrix.getColumnDimension() != varMatrix.getRowDimension())
        throw new XMLParseException("The variance matrix is not square");
    return new MVOUCovarianceOperator(mixingFactor, varMatrix, priorDf, weight, mode);
}
Also used : AdaptationMode(dr.inference.operators.AdaptationMode) MVOUCovarianceOperator(dr.inference.operators.MVOUCovarianceOperator) MatrixParameter(dr.inference.model.MatrixParameter)

Example 25 with AdaptationMode

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

the class GaussianProcessSkytrackTreeOperatorParser 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.GaussianProcessSkytrackBlockUpdateOperator");
    // gmrfLogger.setUseParentHandlers(false);
    // if (logRecord) {
    // gmrfLogger.setLevel(Level.FINE);
    // 
    // try {
    // gmrfHandler = new FileHandler("GPBlockUpdate.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 <= 0.0) {
    // 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);
    GaussianProcessSkytrackLikelihood gpLikelihood = (GaussianProcessSkytrackLikelihood) xo.getChild(GaussianProcessSkytrackLikelihood.class);
    TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
    return new GaussianProcessSkytrackTreeOperator(treeModel, gpLikelihood, weight, scaleFactor, mode);
}
Also used : TreeModel(dr.evomodel.tree.TreeModel) AdaptationMode(dr.inference.operators.AdaptationMode) GaussianProcessSkytrackTreeOperator(dr.evomodel.coalescent.operators.GaussianProcessSkytrackTreeOperator) GaussianProcessSkytrackLikelihood(dr.evomodel.coalescent.GaussianProcessSkytrackLikelihood)

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