Search in sources :

Example 1 with ArbitraryBranchRates

use of dr.evomodel.branchratemodel.ArbitraryBranchRates in project beast-mcmc by beast-dev.

the class BranchSubstitutionParameterGradient method numericWrap.

private MultivariateFunction numericWrap(final Parameter parameter) {
    return new MultivariateFunction() {

        @Override
        public double evaluate(double[] argument) {
            if (!(branchRateModel instanceof ArbitraryBranchRates)) {
                throw new RuntimeException("Not yet tested with ProxyParameter.");
            }
            ArbitraryBranchRates branchRates = (ArbitraryBranchRates) branchRateModel;
            Tree tree = treeDataLikelihood.getTree();
            for (int i = 0; i < argument.length; ++i) {
                NodeRef node = tree.getNode(i);
                if (!tree.isRoot(node)) {
                    branchRates.setBranchRate(tree, tree.getNode(i), argument[i]);
                }
            }
            // treeDataLikelihood.makeDirty();
            return treeDataLikelihood.getLogLikelihood();
        }

        @Override
        public int getNumArguments() {
            return parameter.getDimension();
        }

        @Override
        public double getLowerBound(int n) {
            return 0;
        }

        @Override
        public double getUpperBound(int n) {
            return Double.POSITIVE_INFINITY;
        }
    };
}
Also used : MultivariateFunction(dr.math.MultivariateFunction) ArbitraryBranchRates(dr.evomodel.branchratemodel.ArbitraryBranchRates)

Example 2 with ArbitraryBranchRates

use of dr.evomodel.branchratemodel.ArbitraryBranchRates in project beast-mcmc by beast-dev.

the class LocationScaleGradientParser method parseTreeDataLikelihood.

private GradientWrtParameterProvider parseTreeDataLikelihood(XMLObject xo, TreeDataLikelihood treeDataLikelihood, String traitName, boolean useHessian) throws XMLParseException {
    BranchRateModel branchRateModel = treeDataLikelihood.getBranchRateModel();
    DataLikelihoodDelegate delegate = treeDataLikelihood.getDataLikelihoodDelegate();
    if (delegate instanceof ContinuousDataLikelihoodDelegate) {
        throw new XMLParseException("Not yet implemented! ");
    } else if (delegate instanceof BeagleDataLikelihoodDelegate) {
        BeagleDataLikelihoodDelegate beagleData = (BeagleDataLikelihoodDelegate) delegate;
        BranchModel branchModel = beagleData.getBranchModel();
        if (branchRateModel instanceof DefaultBranchRateModel || branchRateModel instanceof ArbitraryBranchRates) {
            if (xo.hasChildNamed(LOCATION)) {
                BranchSpecificFixedEffects location = parseLocation(xo);
                return new LocationGradient(traitName, treeDataLikelihood, beagleData, location, useHessian);
            } else if (xo.hasChildNamed(SCALE)) {
                Parameter scale = (Parameter) xo.getElementFirstChild(SCALE);
                return new ScaleGradient(traitName, treeDataLikelihood, beagleData, scale, useHessian);
            } else {
                throw new XMLParseException("Poorly formed");
            }
        } else if (branchModel instanceof ArbitrarySubstitutionParameterBranchModel) {
            BranchParameter branchParameter = (BranchParameter) xo.getChild(BranchParameter.class);
            if (xo.hasChildNamed(LOCATION)) {
                BranchSpecificFixedEffects location = parseLocation(xo);
                return new BranchSubstitutionParameterLocationGradient(traitName, treeDataLikelihood, beagleData, branchParameter, useHessian, location);
            } else if (xo.hasChildNamed(SCALE)) {
                Parameter scale = (Parameter) xo.getElementFirstChild(SCALE);
                return new BranchSubstitutionParameterScaleGradient(traitName, treeDataLikelihood, beagleData, branchParameter, scale, useHessian);
            } else {
                throw new XMLParseException("Not yet implemented.");
            }
        } else {
            throw new XMLParseException("Only implemented for an arbitrary rates model");
        }
    } else {
        throw new XMLParseException("Unknown likelihood delegate type");
    }
}
Also used : BranchParameter(dr.inference.model.BranchParameter) BeagleDataLikelihoodDelegate(dr.evomodel.treedatalikelihood.BeagleDataLikelihoodDelegate) ArbitrarySubstitutionParameterBranchModel(dr.evomodel.branchmodel.ArbitrarySubstitutionParameterBranchModel) BranchModel(dr.evomodel.branchmodel.BranchModel) DefaultBranchRateModel(dr.evomodel.branchratemodel.DefaultBranchRateModel) ArbitraryBranchRates(dr.evomodel.branchratemodel.ArbitraryBranchRates) ArbitrarySubstitutionParameterBranchModel(dr.evomodel.branchmodel.ArbitrarySubstitutionParameterBranchModel) BranchRateModel(dr.evomodel.branchratemodel.BranchRateModel) DefaultBranchRateModel(dr.evomodel.branchratemodel.DefaultBranchRateModel) ContinuousDataLikelihoodDelegate(dr.evomodel.treedatalikelihood.continuous.ContinuousDataLikelihoodDelegate) BeagleDataLikelihoodDelegate(dr.evomodel.treedatalikelihood.BeagleDataLikelihoodDelegate) DataLikelihoodDelegate(dr.evomodel.treedatalikelihood.DataLikelihoodDelegate) Parameter(dr.inference.model.Parameter) BranchParameter(dr.inference.model.BranchParameter) ContinuousDataLikelihoodDelegate(dr.evomodel.treedatalikelihood.continuous.ContinuousDataLikelihoodDelegate) BranchSpecificFixedEffects(dr.evomodel.branchratemodel.BranchSpecificFixedEffects)

Example 3 with ArbitraryBranchRates

use of dr.evomodel.branchratemodel.ArbitraryBranchRates in project beast-mcmc by beast-dev.

the class NodeHeightGradientParser method parseXMLObject.

public Object parseXMLObject(XMLObject xo) throws XMLParseException {
    String traitName = xo.getAttribute(TRAIT_NAME, DEFAULT_TRAIT_NAME);
    final TreeDataLikelihood treeDataLikelihood = (TreeDataLikelihood) xo.getChild(TreeDataLikelihood.class);
    BranchRateModel branchRateModel = treeDataLikelihood.getBranchRateModel();
    if (branchRateModel instanceof DefaultBranchRateModel || branchRateModel instanceof ArbitraryBranchRates) {
        Parameter branchRates = null;
        if (branchRateModel instanceof ArbitraryBranchRates) {
            branchRates = ((ArbitraryBranchRates) branchRateModel).getRateParameter();
        }
        DataLikelihoodDelegate delegate = treeDataLikelihood.getDataLikelihoodDelegate();
        if (delegate instanceof ContinuousDataLikelihoodDelegate) {
            throw new XMLParseException("Not yet implemented! ");
        } else if (delegate instanceof BeagleDataLikelihoodDelegate) {
            BeagleDataLikelihoodDelegate beagleData = (BeagleDataLikelihoodDelegate) delegate;
            return new NodeHeightGradientForDiscreteTrait(traitName, treeDataLikelihood, beagleData, branchRates);
        } else {
            throw new XMLParseException("Unknown likelihood delegate type");
        }
    } else {
        throw new XMLParseException("Only implemented for an arbitrary rates model");
    }
}
Also used : ArbitraryBranchRates(dr.evomodel.branchratemodel.ArbitraryBranchRates) TreeDataLikelihood(dr.evomodel.treedatalikelihood.TreeDataLikelihood) BranchRateModel(dr.evomodel.branchratemodel.BranchRateModel) DefaultBranchRateModel(dr.evomodel.branchratemodel.DefaultBranchRateModel) ContinuousDataLikelihoodDelegate(dr.evomodel.treedatalikelihood.continuous.ContinuousDataLikelihoodDelegate) BeagleDataLikelihoodDelegate(dr.evomodel.treedatalikelihood.BeagleDataLikelihoodDelegate) DataLikelihoodDelegate(dr.evomodel.treedatalikelihood.DataLikelihoodDelegate) Parameter(dr.inference.model.Parameter) BeagleDataLikelihoodDelegate(dr.evomodel.treedatalikelihood.BeagleDataLikelihoodDelegate) NodeHeightGradientForDiscreteTrait(dr.evomodel.treedatalikelihood.discrete.NodeHeightGradientForDiscreteTrait) ContinuousDataLikelihoodDelegate(dr.evomodel.treedatalikelihood.continuous.ContinuousDataLikelihoodDelegate) DefaultBranchRateModel(dr.evomodel.branchratemodel.DefaultBranchRateModel)

Example 4 with ArbitraryBranchRates

use of dr.evomodel.branchratemodel.ArbitraryBranchRates in project beast-mcmc by beast-dev.

the class ContinuousDataLikelihoodDelegateTest method testLikelihoodFullOUNonSymmetricRelaxed.

public void testLikelihoodFullOUNonSymmetricRelaxed() {
    System.out.println("\nTest Likelihood using Full Non symmetric OU Relaxed:");
    // Diffusion
    List<BranchRateModel> optimalTraitsModels = new ArrayList<BranchRateModel>();
    ArbitraryBranchRates.BranchRateTransform transform = make(false, false, false);
    optimalTraitsModels.add(new ArbitraryBranchRates(treeModel, new Parameter.Default("rate.1", new double[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }), transform, false));
    optimalTraitsModels.add(new ArbitraryBranchRates(treeModel, new Parameter.Default("rate.2", new double[] { 0, -1, 2, -3, 4, -5, 6, -7, 8, -9 }), transform, false));
    optimalTraitsModels.add(new StrictClockBranchRates(new Parameter.Default("rate.3", new double[] { -2.0 })));
    Parameter[] strengthOfSelectionParameters = new Parameter[3];
    strengthOfSelectionParameters[0] = new Parameter.Default(new double[] { 0.5, 0.0, 0.0 });
    strengthOfSelectionParameters[1] = new Parameter.Default(new double[] { 0.2, 100.0, 0.1 });
    strengthOfSelectionParameters[2] = new Parameter.Default(new double[] { 10.0, 0.1, 50.5 });
    MatrixParameter strengthOfSelectionMatrixParam = new MatrixParameter("strengthOfSelectionMatrix", strengthOfSelectionParameters);
    DiffusionProcessDelegate diffusionProcessDelegate = new OUDiffusionModelDelegate(treeModel, diffusionModel, optimalTraitsModels, new MultivariateElasticModel(strengthOfSelectionMatrixParam));
    // CDL
    ContinuousDataLikelihoodDelegate likelihoodDelegate = new ContinuousDataLikelihoodDelegate(treeModel, diffusionProcessDelegate, dataModel, rootPrior, rateTransformation, rateModel, false);
    // Likelihood Computation
    TreeDataLikelihood dataLikelihood = new TreeDataLikelihood(likelihoodDelegate, treeModel, rateModel);
    testLikelihood("likelihoodFullNonSymmetricOURelaxed", dataLikelihood);
    // Conditional moments (preorder)
    testConditionalMoments(dataLikelihood, likelihoodDelegate);
    // Fixed Root
    ContinuousDataLikelihoodDelegate likelihoodDelegateInf = new ContinuousDataLikelihoodDelegate(treeModel, diffusionProcessDelegate, dataModel, rootPriorInf, rateTransformation, rateModel, true);
    TreeDataLikelihood dataLikelihoodInf = new TreeDataLikelihood(likelihoodDelegateInf, treeModel, rateModel);
    testLikelihood("likelihoodFullNonSymmetricOURelaxedInf", dataLikelihoodInf);
    testConditionalMoments(dataLikelihoodInf, likelihoodDelegateInf);
}
Also used : MatrixParameter(dr.inference.model.MatrixParameter) ArrayList(java.util.ArrayList) MultivariateElasticModel(dr.evomodel.continuous.MultivariateElasticModel) StrictClockBranchRates(dr.evomodel.branchratemodel.StrictClockBranchRates) ArbitraryBranchRates(dr.evomodel.branchratemodel.ArbitraryBranchRates) TreeDataLikelihood(dr.evomodel.treedatalikelihood.TreeDataLikelihood) BranchRateModel(dr.evomodel.branchratemodel.BranchRateModel) DefaultBranchRateModel(dr.evomodel.branchratemodel.DefaultBranchRateModel) MatrixParameter(dr.inference.model.MatrixParameter) Parameter(dr.inference.model.Parameter)

Example 5 with ArbitraryBranchRates

use of dr.evomodel.branchratemodel.ArbitraryBranchRates in project beast-mcmc by beast-dev.

the class ContinuousDataLikelihoodDelegateTest method testLikelihoodFullOURelaxed.

public void testLikelihoodFullOURelaxed() {
    System.out.println("\nTest Likelihood using Full OU Relaxed:");
    // Diffusion
    List<BranchRateModel> optimalTraitsModels = new ArrayList<BranchRateModel>();
    ArbitraryBranchRates.BranchRateTransform transform = make(false, false, false);
    optimalTraitsModels.add(new ArbitraryBranchRates(treeModel, new Parameter.Default("rate.1", new double[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }), transform, false));
    optimalTraitsModels.add(new ArbitraryBranchRates(treeModel, new Parameter.Default("rate.2", new double[] { 0, -1, 2, -3, 4, -5, 6, -7, 8, -9 }), transform, false));
    optimalTraitsModels.add(new StrictClockBranchRates(new Parameter.Default("rate.3", new double[] { -2.0 })));
    Parameter[] strengthOfSelectionParameters = new Parameter[3];
    strengthOfSelectionParameters[0] = new Parameter.Default(new double[] { 0.5, 0.2, 0.0 });
    strengthOfSelectionParameters[1] = new Parameter.Default(new double[] { 0.2, 10.5, 0.1 });
    strengthOfSelectionParameters[2] = new Parameter.Default(new double[] { 0.0, 0.1, 100.0 });
    MatrixParameter strengthOfSelectionMatrixParam = new MatrixParameter("strengthOfSelectionMatrix", strengthOfSelectionParameters);
    DiffusionProcessDelegate diffusionProcessDelegate = new OUDiffusionModelDelegate(treeModel, diffusionModel, optimalTraitsModels, new MultivariateElasticModel(strengthOfSelectionMatrixParam));
    // CDL
    ContinuousDataLikelihoodDelegate likelihoodDelegate = new ContinuousDataLikelihoodDelegate(treeModel, diffusionProcessDelegate, dataModel, rootPrior, rateTransformation, rateModel, false);
    // Likelihood Computation
    TreeDataLikelihood dataLikelihood = new TreeDataLikelihood(likelihoodDelegate, treeModel, rateModel);
    testLikelihood("likelihoodFullOURelaxed", dataLikelihood);
    // Conditional moments (preorder)
    testConditionalMoments(dataLikelihood, likelihoodDelegate);
    // Conditional simulations
    MathUtils.setSeed(17890826);
    double[] expectedTraits = new double[] { -1.0, 2.0, 0.0, 1.6349449153945943, 2.8676718538313635, -1.0653412418514505, 0.5, 3.3661883786009166, 5.5, 2.0, 5.0, -8.0, 11.0, 1.0, -1.5, 1.0, 2.5, 4.0 };
    testConditionalSimulations(dataLikelihood, likelihoodDelegate, diffusionModel, dataModel, rootPrior, expectedTraits);
    // Fixed Root
    ContinuousDataLikelihoodDelegate likelihoodDelegateInf = new ContinuousDataLikelihoodDelegate(treeModel, diffusionProcessDelegate, dataModel, rootPriorInf, rateTransformation, rateModel, true);
    TreeDataLikelihood dataLikelihoodInf = new TreeDataLikelihood(likelihoodDelegateInf, treeModel, rateModel);
    testLikelihood("likelihoodFullOURelaxedInf", dataLikelihoodInf);
    testConditionalMoments(dataLikelihoodInf, likelihoodDelegateInf);
}
Also used : MatrixParameter(dr.inference.model.MatrixParameter) ArrayList(java.util.ArrayList) MultivariateElasticModel(dr.evomodel.continuous.MultivariateElasticModel) StrictClockBranchRates(dr.evomodel.branchratemodel.StrictClockBranchRates) ArbitraryBranchRates(dr.evomodel.branchratemodel.ArbitraryBranchRates) TreeDataLikelihood(dr.evomodel.treedatalikelihood.TreeDataLikelihood) BranchRateModel(dr.evomodel.branchratemodel.BranchRateModel) DefaultBranchRateModel(dr.evomodel.branchratemodel.DefaultBranchRateModel) MatrixParameter(dr.inference.model.MatrixParameter) Parameter(dr.inference.model.Parameter)

Aggregations

ArbitraryBranchRates (dr.evomodel.branchratemodel.ArbitraryBranchRates)17 BranchRateModel (dr.evomodel.branchratemodel.BranchRateModel)13 Parameter (dr.inference.model.Parameter)13 DefaultBranchRateModel (dr.evomodel.branchratemodel.DefaultBranchRateModel)12 TreeDataLikelihood (dr.evomodel.treedatalikelihood.TreeDataLikelihood)11 StrictClockBranchRates (dr.evomodel.branchratemodel.StrictClockBranchRates)10 ArrayList (java.util.ArrayList)10 MultivariateElasticModel (dr.evomodel.continuous.MultivariateElasticModel)7 MatrixParameter (dr.inference.model.MatrixParameter)7 DiagonalMatrix (dr.inference.model.DiagonalMatrix)4 BeagleDataLikelihoodDelegate (dr.evomodel.treedatalikelihood.BeagleDataLikelihoodDelegate)3 DataLikelihoodDelegate (dr.evomodel.treedatalikelihood.DataLikelihoodDelegate)3 ContinuousDataLikelihoodDelegate (dr.evomodel.treedatalikelihood.continuous.ContinuousDataLikelihoodDelegate)3 TreeModel (dr.evomodel.tree.TreeModel)2 ArbitrarySubstitutionParameterBranchModel (dr.evomodel.branchmodel.ArbitrarySubstitutionParameterBranchModel)1 BranchModel (dr.evomodel.branchmodel.BranchModel)1 AutoCorrelatedBranchRatesDistribution (dr.evomodel.branchratemodel.AutoCorrelatedBranchRatesDistribution)1 BranchSpecificFixedEffects (dr.evomodel.branchratemodel.BranchSpecificFixedEffects)1 LocalBranchRates (dr.evomodel.branchratemodel.LocalBranchRates)1 RelaxedDriftModel (dr.evomodel.branchratemodel.RelaxedDriftModel)1