Debian Codegraph @

src/dr/evolution/colouring/ColourChangeMatrix.java

raw symbols SCIP
1 /*
2 * ColourChangeMatrix.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.evolution.colouring;
27
28 /**
29 * MigrationMatrix.java
30 *
31 * Represents a migration matrix, both backward and forward in time.
32 *
33 * @author Gerton Lunter
34 *
35 * @version 1
36 */
37
38
39 public class ColourChangeMatrix {
40
41 public ColourChangeMatrix() { }
42
43 public ColourChangeMatrix( double[] changeRates, int numColours ) {
44
45 // For now, assume two colours
46 if (numColours != 2) {
47 throw new IllegalArgumentException("Only 2 colours supported");
48 }
49
50 this.numColours = numColours;
51 this.bwMatrix = new double[numColours][numColours];
52 this.equilibrium = new double[numColours];
53
54 // Populate matrix
55 bwMatrix[0][1] = changeRates[0];
56 bwMatrix[1][0] = changeRates[1];
57 calculateExitRates();
58
59 // Calculate equilibrium distribution
60 calculateEquilibrium();
61
62 }
63
64 /*
65 * populate diagonal of bw migration matrix
66 */
67 private void calculateExitRates() {
68
69 for (int i=0; i<numColours; i++) {
70 double exitRate = 0.0;
71 for (int j=0; j<numColours; j++) {
72 if (i!=j) {
73 exitRate += bwMatrix[i][j];
74 }
75 }
76 bwMatrix[i][i] = -exitRate;
77 }
78
79 }
80
81 private void calculateEquilibrium() {
82
83 if (numColours == 2) {
84
85 double r01 = bwMatrix[0][1];
86 double r10 = bwMatrix[1][0];
87 equilibrium[0] = r10/(r01+r10);
88 equilibrium[1] = r01/(r01+r10);
89
90 } else {
91
92 throw new Error("Only 2 colours supported");
93
94 }
95 }
96
97
98 /*
99 * Returns backward migration rate from i (child) to j (parent)
100 *
101 */
102 public double getBackwardRate(int i, int j) {
103 return bwMatrix[i][j];
104 }
105
106 /*
107 * Returns forward migration rate from (parent) i to (child) j
108 */
109 public double getForwardRate(int i, int j) {
110 return equilibrium[j]*bwMatrix[j][i]/equilibrium[i];
111 }
112
113 /**
114 * The probability of ending in state y after time t, conditional on starting in state x,
115 * according to the forward evolution matrix.
116 *
117 * @param x starting state
118 * @param y end state
119 * @param t elapsed time
120 * @return probability of starting from state x and ending in state y after time t.
121 */
122 public double forwardTimeEvolution(int x, int y, double t) {
123
124 if (t<0) {
125 throw new IllegalArgumentException("Cannot go backwards in time: t="+t);
126 }
127
128 double m[] = {getForwardRate(0,1), getForwardRate(1,0)};
129 double mt = m[0]+m[1];
130
131 if (y==x) {
132 return (m[x]*Math.exp(-mt*t) + m[1-x])/mt;
133 } else {
134 return m[x]*(1.0-Math.exp(-mt*t))/mt;
135 }
136 }
137
138
139 /**
140 * The probability of ending in state y after time t, conditional on starting in state x,
141 * according to the backward evolution matrix.
142 *
143 * @param x starting state (child)
144 * @param y end state (parent)
145 * @param t elapsed time (backwards in time)
146 * @return probability of starting from state x and ending in state y after time t.
147 */
148 public double backwardTimeEvolution(int x, int y, double t) {
149
150 if (t<0) {
151 throw new IllegalArgumentException("Cannot go backwards in time: t="+t);
152 }
153
154 double m[] = {getBackwardRate(0,1), getBackwardRate(1,0)};
155 double mt = m[0]+m[1];
156
157 if (y==x) {
158 // return (m[1-y]*Math.exp(-mt*t) + m[y])/mt;
159 return (m[x]*Math.exp(-mt*t) + m[1-x])/mt;
160 } else {
161 // return m[y]*(1.0-Math.exp(-mt*t))/mt;
162 return m[x]*(1.0-Math.exp(-mt*t))/mt;
163 }
164 }
165
166
167 /**
168 *
169 * @return equilibrium distribution
170 */
171 public double[] getEquilibrium() {
172
173 return equilibrium.clone();
174
175 }
176
177
178 public double getEquilibrium(int i) {
179
180 return equilibrium[i];
181
182 }
183
184
185 public static void main(String[] args) {
186
187 double[] pars = {1.0, 0.5};
188 ColourChangeMatrix mm = new ColourChangeMatrix( pars, 2 );
189
190 System.out.println( "BW 0->0, matrix:"+mm.getBackwardRate(0,0));
191 System.out.println( "BW 0->1, matrix:"+mm.getBackwardRate(0,1));
192 System.out.println( "BW 1->0, matrix:"+mm.getBackwardRate(1,0));
193 System.out.println( "BW 1->1, matrix:"+mm.getBackwardRate(1,1));
194 System.out.println( "FW 0->0, matrix:"+mm.getForwardRate(0,0));
195 System.out.println( "FW 0->1, matrix:"+mm.getForwardRate(0,1));
196 System.out.println( "FW 1->0, matrix:"+mm.getForwardRate(1,0));
197 System.out.println( "FW 1->1, matrix:"+mm.getForwardRate(1,1));
198 System.out.println( "equilibrium="+mm.equilibrium[0]+","+mm.equilibrium[1] );
199 System.out.println( "BW 0->0, t=1:"+mm.backwardTimeEvolution(0,0,1.0));
200 System.out.println( "BW 0->1, t=1:"+mm.backwardTimeEvolution(0,1,1.0));
201 System.out.println( "BW 1->0, t=1:"+mm.backwardTimeEvolution(1,0,1.0));
202 System.out.println( "BW 1->1, t=1:"+mm.backwardTimeEvolution(1,1,1.0));
203 System.out.println( "FW 0->0, t=1:"+mm.forwardTimeEvolution(0,0,1.0));
204 System.out.println( "FW 0->1, t=1:"+mm.forwardTimeEvolution(0,1,1.0));
205 System.out.println( "FW 1->0, t=1:"+mm.forwardTimeEvolution(1,0,1.0));
206 System.out.println( "FW 1->1, t=1:"+mm.forwardTimeEvolution(1,1,1.0));
207
208 System.out.println( "BW 0->0, t=infty:"+mm.backwardTimeEvolution(0,0,1000.0));
209 System.out.println( "BW 0->1, t=infty:"+mm.backwardTimeEvolution(0,1,1000.0));
210 System.out.println( "BW 1->0, t=infty:"+mm.backwardTimeEvolution(1,0,1000.0));
211 System.out.println( "BW 1->1, t=infty:"+mm.backwardTimeEvolution(1,1,1000.0));
212 System.out.println( "FW 0->0, t=infty:"+mm.forwardTimeEvolution(0,0,1000.0));
213 System.out.println( "FW 0->1, t=infty:"+mm.forwardTimeEvolution(0,1,1000.0));
214 System.out.println( "FW 1->0, t=infty:"+mm.forwardTimeEvolution(1,0,1000.0));
215 System.out.println( "FW 1->1, t=infty:"+mm.forwardTimeEvolution(1,1,1000.0));
216
217
218 }
219
220 // private stuff
221 private int numColours;
222 private double[][] bwMatrix;
223 private double[] equilibrium;
224 }
225
226