use of dr.evomodel.continuous.MultivariateElasticModel 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.continuous.MultivariateElasticModel in project beast-mcmc by beast-dev.
the class DiffusionGradientTest method testGradientDiagonalOUWithMissing.
public void testGradientDiagonalOUWithMissing() {
// 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[] { 1.0 })));
optimalTraitsModels.add(new StrictClockBranchRates(new Parameter.Default("rate.5", new double[] { 2.0 })));
optimalTraitsModels.add(new StrictClockBranchRates(new Parameter.Default("rate.6", new double[] { -2.0 })));
DiagonalMatrix strengthOfSelectionMatrixParam = new DiagonalMatrix(new Parameter.Default(new double[] { 0.0, 0.000005, 1.0, 5.0, 10.0, 50.0 }));
// Wrt Precision
DiffusionProcessDelegate diffusionProcessDelegate = new OUDiffusionModelDelegate(treeModel, diffusionModel, optimalTraitsModels, new MultivariateElasticModel(strengthOfSelectionMatrixParam));
System.out.println("\nTest Diagonal OU gradient precision.");
testGradient(diffusionModel, diffusionProcessDelegate, dataModel, precisionMatrix, strengthOfSelectionMatrixParam);
System.out.println("\nTest Diagonal OU gradient precision with missing.");
testGradient(diffusionModel, diffusionProcessDelegate, dataModelMissing, precisionMatrix, strengthOfSelectionMatrixParam);
// Wrt Variance
DiffusionProcessDelegate diffusionProcessDelegateVariance = new OUDiffusionModelDelegate(treeModel, diffusionModelVar, optimalTraitsModels, new MultivariateElasticModel(strengthOfSelectionMatrixParam));
System.out.println("\nTest Diagonal OU gradient variance.");
testGradient(diffusionModelVar, diffusionProcessDelegateVariance, dataModel, precisionMatrixInv, strengthOfSelectionMatrixParam);
System.out.println("\nTest Diagonal OU gradient variance with missing.");
testGradient(diffusionModelVar, diffusionProcessDelegateVariance, dataModelMissing, precisionMatrixInv, strengthOfSelectionMatrixParam);
// Factor Model
List<BranchRateModel> optimalTraitsModelsFactor = new ArrayList<BranchRateModel>();
optimalTraitsModelsFactor.add(new StrictClockBranchRates(new Parameter.Default("rate.1", new double[] { 0.0 })));
optimalTraitsModelsFactor.add(new StrictClockBranchRates(new Parameter.Default("rate.2", new double[] { 2.0 })));
DiagonalMatrix strengthOfSelectionMatrixParamFactor = new DiagonalMatrix(new Parameter.Default(new double[] { 0.0, 50.0 }));
DiffusionProcessDelegate diffusionProcessDelegateFactor = new OUDiffusionModelDelegate(treeModel, diffusionModelFactor, optimalTraitsModelsFactor, new MultivariateElasticModel(strengthOfSelectionMatrixParamFactor));
System.out.println("\nTest gradient precision.");
testGradient(diffusionModelFactor, diffusionProcessDelegateFactor, dataModelFactor, rootPriorFactor, rootMeanFactor, precisionMatrixFactor, false, strengthOfSelectionMatrixParamFactor, null, null);
// Repeated Measures Model
System.out.println("\nTest gradient precision repeated measures.");
testGradient(diffusionModel, diffusionProcessDelegate, dataModelRepeatedMeasures, rootPrior, meanRoot, precisionMatrix, false, strengthOfSelectionMatrixParam, null, samplingPrecision);
testGradient(diffusionModel, diffusionProcessDelegate, dataModelRepeatedMeasuresInv, rootPrior, meanRoot, precisionMatrix, false, strengthOfSelectionMatrixParam, null, samplingPrecisionInv);
}
use of dr.evomodel.continuous.MultivariateElasticModel in project beast-mcmc by beast-dev.
the class DiffusionGradientTest method testGradientOUWithMissing.
public void testGradientOUWithMissing() {
// 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[] { 1.0 })));
optimalTraitsModels.add(new StrictClockBranchRates(new Parameter.Default("rate.5", new double[] { 2.0 })));
optimalTraitsModels.add(new StrictClockBranchRates(new Parameter.Default("rate.6", new double[] { -2.0 })));
Parameter[] strengthOfSelectionParameters = new Parameter[6];
strengthOfSelectionParameters[0] = new Parameter.Default(new double[] { 10, 1.0, 0.0, 0.0, 0.0, 2.0 });
strengthOfSelectionParameters[1] = new Parameter.Default(new double[] { 1.0, 20, 0.0, 0.0, 0.0, 0.0 });
strengthOfSelectionParameters[2] = new Parameter.Default(new double[] { 0.0, 0.0, 30, 0.0, 0.0, 0.0 });
strengthOfSelectionParameters[3] = new Parameter.Default(new double[] { 0.0, 0.0, 0.0, 40, 3.0, 0.0 });
strengthOfSelectionParameters[4] = new Parameter.Default(new double[] { 0.0, 0.0, 0.0, 3.0, 50, 0.0 });
strengthOfSelectionParameters[5] = new Parameter.Default(new double[] { 2.0, 0.0, 0.0, 0.0, 0.0, 60 });
MatrixParameter strengthOfSelectionMatrixParam = new MatrixParameter("strengthOfSelectionMatrix", strengthOfSelectionParameters);
// Wrt Precision
DiffusionProcessDelegate diffusionProcessDelegate = new OUDiffusionModelDelegate(treeModel, diffusionModel, optimalTraitsModels, new MultivariateElasticModel(strengthOfSelectionMatrixParam));
System.out.println("\nTest OU gradient precision.");
testGradient(diffusionModel, diffusionProcessDelegate, dataModel, precisionMatrix);
System.out.println("\nTest OU gradient precision with missing.");
testGradient(diffusionModel, diffusionProcessDelegate, dataModelMissing, precisionMatrix);
// Wrt Variance
DiffusionProcessDelegate diffusionProcessDelegateVariance = new OUDiffusionModelDelegate(treeModel, diffusionModelVar, optimalTraitsModels, new MultivariateElasticModel(strengthOfSelectionMatrixParam));
System.out.println("\nTest OU gradient variance.");
testGradient(diffusionModelVar, diffusionProcessDelegateVariance, dataModel, precisionMatrixInv);
System.out.println("\nTest OU gradient variance with missing.");
testGradient(diffusionModelVar, diffusionProcessDelegateVariance, dataModelMissing, precisionMatrixInv);
// Factor Model
List<BranchRateModel> optimalTraitsModelsFactor = new ArrayList<BranchRateModel>();
optimalTraitsModelsFactor.add(new StrictClockBranchRates(new Parameter.Default("rate.1", new double[] { 0.0 })));
optimalTraitsModelsFactor.add(new StrictClockBranchRates(new Parameter.Default("rate.2", new double[] { 2.0 })));
Parameter[] strengthOfSelectionParametersFactor = new Parameter[2];
strengthOfSelectionParametersFactor[0] = new Parameter.Default(new double[] { 10, 1.0 });
strengthOfSelectionParametersFactor[1] = new Parameter.Default(new double[] { 1.0, 20 });
MatrixParameter strengthOfSelectionMatrixParamFactor = new MatrixParameter("strengthOfSelectionMatrix", strengthOfSelectionParametersFactor);
DiffusionProcessDelegate diffusionProcessDelegateFactor = new OUDiffusionModelDelegate(treeModel, diffusionModelFactor, optimalTraitsModelsFactor, new MultivariateElasticModel(strengthOfSelectionMatrixParamFactor));
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);
// ************//
// Single opt
List<BranchRateModel> optimalTraitsModelsSingle = new ArrayList<BranchRateModel>();
CompoundParameter optParamSingle = new CompoundParameter("opt");
for (int i = 0; i < dimTrait; i++) {
Parameter rate = new Parameter.Default("opt." + (i + 1), new double[] { 2.2 * i + 3.1 });
optParamSingle.addParameter(rate);
optimalTraitsModelsSingle.add(new StrictClockBranchRates(rate));
}
// Wrt Precision
DiffusionProcessDelegate diffusionProcessDelegateSingle = new OUDiffusionModelDelegate(treeModel, diffusionModel, optimalTraitsModelsSingle, new MultivariateElasticModel(strengthOfSelectionMatrixParam));
System.out.println("\nTest OU single opt gradient precision.");
testGradient(diffusionModel, diffusionProcessDelegateSingle, dataModel, precisionMatrix, optParamSingle);
System.out.println("\nTest OU single opt gradient precision with missing.");
testGradient(diffusionModel, diffusionProcessDelegateSingle, dataModelMissing, precisionMatrix, optParamSingle);
// Wrt Variance
DiffusionProcessDelegate diffusionProcessDelegateVarianceSingle = new OUDiffusionModelDelegate(treeModel, diffusionModelVar, optimalTraitsModelsSingle, new MultivariateElasticModel(strengthOfSelectionMatrixParam));
System.out.println("\nTest OU single opt gradient variance.");
testGradient(diffusionModelVar, diffusionProcessDelegateVarianceSingle, dataModel, precisionMatrixInv, optParamSingle);
System.out.println("\nTest OU single opt gradient variance with missing.");
testGradient(diffusionModelVar, diffusionProcessDelegateVarianceSingle, dataModelMissing, precisionMatrixInv, optParamSingle);
// Repeated Measures Model
System.out.println("\nTest gradient precision repeated measures.");
testGradient(diffusionModel, diffusionProcessDelegateSingle, dataModelRepeatedMeasures, rootPrior, meanRoot, precisionMatrix, false, null, optParamSingle, samplingPrecision);
testGradient(diffusionModel, diffusionProcessDelegateSingle, dataModelRepeatedMeasuresInv, rootPrior, meanRoot, precisionMatrix, false, null, optParamSingle, samplingPrecisionInv);
// **************//
// Same mean
// Diffusion
List<BranchRateModel> optimalTraitsModelsSame = new ArrayList<BranchRateModel>();
for (int i = 0; i < dimTrait; i++) {
optimalTraitsModelsSame.add(new StrictClockBranchRates(meanRoot.getParameter(i)));
}
// Wrt Precision
DiffusionProcessDelegate diffusionProcessDelegateSame = new OUDiffusionModelDelegate(treeModel, diffusionModel, optimalTraitsModelsSame, new MultivariateElasticModel(strengthOfSelectionMatrixParam));
System.out.println("\nTest OU Same opt gradient precision.");
testGradient(diffusionModel, diffusionProcessDelegateSame, dataModel, precisionMatrix, meanRoot);
System.out.println("\nTest OU Same opt gradient precision with missing.");
testGradient(diffusionModel, diffusionProcessDelegateSame, dataModelMissing, precisionMatrix, meanRoot);
// Wrt Variance
DiffusionProcessDelegate diffusionProcessDelegateVarianceSame = new OUDiffusionModelDelegate(treeModel, diffusionModelVar, optimalTraitsModelsSame, new MultivariateElasticModel(strengthOfSelectionMatrixParam));
System.out.println("\nTest OU Same opt gradient variance.");
testGradient(diffusionModelVar, diffusionProcessDelegateVarianceSame, dataModel, precisionMatrixInv, meanRoot);
System.out.println("\nTest OU Same opt gradient variance with missing.");
testGradient(diffusionModelVar, diffusionProcessDelegateVarianceSame, dataModelMissing, precisionMatrixInv, meanRoot);
// Repeated Measures Model
System.out.println("\nTest gradient precision repeated measures.");
testGradient(diffusionModel, diffusionProcessDelegateSame, dataModelRepeatedMeasures, rootPrior, meanRoot, precisionMatrix, false, null, meanRoot, samplingPrecision);
testGradient(diffusionModel, diffusionProcessDelegateSame, dataModelRepeatedMeasuresInv, rootPrior, meanRoot, precisionMatrix, false, null, meanRoot, samplingPrecisionInv);
}
use of dr.evomodel.continuous.MultivariateElasticModel in project beast-mcmc by beast-dev.
the class ContinuousDataLikelihoodDelegateTest method testLikelihoodFullOUFactor.
public void testLikelihoodFullOUFactor() {
System.out.println("\nTest Likelihood using full OU and factor:");
// 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.1", new double[] { 2.0 })));
Parameter[] strengthOfSelectionParameters = new Parameter[2];
strengthOfSelectionParameters[0] = new Parameter.Default(new double[] { 0.5, 0.05 });
strengthOfSelectionParameters[1] = new Parameter.Default(new double[] { 0.05, 25.5 });
MatrixParameter strengthOfSelectionMatrixParam = new MatrixParameter("strengthOfSelectionMatrix", strengthOfSelectionParameters);
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("likelihoodFullOUFactor", dataModelFactor, dataLikelihoodFactors);
// Conditional simulations
MathUtils.setSeed(17890826);
double[] expectedTraits = new double[] { 0.4889263054598222, 1.866143125522109, 0.41845209107775877, 1.978457443711536, 0.5589398189015322, 1.8942177991552116, 0.9699471556784252, 2.0423474270630155, 0.3288819110219145, 1.9759942582707206, 0.8081782260054755, 2.038299849681893 };
testConditionalSimulations(dataLikelihoodFactors, likelihoodDelegateFactors, diffusionModelFactor, dataModelFactor, rootPriorFactor, expectedTraits);
}
use of dr.evomodel.continuous.MultivariateElasticModel in project beast-mcmc by beast-dev.
the class ContinuousDataLikelihoodDelegateTest method testLikelihoodDiagonalOUBMInd.
public void testLikelihoodDiagonalOUBMInd() {
System.out.println("\nTest Likelihood using Diagonal OU / BM:");
// 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[] { -3.0 })));
optimalTraitsModels.add(new StrictClockBranchRates(new Parameter.Default("rate.3", new double[] { -2.0 })));
DiagonalMatrix strengthOfSelectionMatrixParamOUBM = new DiagonalMatrix(new Parameter.Default(new double[] { 0.0, 0.0, 50.0 }));
DiagonalMatrix strengthOfSelectionMatrixParamOU = new DiagonalMatrix(new Parameter.Default(new double[] { 10.0, 20.0, 50.0 }));
DiagonalMatrix diffusionPrecisionMatrixParameter = new DiagonalMatrix(new Parameter.Default(new double[] { 1.0, 2.0, 3.0 }));
MultivariateDiffusionModel diffusionModel = new MultivariateDiffusionModel(diffusionPrecisionMatrixParameter);
DiffusionProcessDelegate diffusionProcessDelegateOUBM = new OUDiffusionModelDelegate(treeModel, diffusionModel, optimalTraitsModels, new MultivariateElasticModel(strengthOfSelectionMatrixParamOUBM));
DiffusionProcessDelegate diffusionProcessDelegateOU = new OUDiffusionModelDelegate(treeModel, diffusionModel, optimalTraitsModels, new MultivariateElasticModel(strengthOfSelectionMatrixParamOU));
DiffusionProcessDelegate diffusionProcessDelegateBM = new HomogeneousDiffusionModelDelegate(treeModel, diffusionModel);
// CDL
ContinuousDataLikelihoodDelegate likelihoodDelegateOUBM = new ContinuousDataLikelihoodDelegate(treeModel, diffusionProcessDelegateOUBM, dataModel, rootPriorInf, rateTransformation, rateModel, false);
ContinuousDataLikelihoodDelegate likelihoodDelegateOU = new ContinuousDataLikelihoodDelegate(treeModel, diffusionProcessDelegateOU, dataModel, rootPriorInf, rateTransformation, rateModel, false);
ContinuousDataLikelihoodDelegate likelihoodDelegateBM = new ContinuousDataLikelihoodDelegate(treeModel, diffusionProcessDelegateBM, dataModel, rootPriorInf, rateTransformation, rateModel, false);
// Likelihood Computation
TreeDataLikelihood dataLikelihoodOUBM = new TreeDataLikelihood(likelihoodDelegateOUBM, treeModel, rateModel);
TreeDataLikelihood dataLikelihoodOU = new TreeDataLikelihood(likelihoodDelegateOU, treeModel, rateModel);
TreeDataLikelihood dataLikelihoodBM = new TreeDataLikelihood(likelihoodDelegateBM, treeModel, rateModel);
// Conditional simulations
MathUtils.setSeed(17890826);
double[] traitsOUBM = getConditionalSimulations(dataLikelihoodOUBM, likelihoodDelegateOUBM, diffusionModel, dataModel, rootPriorInf, treeModel, rateTransformation);
System.err.println(new Vector(traitsOUBM));
MathUtils.setSeed(17890826);
double[] traitsOU = getConditionalSimulations(dataLikelihoodOU, likelihoodDelegateOU, diffusionModel, dataModel, rootPriorInf, treeModel, rateTransformation);
System.err.println(new Vector(traitsOU));
MathUtils.setSeed(17890826);
double[] traitsBM = getConditionalSimulations(dataLikelihoodBM, likelihoodDelegateBM, diffusionModel, dataModel, rootPriorInf, treeModel, rateTransformation);
System.err.println(new Vector(traitsBM));
// Check that missing dimensions with the same process have the same values
assertEquals(format.format(traitsBM[3]), format.format(traitsOUBM[3]));
assertEquals(format.format(traitsBM[4]), format.format(traitsOUBM[4]));
assertEquals(format.format(traitsBM[7]), format.format(traitsOUBM[7]));
assertEquals(format.format(traitsOU[5]), format.format(traitsOUBM[5]));
}
Aggregations