| 1 |
package test.dr.evomodel.arg.operators; |
| 2 |
|
| 3 |
import dr.evolution.io.Importer; |
| 4 |
import dr.evolution.io.NewickImporter; |
| 5 |
import dr.evomodel.arg.ARGLogger; |
| 6 |
import dr.evomodel.arg.ARGModel; |
| 7 |
import dr.evomodel.arg.ARGReassortmentNodeCountStatistic; |
| 8 |
import dr.evomodel.arg.coalescent.ARGUniformPrior; |
| 9 |
import dr.evomodelxml.tree.TreeModelParser; |
| 10 |
import dr.inference.distribution.DistributionLikelihood; |
| 11 |
import dr.inference.loggers.ArrayLogFormatter; |
| 12 |
import dr.inference.loggers.MCLogger; |
| 13 |
import dr.inference.loggers.TabDelimitedFormatter; |
| 14 |
import dr.inference.mcmc.MCMC; |
| 15 |
import dr.inference.mcmc.MCMCOptions; |
| 16 |
import dr.inference.model.CompoundLikelihood; |
| 17 |
import dr.inference.model.CompoundParameter; |
| 18 |
import dr.inference.model.Likelihood; |
| 19 |
import dr.inference.operators.AdaptationMode; |
| 20 |
import dr.inference.operators.OperatorSchedule; |
| 21 |
import dr.inference.operators.ScaleOperator; |
| 22 |
import dr.inference.operators.SimpleOperatorSchedule; |
| 23 |
import dr.inference.trace.ArrayTraceList; |
| 24 |
import dr.inference.trace.Trace; |
| 25 |
import dr.inference.trace.TraceCorrelation; |
| 26 |
import dr.math.distributions.GammaDistribution; |
| 27 |
import dr.math.distributions.PoissonDistribution; |
| 28 |
import test.dr.inference.trace.TraceCorrelationAssert; |
| 29 |
|
| 30 |
import java.io.File; |
| 31 |
import java.io.FileOutputStream; |
| 32 |
import java.io.IOException; |
| 33 |
import java.util.ArrayList; |
| 34 |
import java.util.List; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public class ARGAddRemoveOperatorTest extends TraceCorrelationAssert { |
| 41 |
|
| 42 |
public ARGAddRemoveOperatorTest(String name) { |
| 43 |
super(name); |
| 44 |
} |
| 45 |
|
| 46 |
public void setUp() throws Exception { |
| 47 |
super.setUp(); |
| 48 |
|
| 49 |
NewickImporter importer = new NewickImporter( |
| 50 |
"(((A:1.0,B:1.0):1.0,C:2.0):1.0,D:3.0);"); |
| 51 |
arg4 = new ARGModel(importer.importTree(null)); |
| 52 |
arg4.setupHeightBounds(); |
| 53 |
arg4.addLikelihoodCalculator(null); |
| 54 |
arg4.addLikelihoodCalculator(null); |
| 55 |
|
| 56 |
importer = new NewickImporter( |
| 57 |
"((((A:1.0,B:1.0):1.0,C:2.0):1.0,D:3.0):1.0, E:4.0);"); |
| 58 |
ARGModel arg5 = new ARGModel(importer.importTree(null)); |
| 59 |
|
| 60 |
importer = new NewickImporter( |
| 61 |
"(((((A:1.0,B:1.0):1.0,C:2.0):1.0,D:3.0):1.0, E:4.0),F:5.0);"); |
| 62 |
ARGModel arg6 = new ARGModel(importer.importTree(null)); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
public void testFlatPrior4() throws IOException, Importer.ImportException { |
| 67 |
flatPriorTester(arg4, 2000000, 1000, 2.0, 100.0, 0.5, 3); |
| 68 |
} |
| 69 |
|
| 70 |
private void flatPriorTester(ARGModel arg, int chainLength, int sampleTreeEvery, |
| 71 |
double nodeCountSetting, double rootHeightAlpha, double rootHeightBeta, int maxCount) |
| 72 |
throws IOException, Importer.ImportException { |
| 73 |
|
| 74 |
MCMC mcmc = new MCMC("mcmc1"); |
| 75 |
MCMCOptions options = new MCMCOptions(chainLength); |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
OperatorSchedule schedule = getSchedule(arg); |
| 82 |
|
| 83 |
ARGUniformPrior uniformPrior = new ARGUniformPrior(arg, maxCount, arg.getExternalNodeCount()); |
| 84 |
|
| 85 |
PoissonDistribution poisson = new PoissonDistribution(nodeCountSetting); |
| 86 |
|
| 87 |
DistributionLikelihood nodeCountPrior = new DistributionLikelihood(poisson, 0.0); |
| 88 |
ARGReassortmentNodeCountStatistic nodeCountStatistic = new ARGReassortmentNodeCountStatistic("nodeCount", arg); |
| 89 |
nodeCountPrior.addData(nodeCountStatistic); |
| 90 |
|
| 91 |
DistributionLikelihood rootPrior = new DistributionLikelihood(new GammaDistribution(rootHeightAlpha, rootHeightBeta), 0.0); |
| 92 |
CompoundParameter rootHeight = (CompoundParameter) arg.createNodeHeightsParameter(true, false, false); |
| 93 |
rootPrior.addData(rootHeight); |
| 94 |
|
| 95 |
List<Likelihood> likelihoods = new ArrayList<Likelihood>(); |
| 96 |
likelihoods.add(uniformPrior); |
| 97 |
likelihoods.add(rootPrior); |
| 98 |
likelihoods.add(nodeCountPrior); |
| 99 |
|
| 100 |
CompoundLikelihood compoundLikelihood = new CompoundLikelihood(1, likelihoods); |
| 101 |
compoundLikelihood.setId("likelihood1"); |
| 102 |
|
| 103 |
MCLogger[] loggers = new MCLogger[3]; |
| 104 |
|
| 105 |
loggers[0] = new MCLogger(new TabDelimitedFormatter(System.out), 10000, false); |
| 106 |
loggers[0].add(compoundLikelihood); |
| 107 |
loggers[0].add(arg); |
| 108 |
|
| 109 |
File file = new File("test.args"); |
| 110 |
file.deleteOnExit(); |
| 111 |
FileOutputStream out = new FileOutputStream(file); |
| 112 |
|
| 113 |
loggers[1] = new ARGLogger(arg, new TabDelimitedFormatter(out), sampleTreeEvery, "test"); |
| 114 |
|
| 115 |
ArrayLogFormatter formatter = new ArrayLogFormatter(false); |
| 116 |
|
| 117 |
loggers[2] = new MCLogger(formatter, sampleTreeEvery, false); |
| 118 |
loggers[2].add(arg); |
| 119 |
arg.getRootHeightParameter().setId("root"); |
| 120 |
loggers[2].add(arg.getRootHeightParameter()); |
| 121 |
|
| 122 |
mcmc.setShowOperatorAnalysis(true); |
| 123 |
|
| 124 |
mcmc.init(options, compoundLikelihood, schedule, loggers); |
| 125 |
|
| 126 |
mcmc.run(); |
| 127 |
out.flush(); |
| 128 |
out.close(); |
| 129 |
|
| 130 |
List<Trace> traces = formatter.getTraces(); |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
ArrayTraceList traceList = new ArrayTraceList("ARGTest", traces, 0); |
| 143 |
|
| 144 |
for (int i = 1; i < traces.size(); i++) { |
| 145 |
traceList.analyseTrace(i); |
| 146 |
} |
| 147 |
|
| 148 |
TraceCorrelation nodeCountStats = traceList.getCorrelationStatistics(1); |
| 149 |
TraceCorrelation rootHeightStats = traceList.getCorrelationStatistics(4); |
| 150 |
|
| 151 |
assertExpectation("nodeCount", nodeCountStats, poisson.truncatedMean(maxCount)); |
| 152 |
assertExpectation(TreeModelParser.ROOT_HEIGHT, rootHeightStats, rootHeightAlpha * rootHeightBeta); |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
public static OperatorSchedule getSchedule(ARGModel arg) { |
| 157 |
|
| 158 |
CompoundParameter rootHeight = (CompoundParameter) arg.createNodeHeightsParameter(true, false, false); |
| 159 |
CompoundParameter internalHeights = (CompoundParameter) arg.createNodeHeightsParameter(false, true, false); |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
ScaleOperator operator2 = new ScaleOperator(rootHeight, 0.75, AdaptationMode.ADAPTATION_ON, 5); |
| 167 |
ScaleOperator operator3 = new ScaleOperator(internalHeights, 0.75, AdaptationMode.ADAPTATION_ON, 10); |
| 168 |
|
| 169 |
OperatorSchedule schedule = new SimpleOperatorSchedule(); |
| 170 |
|
| 171 |
schedule.addOperator(operator2); |
| 172 |
schedule.addOperator(operator3); |
| 173 |
|
| 174 |
return schedule; |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
private ARGModel arg4; |
| 202 |
} |