Debian Codegraph @

src/test/dr/evomodel/operators/GibbsSubtreeSwapTestProblem.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.Test;
11 import junit.framework.TestCase;
12 import junit.framework.TestSuite;
13 //import dr.evolution.io.NewickImporter;
14 import dr.evolution.io.Importer.ImportException;
15 //import dr.evolution.tree.FlexibleTree;
16 import dr.evomodel.operators.GibbsSubtreeSwap;
17 //import dr.evomodel.operators.ImportanceSubtreeSwap;
18 import dr.evomodel.tree.TreeModel;
19 import dr.inference.model.Parameter;
20 import dr.inference.operators.AdaptationMode;
21
22
23 /**
24 * @author Sebastian Hoehna
25 *
26 */
27 public class GibbsSubtreeSwapTestProblem extends OperatorAssert{
28
29
30 public static Test suite() {
31 return new TestSuite(GibbsSubtreeSwapTestProblem.class);
32 }
33
34 /**
35 * Test method for {@link dr.evomodel.operators.GibbsSubtreeSwap}.
36 * @throws ImportException
37 * @throws IOException
38 */
39 public void testDoOperation() throws IOException, ImportException {
40 // assumes that the posterior is equal for all trees!!!
41
42 // probability of picking (A,B) node is 1/(2n-3) = 1/7
43 // probability of swapping with D is 1/2
44 // total = 1/14
45
46 //probability of picking {D} node is 1/(2n-3) = 1/7
47 //probability of picking {A,B} is 1/5
48 // total = 1/35
49
50 //total = 1/14 + 1/35 = 7/70 = 0.1
51
52 // now we calculate the same for the backward proposal
53 // this is needed for the Hastings ratio
54
55 // probability of picking (A,B) node is 1/(2n-3) = 1/7
56 // probability of swapping with D is 1/3
57 // total = 1/21
58
59 //probability of picking {D} node is 1/(2n-3) = 1/7
60 //probability of picking {A,B} is 1/4
61 // total = 1/28
62
63 //total = 1/21 + 1/28 = 4/84 + 3/84 = 7/84 = 1/12
64
65 System.out.println("Test 1: Forward");
66
67 String treeMatch = "(((D,C),(A,B)),E);";
68
69 int count = 0;
70 int reps = 100000;
71
72 for (int i = 0; i < reps; i++) {
73
74 TreeModel treeModel = new TreeModel("treeModel", tree5);
75 GibbsSubtreeSwap operator = new GibbsSubtreeSwap(treeModel, false, 1.0);
76 double hr = operator.operate(null);
77
78 String tree = TreeUtils.newickNoLengths(treeModel);
79
80 if (tree.equals(treeMatch)) {
81 // System.out.println("Expected Hastings ratio = " + 5.0/6.0 + " in log = " + Math.log(5.0/6.0));
82 // System.out.println("Obtained Hastings ratio = " + Math.exp(hr) + " in log = " + hr);
83 TestCase.assertEquals(Math.log(5.0/6.0), hr, 0.00000001);
84 count += 1;
85 }
86
87
88 }
89 double p_1 = (double) count / (double) reps;
90
91 System.out.println("Number of proposals:\t" + count);
92 System.out.println("Number of tries:\t" + reps);
93 System.out.println("Number of ratio:\t" + p_1);
94 System.out.println("Number of expected ratio:\t" + 0.1);
95 assertExpectation(0.1, p_1, reps);
96 }
97
98 public OperatorSchedule getOperatorSchedule(TreeModel treeModel) {
99
100 Parameter rootParameter = treeModel.createNodeHeightsParameter(true, false, false);
101 Parameter internalHeights = treeModel.createNodeHeightsParameter(false, true, false);
102
103 GibbsSubtreeSwap operator = new GibbsSubtreeSwap(treeModel, false, 1.0);
104 ScaleOperator scaleOperator = new ScaleOperator(rootParameter, 0.75, AdaptationMode.ADAPTATION_ON, 1.0);
105 UniformOperator uniformOperator = new UniformOperator(internalHeights, 1.0);
106
107 OperatorSchedule schedule = new SimpleOperatorSchedule();
108 schedule.addOperator(operator);
109 schedule.addOperator(scaleOperator);
110 schedule.addOperator(uniformOperator);
111
112 return schedule;
113 }
114 }