| 1 |
package test.dr.evomodel.speciation; |
| 2 |
|
| 3 |
import dr.evolution.io.NewickImporter; |
| 4 |
import dr.evolution.tree.FlexibleTree; |
| 5 |
import dr.evolution.util.Units; |
| 6 |
import dr.evomodel.operators.SubtreeSlideOperator; |
| 7 |
import dr.evomodel.speciation.BirthDeathGernhard08Model; |
| 8 |
import dr.evomodel.speciation.SpeciationLikelihood; |
| 9 |
import dr.evomodel.speciation.SpeciationModel; |
| 10 |
import dr.evomodel.tree.TreeHeightStatistic; |
| 11 |
import dr.evomodel.tree.TreeLengthStatistic; |
| 12 |
import dr.evomodel.tree.TreeModel; |
| 13 |
import dr.evomodelxml.tree.TreeModelParser; |
| 14 |
import dr.inference.loggers.ArrayLogFormatter; |
| 15 |
import dr.inference.loggers.MCLogger; |
| 16 |
import dr.inference.loggers.TabDelimitedFormatter; |
| 17 |
import dr.inference.mcmc.MCMC; |
| 18 |
import dr.inference.mcmc.MCMCOptions; |
| 19 |
import dr.inference.model.Likelihood; |
| 20 |
import dr.inference.model.Parameter; |
| 21 |
import dr.inference.operators.*; |
| 22 |
import dr.inference.trace.ArrayTraceList; |
| 23 |
import dr.inference.trace.Trace; |
| 24 |
import dr.inference.trace.TraceCorrelation; |
| 25 |
import dr.math.MathUtils; |
| 26 |
import junit.framework.Test; |
| 27 |
import junit.framework.TestSuite; |
| 28 |
import test.dr.inference.trace.TraceCorrelationAssert; |
| 29 |
|
| 30 |
import java.util.List; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class YuleModelTest extends TraceCorrelationAssert { |
| 40 |
|
| 41 |
static final String TL = "TL"; |
| 42 |
static final String TREE_HEIGHT = TreeModelParser.ROOT_HEIGHT; |
| 43 |
|
| 44 |
private FlexibleTree tree; |
| 45 |
|
| 46 |
public YuleModelTest(String name) { |
| 47 |
super(name); |
| 48 |
} |
| 49 |
|
| 50 |
public void setUp() throws Exception { |
| 51 |
super.setUp(); |
| 52 |
|
| 53 |
MathUtils.setSeed(666); |
| 54 |
|
| 55 |
NewickImporter importer = new NewickImporter("((1:1.0,2:1.0):1.0,(3:1.0,4:1.0):1.0);"); |
| 56 |
tree = (FlexibleTree) importer.importTree(null); |
| 57 |
} |
| 58 |
|
| 59 |
public void testYuleWithSubtreeSlide() { |
| 60 |
|
| 61 |
TreeModel treeModel = new TreeModel("treeModel", tree); |
| 62 |
|
| 63 |
OperatorSchedule schedule = new SimpleOperatorSchedule(); |
| 64 |
MCMCOperator operator = |
| 65 |
new SubtreeSlideOperator(treeModel, 1, 1, true, false, false, false, AdaptationMode.ADAPTATION_ON, AdaptableMCMCOperator.DEFAULT_ADAPTATION_TARGET); |
| 66 |
schedule.addOperator(operator); |
| 67 |
|
| 68 |
yuleTester(treeModel, schedule); |
| 69 |
|
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
private void yuleTester(TreeModel treeModel, OperatorSchedule schedule) { |
| 81 |
|
| 82 |
MCMC mcmc = new MCMC("mcmc1"); |
| 83 |
MCMCOptions options = new MCMCOptions(1000000); |
| 84 |
|
| 85 |
TreeLengthStatistic tls = new TreeLengthStatistic(TL, treeModel); |
| 86 |
TreeHeightStatistic rootHeight = new TreeHeightStatistic(TREE_HEIGHT, treeModel); |
| 87 |
|
| 88 |
Parameter b = new Parameter.Default("b", 2.0, 0.0, Double.MAX_VALUE); |
| 89 |
Parameter d = new Parameter.Default("d", 0.0, 0.0, Double.MAX_VALUE); |
| 90 |
|
| 91 |
SpeciationModel speciationModel = new BirthDeathGernhard08Model(b, d, null, BirthDeathGernhard08Model.TreeType.TIMESONLY, |
| 92 |
Units.Type.YEARS); |
| 93 |
Likelihood likelihood = new SpeciationLikelihood(treeModel, speciationModel, "yule.like"); |
| 94 |
|
| 95 |
ArrayLogFormatter formatter = new ArrayLogFormatter(false); |
| 96 |
|
| 97 |
MCLogger[] loggers = new MCLogger[2]; |
| 98 |
loggers[0] = new MCLogger(formatter, 100, false); |
| 99 |
loggers[0].add(likelihood); |
| 100 |
loggers[0].add(rootHeight); |
| 101 |
loggers[0].add(tls); |
| 102 |
|
| 103 |
loggers[1] = new MCLogger(new TabDelimitedFormatter(System.out), 100000, false); |
| 104 |
loggers[1].add(likelihood); |
| 105 |
loggers[1].add(rootHeight); |
| 106 |
loggers[1].add(tls); |
| 107 |
|
| 108 |
mcmc.setShowOperatorAnalysis(true); |
| 109 |
|
| 110 |
mcmc.init(options, likelihood, schedule, loggers); |
| 111 |
|
| 112 |
mcmc.run(); |
| 113 |
|
| 114 |
List<Trace> traces = formatter.getTraces(); |
| 115 |
ArrayTraceList traceList = new ArrayTraceList("yuleModelTest", traces, 0); |
| 116 |
|
| 117 |
for (int i = 1; i < traces.size(); i++) { |
| 118 |
traceList.analyseTrace(i); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
TraceCorrelation tlStats = |
| 126 |
traceList.getCorrelationStatistics(traceList.getTraceIndex(TL)); |
| 127 |
|
| 128 |
assertExpectation(TL, tlStats, 1.5); |
| 129 |
|
| 130 |
TraceCorrelation treeHeightStats = |
| 131 |
traceList.getCorrelationStatistics(traceList.getTraceIndex(TREE_HEIGHT)); |
| 132 |
|
| 133 |
assertExpectation(TREE_HEIGHT, treeHeightStats, 0.5416666); |
| 134 |
|
| 135 |
|
| 136 |
} |
| 137 |
|
| 138 |
public static Test suite() { |
| 139 |
return new TestSuite(YuleModelTest.class); |
| 140 |
} |
| 141 |
} |