use of beast.core.MCMC in project beast2 by CompEvol.
the class BEASTObjectDialog method main.
/**
* rudimentary test *
*/
public static void main(String[] args) {
BEASTObjectDialog dlg = null;
try {
if (args.length == 0) {
dlg = new BEASTObjectDialog(new BEASTObjectPanel(new MCMC(), Runnable.class, null), null);
} else if (args[0].equals("-x")) {
StringBuilder text = new StringBuilder();
String NL = System.getProperty("line.separator");
Scanner scanner = new Scanner(new File(args[1]));
try {
while (scanner.hasNextLine()) {
text.append(scanner.nextLine() + NL);
}
} finally {
scanner.close();
}
BEASTInterface beastObject = new beast.util.XMLParser().parseBareFragment(text.toString(), false);
dlg = new BEASTObjectDialog(new BEASTObjectPanel(beastObject, beastObject.getClass(), null), null);
} else if (args.length == 1) {
dlg = new BEASTObjectDialog(new BEASTObjectPanel((BEASTInterface) Class.forName(args[0]).newInstance(), Class.forName(args[0]), null), null);
} else if (args.length == 2) {
dlg = new BEASTObjectDialog(new BEASTObjectPanel((BEASTInterface) Class.forName(args[0]).newInstance(), Class.forName(args[1]), null), null);
} else {
throw new IllegalArgumentException("Incorrect number of arguments");
}
} catch (Exception e) {
e.printStackTrace();
System.err.println("Usage: " + BEASTObjectDialog.class.getName() + " [-x file ] [class [type]]\n" + "where [class] (optional, default MCMC) is a BEASTObject to edit\n" + "and [type] (optional only if class is specified, default Runnable) the type of the BEASTObject.\n" + "for example\n" + "");
System.exit(1);
}
dlg.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
if (dlg.showDialog()) {
BEASTInterface beastObject = dlg.m_panel.m_beastObject;
String xml = new XMLProducer().modelToXML(beastObject);
System.out.println(xml);
}
}
use of beast.core.MCMC in project beast2 by CompEvol.
the class BEASTObjectPanel method main.
/**
* rudimentary test *
*/
public static void main(String[] args) {
init();
BEASTObjectPanel pluginPanel = null;
try {
if (args.length == 0) {
pluginPanel = new BEASTObjectPanel(new MCMC(), Runnable.class, null);
} else if (args[0].equals("-x")) {
StringBuilder text = new StringBuilder();
String NL = System.getProperty("line.separator");
Scanner scanner = new Scanner(new File(args[1]));
try {
while (scanner.hasNextLine()) {
text.append(scanner.nextLine() + NL);
}
} finally {
scanner.close();
}
BEASTInterface beastObject = new beast.util.XMLParser().parseBareFragment(text.toString(), false);
pluginPanel = new BEASTObjectPanel(beastObject, beastObject.getClass(), null);
} else if (args.length == 1) {
pluginPanel = new BEASTObjectPanel((BEASTInterface) Class.forName(args[0]).newInstance(), Class.forName(args[0]), null);
} else if (args.length == 2) {
pluginPanel = new BEASTObjectPanel((BEASTInterface) Class.forName(args[0]).newInstance(), Class.forName(args[1]), null);
} else {
throw new IllegalArgumentException("Incorrect number of arguments");
}
} catch (Exception e) {
e.printStackTrace();
System.err.println("Usage: " + BEASTObjectPanel.class.getName() + " [-x file ] [class [type]]\n" + "where [class] (optional, default MCMC) is a BEASTObject to edit\n" + "and [type] (optional only if class is specified, default Runnable) the type of the BEASTObject.\n" + "for example\n" + "");
System.exit(1);
}
pluginPanel.setVisible(true);
if (pluginPanel.m_bOK) {
BEASTInterface beastObject = pluginPanel.m_beastObject;
String xml = new XMLProducer().modelToXML(beastObject);
System.out.println(xml);
}
}
use of beast.core.MCMC in project MultiTypeTree by tgvaughan.
the class NSR_Test method test.
@Test
public void test() throws Exception {
System.out.println("NSR test");
// Fix seed.
Randomizer.setSeed(42);
// 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 initial MultiTypeTree
MultiTypeTree mtTree = new StructuredCoalescentMultiTypeTree();
mtTree.initByName("typeLabel", "deme", "migrationModel", migModel, "leafTypes", "1 0");
// Set up state:
State state = new State();
state.initByName("stateNode", mtTree);
// Assemble distribution:
StructuredCoalescentTreeDensity distribution = new StructuredCoalescentTreeDensity();
distribution.initByName("migrationModel", migModel, "multiTypeTree", mtTree);
// Set up operators:
Operator operatorNSR = new NodeShiftRetype();
operatorNSR.initByName("weight", 1.0, "multiTypeTree", mtTree, "migrationModel", migModel);
// Set up stat analysis logger:
MultiTypeTreeStatLogger logger = new MultiTypeTreeStatLogger();
logger.initByName("multiTypeTree", mtTree, "burninFrac", 0.1, "logEvery", 100);
// Set up MCMC:
MCMC mcmc = new MCMC();
mcmc.initByName("chainLength", "1000000", "state", state, "distribution", distribution, "operator", operatorNSR, "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() > 2000) && (Math.abs(logger.getHeightMean() - 19.0) < 0.5) && (Math.abs(logger.getHeightVar() - 291) < 30);
Assert.assertTrue(withinTol);
}
use of beast.core.MCMC in project MultiTypeTree by tgvaughan.
the class STXR_NRR_MTU_TS_Test method test.
@Test
public void test() throws Exception {
System.out.println("STXR_NRR_MTU_TS test");
// Fix seed.
Randomizer.setSeed(42);
// 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 initial MultiTypeTree
MultiTypeTree mtTree = new StructuredCoalescentMultiTypeTree();
mtTree.initByName("typeLabel", "deme", "migrationModel", migModel, "leafTypes", "1 1 0 0");
// Set up state:
State state = new State();
state.initByName("stateNode", mtTree);
// Assemble distribution:
StructuredCoalescentTreeDensity distribution = new StructuredCoalescentTreeDensity();
distribution.initByName("migrationModel", migModel, "multiTypeTree", mtTree);
// Set up operators:
Operator operatorSTXR = new TypedSubtreeExchangeRandom();
operatorSTXR.initByName("weight", 1.0, "multiTypeTree", mtTree, "migrationModel", migModel, "mu", 0.2);
Operator operatorNRR = new NodeRetypeRandom();
operatorNRR.initByName("weight", 1.0, "multiTypeTree", mtTree, "migrationModel", migModel, "mu", 0.2);
Operator operatorMTU = new MultiTypeUniform();
operatorMTU.initByName("weight", 1.0, "multiTypeTree", mtTree, "migrationModel", migModel);
Operator operatorMTTS = new MultiTypeTreeScale();
operatorMTTS.initByName("weight", 1.0, "multiTypeTree", mtTree, "migrationModel", migModel, "scaleFactor", 1.5, "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", "10000000", "state", state, "distribution", distribution, "operator", operatorSTXR, "operator", operatorNRR, "operator", operatorMTU, "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() > 2000) && (Math.abs(logger.getHeightMean() - 25.8) < 0.5) && (Math.abs(logger.getHeightVar() - 320) < 30);
Assert.assertTrue(withinTol);
}
use of beast.core.MCMC 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);
}
Aggregations