use of beast.core.parameter.RealParameter in project beast2 by CompEvol.
the class InputForAnnotatedConstructor method setStringValue.
/**
* Try to parse value of string into Integer, Double or Boolean,
* or it this types differs, just assign as string.
*
* @param valueString value representation
* @throws IllegalArgumentException when all conversions fail
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
private void setStringValue(final String valueString) {
// figure out the type of T and create object based on T=Integer, T=Double, T=Boolean, T=Valuable
if (value instanceof List<?>) {
List list = (List) get();
list.clear();
// remove start and end spaces
String valueString2 = valueString.replaceAll("^\\s+", "");
valueString2 = valueString2.replaceAll("\\s+$", "");
// split into space-separated bits
String[] valuesString = valueString2.split("\\s+");
for (int i = 0; i < valuesString.length; i++) {
if (theClass.equals(Integer.class)) {
list.add(new Integer(valuesString[i % valuesString.length]));
} else if (theClass.equals(Double.class)) {
list.add(new Double(valuesString[i % valuesString.length]));
} else if (theClass.equals(Boolean.class)) {
String str = valuesString[i % valuesString.length].toLowerCase();
list.add(str.equals("1") || str.equals("true") || str.equals("yes"));
} else if (theClass.equals(String.class)) {
list.add(new String(valuesString[i % valuesString.length]));
}
}
return;
}
if (theClass.equals(Integer.class)) {
setValue(new Integer(valueString));
return;
}
if (theClass.equals(Double.class)) {
setValue(new Double(valueString));
return;
}
if (theClass.equals(Boolean.class)) {
final String valueString2 = valueString.toLowerCase();
if (valueString2.equals("yes") || valueString2.equals("true")) {
setValue(Boolean.TRUE);
return;
} else if (valueString2.equals("no") || valueString2.equals("false")) {
setValue(Boolean.FALSE);
return;
}
}
if (theClass.equals(Function.class)) {
final RealParameter param = new RealParameter();
param.initByName("value", valueString, "upper", 0.0, "lower", 0.0, "dimension", 1);
param.initAndValidate();
setValue(param);
param.getOutputs().add(beastObject);
return;
}
if (theClass.isEnum()) {
if (possibleValues == null) {
possibleValues = (T[]) theClass.getDeclaringClass().getEnumConstants();
}
for (final T t : possibleValues) {
if (valueString.equals(t.toString())) {
setValue(t);
return;
}
}
throw new IllegalArgumentException("Input 104: value " + valueString + " not found. Select one of " + Arrays.toString(possibleValues));
}
// call a string constructor of theClass
try {
Constructor ctor;
Object v = valueString;
try {
ctor = theClass.getDeclaredConstructor(String.class);
} catch (NoSuchMethodException e) {
// try integer constructor instead
try {
if (valueString.startsWith("0x")) {
v = Integer.parseInt(valueString.substring(2), 16);
} else {
v = Integer.parseInt(valueString);
}
ctor = theClass.getDeclaredConstructor(int.class);
} catch (NumberFormatException e2) {
// could not parse as integer, try double instead
v = Double.parseDouble(valueString);
ctor = theClass.getDeclaredConstructor(double.class);
}
}
ctor.setAccessible(true);
final Object o = ctor.newInstance(v);
setValue(o);
if (o instanceof BEASTInterface) {
((BEASTInterface) o).getOutputs().add(beastObject);
}
} catch (Exception e) {
throw new IllegalArgumentException("Input 103: type mismatch, cannot initialize input '" + getName() + "' with value '" + valueString + "'.\nExpected something of type " + getType().getName() + ". " + (e.getMessage() != null ? e.getMessage() : ""));
}
}
use of beast.core.parameter.RealParameter 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.core.parameter.RealParameter 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.parameter.RealParameter 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.parameter.RealParameter 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