Debian Codegraph @

src/dr/evomodel/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.evomodel.substmodel;
27
28 import dr.evomodelxml.substmodel.BinaryCovarionModelParser;
29 import dr.evolution.datatype.TwoStateCovarion;
30 import dr.oldevomodel.substmodel.SubstitutionModelUtils;
31 import dr.inference.model.Parameter;
32
33 /**
34 * @author Alexei Drummond
35 * @author Marc A. Suchard
36 * <p/>
37 * a the rate of the slow rate class
38 * 1 the rate of the fast rate class
39 * p0 the equilibrium frequency of zero states
40 * p1 1 - p0, the equilibrium frequency of one states
41 * f0 the equilibrium frequency of slow rate class
42 * f1 1 - f0, the equilibrium frequency of fast rate class
43 * s the rate of switching
44 * <p/>
45 * then the (unnormalized) instantaneous rate matrix (unnormalized Q) should be
46 * <p/>
47 * [ -(a*p1)-s , a*p1 , s , 0 ]
48 * [ a*p0 , -(a*p0)-s , 0 , s ]
49 * [ s , 0 , -p1-s , p1 ]
50 * [ 0 , s , p0 , -p0-s ]
51 */
52 public class BinaryCovarionModel extends AbstractCovarionModel {
53
54 /**
55 * @param dataType the data type
56 * @param frequencies the frequencies of the visible states
57 * @param hiddenFrequencies the frequencies of the hidden rates
58 * @param alphaParameter the rate of evolution in slow mode
59 * @param switchingParameter the rate of flipping between slow and fast modes
60 */
61 public BinaryCovarionModel(TwoStateCovarion dataType,
62 Parameter frequencies,
63 Parameter hiddenFrequencies,
64 Parameter alphaParameter,
65 Parameter switchingParameter,
66 dr.oldevomodel.substmodel.BinaryCovarionModel.Version version) {
67
68 super(BinaryCovarionModelParser.COVARION_MODEL, dataType, frequencies, hiddenFrequencies);
69
70 this.alpha = alphaParameter;
71 this.switchRate = switchingParameter;
72 this.frequencies = frequencies;
73 this.hiddenFrequencies = hiddenFrequencies;
74 this.version = version;
75
76 addVariable(alpha);
77 addVariable(switchRate);
78 addVariable(frequencies);
79 addVariable(hiddenFrequencies);
80 }
81
82 protected void setupQMatrix(double[] rates, double[] pi, double[][] unnormalizedQ) {
83
84 double a = alpha.getParameterValue(0);
85 double s = switchRate.getParameterValue(0);
86 double f0 = hiddenFrequencies.getParameterValue(0);
87 double f1 = hiddenFrequencies.getParameterValue(1);
88 double p0 = frequencies.getParameterValue(0); // TODO Use pi[0], ..., pi[3]
89 double p1 = frequencies.getParameterValue(1);
90
91 assert Math.abs(1.0 - f0 - f1) < 1e-8;
92 assert Math.abs(1.0 - p0 - p1) < 1e-8;
93
94 f0 = version.getF0(f0);
95 f1 = version.getF1(f1);
96
97 unnormalizedQ[0][1] = a * p1;
98 unnormalizedQ[0][2] = s * f0;
99 unnormalizedQ[0][3] = 0.0;
100
101 unnormalizedQ[1][0] = a * p0;
102 unnormalizedQ[1][2] = 0.0;
103 unnormalizedQ[1][3] = s * f0;
104
105 unnormalizedQ[2][0] = s * f1;
106 unnormalizedQ[2][1] = 0.0;
107 unnormalizedQ[2][3] = p1;
108
109 unnormalizedQ[3][0] = 0.0;
110 unnormalizedQ[3][1] = s * f1;
111 unnormalizedQ[3][2] = p0;
112 }
113
114 protected void frequenciesChanged() {
115 }
116
117 protected void ratesChanged() {
118 }
119
120 public String toString() {
121
122 final int stateCount = dataType.getStateCount();
123 double[] tmp = new double[stateCount * stateCount];
124 getInfinitesimalMatrix(tmp);
125 double[][] q = new double[stateCount][stateCount];
126 for (int i = 0; i < stateCount; ++i) {
127 for (int j = 0; j < stateCount; ++j) {
128 q[i][j] = tmp[i * stateCount + j];
129 }
130 }
131 setupQMatrix(null, null, q);
132 return SubstitutionModelUtils.toString(q, dataType, 2);
133 }
134
135 /**
136 * Normalize rate matrix to one expected substitution per unit time
137 *
138 * @param matrix the matrix to normalize to one expected substitution
139 * @param pi the equilibrium distribution of states
140 */
141 protected double getNormalizationValue(double[][] matrix, double[] pi) {
142
143 double subst = 0.0;
144 int dimension = pi.length;
145
146 for (int i = 0; i < dimension; i++) {
147 subst += -matrix[i][i] * pi[i];
148 }
149
150 // normalize, including switches
151 for (int i = 0; i < dimension; i++) {
152 for (int j = 0; j < dimension; j++) {
153 matrix[i][j] = matrix[i][j] / subst;
154 }
155 }
156
157 double switchingProportion = 0.0;
158 switchingProportion += matrix[0][2] * pi[2];
159 switchingProportion += matrix[2][0] * pi[0];
160 switchingProportion += matrix[1][3] * pi[3];
161 switchingProportion += matrix[3][1] * pi[1];
162
163 // normalize, removing switches
164 for (int i = 0; i < dimension; i++) {
165 for (int j = 0; j < dimension; j++) {
166 matrix[i][j] = matrix[i][j] / (1.0 - switchingProportion);
167 }
168 }
169 return 1.0; // Already normalized
170 }
171
172 private Parameter alpha;
173 private Parameter switchRate;
174 private Parameter frequencies;
175 private Parameter hiddenFrequencies;
176 final private dr.oldevomodel.substmodel.BinaryCovarionModel.Version version;
177
178 }