Debian Codegraph @

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

raw symbols SCIP
1 package test.dr.evomodel.substmodel;
2
3 import dr.evolution.datatype.DataType;
4 import dr.evolution.datatype.TwoStateCovarion;
5 import dr.oldevomodel.substmodel.FrequencyModel;
6 import dr.oldevomodel.substmodel.SubstitutionModelUtils;
7 import dr.oldevomodel.substmodel.TwoStateCovarionModel;
8 import dr.inference.model.Parameter;
9 import junit.framework.Test;
10 import junit.framework.TestCase;
11 import junit.framework.TestSuite;
12
13 /**
14 * TwoStateCovarionModel Tester.
15 *
16 * @author Alexei Drummond
17 */
18 public class TwoStateCovarionModelTest extends TestCase {
19 public TwoStateCovarionModelTest(String name) {
20 super(name);
21 }
22
23 public void setUp() throws Exception {
24 super.setUp();
25
26 frequencies = new Parameter.Default(new double[]{0.25, 0.25, 0.25, 0.25});
27 alpha = new Parameter.Default(0.0);
28 switchingRate = new Parameter.Default(1.0);
29
30 FrequencyModel freqModel = new FrequencyModel(TwoStateCovarion.INSTANCE, frequencies);
31 model = new TwoStateCovarionModel(TwoStateCovarion.INSTANCE, freqModel, alpha, switchingRate);
32 dataType = model.getDataType();
33 }
34
35 public void testTransitionProbabilities() {
36
37 // with alpha == 1, the transition probability should be the same as binary jukes cantor
38 alpha.setParameterValue(0, 1.0);
39 switchingRate.setParameterValue(0, 1.0);
40
41 model.setupMatrix();
42
43 double[] matrix = new double[16];
44
45 double[] pi = model.getFrequencyModel().getFrequencies();
46
47 for (double distance = 0.01; distance <= 1; distance += 0.01) {
48 model.getTransitionProbabilities(distance, matrix);
49
50 double pChange =
51 (matrix[1] + matrix[3]) * pi[0] +
52 (matrix[4] + matrix[6]) * pi[1] +
53 (matrix[9] + matrix[11]) * pi[2] +
54 (matrix[12] + matrix[14]) * pi[3];
55
56 // analytical result for the probability of a mismatch in binary jukes cantor model
57 double jc = 0.5 * (1 - Math.exp(-2.0 * distance));
58
59 // System.err.println("Testing d=" + distance);
60 assertEquals(pChange, jc, 1e-14);
61 }
62 }
63 /*
64 public void testCompareToScilabCode() {
65
66 // test against Scilab results for alpha = 0.0 and switching rate = 1.0, visible state freq = {0.25, 0.75}
67
68 frequencies = new Parameter.Default(new double[]{0.125, 0.125, 0.375, 0.375});
69
70 FrequencyModel freqModel = new FrequencyModel(TwoStateCovarion.INSTANCE, frequencies);
71 model = new TwoStateCovarionModel(TwoStateCovarion.INSTANCE, freqModel, alpha, switchingRate);
72 dataType = model.getDataType();
73
74 alpha.setParameterValue(0, 0.5);
75 switchingRate.setParameterValue(0, 1.0);
76
77 model.setupMatrix();
78
79 double[] matrix = new double[16];
80
81 double[] pi = model.getFrequencyModel().getFrequencies();
82
83 model.setupMatrix();
84
85 int index = 0;
86 for (double distance = 0.01; distance <= 1.005; distance += 0.01) {
87 model.getTransitionProbabilities(distance, matrix);
88
89 double pChange =
90 (matrix[1] + matrix[3]) * pi[0] +
91 (matrix[4] + matrix[6]) * pi[1] +
92 (matrix[9] + matrix[11]) * pi[2] +
93 (matrix[12] + matrix[14]) * pi[3];
94
95 //System.out.println(distance + "\t" + pChange + "\t");
96
97 double pChangeIndependent = matLabPChange[index];
98
99 System.err.println("Testing against scilab d=" + distance);
100 assertEquals(pChange, pChangeIndependent, 1e-14);
101
102 index += 1;
103 }
104 } */
105
106 public void testSetupRelativeRates() throws Exception {
107
108 model.setupMatrix();
109
110 assertEquals(alpha.getParameterValue(0), model.getRelativeRates()[0], 1e-8);
111 assertEquals(switchingRate.getParameterValue(0), model.getRelativeRates()[1], 1e-8);
112 assertEquals(0.0, model.getRelativeRates()[2], 1e-8);
113 assertEquals(0.0, model.getRelativeRates()[3], 1e-8);
114 assertEquals(switchingRate.getParameterValue(0), model.getRelativeRates()[4], 1e-8);
115 assertEquals(1.0, model.getRelativeRates()[5], 1e-8);
116 }
117
118 public void testNormalize() {
119
120 model.setupMatrix();
121
122 double[] pi = model.getFrequencyModel().getFrequencies();
123
124 int stateCount = dataType.getStateCount();
125
126 double totalRate = 0.0;
127 for (int i = 0; i < stateCount; i++) {
128 for (int j = 0; j < stateCount; j++) {
129 int diff = Math.abs(i - j);
130 if (diff != 2 && diff != 0) {
131 totalRate += model.getQ()[i][j] * pi[i];
132 }
133 }
134 }
135
136 System.out.println(SubstitutionModelUtils.toString(model.getQ(), dataType, 2));
137
138 assertEquals(1.0, totalRate, 1e-8);
139 }
140
141 public static Test suite() {
142 return new TestSuite(TwoStateCovarionModelTest.class);
143 }
144
145 TwoStateCovarionModel model;
146 DataType dataType;
147 Parameter frequencies;
148 Parameter switchingRate;
149 Parameter alpha;
150
151
152 /*
153 // The following Scilab code was written by Alexei Drummond
154 // (adapted from Matlab code by David Bryant)
155
156 Q = [-1,1;1,-1];
157
158 // frequencies of visible states
159 pi = diag([0.25,0.75]);
160
161 Q = Q*pi;
162
163 G = [-1,1;1,-1];
164 D = diag([0.5,1]);
165 I = eye(2,2);
166 R = kron(D,Q) + kron(G, I);
167
168 // frequencies of hidden states
169 f = diag([0.5, 0.5]);
170
171 // frequencies of big matrix
172 pif = kron(f, pi);
173
174 C = [0,1,0,1;1,0,1,0;0,1,0,1;1,0,1,0]
175
176 rate = sum(sum(C.*(pif*R)))
177
178 Rn = R/rate;
179
180 for i = 1:1:100
181 t = i*0.01;
182 P = expm(Rn*t);
183 pchange(i) = sum(sum(C .*(pif*P)));
184 end;
185
186
187 */
188 static final double[] matLabPChange = {
189 0.009853754858257556,
190 0.01942241145387763,
191 0.028716586161758886,
192 0.03774630552222203,
193 0.046521051427055954,
194 0.055049802312796506,
195 0.06334107073053451,
196 0.07140293762693993,
197 0.07924308363985522,
198 0.08686881768339709,
199 0.09428710307179183,
200 0.1015045814078466,
201 0.10852759444084098,
202 0.11536220407949097,
203 0.1220142107282943,
204 0.12848917009986108,
205 0.134792408641597,
206 0.14092903770220686,
207 0.14690396655180843,
208 0.15272191435884597,
209 0.15838742121740867,
210 0.16390485830985116,
211 0.1692784372817475,
212 0.17451221889905671,
213 0.17961012105092147,
214 0.18457592615563362,
215 0.18941328802201546,
216 0.19412573821362156,
217 0.1987166919588106,
218 0.20318945364578556,
219 0.20754722193809755,
220 0.21179309454286796,
221 0.21593007266103278,
222 0.2199610651462345,
223 0.2238888923965756,
224 0.22771629000123267,
225 0.231445912161957,
226 0.23508033490766292,
227 0.23862205911867845,
228 0.2420735133757424,
229 0.24543705664747917,
230 0.24871498082886684,
231 0.25190951314210097,
232 0.2550228184102454,
233 0.25805700121315506,
234 0.2610141079343201,
235 0.26389612870652945,
236 0.26670499926357183,
237 0.2694426027045621,
238 0.2721107711769275,
239 0.27471128748357165,
240 0.27724588661926514,
241 0.27971625724089505,
242 0.2821240430758165,
243 0.2844708442722035,
244 0.28675821869497176,
245 0.28898768317056994,
246 0.2911607146836553,
247 0.2932787515284428,
248 0.29534319441730017,
249 0.2973554075489424,
250 0.2993167196384335,
251 0.30122842491099766,
252 0.3030917840615263,
253 0.3049080251815066,
254 0.30667834465498195,
255 0.3084039080250403,
256 0.31008585083221957,
257 0.3117252794261185,
258 0.3133232717514247,
259 0.314880878109485,
260 0.3163991218964633,
261 0.3178790003190806,
262 0.31932148508884917,
263 0.3207275230956765,
264 0.3220980370616438,
265 0.3234339261757309,
266 0.3247360667102066,
267 0.32600531261936083,
268 0.32724249612123,
269 0.3284484282629134,
270 0.32962389947006454,
271 0.33076968008109947,
272 0.33188652086664117,
273 0.3329751535346926,
274 0.3340362912220076,
275 0.3350706289721107,
276 0.3360788442003818,
277 0.337061597146628,
278 0.33801953131551693,
279 0.33895327390525487,
280 0.3398634362248641,
281 0.34075061410039437,
282 0.3416153882704134,
283 0.34245832477107263,
284 0.34327997531106624,
285 0.3440808776367703,
286 0.34486155588783984,
287 0.34562252094354134,
288 0.3463642707600772
289 };
290
291 }