use of dr.util.NumberFormatter in project beast-mcmc by beast-dev.
the class TestCalibratedYuleModel method yuleTester.
private void yuleTester(TreeModel treeModel, OperatorSchedule schedule, Parameter brParameter, double S, int chainLength) throws IOException, TreeUtils.MissingTaxonException {
MCMC mcmc = new MCMC("mcmc1");
MCMCOptions options = new MCMCOptions(chainLength);
TreeLengthStatistic tls = new TreeLengthStatistic(TL, treeModel);
TreeHeightStatistic rootHeight = new TreeHeightStatistic(TREE_HEIGHT, treeModel);
SpeciationModel speciationModel = new BirthDeathGernhard08Model("yule", brParameter, null, null, BirthDeathGernhard08Model.TreeType.UNSCALED, Units.Type.SUBSTITUTIONS, false);
Likelihood speciationLikelihood = new SpeciationLikelihood(treeModel, speciationModel, "yule.like");
Taxa halfTaxa = new Taxa();
for (int i = 0; i < taxa.getTaxonCount() / 2; i++) {
halfTaxa.addTaxon(new Taxon("T" + Integer.toString(i)));
}
TMRCAStatistic tmrca = new TMRCAStatistic("tmrca(halfTaxa)", treeModel, halfTaxa, false, false);
DistributionLikelihood logNormalLikelihood = new DistributionLikelihood(new LogNormalDistribution(M, S), // meanInRealSpace="false"
0);
logNormalLikelihood.addData(tmrca);
MonophylyStatistic monophylyStatistic = new MonophylyStatistic("monophyly(halfTaxa)", treeModel, halfTaxa, null);
BooleanLikelihood booleanLikelihood = new BooleanLikelihood();
booleanLikelihood.addData(monophylyStatistic);
// CompoundLikelihood
List<Likelihood> likelihoods = new ArrayList<Likelihood>();
likelihoods.add(speciationLikelihood);
likelihoods.add(logNormalLikelihood);
likelihoods.add(booleanLikelihood);
Likelihood prior = new CompoundLikelihood(0, likelihoods);
prior.setId(CompoundLikelihoodParser.PRIOR);
ArrayLogFormatter logformatter = new ArrayLogFormatter(false);
MCLogger[] loggers = new MCLogger[1];
loggers[0] = new MCLogger(logformatter, (int) (options.getChainLength() / 10000), false);
loggers[0].add(speciationLikelihood);
loggers[0].add(rootHeight);
loggers[0].add(tmrca);
loggers[0].add(tls);
loggers[0].add(brParameter);
mcmc.setShowOperatorAnalysis(false);
mcmc.init(options, prior, schedule, loggers);
mcmc.run();
List<Trace> traces = logformatter.getTraces();
ArrayTraceList traceList = new ArrayTraceList("yuleModelTest", traces, 1000);
for (int i = 1; i < traces.size(); i++) {
traceList.analyseTrace(i);
}
NumberFormatter formatter = new NumberFormatter(8);
TraceCorrelation tlStats = traceList.getCorrelationStatistics(traceList.getTraceIndex(TL));
TraceCorrelation treeHeightStats = traceList.getCorrelationStatistics(traceList.getTraceIndex("tmrca(halfTaxa)"));
// out.write("tmrcaHeight = \t");
out.write(formatter.format(treeHeightStats.getMean()));
out.write("\t");
double expectedNodeHeight = Math.pow(Math.E, (M + (Math.pow(S, 2) / 2)));
// out.write("expectation = \t");
out.write(formatter.format(expectedNodeHeight));
out.write("\t");
double error = Math.abs((treeHeightStats.getMean() - expectedNodeHeight) / expectedNodeHeight);
NumberFormat percentFormatter = NumberFormat.getNumberInstance();
percentFormatter.setMinimumFractionDigits(5);
percentFormatter.setMinimumFractionDigits(5);
// out.write("error = \t");
out.write(percentFormatter.format(error));
out.write("\t");
// out.write("tl.ess = \t");
out.write(Double.toString(tlStats.getESS()));
System.out.println("tmrcaHeight = " + formatter.format(treeHeightStats.getMean()) + "; expectation = " + formatter.format(expectedNodeHeight) + "; error = " + percentFormatter.format(error) + "; tl.ess = " + tlStats.getESS());
}
use of dr.util.NumberFormatter in project beast-mcmc by beast-dev.
the class TempestFrame method setStatusMessage.
private void setStatusMessage() {
Tree tree = tempestPanel.getTree();
if (tree != null) {
String message = "";
message += "Tree loaded, " + tree.getTaxonCount() + " taxa";
TemporalRooting tr = tempestPanel.getTemporalRooting();
if (tr.isContemporaneous()) {
message += ", contemporaneous tips";
} else {
NumberFormatter nf = new NumberFormatter(3);
message += ", dated tips with range " + nf.format(tr.getDateRange());
}
statusLabel.setText(message);
}
}
use of dr.util.NumberFormatter in project beast-mcmc by beast-dev.
the class ARGModel method toARGSummary.
/**
* Gives a summary of the ARG model. Should only
* be used for debugging purposes because it's really slow.
*
* @return a summary of the ARG model.
*/
public String toARGSummary() {
NumberFormatter format = new NumberFormatter(4);
String space = " ";
String a = "----------------------\n" + "ARG Summary \n---------------------- \n";
a += "Number of nodes: " + nodes.size() + "\n";
a += "Number of partitions: " + maxNumberOfPartitions + "\n";
a += "Number of Reassorments: " + this.getReassortmentNodeCount() + "\n";
a += "Root number: " + getRoot().getNumber() + "\n";
a += "Node Summary" + "\n----------------------------------------\n" + "ID LP RP LC RC Height" + space + "TX \n" + "----------------------------------------\n";
for (Node node : nodes) {
a += node.getNumber() + space;
if (node.leftParent == null)
a += "-1" + space;
else
a += " " + node.leftParent.number + space;
if (node.rightParent == null)
a += "-1" + space;
else
a += " " + node.rightParent.number + space;
if (node.leftChild == null)
a += "-1" + space;
else
a += " " + node.leftChild.number + space;
if (node.rightChild == null)
a += "-1" + space;
else
a += " " + node.rightChild.number + space;
a += format.formatDecimal(getNodeHeight(node), 4) + space;
if (node.partitioning != null) {
for (int i = 0, n = getNumberOfPartitions(); i < n; i++) {
a += node.partitioning.getParameterValue(i) + space;
}
}
if (node.taxon == null) {
a += "internal" + space;
} else {
a += node.taxon + space;
}
a += "\n";
}
a += "\nInduced Trees" + "\n----------------------------------------\n";
return a;
}
use of dr.util.NumberFormatter in project beast-mcmc by beast-dev.
the class ARGTraceAnalysis method report.
public void report() throws IOException {
int fieldWidth = 14;
NumberFormatter formatter = new NumberFormatter(6);
formatter.setPadding(true);
formatter.setFieldWidth(fieldWidth);
int n = argSet.size();
int totalTrees = argSet.getSumFrequency();
System.out.println();
System.out.println("burnIn=" + burnin);
System.out.println("total ARGs used =" + totalTrees);
System.out.println();
System.out.println("95% credible set (" + n + " unique ARGs, " + totalTrees + " total):");
System.out.println("Count\tPercent\tARG");
int credSet = (95 * totalTrees) / 100;
int sumFreq = 0;
NumberFormatter nf = new NumberFormatter(8);
for (int i = 0; i < n; i++) {
int freq = argSet.getFrequency(i);
double prop = ((double) freq) / totalTrees;
System.out.print(freq);
System.out.print("\t" + nf.formatDecimal(prop * 100.0, 2) + "%");
sumFreq += freq;
double sumProp = ((double) sumFreq) / totalTrees;
System.out.print("\t" + nf.formatDecimal(sumProp * 100.0, 2) + "%");
// (String)argSet.get(i);
String newickTree = argSet.get(i).toString();
// if (freq > 100) {
// calculate conditional average node heights
// Tree meanTree = analyzeTree(newickTree);
// System.out.println("\t" + Tree.Utils.newick(meanTree));
/*for (int k = 0; k < meanTree.getInternalNodeCount(); k++) {
NodeRef node = meanTree.getInternalNode(k);
System.out.println("node " + k + "\t" +
meanTree.getNodeHeight(node) + "\t" +
meanTree.getNodeAttribute(node, "lower") + "\t" +
meanTree.getNodeAttribute(node, "upper"));
}*/
// } else {
System.out.println("\t" + newickTree);
if (sumFreq >= credSet) {
System.out.println();
System.out.println("95% credible set has " + (i + 1) + " ARGs.");
break;
}
}
System.out.println();
System.out.println("DOT Format for most probable ARG:");
System.out.println(GraphMLUtils.dotFormat(argSet.get(0).toXML()));
// System.out.println(argSet.);
/* System.out.println("Majority rule clades (" + cladeSet.size() + " unique clades):");
n = cladeSet.size();
for (int i = 0; i < n; i++) {
int freq = cladeSet.getFrequency(i);
double prop = ((double)freq) / totalTrees;
if (prop > 0.5) {
System.out.print(freq);
System.out.print("\t" + nf.formatDecimal(prop * 100.0, 2) + "%");
System.out.println("\t" + cladeSet.getClade(i));
}
}*/
System.out.flush();
}
Aggregations