use of beast.evolution.tree.MultiTypeTreeFromNewick in project MultiTypeTree by tgvaughan.
the class SCLikelihoodTest method testCalculateLogP.
/**
* Test of calculateLogP method, of class StructuredCoalescentLikelihood.
*/
@Test
public void testCalculateLogP() throws Exception {
System.out.println("SCLikelihoodTest");
// Assemble test MultiTypeTree:
String newickStr = "(((A[&state=1]:0.25)[&state=0]:0.25,B[&state=0]:0.5)[&state=0]:1.5," + "(C[&state=0]:1.0,D[&state=0]:1.0)[&state=0]:1.0)[&state=0]:0.0;";
MultiTypeTreeFromNewick mtTree = new MultiTypeTreeFromNewick();
mtTree.initByName("value", newickStr, "typeLabel", "state");
// Assemble migration model:
RealParameter rateMatrix = new RealParameter();
rateMatrix.initByName("value", "2.0 1.0");
RealParameter popSizes = new RealParameter();
popSizes.initByName("value", "5.0 10.0");
SCMigrationModel migrationModel = new SCMigrationModel();
migrationModel.initByName("rateMatrix", rateMatrix, "popSizes", popSizes, "typeSet", new TypeSet("A", "B"));
// Set up likelihood instance:
StructuredCoalescentTreeDensity likelihood = new StructuredCoalescentTreeDensity();
likelihood.initByName("migrationModel", migrationModel, "multiTypeTree", mtTree);
// Calculated by hand
double expResult = -16.52831;
double result = likelihood.calculateLogP();
System.out.println(result);
assertEquals(expResult, result, 1e-5);
}
use of beast.evolution.tree.MultiTypeTreeFromNewick in project MultiTypeTree by tgvaughan.
the class TWBR_TS_Test method test2.
@Test
public void test2() throws Exception {
System.out.println("TWBR_test 2");
// Fix seed.
Randomizer.setSeed(42);
// Assemble initial MultiTypeTree
String newickStr = "((1[&deme=1]:1,2[&deme=0]:1)[&deme=0]:1," + "3[&deme=0]:2)[&deme=0]:0;";
MultiTypeTreeFromNewick mtTree = new MultiTypeTreeFromNewick();
mtTree.initByName("value", newickStr, "typeLabel", "deme");
// Assemble migration model:
RealParameter rateMatrix = new RealParameter("0.1 0.1");
RealParameter popSizes = new RealParameter("7.0 7.0");
SCMigrationModel migModel = new SCMigrationModel();
migModel.initByName("rateMatrix", rateMatrix, "popSizes", popSizes, "typeSet", new TypeSet("A", "B"));
// Assemble distribution:
StructuredCoalescentTreeDensity distribution = new StructuredCoalescentTreeDensity();
distribution.initByName("migrationModel", migModel, "multiTypeTree", mtTree);
// Set up state:
State state = new State();
state.initByName("stateNode", mtTree);
// Set up operator:
TypedWilsonBaldingRandom operatorTWBR = new TypedWilsonBaldingRandom();
operatorTWBR.initByName("weight", 1.0, "multiTypeTree", mtTree, "migrationModel", migModel, "mu", 0.2, "alpha", 0.2);
Operator operatorMTTS = new MultiTypeTreeScale();
operatorMTTS.initByName("weight", 1.0, "multiTypeTree", mtTree, "migrationModel", migModel, "scaleFactor", 0.8, "useOldTreeScaler", false);
// Set up stat analysis logger:
MultiTypeTreeStatLogger logger = new MultiTypeTreeStatLogger();
logger.initByName("multiTypeTree", mtTree, "burninFrac", 0.1, "logEvery", 1000);
// Set up MCMC:
MCMC mcmc = new MCMC();
mcmc.initByName("chainLength", "1000000", "state", state, "distribution", distribution, "operator", operatorTWBR, "operator", operatorMTTS, "logger", logger);
// Run MCMC:
mcmc.run();
System.out.format("height mean = %s\n", logger.getHeightMean());
System.out.format("height var = %s\n", logger.getHeightVar());
System.out.format("height ESS = %s\n", logger.getHeightESS());
// Compare analysis results with truth:
boolean withinTol = (logger.getHeightESS() > 200) && (Math.abs(logger.getHeightMean() - 23) < 1) && (Math.abs(logger.getHeightVar() - 300) < 30);
Assert.assertTrue(withinTol);
}
use of beast.evolution.tree.MultiTypeTreeFromNewick in project MultiTypeTree by tgvaughan.
the class TWBR_TS_Test method test1.
@Test
public void test1() throws Exception {
System.out.println("TWBR_test 1");
// Fix seed.
Randomizer.setSeed(42);
// Assemble initial MultiTypeTree
String newickStr = "((1[&deme=0]:1,2[&deme=0]:1)[&deme=0]:1," + "3[&deme=0]:2)[&deme=0]:0;";
MultiTypeTreeFromNewick mtTree = new MultiTypeTreeFromNewick();
mtTree.initByName("value", newickStr, "typeLabel", "deme");
// Assemble migration model:
RealParameter rateMatrix = new RealParameter("0.1 0.1");
RealParameter popSizes = new RealParameter("7.0 7.0");
SCMigrationModel migModel = new SCMigrationModel();
migModel.initByName("rateMatrix", rateMatrix, "popSizes", popSizes, "typeSet", new TypeSet("A", "B"));
// Assemble distribution:
StructuredCoalescentTreeDensity distribution = new StructuredCoalescentTreeDensity();
distribution.initByName("migrationModel", migModel, "multiTypeTree", mtTree);
// Set up state:
State state = new State();
state.initByName("stateNode", mtTree);
// Set up operator:
TypedWilsonBaldingRandom operatorTWBR = new TypedWilsonBaldingRandom();
operatorTWBR.initByName("weight", 1.0, "multiTypeTree", mtTree, "migrationModel", migModel, "mu", 0.2, "alpha", 0.2);
Operator operatorMTTS = new MultiTypeTreeScale();
operatorMTTS.initByName("weight", 1.0, "multiTypeTree", mtTree, "migrationModel", migModel, "scaleFactor", 0.8, "useOldTreeScaler", false);
// Set up stat analysis logger:
MultiTypeTreeStatLogger logger = new MultiTypeTreeStatLogger();
logger.initByName("multiTypeTree", mtTree, "burninFrac", 0.2, "logEvery", 1000);
// Set up MCMC:
MCMC mcmc = new MCMC();
mcmc.initByName("chainLength", "1000000", "state", state, "distribution", distribution, "operator", operatorTWBR, "operator", operatorMTTS, "logger", logger);
// Run MCMC:
mcmc.run();
System.out.format("height mean = %s\n", logger.getHeightMean());
System.out.format("height var = %s\n", logger.getHeightVar());
System.out.format("height ESS = %s\n", logger.getHeightESS());
// Compare analysis results with truth:
boolean withinTol = (logger.getHeightESS() > 500) && (Math.abs(logger.getHeightMean() - 19) < 0.5) && (Math.abs(logger.getHeightVar() - 300) < 50);
Assert.assertTrue(withinTol);
}
use of beast.evolution.tree.MultiTypeTreeFromNewick in project MultiTypeTree by tgvaughan.
the class StructuredCoalescentTreeDensity method main.
/**
* Test likelihood result. Duplicate of JUnit test for debugging purposes.
*
* @param argv
* @throws java.lang.Exception
*/
public static void main(String[] argv) throws Exception {
// Assemble test MultiTypeTree:
String newickStr = "(((A[state=1]:0.25)[state=0]:0.25,B[state=0]:0.5)[state=0]:1.5," + "(C[state=0]:1.0,D[state=0]:1.0)[state=0]:1.0)[state=0]:0.0;";
MultiTypeTreeFromNewick mtTree = new MultiTypeTreeFromNewick();
mtTree.initByName("newick", newickStr, "typeLabel", "state", "nTypes", 2);
// Assemble migration model:
RealParameter rateMatrix = new RealParameter();
rateMatrix.initByName("value", "2.0 1.0");
RealParameter popSizes = new RealParameter();
popSizes.initByName("value", "5.0 10.0");
SCMigrationModel migrationModel = new SCMigrationModel();
migrationModel.initByName("rateMatrix", rateMatrix, "popSizes", popSizes);
// Set up likelihood instance:
StructuredCoalescentTreeDensity likelihood = new StructuredCoalescentTreeDensity();
likelihood.initByName("migrationModel", migrationModel, "multiTypeTree", mtTree);
// Calculated by hand
double expResult = -16.52831;
double result = likelihood.calculateLogP();
System.out.println("Expected result: " + expResult);
System.out.println("Actual result: " + result);
System.out.println("Difference: " + String.valueOf(result - expResult));
}
use of beast.evolution.tree.MultiTypeTreeFromNewick in project MultiTypeTree by tgvaughan.
the class Ewing_Test method test2.
@Test
public void test2() throws Exception {
System.out.println("Ewing_test 2");
// Fix seed.
Randomizer.setSeed(42);
// Assemble initial MultiTypeTree
String newickStr = "(((1[&deme=1]:0.5)[&deme=0]:0.5,2[&deme=0]:1)[&deme=0]:1," + "3[&deme=0]:2)[&deme=0]:0;";
MultiTypeTreeFromNewick mtTree = new MultiTypeTreeFromNewick();
mtTree.initByName("value", newickStr, "typeLabel", "deme");
// Assemble migration model:
RealParameter rateMatrix = new RealParameter("0.1 0.1");
RealParameter popSizes = new RealParameter("7.0 7.0");
SCMigrationModel migModel = new SCMigrationModel();
migModel.initByName("rateMatrix", rateMatrix, "popSizes", popSizes, "typeSet", new TypeSet("A", "B"));
// Assemble distribution:
StructuredCoalescentTreeDensity distribution = new StructuredCoalescentTreeDensity();
distribution.initByName("migrationModel", migModel, "multiTypeTree", mtTree, "checkValidity", true);
// Set up state:
State state = new State();
state.initByName("stateNode", mtTree);
// Set up operators:
TypedWilsonBaldingEasy twbOperator = new TypedWilsonBaldingEasy();
twbOperator.initByName("weight", 1.0, "migrationModel", migModel, "multiTypeTree", mtTree);
TypedSubtreeExchangeEasy tsxOperator = new TypedSubtreeExchangeEasy();
tsxOperator.initByName("weight", 1.0, "multiTypeTree", mtTree, "migrationModel", migModel, "isNarrow", true);
MultiTypeUniform mtuOperator = new MultiTypeUniform();
mtuOperator.initByName("weight", 1.0, "migrationModel", migModel, "multiTypeTree", mtTree);
MultiTypeTreeScale mttsOperator = new MultiTypeTreeScale();
mttsOperator.initByName("weight", 1.0, "scaleFactor", 0.8, "useOldTreeScaler", true, "migrationModel", migModel, "multiTypeTree", mtTree);
TypePairBirthDeath tpbdOperator = new TypePairBirthDeath();
tpbdOperator.initByName("weight", 1.0, "migrationModel", migModel, "multiTypeTree", mtTree);
TypeMergeSplit tmsOperator = new TypeMergeSplit();
tmsOperator.initByName("weight", 1.0, "multiTypeTree", mtTree, "migrationModel", migModel, "includeRoot", true);
// Set up stat analysis logger:
MultiTypeTreeStatLogger logger = new MultiTypeTreeStatLogger();
logger.initByName("multiTypeTree", mtTree, "burninFrac", 0.1, "logEvery", 1000);
// Set up MCMC:
MCMC mcmc = new MCMC();
mcmc.initByName("chainLength", "10000000", "state", state, "distribution", distribution, "operator", twbOperator, "operator", tsxOperator, "operator", mtuOperator, "operator", mttsOperator, "operator", tpbdOperator, "operator", tmsOperator, "logger", logger);
// Run MCMC:
mcmc.run();
System.out.format("height mean = %s\n", logger.getHeightMean());
System.out.format("height var = %s\n", logger.getHeightVar());
System.out.format("height ESS = %s\n", logger.getHeightESS());
// Compare analysis results with truth:
boolean withinTol = (logger.getHeightESS() > 1000) && (Math.abs(logger.getHeightMean() - 23) < 0.5) && (Math.abs(logger.getHeightVar() - 300) < 30.0);
Assert.assertTrue(withinTol);
}
Aggregations