use of dr.evomodel.branchratemodel.ArbitraryBranchRates in project beast-mcmc by beast-dev.
the class ContinuousDataLikelihoodDelegateTest method testLikelihoodDiagonalOURelaxedFactor.
public void testLikelihoodDiagonalOURelaxedFactor() {
System.out.println("\nTest Likelihood using diagonal Relaxed OU and factor:");
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 StrictClockBranchRates(new Parameter.Default("rate.2", new double[] { -1.5 })));
DiagonalMatrix strengthOfSelectionMatrixParam = new DiagonalMatrix(new Parameter.Default(new double[] { 1.5, 20.0 }));
DiffusionProcessDelegate diffusionProcessDelegate = new OUDiffusionModelDelegate(treeModel, diffusionModelFactor, optimalTraitsModels, new MultivariateElasticModel(strengthOfSelectionMatrixParam));
// CDL
ContinuousDataLikelihoodDelegate likelihoodDelegateFactors = new ContinuousDataLikelihoodDelegate(treeModel, diffusionProcessDelegate, dataModelFactor, rootPriorFactor, rateTransformation, rateModel, false);
dataModelFactor.setLikelihoodDelegate(likelihoodDelegateFactors);
// Likelihood Computation
TreeDataLikelihood dataLikelihoodFactors = new TreeDataLikelihood(likelihoodDelegateFactors, treeModel, rateModel);
testLikelihood("likelihoodDiagonalOURelaxedFactor", dataModelFactor, dataLikelihoodFactors);
// Conditional simulations
MathUtils.setSeed(17890826);
double[] expectedTraits = new double[] { 1.2546097113922914, -1.1761389606670978, 1.305611773283861, -1.0644815941127401, 1.4571577864569687, -1.1477885449972944, 1.749551506462585, -0.9890375857170963, 1.0763987351136657, -1.0671848958534547, 1.5276137550128892, -0.9822950795368887 };
testConditionalSimulations(dataLikelihoodFactors, likelihoodDelegateFactors, diffusionModelFactor, dataModelFactor, rootPriorFactor, expectedTraits);
}
use of dr.evomodel.branchratemodel.ArbitraryBranchRates in project beast-mcmc by beast-dev.
the class ContinuousDataLikelihoodDelegateTest method testLikelihoodFullDiagonalOUFactor.
public void testLikelihoodFullDiagonalOUFactor() {
System.out.println("\nTest Likelihood comparing full and diagonal OU and factor:");
// 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 StrictClockBranchRates(new Parameter.Default("rate.2", new double[] { 1.5 })));
Parameter[] strengthOfSelectionParameters = new Parameter[2];
strengthOfSelectionParameters[0] = new Parameter.Default(new double[] { 0.5, 0.0 });
strengthOfSelectionParameters[1] = new Parameter.Default(new double[] { 0.0, 1.5 });
MatrixParameter strengthOfSelectionMatrixParam = new MatrixParameter("strengthOfSelectionMatrix", strengthOfSelectionParameters);
DiagonalMatrix strengthOfSelectionMatrixParamDiagonal = new DiagonalMatrix(new Parameter.Default(new double[] { 0.5, 1.5 }));
DiffusionProcessDelegate diffusionProcessDelegate = new OUDiffusionModelDelegate(treeModel, diffusionModelFactor, optimalTraitsModels, new MultivariateElasticModel(strengthOfSelectionMatrixParam));
DiffusionProcessDelegate diffusionProcessDelegateDiagonal = new OUDiffusionModelDelegate(treeModel, diffusionModelFactor, optimalTraitsModels, new MultivariateElasticModel(strengthOfSelectionMatrixParamDiagonal));
// CDL
ContinuousDataLikelihoodDelegate likelihoodDelegateFactors = new ContinuousDataLikelihoodDelegate(treeModel, diffusionProcessDelegate, dataModelFactor, rootPriorFactor, rateTransformation, rateModel, false);
ContinuousDataLikelihoodDelegate likelihoodDelegateFactorsDiagonal = new ContinuousDataLikelihoodDelegate(treeModel, diffusionProcessDelegateDiagonal, dataModelFactor, rootPriorFactor, rateTransformation, rateModel, false);
dataModelFactor.setLikelihoodDelegate(likelihoodDelegateFactors);
// Likelihood Computation
TreeDataLikelihood dataLikelihoodFactors = new TreeDataLikelihood(likelihoodDelegateFactors, treeModel, rateModel);
TreeDataLikelihood dataLikelihoodFactorsDiagonal = new TreeDataLikelihood(likelihoodDelegateFactorsDiagonal, treeModel, rateModel);
double likelihoodFactorData = dataLikelihoodFactors.getLogLikelihood();
double likelihoodFactorDiffusion = dataModelFactor.getLogLikelihood();
double likelihoodFactorDataDiagonal = dataLikelihoodFactorsDiagonal.getLogLikelihood();
double likelihoodFactorDiffusionDiagonal = dataModelFactor.getLogLikelihood();
assertEquals("likelihoodFullDiagonalOUFactor", format.format(likelihoodFactorData + likelihoodFactorDiffusion), format.format(likelihoodFactorDataDiagonal + likelihoodFactorDiffusionDiagonal));
}
use of dr.evomodel.branchratemodel.ArbitraryBranchRates in project beast-mcmc by beast-dev.
the class ContinuousDataLikelihoodDelegateTest method testLikelihoodFullAndDiagonalOU.
public void testLikelihoodFullAndDiagonalOU() {
System.out.println("\nTest Likelihood comparing Full and Diagonal OU:");
// 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.0, 10.5, 0.0 });
strengthOfSelectionParameters[2] = new Parameter.Default(new double[] { 0.0, 0.0, 100.0 });
MatrixParameter strengthOfSelectionMatrixParam = new MatrixParameter("strengthOfSelectionMatrix", strengthOfSelectionParameters);
DiagonalMatrix strengthOfSelectionMatrixParamDiagonal = new DiagonalMatrix(new Parameter.Default(new double[] { 0.5, 10.5, 100.0 }));
DiffusionProcessDelegate diffusionProcessDelegate = new OUDiffusionModelDelegate(treeModel, diffusionModel, optimalTraitsModels, new MultivariateElasticModel(strengthOfSelectionMatrixParam));
DiffusionProcessDelegate diffusionProcessDelegateDiagonal = new OUDiffusionModelDelegate(treeModel, diffusionModel, optimalTraitsModels, new MultivariateElasticModel(strengthOfSelectionMatrixParamDiagonal));
// CDL
ContinuousDataLikelihoodDelegate likelihoodDelegate = new ContinuousDataLikelihoodDelegate(treeModel, diffusionProcessDelegate, dataModel, rootPrior, rateTransformation, rateModel, false);
ContinuousDataLikelihoodDelegate likelihoodDelegateDiagonal = new ContinuousDataLikelihoodDelegate(treeModel, diffusionProcessDelegateDiagonal, dataModel, rootPrior, rateTransformation, rateModel, false);
// Likelihood Computation
TreeDataLikelihood dataLikelihood = new TreeDataLikelihood(likelihoodDelegate, treeModel, rateModel);
TreeDataLikelihood dataLikelihoodDiagonal = new TreeDataLikelihood(likelihoodDelegateDiagonal, treeModel, rateModel);
assertEquals("likelihoodFullDiagonalOU", format.format(dataLikelihood.getLogLikelihood()), format.format(dataLikelihoodDiagonal.getLogLikelihood()));
}
use of dr.evomodel.branchratemodel.ArbitraryBranchRates in project beast-mcmc by beast-dev.
the class BranchRateGradientParser method parseTreeDataLikelihood.
private GradientWrtParameterProvider parseTreeDataLikelihood(TreeDataLikelihood treeDataLikelihood, String traitName, boolean useHessian) throws XMLParseException {
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) {
ContinuousDataLikelihoodDelegate continuousData = (ContinuousDataLikelihoodDelegate) delegate;
return new BranchRateGradient(traitName, treeDataLikelihood, continuousData, branchRates);
} else if (delegate instanceof BeagleDataLikelihoodDelegate) {
BeagleDataLikelihoodDelegate beagleData = (BeagleDataLikelihoodDelegate) delegate;
if (branchRateModel instanceof LocalBranchRates) {
return new LocalBranchRateGradientForDiscreteTrait(traitName, treeDataLikelihood, beagleData, branchRates, useHessian);
} else {
return new BranchRateGradientForDiscreteTrait(traitName, treeDataLikelihood, beagleData, branchRates, useHessian);
}
} 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 RelaxedDriftModelParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
TreeModel tree = (TreeModel) xo.getChild(TreeModel.class);
Parameter ratesParameter = (Parameter) xo.getElementFirstChild(RATES);
Parameter rateIndicatorParameter = (Parameter) xo.getElementFirstChild(RATE_IND);
Parameter driftRates = null;
if (xo.hasChildNamed(DRIFT_RATES)) {
driftRates = (Parameter) xo.getElementFirstChild(DRIFT_RATES);
}
ArbitraryBranchRates branchChanges = null;
if (xo.hasChildNamed(BRANCH_CHANGES)) {
branchChanges = (ArbitraryBranchRates) xo.getElementFirstChild(BRANCH_CHANGES);
}
Logger.getLogger("dr.evomodel").info("Using relaxed drift model.");
return new RelaxedDriftModel(tree, rateIndicatorParameter, ratesParameter, driftRates, branchChanges);
}
Aggregations