use of beast.core.parameter.IntegerParameter in project MultiTypeTree by tgvaughan.
the class TWB_TS_Test method test1.
@Test
public void test1() throws Exception {
System.out.println("TWB_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:
TypedWilsonBalding operatorTWB = new TypedWilsonBalding();
operatorTWB.initByName("weight", 1.0, "multiTypeTree", mtTree, "migrationModel", migModel, "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", operatorTWB, "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());
// Direct simulation:
double[] heights = UtilMethods.getSimulatedHeights(migModel, new IntegerParameter("0 0 0"));
double simHeightMean = DiscreteStatistics.mean(heights);
double simHeightVar = DiscreteStatistics.variance(heights);
// Compare results with simulation results:
boolean withinTol = (logger.getHeightESS() > 400) && (Math.abs(logger.getHeightMean() - simHeightMean) < 1.0) && (Math.abs(logger.getHeightVar() - simHeightVar) < 30);
Assert.assertTrue(withinTol);
}
use of beast.core.parameter.IntegerParameter in project MultiTypeTree by tgvaughan.
the class TWB_TS_Test method testTWB2.
@Test
public void testTWB2() throws Exception {
System.out.println("TWB_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:
TypedWilsonBalding operatorTWB = new TypedWilsonBalding();
operatorTWB.initByName("weight", 1.0, "multiTypeTree", mtTree, "migrationModel", migModel, "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", operatorTWB, "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());
// Direct simulation:
double[] heights = UtilMethods.getSimulatedHeights(migModel, new IntegerParameter("1 0 0"));
double simHeightMean = DiscreteStatistics.mean(heights);
double simHeightVar = DiscreteStatistics.variance(heights);
// Compare analysis results with truth:
boolean withinTol = (logger.getHeightESS() > 400) && (Math.abs(logger.getHeightMean() - simHeightMean) < 1.0) && (Math.abs(logger.getHeightVar() - simHeightVar) < 30);
Assert.assertTrue(withinTol);
}
use of beast.core.parameter.IntegerParameter in project MultiTypeTree by tgvaughan.
the class SCSimTest method test.
@Test
public void test() throws Exception {
System.out.println("SCSim test");
Randomizer.setSeed(42);
// Set up migration model.
RealParameter rateMatrix = new RealParameter();
rateMatrix.initByName("dimension", 2, "value", "0.1 0.1");
RealParameter popSizes = new RealParameter();
popSizes.initByName("value", "7.0 7.0");
SCMigrationModel migrationModel = new SCMigrationModel();
migrationModel.initByName("rateMatrix", rateMatrix, "popSizes", popSizes, "typeSet", new TypeSet("A", "B"));
// Specify leaf types:
IntegerParameter leafTypes = new IntegerParameter();
leafTypes.initByName("value", "0 0 0");
// Generate ensemble:
int reps = 100000;
double[] heights = new double[reps];
for (int i = 0; i < reps; i++) {
beast.evolution.tree.StructuredCoalescentMultiTypeTree sctree;
sctree = new beast.evolution.tree.StructuredCoalescentMultiTypeTree();
sctree.initByName("migrationModel", migrationModel, "leafTypes", leafTypes);
heights[i] = sctree.getRoot().getHeight();
}
double meanHeights = DiscreteStatistics.mean(heights);
double varHeights = DiscreteStatistics.variance(heights);
boolean withinTol = (Math.abs(meanHeights - 19.2) < 0.2) && (Math.abs(varHeights - 310) < 20);
Assert.assertTrue(withinTol);
}
use of beast.core.parameter.IntegerParameter in project beast2 by CompEvol.
the class BayesianSkyline method initAndValidate.
// /**
// * This pseudo-constructor is only used for junit tests
// *
// * @param populationSize
// * @param groupSizes
// * @param tree
// */
// public void init(RealParameter populationSize, IntegerParameter
// groupSizes, Tree tree) {
// super.init(populationSize, groupSizes, tree);
// }
@Override
public void initAndValidate() {
if (treeInput.get() != null) {
throw new IllegalArgumentException("only tree intervals (not tree) should not be specified");
}
intervals = treeIntervalsInput.get();
groupSizes = groupSizeParamInput.get();
popSizes = popSizeParamInput.get();
// make sure that the sum of groupsizes == number of coalescent events
int events = intervals.treeInput.get().getInternalNodeCount();
if (groupSizes.getDimension() > events) {
throw new IllegalArgumentException("There are more groups than coalescent nodes in the tree.");
}
int paramDim2 = groupSizes.getDimension();
int eventsCovered = 0;
for (int i = 0; i < groupSizes.getDimension(); i++) {
eventsCovered += groupSizes.getValue(i);
}
if (eventsCovered != events) {
if (eventsCovered == 0 || eventsCovered == paramDim2) {
// double[] uppers = new double[paramDim2];
// double[] lowers = new double[paramDim2];
// For these special cases we assume that the XML has not
// specified initial group sizes
// or has set all to 1 and we set them here automatically...
int eventsEach = events / paramDim2;
int eventsExtras = events % paramDim2;
Integer[] values = new Integer[paramDim2];
for (int i = 0; i < paramDim2; i++) {
if (i < eventsExtras) {
values[i] = eventsEach + 1;
} else {
values[i] = eventsEach;
}
// uppers[i] = Double.MAX_VALUE;
// lowers[i] = 1.0;
}
// if (type == EXPONENTIAL_TYPE || type == LINEAR_TYPE) {
// lowers[0] = 2.0;
// }
IntegerParameter parameter = new IntegerParameter(values);
parameter.setBounds(1, Integer.MAX_VALUE);
groupSizes.assignFromWithoutID(parameter);
} else {
// initial group sizes.
throw new IllegalArgumentException("The sum of the initial group sizes does not match the number of coalescent events in the tree.");
}
}
prepare();
}
use of beast.core.parameter.IntegerParameter in project beast2 by CompEvol.
the class IntegerParameterListTest method test1.
@Test
public void test1() throws Exception {
IntegerParameterList parameterList = new IntegerParameterList();
// Parameters with which to initialise list
IntegerParameter param1 = new IntegerParameter();
param1.initByName("value", "2");
IntegerParameter param2 = new IntegerParameter();
param2.initByName("value", "3");
// Initialise parameter list
parameterList.initByName("initialParam", param1, "initialParam", param2);
// Create dummy state to allow statenode editing
State state = new State();
state.initByName("stateNode", parameterList);
state.initialise();
// Test parameter value modification
parameterList.get(0).setValue(20);
// Test parameter creation and modification
Parameter<Integer> newParam = parameterList.addNewParam();
newParam.setValue(53);
assertTrue(parameterList.get(0).getValue() == 20);
assertTrue(parameterList.get(0).getKey() == 0);
assertTrue(parameterList.get(1).getValue() == 3);
assertTrue(parameterList.get(1).getKey() == 1);
assertTrue(parameterList.get(2).getValue() == 53);
assertTrue(parameterList.get(2).getKey() == 2);
assertTrue(parameterList.size() == 3);
parameterList.remove(1);
newParam = parameterList.addNewParam();
newParam.setValue(42);
assertTrue(parameterList.get(0).getValue() == 20);
assertTrue(parameterList.get(0).getKey() == 0);
assertTrue(parameterList.get(1).getValue() == 53);
assertTrue(parameterList.get(1).getKey() == 2);
assertTrue(parameterList.get(2).getValue() == 42);
assertTrue(parameterList.get(2).getKey() == 1);
assertTrue(parameterList.size() == 3);
// Test state restore
parameterList.restore();
assertTrue(parameterList.get(0).getValue() == 2);
assertTrue(parameterList.get(0).getKey() == 0);
assertTrue(parameterList.get(1).getValue() == 3);
assertTrue(parameterList.get(1).getKey() == 1);
assertTrue(parameterList.size() == 2);
// Test serialization
String xmlStr = parameterList.toXML();
assertEquals(xmlStr, "<statenode id='null'>" + "Dimension: [1, 1], " + "Bounds: [-2147483647,2147483646], " + "AvailableKeys: [], " + "NextKey: 2, " + "Parameters: [[2],[3]], " + "ParameterKeys: [0,1]" + "</statenode>\n");
// Test deserialization
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Document doc = factory.newDocumentBuilder().parse(new ByteArrayInputStream(xmlStr.getBytes()));
doc.normalize();
NodeList nodes = doc.getElementsByTagName("*");
org.w3c.dom.Node docNode = nodes.item(0);
IntegerParameterList newParameterList = new IntegerParameterList();
newParameterList.initAndValidate();
newParameterList.fromXML(docNode);
assertTrue(newParameterList.get(0).getValue() == 2);
assertTrue(newParameterList.get(0).getKey() == 0);
assertTrue(newParameterList.get(1).getValue() == 3);
assertTrue(newParameterList.get(1).getKey() == 1);
assertTrue(newParameterList.size() == 2);
}
Aggregations