Debian Codegraph @

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

raw symbols SCIP
1 package test.dr.evomodel.operators;
2
3 import java.io.IOException;
4
5 import dr.evolution.io.NewickImporter;
6 import dr.evolution.io.Importer.ImportException;
7 import dr.evolution.tree.FlexibleTree;
8 import dr.evolution.tree.TreeUtils;
9 import dr.evomodel.operators.ExchangeOperator;
10 import dr.evomodel.tree.TreeModel;
11 import dr.inference.model.Parameter;
12 import dr.inference.operators.*;
13 import junit.framework.Test;
14 import junit.framework.TestSuite;
15
16 /**
17 * @author Alexei Drummond
18 */
19 public class ExchangeOperatorTest extends OperatorAssert {
20
21 static final String TL = "TL";
22 //static final String TREE_HEIGHT = "rootHeight";
23
24 public static Test suite() {
25 return new TestSuite(ExchangeOperatorTest.class);
26 }
27
28 public void testWideExchangeOperator2() throws IOException, ImportException {
29
30 // probability of picking (A,B) node is 1/(2n-2) = 1/8
31 // probability of swapping with D is 1/2
32 // total = 1/16
33
34 //probability of picking {D} node is 1/(2n-2) = 1/8
35 //probability of picking {A,B} is 1/5
36 // total = 1/40
37
38 //total = 1/16 + 1/40 = 0.0625 + 0.025 = 0.0875
39
40 // new test:
41 // probability of picking (A,B) node is 1/(2n-2) = 1/8
42 // probability of swapping with D is 1/(2n-3) = 1/7
43 // total = 1/56
44
45 //probability of picking {D} node is 1/(2n-2) = 1/8
46 //probability of picking {A,B} is 1/(2n-3) = 1/7
47 // total = 1/56
48
49 //total = 1/56 + 1/56 = 1/28
50
51
52 System.out.println("Test 1: Forward");
53
54 String treeMatch = "(((D,C),(A,B)),E);";
55
56 int count = 0;
57 int reps = 1000000;
58
59 for (int i = 0; i < reps; i++) {
60
61 TreeModel treeModel = new TreeModel("treeModel", tree5);
62 ExchangeOperator operator = new ExchangeOperator(ExchangeOperator.WIDE, treeModel, 1.0);
63 operator.doOperation();
64
65 String tree = TreeUtils.newickNoLengths(treeModel);
66
67 if (tree.equals(treeMatch)) {
68 count += 1;
69 }
70
71 }
72 double p_1 = (double) count / (double) reps;
73
74 System.out.println("Number of proposals:\t" + count);
75 System.out.println("Number of tries:\t" + reps);
76 System.out.println("Number of ratio:\t" + p_1);
77 System.out.println("Number of expected ratio:\t" + 1.0/28.0);
78 assertExpectation(1.0/28.0, p_1, reps);
79
80 // since this operator is supposed to be symmetric it got a hastings ratio of one
81 // this means, it should propose the same move just backwards with the same probability
82
83 // BUT:
84
85 // (((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)
86
87 // probability of picking (A,B) node is 1/(2n-2) = 1/8
88 // probability of swapping with D is 1/3
89 // total = 1/24
90
91 //probability of picking {D} node is 1/(2n-2) = 1/8
92 //probability of picking {A,B} is 1/4
93 // total = 1/32
94
95 //total = 1/24 + 1/32 = 7/96 = 0.07291666666
96
97 // new test:
98 // probability of picking (A,B) node is 1/(2n-2) = 1/8
99 // probability of swapping with D is 1/(2n-3) = 1/7
100 // total = 1/56
101
102 //probability of picking {D} node is 1/(2n-2) = 1/8
103 //probability of picking {A,B} is 1/(2n-3) = 1/7
104 // total = 1/56
105
106 //total = 1/56 + 1/56 = 1/28
107
108 System.out.println("Test 2: Backward");
109
110 treeMatch = "((((A,B),C),D),E);";
111 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);");
112 FlexibleTree tree5_2 = (FlexibleTree) importer.importTree(null);
113
114 count = 0;
115
116 for (int i = 0; i < reps; i++) {
117
118 TreeModel treeModel = new TreeModel("treeModel", tree5_2);
119 ExchangeOperator operator = new ExchangeOperator(ExchangeOperator.WIDE, treeModel, 1.0);
120 operator.doOperation();
121
122 String tree = TreeUtils.newickNoLengths(treeModel);
123
124 if (tree.equals(treeMatch)) {
125 count += 1;
126 }
127
128 }
129 double p_2 = (double) count / (double) reps;
130
131 System.out.println("Number of proposals:\t" + count);
132 System.out.println("Number of tries:\t" + reps);
133 System.out.println("Number of ratio:\t" + p_2);
134 System.out.println("Number of expected ratio:\t" + 1.0/28.0);
135 assertExpectation(1.0/28.0, p_2, reps);
136 }
137
138 // STATIC METHODS
139
140 public OperatorSchedule getOperatorSchedule(TreeModel treeModel) {
141
142 Parameter rootParameter = treeModel.createNodeHeightsParameter(true, false, false);
143 Parameter internalHeights = treeModel.createNodeHeightsParameter(false, true, false);
144
145 ExchangeOperator operator = new ExchangeOperator(ExchangeOperator.WIDE, treeModel, 1.0);
146 ScaleOperator scaleOperator = new ScaleOperator(rootParameter, 0.75, AdaptationMode.ADAPTATION_ON, 1.0);
147 UniformOperator uniformOperator = new UniformOperator(internalHeights, 1.0);
148
149 OperatorSchedule schedule = new SimpleOperatorSchedule();
150 schedule.addOperator(operator);
151 schedule.addOperator(scaleOperator);
152 schedule.addOperator(uniformOperator);
153
154 return schedule;
155 }
156 }