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;
}
};
}
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");
}
}
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");
}
}
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);
}
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);
}
Aggregations