Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * EigendecompositionTest.java
3 *
4 * Copyright (c) 2002-2012 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.evomodel.substmodel.ComplexSubstitutionModel;
29 import dr.evomodel.substmodel.EigenDecomposition;
30 import dr.evomodel.substmodel.FrequencyModel;
31 import dr.evolution.datatype.GeneralDataType;
32 import dr.inference.model.Parameter;
33 import dr.math.MathUtils;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.Scanner;
37
38 public class EigendecompositionTest {
39
40 int dim;
41 int[][] columnNumberLookup;
42 double tolerance;
43 GeneralDataType dataType;
44 Parameter.Default freqVector;
45 Parameter.Default rateMatrix;
46 FrequencyModel freqModel;
47
48 public EigendecompositionTest(int dim, double tolerance){
49 this.dim = dim;
50 this.tolerance = tolerance;
51 columnNumberLookup = new int[dim][dim];
52 int columnCount=0;
53 for(int row=0; row<dim; row++){
54 for(int column=0; column<dim; column++){
55 if(row==column){
56 columnNumberLookup[row][column]=-1;
57 } else if(row<column){
58 columnNumberLookup[row][column]=columnCount;
59 columnCount++;
60 }
61 }
62 }
63 for(int column=0; column<dim; column++){
64 for(int row=0; row<dim; row++){
65 if(row>column){
66 columnNumberLookup[row][column]=columnCount;
67 columnCount++;
68 }
69 }
70 }
71 String[] labels = new String[dim];
72 for(Integer i=0; i<dim; i++){
73 labels[i] = i.toString();
74 }
75 dataType = new GeneralDataType(labels);
76 freqVector = new Parameter.Default(dim);
77 for(int i=0; i<dim; i++){
78 freqVector.setParameterValue(i,(double)1/dim);
79 }
80 freqModel = new FrequencyModel(dataType, freqVector);
81 rateMatrix = new Parameter.Default(dim*(dim-1));
82 }
83
84 private Parameter.Default inputMatrix(){
85 Scanner keyboard = new Scanner(System.in);
86 Parameter.Default out = new Parameter.Default(dim*(dim-1));
87 System.out.println("Please enter offdiagonal rows of the rate matrix");
88 for(int row=0; row<dim; row++){
89 for(int col=0; col<dim; col++){
90 if(row!=col){
91 System.out.print("Entry "+row+", "+col+": ");
92 out.setParameterValue(columnNumberLookup[row][col],keyboard.nextDouble());
93 }
94 }
95 }
96
97 return out;
98
99 }
100
101
102 private void doTest(Parameter.Default rateMatrix){
103
104 normalise(rateMatrix);
105
106 ComplexSubstitutionModel substModel = new ComplexSubstitutionModel("test", dataType, freqModel, rateMatrix);
107
108 boolean eigenDecompOK = testEigenDecomposition(substModel);
109
110 double[] probs = new double[dim*dim];
111
112 substModel.getTransitionProbabilities(1, probs);
113
114 boolean connCheckResult = true;
115
116 String eSuccess = eigenDecompOK ? "Succeeded" : "Failed (all zero eigenvectors returned)";
117 String cSuccess = connCheckResult ? "Passed" : "Failed";
118
119 System.out.println("Eigendecomposition: "+eSuccess);
120 System.out.println("Connectivity check: "+cSuccess);
121 System.out.println("Transition probability matrix:");
122
123 for(int row=0; row<dim; row++){
124 for(int col=0; col<dim; col++){
125 System.out.print(probs[row*dim+col]+" ");
126 }
127 System.out.println();
128 }
129
130
131
132 }
133
134 private void doRandomTests(){
135
136 ComplexSubstitutionModel substModel = new ComplexSubstitutionModel("test", dataType, freqModel, rateMatrix);
137
138 ArrayList<Integer[]> indicators = makeMatrices(getAllPossibilities(dim), columnNumberLookup);
139
140 for(Integer[] ind:indicators){
141
142 int successED = 0;
143 int[] successTP = new int[dim];
144
145 int[] zeroRows = new int[dim];
146
147
148 for(int row=0; row<dim; row++){
149 boolean zeroRow = true;
150 for(int col=0; col<dim; col++){
151 if(row!=col && ind[columnNumberLookup[row][col]]!=0){
152 zeroRow = false;
153 }
154 }
155 zeroRows[row] = zeroRow ? 1:0;
156 }
157
158 for(int run = 0; run<100; run++){
159
160 for(int i=0; i<dim*(dim-1); i++){
161 rateMatrix.setParameterValue(i, MathUtils.nextDouble()*ind[i]);
162 }
163
164 normalise(rateMatrix);
165
166 boolean EigenDecompOK = testEigenDecomposition(substModel);
167 if(!EigenDecompOK){
168 for(int row=0; row<dim; row++){
169 for(int col=0; col<dim; col++){
170 if(row==col){
171 System.out.print("- ");
172 } else {
173 System.out.print(rateMatrix.getParameterValue(columnNumberLookup[row][col])+" ");
174 }
175 }
176 System.out.println();
177 }
178 System.out.println();
179 }
180
181
182 Boolean[] transProbsOK = new Boolean[dim];
183
184 for(int i=0; i<dim; i++){
185 transProbsOK[i] = zeroRows[i] != 1 || testProbMatrix(substModel, i, dim, tolerance);
186 }
187
188 if(EigenDecompOK){successED++;}
189 for(int i=0; i<dim; i++)
190 if(EigenDecompOK && transProbsOK[i]){successTP[i]++;}
191 }
192 if(successED!=100){
193 System.out.print("Indicators: ");
194 for(int i=0;i<dim*(dim-1);i++){
195 System.out.print(ind[i]+" ");
196 }
197 System.out.println();
198 System.out.println("Eigendecomposition succeeded "+successED+" times out of 100");
199 for(int i=0;i<dim;i++){
200 if(zeroRows[i]==1){
201 System.out.println("Row " + (i+1) + ": Connectivity check would work "+successTP[i]+" times out of "+successED);
202 }
203 }
204 System.out.println();
205 }
206 }
207
208 }
209 private static boolean testEigenDecomposition(ComplexSubstitutionModel model){
210 EigenDecomposition eigen = model.getEigenDecomposition();
211
212 for(int i=0; i<eigen.getEigenVectors().length; i++){
213 if(eigen.getEigenVectors()[i]!=0){
214 return true;
215 }
216 }
217
218
219
220 return false;
221
222 }
223
224 private static ArrayList<Integer[]> getAllPossibleZeroRows(int dim){
225 ArrayList<Integer[]> allPossibleZeroRows = new ArrayList<Integer[]>();
226 for(int zeroRow=0; zeroRow<dim; zeroRow++){
227 Integer[] possibility = new Integer[dim];
228 for(int i=0; i<dim; i++){
229 possibility[i] = i==zeroRow ? 0:1;
230
231 }
232 allPossibleZeroRows.add(possibility);
233 }
234 return allPossibleZeroRows;
235 }
236
237
238 private static ArrayList<Integer[]> makeMatrices(ArrayList<Integer[]> configs, int[][] lookup){
239 ArrayList<Integer[]> out = new ArrayList<Integer[]>();
240 int dim = configs.get(0).length;
241 for(Integer[] config: configs){
242 boolean allZeroes = true;
243 for(int i=0; i<dim; i++){
244 if(config[i]!=0){
245 allZeroes=false;
246 }
247 }
248 if(!allZeroes){
249 Integer[] possibility = new Integer[dim*(dim-1)];
250 for(int row=0; row<dim; row++){
251 for(int col=0; col<dim; col++){
252 if(row!=col){
253 possibility[lookup[row][col]] = config[row]==0 ? 0:1;
254 }
255 }
256 }
257 out.add(possibility);
258 }
259 }
260 return out;
261 }
262
263 private static ArrayList<Integer[]> getAllPossibilities(int length){
264 ArrayList<Integer[]> allPossibilities = new ArrayList<Integer[]>();
265 if(length == 1){
266 Integer[] one = new Integer[1];
267 one[0] = 1;
268 Integer[] zero = new Integer[1];
269 zero[0] = 0;
270 allPossibilities.add(one);
271 allPossibilities.add(zero);
272 } else {
273 for(Integer[] tail: getAllPossibilities(length - 1)){
274 Integer[] one = new Integer[1];
275 one[0] = 1;
276 Integer[] zero = new Integer[1];
277 zero[0] = 0;
278 Integer[] onePlus = concat(one,tail);
279 Integer[] zeroPlus = concat(zero,tail);
280 allPossibilities.add(onePlus);
281 allPossibilities.add(zeroPlus);
282 }
283 }
284 return allPossibilities;
285 }
286
287 public static <T> T[] concat(T[] first, T[] second) {
288 T[] result = Arrays.copyOf(first, first.length + second.length);
289 System.arraycopy(second, 0, result, first.length, second.length);
290 return result;
291 }
292
293 private static Parameter.Default normalise(Parameter.Default matrix){
294 double sum = getMatrixSum(matrix);
295 int dim = matrix.getDimension();
296
297 for(int i=0; i<dim; i++){
298 matrix.setParameterValue(i,matrix.getParameterValue(i)*dim/sum);
299 }
300
301 return matrix;
302 }
303
304 private static boolean testProbMatrix(ComplexSubstitutionModel model, int rowOfInterest, int dim, double tolerance){
305 boolean out = true;
306
307 double[] probs = new double[dim*dim];
308
309 model.getTransitionProbabilities(1, probs);
310
311 for(int i=rowOfInterest*dim; i<rowOfInterest*dim+dim; i++){
312 if(i!=rowOfInterest*dim+rowOfInterest){
313 if(probs[i]>tolerance){
314 out = false;
315 }
316 }
317 }
318
319
320 return out;
321
322 }
323
324 private static double getMatrixSum(Parameter.Default matrix){
325 double sum = 0;
326 for(int i=0; i<matrix.getDimension(); i++){
327 sum += matrix.getParameterValue(i);
328 }
329 return sum;
330 }
331
332 public static void main(String[] args){
333 Scanner keyboard = new Scanner(System.in);
334
335 System.out.print("Enter dimension of matrices (number of discrete states): ");
336 int dim = keyboard.nextInt();
337
338 System.out.print("Enter tolerance for declaring transition probability matrix entries to be zero: ");
339 double tolerance = keyboard.nextDouble();
340
341 EigendecompositionTest run = new EigendecompositionTest(dim, tolerance);
342
343 // run.doRandomTests();
344
345
346
347
348 Parameter.Default matrix = run.inputMatrix();
349 run.doTest(matrix);
350 }
351
352 }