Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * SpecificEigendecompositionTest.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.GeneralDataType;
29 import dr.evomodel.substmodel.*;
30 import dr.inference.model.Parameter;
31 import dr.math.matrixAlgebra.Vector;
32 import test.dr.math.MathTestCase;
33
34 import java.util.Arrays;
35
36
37 public class SpecificEigendecompositionTest extends MathTestCase {
38
39 public SpecificEigendecompositionTest() {
40 super();
41 }
42
43 private ComplexSubstitutionModel setupModel(int dim, double[] rates) {
44 String[] labels = new String[dim];
45 for (Integer i = 0; i < dim; i++) {
46 labels[i] = i.toString();
47 }
48 GeneralDataType dataType = new GeneralDataType(labels);
49 Parameter freqVector = new Parameter.Default(dim);
50 for (int i = 0; i < dim; i++) {
51 freqVector.setParameterValue(i, (double) 1 / dim);
52 }
53 FrequencyModel freqModel = new FrequencyModel(dataType, freqVector);
54 Parameter rateVector = new Parameter.Default(rates);
55
56 return new ComplexSubstitutionModel("test", dataType, freqModel, rateVector) {
57 protected EigenSystem getDefaultEigenSystem(int stateCount) {
58 return new ComplexColtEigenSystem(stateCount, false, ColtEigenSystem.defaultMaxConditionNumber, ColtEigenSystem.defaultMaxIterations);
59 }
60 };
61 }
62
63 // private static int dim = 4;
64 //
65 // private static double[] testRates = {
66 // 0.27577, 0.39669, 0.07341,
67 // 0.07491, 0.08690,
68 // 0.0,
69 // 0.75, 0.0, 0.19029,
70 // 0.0, 0.44924,
71 // 0.70278
72 // };
73 //
74 // private static double[] checkEigenvalues = {-1.7950052, -1.7884673, -0.4165275, 0.0000000, 0.0, 0.0, 0.0, 0.0};
75
76 // private static int dim = 5;
77 //
78 // private static double[] testRates = {
79 // 0.0, 0.0, 0.0, 0.0,
80 // 0.0, 0.0, 0.0,
81 // 3.478773070125323, 0.2848265288341367,
82 // 0.0,
83 // 0.0, 3.606696517465288, 0.0, 0.3708731237136557,
84 // 4.421855313152474, 0.0, 2.6121833491266533,
85 // 0.0, 3.997241604528838,
86 // 1.227550493053631
87 // };
88 //
89 // private static double[] checkEigenvalues = {-3.0214359488392066, -1.978564049686774, 0.0, 0.0, 0.0};
90
91 private static int dim = 6;
92
93 private static double[] testRates = {
94 0.0, 0.0, 0.0, 0.0, 0.0,
95 0.0, 0.0, 0.0, 0.0,
96 0.0, 0.0, 0.0,
97 0.0, 0.0,
98 3.5409959027854936,
99 0.0, 0.0, 0.0, 1.9115985677138407, 3.6880365950035827,
100 0.0, 0.0, 3.135364040895322, 1.2936152589276488,
101 0.0, 2.4748026323565226, 4.738559654533422,
102 3.4092727663178453, 2.1723540463137088,
103 3.635400535152609
104
105 };
106
107 private static double[] checkEigenvalues = {-3.72531, -2.27469, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
108
109
110 // private static double[] testRates = {
111 // 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
112 // };
113
114
115 private static double tolerance = 1E-4;
116
117 public void testEigendecomposition() {
118 System.out.println("Testing specific eigendecomposition...");
119 ComplexSubstitutionModel csm = setupModel(dim, testRates);
120 double[] tmp = new double[dim * dim];
121 csm.getInfinitesimalMatrix(tmp);
122 System.out.println("Rates: " + new Vector(tmp) + "\n");
123
124 EigenDecomposition e = csm.getEigenDecomposition();
125 System.out.println("Val: " + new Vector(e.getEigenValues()));
126 System.out.println("Vec: " + new Vector(e.getEigenVectors()));
127 System.out.println("Inv: " + new Vector(e.getInverseEigenVectors()));
128
129 csm.getTransitionProbabilities(1.0, tmp);
130 System.out.println(new Vector(tmp));
131
132 double[] eigenValues = e.getEigenValues();
133 Arrays.sort(eigenValues);
134
135 assertEquals(checkEigenvalues, e.getEigenValues(), tolerance);
136
137 }
138
139 }