Debian Codegraph @

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

raw symbols SCIP
1 /**
2 *
3 */
4 package test.dr.evomodel.operators;
5
6 import java.io.IOException;
7
8 import dr.evolution.tree.TreeUtils;
9 import dr.inference.operators.*;
10 import junit.framework.TestSuite;
11 import junit.framework.Test;
12
13 import dr.evolution.io.NewickImporter;
14 import dr.evolution.io.Importer.ImportException;
15 import dr.evolution.tree.FlexibleTree;
16 import dr.evomodel.operators.ImportancePruneAndRegraft;
17 import dr.evomodel.tree.TreeModel;
18 import dr.inference.model.Parameter;
19
20
21 /**
22 * @author Sebastian Hoehna
23 *
24 */
25 public class ImportancePruneAndRegraftTestProblem extends OperatorAssert{
26
27 public static Test suite() {
28 return new TestSuite(ImportancePruneAndRegraftTestProblem.class);
29 }
30
31 /**
32 * Test method for {@link SimpleMCMCOperator#doOperation()}.
33 * @throws ImportException
34 * @throws IOException
35 */
36 public void testDoOperation() throws IOException, ImportException {
37 // probability of picking (A,B) node is 1/(2n-3) = 1/7
38 // probability of swapping with D is 1/2
39 // total = 1/14
40
41 //probability of picking {D} node is 1/(2n-3) = 1/7
42 //probability of picking {A,B} is 1/5
43 // total = 1/35
44
45 //total = 1/14 + 1/35 = 7/70 = 0.1
46
47 System.out.println("Test 1: Forward");
48
49 String treeMatch = "(((D,C),(A,B)),E);";
50
51 int count = 0;
52 int reps = 1000000;
53
54 for (int i = 0; i < reps; i++) {
55
56 TreeModel treeModel = new TreeModel("treeModel", tree5);
57 ImportancePruneAndRegraft operator = new ImportancePruneAndRegraft(treeModel, 1.0, 0);
58 operator.doOperation();
59
60 String tree = TreeUtils.newickNoLengths(treeModel);
61
62 if (tree.equals(treeMatch)) {
63 count += 1;
64 }
65
66 }
67 double p_1 = (double) count / (double) reps;
68
69 System.out.println("Number of proposals:\t" + count);
70 System.out.println("Number of tries:\t" + reps);
71 System.out.println("Number of ratio:\t" + p_1);
72 System.out.println("Number of expected ratio:\t" + 0.1);
73 assertExpectation(0.1, p_1, reps);
74
75 // lets see what the backward probability is for the hastings ratio
76
77 // (((D:2.0,C:2.0):1.0,(A:1.0,B:1.0):2.0):1.0,E:4.0) -> ((((A,B),C),D),E)
78
79 // probability of picking (A,B) node is 1/(2n-3) = 1/7
80 // probability of swapping with D is 1/3
81 // total = 1/21
82
83 //probability of picking {D} node is 1/(2n-2) = 1/7
84 //probability of picking {A,B} is 1/4
85 // total = 1/28
86
87 //total = 1/21 + 1/28 = 7/84 = 0.08333333
88
89 System.out.println("Test 2: Backward");
90
91 treeMatch = "((((A,B),C),D),E);";
92 NewickImporter importer = new NewickImporter("(((D:2.0,C:2.0):1.0,(A:1.0,B:1.0):2.0):1.0,E:4.0);");
93 FlexibleTree tree5_2 = (FlexibleTree) importer.importTree(null);
94
95 count = 0;
96
97 for (int i = 0; i < reps; i++) {
98
99 TreeModel treeModel = new TreeModel("treeModel", tree5_2);
100 ImportancePruneAndRegraft operator = new ImportancePruneAndRegraft(treeModel, 1.0, 1);
101 operator.doOperation();
102
103 String tree = TreeUtils.newickNoLengths(treeModel);
104
105 if (tree.equals(treeMatch)) {
106 count += 1;
107 }
108
109 }
110 double p_2 = (double) count / (double) reps;
111
112 System.out.println("Number of proposals:\t" + count);
113 System.out.println("Number of tries:\t" + reps);
114 System.out.println("Number of ratio:\t" + p_2);
115 System.out.println("Number of expected ratio:\t" + 0.0833333);
116 assertExpectation(0.0833333, p_2, reps);
117 }
118
119 public OperatorSchedule getOperatorSchedule(TreeModel treeModel) {
120
121 Parameter rootParameter = treeModel.createNodeHeightsParameter(true, false, false);
122 Parameter internalHeights = treeModel.createNodeHeightsParameter(false, true, false);
123
124 ImportancePruneAndRegraft operator = new ImportancePruneAndRegraft(treeModel, 1.0, 1);
125 ScaleOperator scaleOperator = new ScaleOperator(rootParameter, 0.75, AdaptationMode.ADAPTATION_ON, 1.0);
126 UniformOperator uniformOperator = new UniformOperator(internalHeights, 1.0);
127
128 OperatorSchedule schedule = new SimpleOperatorSchedule();
129 schedule.addOperator(operator);
130 schedule.addOperator(scaleOperator);
131 schedule.addOperator(uniformOperator);
132
133 return schedule;
134 }
135 }