Debian Codegraph @

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

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