use of dr.evomodel.branchratemodel.StrictClockBranchRates in project beast-mcmc by beast-dev.
the class ContinuousDataLikelihoodDelegateTest method testLikelihoodDriftRelaxed.
public void testLikelihoodDriftRelaxed() {
System.out.println("\nTest Likelihood using Drifted relaxed BM:");
// Diffusion
List<BranchRateModel> driftModels = new ArrayList<BranchRateModel>();
ArbitraryBranchRates.BranchRateTransform transform = make(false, false, false);
driftModels.add(new ArbitraryBranchRates(treeModel, new Parameter.Default("rate.1", new double[] { 0, 100, 200, 300, 400, 500, 600, 700, 800, 900 }), transform, false));
driftModels.add(new ArbitraryBranchRates(treeModel, new Parameter.Default("rate.2", new double[] { 0, -100, 200, -300, 400, -500, 600, -700, 800, -900 }), transform, false));
driftModels.add(new StrictClockBranchRates(new Parameter.Default("rate.3", new double[] { -2.0 })));
DiffusionProcessDelegate diffusionProcessDelegate = new DriftDiffusionModelDelegate(treeModel, diffusionModel, driftModels);
// CDL
ContinuousDataLikelihoodDelegate likelihoodDelegate = new ContinuousDataLikelihoodDelegate(treeModel, diffusionProcessDelegate, dataModel, rootPrior, rateTransformation, rateModel, false);
// Likelihood Computation
TreeDataLikelihood dataLikelihood = new TreeDataLikelihood(likelihoodDelegate, treeModel, rateModel);
testLikelihood("likelihoodDriftRelaxed", dataLikelihood);
// Conditional moments (preorder)
testConditionalMoments(dataLikelihood, likelihoodDelegate);
// Conditional simulations
MathUtils.setSeed(17890826);
double[] expectedTraits = new double[] { -1.0, 2.0, 0.0, 2.843948876154644, 10.866053719140933, 3.467579698926694, 0.5, 12.000214659757933, 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("likelihoodDriftRelaxedInf", dataLikelihoodInf);
testConditionalMoments(dataLikelihoodInf, likelihoodDelegateInf);
}
use of dr.evomodel.branchratemodel.StrictClockBranchRates in project beast-mcmc by beast-dev.
the class RepeatedMeasureFactorTest method testLikelihoodOU.
public void testLikelihoodOU() {
System.out.println("\nTest Likelihood using full OU:");
// Diffusion
List<BranchRateModel> optimalTraitsModels = new ArrayList<BranchRateModel>();
optimalTraitsModels.add(new StrictClockBranchRates(new Parameter.Default("rate.1", new double[] { 1.0 })));
optimalTraitsModels.add(new StrictClockBranchRates(new Parameter.Default("rate.2", new double[] { 2.0 })));
optimalTraitsModels.add(new StrictClockBranchRates(new Parameter.Default("rate.3", new double[] { -2.0 })));
optimalTraitsModels.add(new StrictClockBranchRates(new Parameter.Default("rate.4", new double[] { 10.0 })));
optimalTraitsModels.add(new StrictClockBranchRates(new Parameter.Default("rate.5", new double[] { 20.0 })));
optimalTraitsModels.add(new StrictClockBranchRates(new Parameter.Default("rate.6", new double[] { -20.0 })));
Parameter[] strengthOfSelectionParameters = new Parameter[6];
strengthOfSelectionParameters[0] = new Parameter.Default(new double[] { 1.0, 0.1, 0.0, 0.0, 0.5, 2.0 });
strengthOfSelectionParameters[1] = new Parameter.Default(new double[] { 0.1, 10., 0.0, 0.0, 0.0, 0.0 });
strengthOfSelectionParameters[2] = new Parameter.Default(new double[] { 0.0, 0.0, 20., 0.3, 0.0, 0.0 });
strengthOfSelectionParameters[3] = new Parameter.Default(new double[] { 0.0, 0.0, 0.3, 30., 3.0, 0.0 });
strengthOfSelectionParameters[4] = new Parameter.Default(new double[] { 1.0, 0.0, 0.0, 3.0, 40., 0.0 });
strengthOfSelectionParameters[5] = new Parameter.Default(new double[] { 0.0, 0.0, 0.5, 0.0, 0.0, 50. });
MatrixParameter strengthOfSelectionMatrixParam = new MatrixParameter("strengthOfSelectionMatrix", strengthOfSelectionParameters);
DiffusionProcessDelegate diffusionProcessDelegate = new OUDiffusionModelDelegate(treeModel, diffusionModel, optimalTraitsModels, new MultivariateElasticModel(strengthOfSelectionMatrixParam));
// Rates
ContinuousRateTransformation rateTransformation = new ContinuousRateTransformation.Default(treeModel, false, false);
BranchRateModel rateModel = new DefaultBranchRateModel();
// // Factor Model //// *****************************************************************************************
// CDL
ContinuousDataLikelihoodDelegate likelihoodDelegateFactors = new ContinuousDataLikelihoodDelegate(treeModel, diffusionProcessDelegate, dataModelFactor, rootPrior, rateTransformation, rateModel, true);
dataModelFactor.setLikelihoodDelegate(likelihoodDelegateFactors);
// Likelihood Computation
TreeDataLikelihood dataLikelihoodFactors = new TreeDataLikelihood(likelihoodDelegateFactors, treeModel, rateModel);
double logDatumLikelihoodFactor = getLogDatumLikelihood(dataModelFactor);
double likelihoodFactorData = dataLikelihoodFactors.getLogLikelihood();
double likelihoodFactorDiffusion = dataModelFactor.getLogLikelihood();
assertEquals("likelihoodOUFactor", format.format(logDatumLikelihoodFactor), format.format(likelihoodFactorData + likelihoodFactorDiffusion));
System.out.println("likelihoodOUFactor: " + format.format(logDatumLikelihoodFactor));
// Simulation
MathUtils.setSeed(17890826);
double[] traitsFactors = getConditionalSimulations(dataLikelihoodFactors, likelihoodDelegateFactors, diffusionModel, dataModelFactor, rootPrior, treeModel, rateTransformation);
System.err.println(new Vector(traitsFactors));
// // Repeated Measures //// ************************************************************************************
// CDL
ContinuousDataLikelihoodDelegate likelihoodDelegateRepMea = new ContinuousDataLikelihoodDelegate(treeModel, diffusionProcessDelegate, dataModelRepeatedMeasures, rootPrior, rateTransformation, rateModel, true);
// Likelihood Computation
TreeDataLikelihood dataLikelihoodRepMea = new TreeDataLikelihood(likelihoodDelegateRepMea, treeModel, rateModel);
double logDatumLikelihoodRepMea = getLogDatumLikelihood(dataLikelihoodRepMea);
double likelihoodRepMeaDiffusion = dataLikelihoodRepMea.getLogLikelihood();
assertEquals("likelihoodOURepMea", format.format(logDatumLikelihoodRepMea), format.format(likelihoodRepMeaDiffusion));
System.out.println("likelihoodOURepMea: " + format.format(logDatumLikelihoodRepMea));
// Simulation
MathUtils.setSeed(17890826);
double[] traitsRepMea = getConditionalSimulations(dataLikelihoodRepMea, likelihoodDelegateRepMea, diffusionModel, dataModelRepeatedMeasures, rootPrior, treeModel, rateTransformation);
System.err.println(new Vector(traitsRepMea));
// // Equal ? //// **********************************************************************************************
assertEquals("likelihoodOURepFactor", format.format(likelihoodFactorData + likelihoodFactorDiffusion), format.format(likelihoodRepMeaDiffusion));
for (int i = 0; i < traitsFactors.length; i++) {
assertEquals(format.format(traitsRepMea[i]), format.format(traitsFactors[i]));
}
// // Repeated Measures Full //// *******************************************************************************
// CDL
ContinuousDataLikelihoodDelegate likelihoodDelegateRepMeaFull = new ContinuousDataLikelihoodDelegate(treeModel, diffusionProcessDelegate, dataModelRepeatedMeasuresFull, rootPrior, rateTransformation, rateModel, true);
// Likelihood Computation
TreeDataLikelihood dataLikelihoodRepMeaFull = new TreeDataLikelihood(likelihoodDelegateRepMeaFull, treeModel, rateModel);
double logDatumLikelihoodRepMeaFull = getLogDatumLikelihood(dataLikelihoodRepMeaFull);
double likelihoodRepMeaDiffusionFull = dataLikelihoodRepMeaFull.getLogLikelihood();
assertEquals("likelihoodBMRepMea", format.format(logDatumLikelihoodRepMeaFull), format.format(likelihoodRepMeaDiffusionFull));
System.out.println("likelihoodBMRepMeaFull: " + format.format(logDatumLikelihoodRepMeaFull));
}
use of dr.evomodel.branchratemodel.StrictClockBranchRates in project beast-mcmc by beast-dev.
the class DiffusionGradientTest method testGradientSingleDriftWithMissing.
public void testGradientSingleDriftWithMissing() {
// Diffusion
List<BranchRateModel> driftModels = new ArrayList<BranchRateModel>();
CompoundParameter driftParam = new CompoundParameter("drift");
for (int i = 0; i < dimTrait; i++) {
Parameter rate = new Parameter.Default("rate." + (i + 1), new double[] { 2.2 * i + 3.1 });
driftParam.addParameter(rate);
driftModels.add(new StrictClockBranchRates(rate));
}
// Wrt Precision
DiffusionProcessDelegate diffusionProcessDelegate = new DriftDiffusionModelDelegate(treeModel, diffusionModel, driftModels);
System.out.println("\nTest single drift gradient drift.");
testGradient(diffusionModel, diffusionProcessDelegate, dataModel, precisionMatrix, driftParam);
System.out.println("\nTest single drift gradient precision with missing.");
testGradient(diffusionModel, diffusionProcessDelegate, dataModelMissing, precisionMatrix, driftParam);
// Wrt Variance
DiffusionProcessDelegate diffusionProcessDelegateVariance = new DriftDiffusionModelDelegate(treeModel, diffusionModelVar, driftModels);
System.out.println("\nTest single drift gradient variance.");
testGradient(diffusionModelVar, diffusionProcessDelegateVariance, dataModel, precisionMatrixInv, driftParam);
System.out.println("\nTest single drift gradient variance with missing.");
testGradient(diffusionModelVar, diffusionProcessDelegateVariance, dataModelMissing, precisionMatrixInv, driftParam);
// Repeated Measures Model
System.out.println("\nTest gradient precision repeated measures.");
testGradient(diffusionModel, diffusionProcessDelegate, dataModelRepeatedMeasures, rootPrior, meanRoot, precisionMatrix, false, null, driftParam, samplingPrecision);
testGradient(diffusionModel, diffusionProcessDelegate, dataModelRepeatedMeasuresInv, rootPrior, meanRoot, precisionMatrix, false, null, driftParam, samplingPrecisionInv);
}
use of dr.evomodel.branchratemodel.StrictClockBranchRates in project beast-mcmc by beast-dev.
the class DiffusionGradientTest method testGradientDriftWithMissing.
public void testGradientDriftWithMissing() {
// Diffusion
List<BranchRateModel> driftModels = new ArrayList<BranchRateModel>();
driftModels.add(new StrictClockBranchRates(new Parameter.Default("rate.1", new double[] { 0.0 })));
driftModels.add(new StrictClockBranchRates(new Parameter.Default("rate.2", new double[] { 200.0 })));
driftModels.add(new StrictClockBranchRates(new Parameter.Default("rate.3", new double[] { -200.0 })));
driftModels.add(new StrictClockBranchRates(new Parameter.Default("rate.4", new double[] { 1.0 })));
driftModels.add(new StrictClockBranchRates(new Parameter.Default("rate.5", new double[] { 200.0 })));
driftModels.add(new StrictClockBranchRates(new Parameter.Default("rate.6", new double[] { -200.0 })));
// Wrt Precision
DiffusionProcessDelegate diffusionProcessDelegate = new DriftDiffusionModelDelegate(treeModel, diffusionModel, driftModels);
System.out.println("\nTest drift gradient precision.");
testGradient(diffusionModel, diffusionProcessDelegate, dataModel, precisionMatrix);
System.out.println("\nTest drift gradient precision with missing.");
testGradient(diffusionModel, diffusionProcessDelegate, dataModelMissing, precisionMatrix);
// Wrt Variance
DiffusionProcessDelegate diffusionProcessDelegateVariance = new DriftDiffusionModelDelegate(treeModel, diffusionModelVar, driftModels);
System.out.println("\nTest drift gradient variance.");
testGradient(diffusionModelVar, diffusionProcessDelegateVariance, dataModel, precisionMatrixInv);
System.out.println("\nTest drift gradient variance with missing.");
testGradient(diffusionModelVar, diffusionProcessDelegateVariance, dataModelMissing, precisionMatrixInv);
// Factor Model
// Diffusion
List<BranchRateModel> driftModelsFactor = new ArrayList<BranchRateModel>();
driftModelsFactor.add(new StrictClockBranchRates(new Parameter.Default("rate.1", new double[] { 0.0 })));
driftModelsFactor.add(new StrictClockBranchRates(new Parameter.Default("rate.2", new double[] { 200.0 })));
DiffusionProcessDelegate diffusionProcessDelegateFactor = new DriftDiffusionModelDelegate(treeModel, diffusionModelFactor, driftModelsFactor);
System.out.println("\nTest gradient precision.");
testGradient(diffusionModelFactor, diffusionProcessDelegateFactor, dataModelFactor, rootPriorFactor, rootMeanFactor, precisionMatrixFactor, false);
// Repeated Measures Model
System.out.println("\nTest gradient precision repeated measures.");
testGradient(diffusionModel, diffusionProcessDelegate, dataModelRepeatedMeasures, rootPrior, meanRoot, precisionMatrix, false, null, null, samplingPrecision);
testGradient(diffusionModel, diffusionProcessDelegate, dataModelRepeatedMeasuresInv, rootPrior, meanRoot, precisionMatrix, false, null, null, samplingPrecisionInv);
}
use of dr.evomodel.branchratemodel.StrictClockBranchRates in project beast-mcmc by beast-dev.
the class BranchSpecificGradientTest method testRateGradient.
public void testRateGradient() {
System.out.println("\nTest Likelihood using vanilla BM:");
// Diffusion
List<BranchRateModel> driftModels = new ArrayList<BranchRateModel>();
driftModels.add(new StrictClockBranchRates(new Parameter.Default("rate.1", new double[] { 1.0 })));
driftModels.add(new StrictClockBranchRates(new Parameter.Default("rate.2", new double[] { 2.0 })));
driftModels.add(new StrictClockBranchRates(new Parameter.Default("rate.3", new double[] { -2.0 })));
DiffusionProcessDelegate diffusionProcessDelegate = new DriftDiffusionModelDelegate(treeModel, diffusionModel, driftModels);
// Rates
ArbitraryBranchRates.BranchRateTransform transform = make(false, true, false, null, null);
Parameter branchRates = new Parameter.Default(new double[] { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 });
ArbitraryBranchRates rateModel = new ArbitraryBranchRates(treeModel, branchRates, transform, false);
ContinuousRateTransformation rateTransformation = new ContinuousRateTransformation.Default(treeModel, false, false);
// CDL
ContinuousDataLikelihoodDelegate likelihoodDelegate = new ContinuousDataLikelihoodDelegate(treeModel, diffusionProcessDelegate, dataModel, rootPrior, rateTransformation, rateModel, false);
// Likelihood Computation
TreeDataLikelihood dataLikelihood = new TreeDataLikelihood(likelihoodDelegate, treeModel, rateModel);
// Gradient (Rates)
BranchRateGradient branchGradient1 = new BranchRateGradient("trait", dataLikelihood, likelihoodDelegate, branchRates);
double[] gradient1 = branchGradient1.getGradientLogDensity();
// Gradient (Specific)
ContinuousTraitGradientForBranch.RateGradient traitGradient = new ContinuousTraitGradientForBranch.RateGradient(dimTrait, treeModel, rateModel);
BranchSpecificGradient branchGradient2 = new BranchSpecificGradient("trait", dataLikelihood, likelihoodDelegate, traitGradient, branchRates);
double[] gradient2 = branchGradient2.getGradientLogDensity();
double[] numericalGradient = branchGradient1.getNumericalGradient();
System.err.println("\tGradient with rate method = " + new dr.math.matrixAlgebra.Vector(gradient1));
System.err.println("\tGradient with general method = " + new dr.math.matrixAlgebra.Vector(gradient2));
System.err.println("\tNumerical gradient = " + new dr.math.matrixAlgebra.Vector(numericalGradient));
assertEquals("length", gradient1.length, gradient2.length);
for (int i = 0; i < gradient1.length; i++) {
assertEquals("numeric " + i, gradient1[i], numericalGradient[i], 1E-4);
}
for (int i = 0; i < gradient1.length; i++) {
assertEquals("gradient " + i, format.format(gradient1[i]), format.format(gradient2[i]));
}
}
Aggregations