Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * BinaryCovarionModelTest.java
3 *
4 * Copyright (c) 2002-2014 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package test.dr.evomodel.substmodel;
27
28 import dr.evolution.datatype.DataType;
29 import dr.evolution.datatype.TwoStateCovarion;
30 import dr.evolution.datatype.TwoStates;
31 import dr.oldevomodel.substmodel.BinaryCovarionModel;
32 import dr.oldevomodel.substmodel.FrequencyModel;
33 import dr.oldevomodel.substmodel.GeneralSubstitutionModel;
34 import dr.oldevomodel.substmodel.SubstitutionModelUtils;
35 import dr.inference.model.Parameter;
36 import dr.math.matrixAlgebra.IllegalDimension;
37 import dr.math.matrixAlgebra.Matrix;
38 import dr.math.matrixAlgebra.Vector;
39 import junit.framework.Test;
40 import junit.framework.TestCase;
41 import junit.framework.TestSuite;
42
43 /**
44 * BinaryCovarionModel Tester.
45 *
46 * @author Alexei Drummond
47 * @version 1.0
48 * @since <pre>08/26/2007</pre>
49 */
50 public class BinaryCovarionModelTest extends TestCase {
51 public BinaryCovarionModelTest(String name) {
52 super(name);
53 }
54
55 @Override
56 public void setUp() throws Exception {
57 super.setUp();
58
59 frequencies = new Parameter.Default(new double[]{0.5, 0.5});
60 hiddenFrequencies = new Parameter.Default(new double[]{0.5, 0.5});
61 alpha = new Parameter.Default(0.0);
62 switchingRate = new Parameter.Default(1.0);
63
64 model = new BinaryCovarionModel(TwoStateCovarion.INSTANCE, frequencies, hiddenFrequencies, alpha, switchingRate,
65 BinaryCovarionModel.Version.VERSION1);
66 dataType = model.getDataType();
67 }
68
69 @Override
70 public void tearDown() throws Exception {
71 super.tearDown();
72 }
73
74 /**
75 * Tests that pi*Q = 0
76 */
77 public void testEquilibriumDistribution() {
78
79 alpha.setParameterValue(0, 0.1);
80 switchingRate.setParameterValue(0, 1.0);
81
82 model.setupMatrix();
83
84 double[] pi = model.getFrequencyModel().getFrequencies();
85
86 try {
87 Matrix m = new Matrix(model.getQ());
88 Vector p = new Vector(pi);
89 Vector y = m.product(p);
90
91 assertEquals(0.0, y.norm(), 1e-14);
92 } catch (IllegalDimension illegalDimension) {
93 }
94 }
95
96 public void testTransitionProbabilitiesAgainstEqualBaseFreqsEqualRates() {
97
98 // with alpha == 1, the transition probability should be the same as binary jukes cantor
99 alpha.setParameterValue(0, 1.0);
100 switchingRate.setParameterValue(0, 1.0);
101
102 model.setupMatrix();
103
104 double[] matrix = new double[16];
105
106 double[] pi = model.getFrequencyModel().getFrequencies();
107
108 for (double distance = 0.01; distance <= 1.005; distance += 0.01) {
109 model.getTransitionProbabilities(distance, matrix);
110
111 double pChange =
112 (matrix[1] + matrix[3]) * pi[0] +
113 (matrix[4] + matrix[6]) * pi[1] +
114 (matrix[9] + matrix[11]) * pi[2] +
115 (matrix[12] + matrix[14]) * pi[3];
116
117 // analytical result for the probability of a mismatch in binary jukes cantor model
118 double jc = 0.5 * (1 - Math.exp(-2.0 * distance));
119
120 assertEquals(pChange, jc, 1e-14);
121 }
122 }
123
124 public void testTransitionProbabilitiesUnequalBaseFreqsEqualRates() {
125
126 // with alpha == 1, the transition probability should be the same as binary jukes cantor
127 alpha.setParameterValue(0, 1.0);
128 switchingRate.setParameterValue(0, 1.0);
129 frequencies.setParameterValue(0, 0.25);
130 frequencies.setParameterValue(1, 0.75);
131
132 FrequencyModel freqModel = new FrequencyModel(TwoStates.INSTANCE, frequencies);
133
134 GeneralSubstitutionModel modelToCompare = new GeneralSubstitutionModel(TwoStates.INSTANCE, freqModel, null, 0);
135
136 model.setupMatrix();
137
138 double[] matrix = new double[16];
139 double[] m = new double[4];
140
141 double[] pi = model.getFrequencyModel().getFrequencies();
142
143 for (double distance = 0.01; distance <= 1.005; distance += 0.01) {
144 model.getTransitionProbabilities(distance, matrix);
145
146 modelToCompare.getTransitionProbabilities(distance, m);
147
148 double pChange =
149 (matrix[1] + matrix[3]) * pi[0] +
150 (matrix[4] + matrix[6]) * pi[1] +
151 (matrix[9] + matrix[11]) * pi[2] +
152 (matrix[12] + matrix[14]) * pi[3];
153
154 double pChange2 = m[1] * frequencies.getParameterValue(0) +
155 m[2] * frequencies.getParameterValue(1);
156
157 System.out.println(distance + "\t" + pChange2 + "\t" + pChange);
158
159 assertEquals(pChange2, pChange, 1e-14);
160 }
161 }
162
163 public void testTransitionProbabilitiesUnequalBaseFreqsUnequalRateFreqsEqualRates() {
164
165 // with alpha == 1, the transition probability should be the same as binary jukes cantor
166 alpha.setParameterValue(0, 1.0);
167 switchingRate.setParameterValue(0, 1.0);
168 frequencies.setParameterValue(0, 0.25);
169 frequencies.setParameterValue(1, 0.75);
170 hiddenFrequencies.setParameterValue(0, 0.1);
171 hiddenFrequencies.setParameterValue(1, 0.9);
172
173 FrequencyModel freqModel = new FrequencyModel(TwoStates.INSTANCE, frequencies);
174
175 GeneralSubstitutionModel modelToCompare = new GeneralSubstitutionModel(TwoStates.INSTANCE, freqModel, null, 0);
176
177 model.setupMatrix();
178
179 double[] matrix = new double[16];
180 double[] m = new double[4];
181
182 double[] pi = model.getFrequencyModel().getFrequencies();
183
184 for (double distance = 0.01; distance <= 1.005; distance += 0.01) {
185 model.getTransitionProbabilities(distance, matrix);
186
187 modelToCompare.getTransitionProbabilities(distance, m);
188
189 double pChange =
190 (matrix[1] + matrix[3]) * pi[0] +
191 (matrix[4] + matrix[6]) * pi[1] +
192 (matrix[9] + matrix[11]) * pi[2] +
193 (matrix[12] + matrix[14]) * pi[3];
194
195 double pChange2 = m[1] * frequencies.getParameterValue(0) +
196 m[2] * frequencies.getParameterValue(1);
197
198 //System.out.println(distance + "\t" + pChange2 + "\t" + pChange);
199
200 assertEquals(pChange2, pChange, 1e-14);
201 }
202 }
203
204
205 public void testTransitionProbabilitiesAgainstMatLab() {
206 // test againt Matlab results for alpha = 0.5 and switching rate = 1.0
207 // and visible state base frequencies = 0.25, 0.75
208
209 alpha.setParameterValue(0, 0.5);
210 switchingRate.setParameterValue(0, 1.0);
211 frequencies.setParameterValue(0, 0.25);
212 frequencies.setParameterValue(1, 0.75);
213
214 model.setupMatrix();
215 double[] matrix = new double[16];
216
217 double[] pi = model.getFrequencyModel().getFrequencies();
218 System.out.println(pi[0] + " " + pi[1] + " " + pi[2] + " " + pi[3]);
219
220 System.out.println(SubstitutionModelUtils.toString(model.getQ(), dataType, 2));
221
222 int index = 0;
223 for (double distance = 0.01; distance <= 1.005; distance += 0.01) {
224 model.getTransitionProbabilities(distance, matrix);
225
226
227 double pChange =
228 (matrix[1] + matrix[3]) * pi[0] +
229 (matrix[4] + matrix[6]) * pi[1] +
230 (matrix[9] + matrix[11]) * pi[2] +
231 (matrix[12] + matrix[14]) * pi[3];
232
233
234 double pChangeIndependent = TwoStateCovarionModelTest.matLabPChange[index];
235
236 //System.out.println(distance + "\t" + pChange + "\t" + pChangeIndependent);
237 assertEquals(pChange, pChangeIndependent, 1e-14);
238
239 index += 1;
240 }
241 }
242
243 public static Test suite() {
244 return new TestSuite(BinaryCovarionModelTest.class);
245 }
246
247 BinaryCovarionModel model;
248 DataType dataType;
249 Parameter frequencies;
250 Parameter hiddenFrequencies;
251 Parameter switchingRate;
252 Parameter alpha;
253 }