Debian Codegraph @

src/dr/oldevomodel/substmodel/BinaryCovarionModel.java

raw symbols SCIP
1 /*
2 * BinaryCovarionModel.java
3 *
4 * Copyright (c) 2002-2015 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 dr.oldevomodel.substmodel;
27
28 import dr.evolution.datatype.TwoStateCovarion;
29 import dr.oldevomodelxml.substmodel.BinaryCovarionModelParser;
30 import dr.inference.model.Parameter;
31
32 /**
33 * @author Alexei Drummond
34 * <p/>
35 * a the rate of the slow rate class
36 * 1 the rate of the fast rate class
37 * p0 the equilibrium frequency of zero states
38 * p1 1 - p0, the equilibrium frequency of one states
39 * f0 the equilibrium frequency of slow rate class
40 * f1 1 - f0, the equilibrium frequency of fast rate class
41 * s the rate of switching
42 * <p/>
43 * then the (unnormalized) instantaneous rate matrix (unnormalized Q) should be
44 * <p/>
45 * [ -(a*p1)-s , a*p1 , s , 0 ]
46 * [ a*p0 , -(a*p0)-s , 0 , s ]
47 * [ s , 0 , -p1-s , p1 ]
48 * [ 0 , s , p0 , -p0-s ]
49 */
50 public class BinaryCovarionModel extends AbstractCovarionModel {
51
52 /**
53 * @param dataType the data type
54 * @param frequencies the frequencies of the visible states
55 * @param hiddenFrequencies the frequencies of the hidden rates
56 * @param alphaParameter the rate of evolution in slow mode
57 * @param switchingParameter the rate of flipping between slow and fast modes
58 */
59 public BinaryCovarionModel(TwoStateCovarion dataType,
60 Parameter frequencies,
61 Parameter hiddenFrequencies,
62 Parameter alphaParameter,
63 Parameter switchingParameter,
64 Version version) {
65
66 super(BinaryCovarionModelParser.COVARION_MODEL, dataType, frequencies, hiddenFrequencies);
67
68 alpha = alphaParameter;
69 this.switchRate = switchingParameter;
70 this.frequencies = frequencies;
71 this.hiddenFrequencies = hiddenFrequencies;
72 this.version = version;
73
74 addVariable(alpha);
75 addVariable(switchRate);
76 addVariable(frequencies);
77 addVariable(hiddenFrequencies);
78 setupUnnormalizedQMatrix();
79 }
80
81 public enum Version {
82 VERSION1("1") {
83 public double getF0(double iF0) {
84 return 1.0;
85 }
86
87 public double getF1(double iF1) {
88 return 1.0;
89 }
90 },
91 VERSION2("2") {
92 public double getF0(double iF0) {
93 return iF0;
94 }
95
96 public double getF1(double iF1) {
97 return iF1;
98 }
99 };
100
101 Version(String text) {
102 this.text = text;
103 }
104
105 public String getText() {
106 return text;
107 }
108
109 private final String text;
110
111 public static Version parseFromString(String text) {
112 for (Version version : Version.values()) {
113 if (version.getText().compareToIgnoreCase(text) == 0)
114 return version;
115 }
116 throw new IllegalArgumentException("Unknown version type: " + text);
117 }
118
119 public String toString() {
120 return getText();
121 }
122
123 abstract public double getF0(double iF0);
124
125 abstract public double getF1(double iF1);
126 }
127
128 protected void setupUnnormalizedQMatrix() {
129
130 double a = alpha.getParameterValue(0);
131 double s = switchRate.getParameterValue(0);
132 double f0 = hiddenFrequencies.getParameterValue(0);
133 double f1 = hiddenFrequencies.getParameterValue(1);
134 double p0 = frequencies.getParameterValue(0);
135 double p1 = frequencies.getParameterValue(1);
136
137 assert Math.abs(1.0 - f0 - f1) < 1e-8;
138 assert Math.abs(1.0 - p0 - p1) < 1e-8;
139
140 f0 = version.getF0(f0);
141 f1 = version.getF1(f1);
142
143 unnormalizedQ[0][1] = a * p1;
144 unnormalizedQ[0][2] = s * f0;
145 unnormalizedQ[0][3] = 0.0;
146
147 unnormalizedQ[1][0] = a * p0;
148 unnormalizedQ[1][2] = 0.0;
149 unnormalizedQ[1][3] = s * f0;
150
151 unnormalizedQ[2][0] = s * f1;
152 unnormalizedQ[2][1] = 0.0;
153 unnormalizedQ[2][3] = p1;
154
155 unnormalizedQ[3][0] = 0.0;
156 unnormalizedQ[3][1] = s * f1;
157 unnormalizedQ[3][2] = p0;
158 }
159
160 protected void frequenciesChanged() {
161 }
162
163 protected void ratesChanged() {
164 }
165
166 public String toString() {
167
168 return SubstitutionModelUtils.toString(unnormalizedQ, dataType, 2);
169 }
170
171 /**
172 * Normalize rate matrix to one expected substitution per unit time
173 *
174 * @param matrix the matrix to normalize to one expected substitution
175 * @param pi the equilibrium distribution of states
176 */
177 void normalize(double[][] matrix, double[] pi) {
178
179 double subst = 0.0;
180 int dimension = pi.length;
181
182 for (int i = 0; i < dimension; i++) {
183 subst += -matrix[i][i] * pi[i];
184 }
185
186 // normalize, including switches
187 for (int i = 0; i < dimension; i++) {
188 for (int j = 0; j < dimension; j++) {
189 matrix[i][j] = matrix[i][j] / subst;
190 }
191 }
192
193 double switchingProportion = 0.0;
194 switchingProportion += matrix[0][2] * pi[2];
195 switchingProportion += matrix[2][0] * pi[0];
196 switchingProportion += matrix[1][3] * pi[3];
197 switchingProportion += matrix[3][1] * pi[1];
198
199 //System.out.println("switchingProportion=" + switchingProportion);
200
201 // normalize, removing switches
202 for (int i = 0; i < dimension; i++) {
203 for (int j = 0; j < dimension; j++) {
204 matrix[i][j] = matrix[i][j] / (1.0 - switchingProportion);
205 }
206 }
207 }
208
209 private Parameter alpha;
210 private Parameter switchRate;
211 private Parameter frequencies;
212 private Parameter hiddenFrequencies;
213 private final Version version;
214
215 }