Search in sources :

Example 6 with AdaptationMode

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

the class SubtreeSlideOperatorParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    boolean swapRates = xo.getAttribute(SWAP_RATES, false);
    boolean swapTraits = xo.getAttribute(SWAP_TRAITS, false);
    boolean scaledDirichletBranches = xo.getAttribute(DIRICHLET_BRANCHES, false);
    AdaptationMode mode = AdaptationMode.DEFAULT;
    if (xo.hasAttribute(AdaptableMCMCOperator.AUTO_OPTIMIZE)) {
        if (xo.getBooleanAttribute(AdaptableMCMCOperator.AUTO_OPTIMIZE)) {
            mode = AdaptationMode.ADAPTATION_ON;
        } else {
            mode = AdaptationMode.ADAPTATION_OFF;
        }
    }
    DefaultTreeModel treeModel = (DefaultTreeModel) xo.getChild(DefaultTreeModel.class);
    final double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
    final double targetAcceptance = xo.getAttribute(TARGET_ACCEPTANCE, 0.234);
    final double size = xo.getAttribute("size", 1.0);
    if (Double.isInfinite(size) || size <= 0.0) {
        throw new XMLParseException("size attribute must be positive and not infinite. was " + size + " for tree " + treeModel.getId());
    }
    final boolean gaussian = xo.getBooleanAttribute("gaussian");
    SubtreeSlideOperator operator = new SubtreeSlideOperator(treeModel, weight, size, gaussian, swapRates, swapTraits, scaledDirichletBranches, mode, targetAcceptance);
    return operator;
}
Also used : AdaptationMode(dr.inference.operators.AdaptationMode) SubtreeSlideOperator(dr.evomodel.operators.SubtreeSlideOperator) DefaultTreeModel(dr.evomodel.tree.DefaultTreeModel)

Example 7 with AdaptationMode

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

the class LatentFactorHamiltonianMCParser method parseXMLObject.

@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    LatentFactorModel lfm = (LatentFactorModel) xo.getChild(LatentFactorModel.class);
    FullyConjugateMultivariateTraitLikelihood tree = (FullyConjugateMultivariateTraitLikelihood) xo.getChild(FullyConjugateMultivariateTraitLikelihood.class);
    double weight = xo.getDoubleAttribute(WEIGHT);
    AdaptationMode mode = AdaptationMode.parseMode(xo);
    int nSteps = xo.getIntegerAttribute(N_STEPS);
    double stepSize = xo.getDoubleAttribute(STEP_SIZE);
    double momentumSd = xo.getDoubleAttribute(MOMENTUM_SD);
    return new LatentFactorHamiltonianMC(lfm, tree, weight, mode, stepSize, nSteps, momentumSd);
}
Also used : AdaptationMode(dr.inference.operators.AdaptationMode) LatentFactorModel(dr.inference.model.LatentFactorModel) FullyConjugateMultivariateTraitLikelihood(dr.evomodel.continuous.FullyConjugateMultivariateTraitLikelihood)

Example 8 with AdaptationMode

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

the class FactorIndependenceOperatorParser method parseXMLObject.

@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    AdaptationMode mode = AdaptationMode.parseMode(xo);
    String scaleFactorTemp = (String) xo.getAttribute(SCALE_FACTOR);
    double scaleFactor = Double.parseDouble(scaleFactorTemp);
    String weightTemp = (String) xo.getAttribute(WEIGHT);
    double weight = Double.parseDouble(weightTemp);
    DiagonalMatrix diffusionMatrix;
    diffusionMatrix = (DiagonalMatrix) xo.getChild(DiagonalMatrix.class);
    LatentFactorModel LFM = (LatentFactorModel) xo.getChild(LatentFactorModel.class);
    boolean randomScan = xo.getAttribute(RANDOM_SCAN, true);
    return new FactorIndependenceOperator(LFM, weight, randomScan, diffusionMatrix, scaleFactor, mode);
}
Also used : AdaptationMode(dr.inference.operators.AdaptationMode) DiagonalMatrix(dr.inference.model.DiagonalMatrix) FactorIndependenceOperator(dr.inference.operators.factorAnalysis.FactorIndependenceOperator) LatentFactorModel(dr.inference.model.LatentFactorModel)

Example 9 with AdaptationMode

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

the class HamiltonianMonteCarloOperatorParser method parseXMLObject.

@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
    int nSteps = xo.getAttribute(N_STEPS, 10);
    double stepSize = xo.getDoubleAttribute(STEP_SIZE);
    int runMode = parseRunMode(xo);
    MassPreconditioner.Type preconditioningType = parsePreconditioning(xo);
    double randomStepFraction = Math.abs(xo.getAttribute(RANDOM_STEP_FRACTION, 0.0));
    if (randomStepFraction > 1) {
        throw new XMLParseException("Random step count fraction must be < 1.0");
    }
    int preconditioningUpdateFrequency = xo.getAttribute(PRECONDITIONING_UPDATE_FREQUENCY, 0);
    int preconditioningDelay = xo.getAttribute(PRECONDITIONING_DELAY, 0);
    int preconditioningMemory = xo.getAttribute(PRECONDITIONING_MEMORY, 0);
    AdaptationMode adaptationMode = AdaptationMode.parseMode(xo);
    GradientWrtParameterProvider derivative = (GradientWrtParameterProvider) xo.getChild(GradientWrtParameterProvider.class);
    if (preconditioningType != MassPreconditioner.Type.NONE && !(derivative instanceof HessianWrtParameterProvider)) {
        throw new XMLParseException("Unable precondition without a Hessian provider");
    }
    Parameter parameter = (Parameter) xo.getChild(Parameter.class);
    if (parameter == null) {
        parameter = derivative.getParameter();
    }
    Transform transform = parseTransform(xo);
    boolean dimensionMismatch = derivative.getDimension() != parameter.getDimension();
    if (transform != null && transform instanceof Transform.MultivariableTransform) {
        dimensionMismatch = ((Transform.MultivariableTransform) transform).getDimension() != parameter.getDimension();
    }
    if (dimensionMismatch) {
        throw new XMLParseException("Gradient (" + derivative.getDimension() + ") must be the same dimensions as the parameter (" + parameter.getDimension() + ")");
    }
    Parameter mask = null;
    if (xo.hasChildNamed(MASK)) {
        mask = (Parameter) xo.getElementFirstChild(MASK);
        if (mask.getDimension() != derivative.getDimension()) {
            throw new XMLParseException("Mask (" + mask.getDimension() + ") must be the same dimension as the gradient (" + derivative.getDimension() + ")");
        }
    }
    int gradientCheckCount = xo.getAttribute(GRADIENT_CHECK_COUNT, 0);
    double gradientCheckTolerance = xo.getAttribute(GRADIENT_CHECK_TOLERANCE, 1E-3);
    int maxIterations = xo.getAttribute(MAX_ITERATIONS, 10);
    double reductionFactor = xo.getAttribute(REDUCTION_FACTOR, 0.1);
    double targetAcceptanceProbability = xo.getAttribute(TARGET_ACCEPTANCE_PROBABILITY, // Stan default
    0.8);
    HamiltonianMonteCarloOperator.Options runtimeOptions = new HamiltonianMonteCarloOperator.Options(stepSize, nSteps, randomStepFraction, preconditioningUpdateFrequency, preconditioningDelay, preconditioningMemory, gradientCheckCount, gradientCheckTolerance, maxIterations, reductionFactor, targetAcceptanceProbability);
    return factory(adaptationMode, weight, derivative, parameter, transform, mask, runtimeOptions, preconditioningType, runMode);
}
Also used : HessianWrtParameterProvider(dr.inference.hmc.HessianWrtParameterProvider) MassPreconditioner(dr.inference.operators.hmc.MassPreconditioner) AdaptationMode(dr.inference.operators.AdaptationMode) GradientWrtParameterProvider(dr.inference.hmc.GradientWrtParameterProvider) Parameter(dr.inference.model.Parameter) Transform(dr.util.Transform) Util.parseTransform(dr.util.Transform.Util.parseTransform) HamiltonianMonteCarloOperator(dr.inference.operators.hmc.HamiltonianMonteCarloOperator)

Example 10 with AdaptationMode

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

the class RateVarianceScaleOperatorParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    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 TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
    final Parameter variance = (Parameter) xo.getChild(Parameter.class);
    if (variance.getDimension() != 1) {
        throw new XMLParseException("dimension of the variance parameter should be 1");
    }
    RateVarianceScaleOperator operator = new RateVarianceScaleOperator(treeModel, variance, scaleFactor, mode);
    operator.setWeight(weight);
    return operator;
}
Also used : TreeModel(dr.evomodel.tree.TreeModel) AdaptationMode(dr.inference.operators.AdaptationMode) Parameter(dr.inference.model.Parameter) RateVarianceScaleOperator(dr.evomodel.operators.RateVarianceScaleOperator)

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