Debian Codegraph @

src/test/dr/evomodel/operators/OperatorAssert.java

raw symbols SCIP
1 /**
2 *
3 */
4 package test.dr.evomodel.operators;
5
6 import dr.evolution.io.Importer;
7 import dr.evolution.io.NewickImporter;
8 import dr.evolution.io.NexusImporter;
9 import dr.evolution.tree.FlexibleTree;
10 import dr.evolution.tree.Tree;
11 import dr.evolution.tree.TreeUtils;
12 import dr.evolution.util.Units;
13 import dr.evomodel.speciation.BirthDeathGernhard08Model;
14 import dr.evomodel.speciation.SpeciationLikelihood;
15 import dr.evomodel.speciation.SpeciationModel;
16 import dr.evomodel.tree.TreeHeightStatistic;
17 import dr.evomodel.tree.TreeLengthStatistic;
18 import dr.evomodel.tree.TreeLogger;
19 import dr.evomodel.tree.TreeModel;
20 import dr.evomodelxml.coalescent.OldCoalescentSimulatorParser;
21 import dr.inference.loggers.MCLogger;
22 import dr.inference.loggers.TabDelimitedFormatter;
23 import dr.inference.mcmc.MCMC;
24 import dr.inference.mcmc.MCMCOptions;
25 import dr.inference.model.Likelihood;
26 import dr.inference.model.Parameter;
27 import dr.inference.operators.OperatorSchedule;
28 import dr.math.MathUtils;
29 import junit.framework.TestCase;
30
31 import java.io.File;
32 import java.io.FileOutputStream;
33 import java.io.FileReader;
34 import java.io.IOException;
35 import java.util.HashMap;
36 import java.util.HashSet;
37 import java.util.Set;
38
39 /**
40 * @author Sebastian Hoehna
41 *
42 */
43 public abstract class OperatorAssert extends TestCase {
44
45 static final String TL = "TL";
46 static final String TREE_HEIGHT = OldCoalescentSimulatorParser.ROOT_HEIGHT;
47
48 protected FlexibleTree tree5;
49 protected FlexibleTree tree6;
50
51 public void setUp() throws Exception {
52 super.setUp();
53
54 MathUtils.setSeed(666);
55
56 NewickImporter 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 tree5 = (FlexibleTree) 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 tree6 = (FlexibleTree) importer.importTree(null);
63 }
64
65 // 5 taxa trees should sample all 105 topologies
66 public void testIrreducibility5() throws IOException, Importer.ImportException {
67 irreducibilityTester(tree5, 105, 1000000, 10);
68 }
69
70 // 6 taxa trees should sample all 945 topologies
71 public void testIrreducibility6() throws IOException, Importer.ImportException {
72 irreducibilityTester(tree6, 945, 2000000, 4);
73 }
74
75 /**
76 * @param ep the expected (binomial) probability of success
77 * @param ap the actual proportion of successes
78 * @param count the number of attempts
79 */
80 protected void assertExpectation(double ep, double ap, int count) {
81
82 if (count * ap < 5 || count * (1 - ap) < 5) throw new IllegalArgumentException();
83
84 double stdev = Math.sqrt(ap * (1.0 - ap) * count) / count;
85 double upper = ap + 2 * stdev;
86 double lower = ap - 2 * stdev;
87
88 assertTrue("Expected p=" + ep + " but got " + ap + " +/- " + stdev,
89 upper > ep && lower < ep);
90
91 }
92
93 private void irreducibilityTester(Tree tree, int numLabelledTopologies, int chainLength, int sampleTreeEvery)
94 throws IOException, Importer.ImportException {
95
96 MCMC mcmc = new MCMC("mcmc1");
97 MCMCOptions options = new MCMCOptions(chainLength);
98
99 TreeModel treeModel = new TreeModel("treeModel", tree);
100 TreeLengthStatistic tls = new TreeLengthStatistic(TL, treeModel);
101 TreeHeightStatistic rootHeight = new TreeHeightStatistic(TREE_HEIGHT, treeModel);
102
103 OperatorSchedule schedule = getOperatorSchedule(treeModel);
104
105 Parameter b = new Parameter.Default("b", 2.0, 0.0, Double.MAX_VALUE);
106 Parameter d = new Parameter.Default("d", 0.0, 0.0, Double.MAX_VALUE);
107
108 SpeciationModel speciationModel = new BirthDeathGernhard08Model(b, d, null, BirthDeathGernhard08Model.TreeType.UNSCALED,
109 Units.Type.YEARS);
110 Likelihood likelihood = new SpeciationLikelihood(treeModel, speciationModel, "yule.like");
111
112 MCLogger[] loggers = new MCLogger[2];
113 // loggers[0] = new MCLogger(new ArrayLogFormatter(false), 100, false);
114 // loggers[0].add(likelihood);
115 // loggers[0].add(rootHeight);
116 // loggers[0].add(tls);
117
118 loggers[0] = new MCLogger(new TabDelimitedFormatter(System.out), 10000, false);
119 loggers[0].add(likelihood);
120 loggers[0].add(rootHeight);
121 loggers[0].add(tls);
122
123 File file = new File("yule.trees");
124 file.deleteOnExit();
125 FileOutputStream out = new FileOutputStream(file);
126
127 loggers[1] = new TreeLogger(treeModel, new TabDelimitedFormatter(out), sampleTreeEvery, true, true, false);
128
129 mcmc.setShowOperatorAnalysis(true);
130
131 mcmc.init(options, likelihood, schedule, loggers);
132
133 mcmc.run();
134 out.flush();
135 out.close();
136
137 Set<String> uniqueTrees = new HashSet<String>();
138 HashMap<String, Integer> topologies = new HashMap<String, Integer>();
139 HashMap<String, HashMap<String, Integer>> treeCounts = new HashMap<String, HashMap<String, Integer>>();
140
141 NexusImporter importer = new NexusImporter(new FileReader(file));
142 int sampleSize = 0;
143 while (importer.hasTree()) {
144 sampleSize++;
145 Tree t = importer.importNextTree();
146 String uniqueNewick = TreeUtils.uniqueNewick(t, t.getRoot());
147 String topology = uniqueNewick.replaceAll("\\w+", "X");
148 if (!uniqueTrees.contains(uniqueNewick)){
149 uniqueTrees.add(uniqueNewick);
150 }
151 HashMap<String, Integer> counts;
152 if (topologies.containsKey(topology)){
153 topologies.put(topology, topologies.get(topology)+1);
154 counts = treeCounts.get(topology);
155 }
156 else {
157 topologies.put(topology, 1);
158 counts = new HashMap<String, Integer>();
159 treeCounts.put(topology, counts);
160 }
161 if (counts.containsKey(uniqueNewick)){
162 counts.put(uniqueNewick, counts.get(uniqueNewick)+1);
163 }
164 else {
165 counts.put(uniqueNewick, 1);
166 }
167 }
168
169 TestCase.assertEquals(numLabelledTopologies, uniqueTrees.size());
170
171 TestCase.assertEquals(sampleSize, chainLength / sampleTreeEvery + 1);
172
173 Set<String> keys = topologies.keySet();
174 double ep = 1.0 / topologies.size();
175 for (String topology : keys){
176 double ap = ((double)topologies.get(topology)) / (sampleSize);
177 // assertExpectation(ep, ap, sampleSize);
178
179 HashMap<String, Integer> counts = treeCounts.get(topology);
180 Set<String> trees = counts.keySet();
181
182 double MSE = 0;
183 double ep1 = 1.0 / counts.size();
184 for (String t : trees){
185 double ap1 = ((double)counts.get(t)) / (topologies.get(topology));
186 // assertExpectation(ep1, ap1, topologies.get(topology));
187 MSE += (ep1-ap1)*(ep1-ap1);
188 }
189 MSE /= counts.size();
190
191 System.out.println("The Mean Square Error for the topolgy " + topology + " is " + MSE);
192 }
193 }
194
195 public abstract OperatorSchedule getOperatorSchedule(TreeModel treeModel);
196 }