Debian Codegraph @

src/test/dr/evomodel/substmodel/AsymQuadTest.java

raw symbols SCIP
1 package test.dr.evomodel.substmodel;
2
3 import junit.framework.TestCase;
4 import dr.inference.model.Parameter;
5 import dr.oldevomodel.substmodel.AsymmetricQuadraticModel;
6 import dr.evolution.datatype.Microsatellite;
7
8 /**
9 * @author Chieh-Hsi Wu
10 *
11 * Tests AsymmetricQuadraticModel exponentiation.
12 */
13 public class AsymQuadTest extends TestCase {
14
15 interface Instance {
16 public Microsatellite getDataType();
17
18 public double getExpanConst();
19
20 public double getExpanLin();
21
22 public double getExpanQuad();
23
24 public double getContractConst();
25
26 public double getContractLin();
27
28 public double getContractQuad();
29
30 double getDistance();
31
32 double[] getExpectedPi();
33
34 public double[] getExpectedResult();
35 }
36
37 Instance test0 = new Instance() {
38 public Microsatellite getDataType(){
39 return new Microsatellite(1,4);
40 }
41 public double getExpanConst(){
42 return 1.0;
43 }
44
45 public double getExpanLin(){
46 return 5.0;
47 }
48
49 public double getExpanQuad(){
50 return 0.0;
51 }
52
53 public double getContractConst(){
54 return 1.0;
55 }
56
57 public double getContractLin(){
58 return 5.0;
59 }
60
61 public double getContractQuad(){
62 return 0.0;
63 }
64
65 public double getDistance() {
66 return 0.1;
67 }
68
69 public double[] getExpectedPi() {
70 return new double[]{
71 0.757532281205165, 0.126255380200861, 0.068866571018651, 0.047345767575323
72 };
73 }
74
75 public double[] getExpectedResult() {
76 return new double[]{
77 0.979555040783480, 0.019216583311851, 0.001139116232520, 0.000089259672149,
78 0.115299499871107, 0.780702902910835, 0.092806213742576, 0.011191383475483,
79 0.012530278557716, 0.170144725194722, 0.654730453041978, 0.162594543205584,
80 0.001428154754382, 0.029843689267955, 0.236501153753577, 0.732227002224086
81 };
82 }
83 };
84
85 Instance test1 = new Instance() {
86 public Microsatellite getDataType(){
87 return new Microsatellite(1,4);
88 }
89 public double getExpanConst(){
90 return 1.0;
91 }
92
93 public double getExpanLin(){
94 return 5.0;
95 }
96
97 public double getExpanQuad(){
98 return 0.0;
99 }
100
101 public double getContractConst(){
102 return 2.0;
103 }
104
105 public double getContractLin(){
106 return 3.0;
107 }
108
109 public double getContractQuad(){
110 return 0.0;
111 }
112
113 public double getDistance() {
114 return 0.2;
115 }
116
117 public double[] getExpectedPi() {
118 return new double[]{
119 0.666666666666667, 0.133333333333333, 0.100000000000000, 0.100000000000000
120 };
121 }
122
123 public double[] getExpectedResult() {
124 return new double[]{
125 0.965025560544615, 0.031394424214122, 0.003139726429413, 0.000440288811850,
126 0.156972121070610, 0.676199129838700, 0.136694646485644, 0.030134102605046,
127 0.020931509529421, 0.182259528647526, 0.546569017275914, 0.250239944547139,
128 0.002935258745666, 0.040178803473394, 0.250239944547139, 0.706645993233800
129 };
130 }
131
132
133 };
134
135 Instance test2 = new Instance() {
136 public Microsatellite getDataType(){
137 return new Microsatellite(1,4);
138 }
139 public double getExpanConst(){
140 return 1.0;
141 }
142
143 public double getExpanLin(){
144 return 5.0;
145 }
146
147 public double getExpanQuad(){
148 return 3.0;
149 }
150
151 public double getContractConst(){
152 return 2.0;
153 }
154
155 public double getContractLin(){
156 return 3.0;
157 }
158
159 public double getContractQuad(){
160 return 5.0;
161 }
162
163 public double getDistance() {
164 return 0.3;
165 }
166
167 public double[] getExpectedPi() {
168 return new double[]{
169 0.873099838521076, 0.087309983852108, 0.028063923381035, 0.011526254245782
170 };
171 }
172
173 public double[] getExpectedResult() {
174 return new double[]{
175 0.951679358560076, 0.039846739718488, 0.006618862899417, 0.001855038822019,
176 0.398467397184877, 0.419766204810971, 0.131559686528619, 0.050206711475533,
177 0.205920179092971, 0.409296802533481, 0.257041454224293, 0.127741564149256,
178 0.140516950382903, 0.380309775814669, 0.311022938798188, 0.168150335004240
179 };
180 }
181
182 };
183
184 Instance[] all = {test0, test1, test2};
185 public void testAsymmetricQuadraticModel() {
186
187 for (Instance test : all) {
188 Parameter expanConst = new Parameter.Default(1,test.getExpanConst());
189 Parameter expanLin = new Parameter.Default(1, test.getExpanLin());
190 Parameter expanQuad = new Parameter.Default(1, test.getExpanQuad());
191 Parameter contractConst = new Parameter.Default(1,test.getContractConst());
192 Parameter contractLin = new Parameter.Default(1, test.getContractLin());
193 Parameter contractQuad = new Parameter.Default(1, test.getContractQuad());
194
195 Microsatellite microsat = test.getDataType();
196 AsymmetricQuadraticModel aqm = new AsymmetricQuadraticModel(microsat,null,
197 expanConst, expanLin, expanQuad, contractConst, contractLin, contractQuad, false);
198
199 aqm.computeStationaryDistribution();
200
201 double[] statDist = aqm.getStationaryDistribution();
202 final double[] expectedStatDist = test.getExpectedPi();
203 for (int k = 0; k < statDist.length; ++k) {
204 assertEquals(statDist[k], expectedStatDist[k], 1e-10);
205 }
206
207 double[] mat = new double[4*4];
208 aqm.getTransitionProbabilities(test.getDistance(), mat);
209 final double[] result = test.getExpectedResult();
210
211 int k;
212 for (k = 0; k < mat.length; ++k) {
213 assertEquals(result[k], mat[k], 1e-10);
214 // System.out.print(" " + (mat[k] - result[k]));
215 }
216
217 k = 0;
218 for(int i = 0; i < microsat.getStateCount(); i ++){
219 for(int j = 0; j < microsat.getStateCount(); j ++){
220 assertEquals(result[k++], aqm.getOneTransitionProbabilityEntry(test.getDistance(), i , j), 1e-10);
221
222 }
223 }
224
225 for(int j = 0; j < microsat.getStateCount();j ++){
226 double[] colTransitionProb = aqm.getColTransitionProbabilities(test.getDistance(), j);
227 for(int i =0 ; i < microsat.getStateCount(); i++){
228 assertEquals(result[i*microsat.getStateCount()+j], colTransitionProb[i], 1e-10);
229 }
230 }
231
232 for(int i = 0; i < microsat.getStateCount();i ++){
233 double[] rowTransitionProb = aqm.getRowTransitionProbabilities(test.getDistance(), i);
234 for(int j =0 ; j < microsat.getStateCount(); j++){
235 assertEquals(result[i*microsat.getStateCount()+j], rowTransitionProb[j], 1e-10);
236 }
237 }
238 }
239 }
240 }