Debian Codegraph @

src/dr/inference/markovjumps/MarkovJumpsCore.java

raw symbols SCIP
1 /*
2 * MarkovJumpsCore.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.inference.markovjumps;
27
28 import java.util.Arrays;
29
30 /**
31 * @author Marc Suchard
32 * @author Vladimir Minin
33 *
34 * A base class for implementing Markov chain-induced counting processes (markovjumps) in BEAST using BEAGLE
35 * This work is supported by NSF grant 0856099
36 *
37 * Minin VN and Suchard MA (2008) Counting labeled transitions in continous-time Markov models of evolution.
38 * Journal of Mathematical Biology, 56, 391-412.
39 *
40 */
41
42 public class MarkovJumpsCore {
43
44 public MarkovJumpsCore(int stateCount) {
45 this.stateCount = stateCount;
46 this.stateCount2 = stateCount * stateCount;
47 auxInt = new double[stateCount2];
48 tmp1 = new double[stateCount2];
49 tmp2 = new double[stateCount2];
50 expEvalScalar = new double[stateCount];
51 }
52
53 //SEXP aux_mat1(SEXP x, SEXP y){
54 // int i, j, nx;
55 // double *vec, scalar;
56 // SEXP ans;
57 //
58 // nx = length(x);
59 // x = coerceVector(x, REALSXP);
60 // y = coerceVector(y, REALSXP);
61 //
62 // vec = REAL(x);
63 // scalar = REAL(y)[0];
64 //
65 //
66 // PROTECT(ans = allocMatrix(REALSXP, nx, nx));
67 //
68 // for(i = 0; i < nx; i++) {
69 // for(j = 0; j < nx; j++){
70 // if ((vec[i]-vec[j]<10e-7) && (vec[i]-vec[j]>-10e-7)){
71 // REAL(ans)[i + nx*j] = exp(vec[j]*scalar)*scalar;
72 // }else{
73 // REAL(ans)[i + nx*j] = (exp(vec[i]*scalar) - exp(vec[j]*scalar))/(vec[i] - vec[j]);
74 // }
75 // }
76 // }
77 //
78 // UNPROTECT(1);
79 // return(ans);
80 //}
81
82 private void populateAuxInt(double[] eval, double scalar, double[] auxInt) {
83
84 for (int i = 0; i < stateCount; i++) {
85 expEvalScalar[i] = Math.exp(eval[i] * scalar);
86 }
87
88 int index = 0;
89 for (int i = 0; i < stateCount; i++) {
90 for (int j = 0; j < stateCount; j++) {
91 if (Math.abs(eval[i] - eval[j]) < 1E-7) {
92 auxInt[index] = expEvalScalar[i] * scalar;
93 } else {
94 auxInt[index] = (expEvalScalar[i] -
95 expEvalScalar[j]) /
96 (eval[i] - eval[j]);
97 }
98 index++;
99 }
100 }
101 }
102
103 public void computeCondStatMarkovJumps(double[] evec,
104 double[] ievc,
105 double[] eval,
106 double[] rateReg,
107 double time,
108 double[] transitionProbs,
109 double[] countMatrix) {
110 computeJointStatMarkovJumps(evec, ievc, eval, rateReg, time, countMatrix);
111 for(int i=0; i<stateCount2; i++) {
112 countMatrix[i] /= transitionProbs[i];
113 }
114 }
115
116 public void computeCondStatMarkovJumpsPrecompute(double[] evec,
117 double[] ievc,
118 double[] eval,
119 double[] ievcRateRegEvc,
120 double time,
121 double[] transitionProbs,
122 double[] countMatrix) {
123 computeJointStatMarkovJumpsPrecompute(evec, ievc, eval, ievcRateRegEvc, time, countMatrix);
124 for(int i=0; i<stateCount2; i++) {
125 countMatrix[i] /= transitionProbs[i];
126 }
127 }
128
129 //joint.mean.markov.jumps = function(rate.eigen, regist.matrix, interval.len){
130 //
131 // if (!("eigen" %in% class(rate.eigen)))
132 // stop("Error: object \"rate.eigen\" is not of class \"eigen\"")
133 //
134 // if (!("revmc" %in% class(rate.eigen)))
135 // stop("Error: object \"rate.eigen\" is not of class \"revmc\"")
136 //
137 // if (!is.matrix(regist.matrix) || (nrow(regist.matrix) != ncol(regist.matrix)))
138 // stop("\"regist.matrix\" must be a square matrix")
139 //
140 // if (nrow(rate.eigen$rate.matrix) != nrow(regist.matrix))
141 // stop("dimensions of rate and register matrices do not match")
142 //
143 // if (prod((regist.matrix == 1) + (regist.matrix == 0)) == 0)
144 // stop("all entries of \"regist.matrix\" must be either 0 or 1")
145 //
146 // space.size = dim(regist.matrix)[1]
147 //
148 // zero.matrix = matrix(0,space.size,space.size)
149 // factorial.moments = zero.matrix
150 // rate.reg = rate.eigen$rate.matrix*regist.matrix
151 //
152 // if (-sum(rate.eigen$stationary*diag(rate.eigen$rate.matrix))*interval.len < 0.001){## if time interval is very small do first order Taylor expansion
153 // factorial.moments = rate.reg*interval.len +
154 // (rate.eigen$rate.matrix%*%rate.reg + rate.reg%*%rate.eigen$rate.matrix)*(interval.len)^2/2 +
155 // rate.eigen$rate.matrix%*%rate.reg%*%rate.eigen$rate.matrix*(interval.len)^3/6
156 // }else{
157 // int.matrix = .Call("aux_mat1", rate.eigen$values, interval.len)
158 //
159 // factorial.moments = rate.eigen$vectors%*%
160 // (int.matrix*(rate.eigen$invvectors%*%rate.reg%*%rate.eigen$vectors))%*%
161 // rate.eigen$invvectors
162 // }
163 //
164 // return(factorial.moments)
165 //}
166
167 public void computeJointStatMarkovJumps(double[] evec,
168 double[] ievc,
169 double[] eval,
170 double[] rateReg,
171 double time,
172 double[] countMatrix) {
173 // Equation (37) from Minin and Suchard
174 populateAuxInt(eval,time,auxInt);
175
176 // Equation (36) from Minin and Suchard
177 // Take rate.reg%*%rate.eigen$vectors
178 matrixMultiply(rateReg, evec, stateCount, tmp1);
179
180 // Take int.matrix*(rate.eigen$invvectors%*%rate.reg%*%rate.eigen$vectors)
181 matrixMultiply(ievc, tmp1, stateCount, tmp2);
182 for (int i = 0; i < stateCount2; i++) {
183 tmp2[i] *= auxInt[i];
184 }
185
186 // Take (int.matrix*(rate.eigen$invvectors%*%rate.reg%*%rate.eigen$vectors))%*%
187 // rate.eigen$invvectors
188 matrixMultiply(tmp2, ievc, stateCount, tmp1);
189
190 // Take factorial.moments = rate.eigen$vectors%*%
191 // (int.matrix*(rate.eigen$invvectors%*%rate.reg%*%rate.eigen$vectors))%*%
192 // rate.eigen$invvectors
193 matrixMultiply(evec, tmp1, stateCount, countMatrix);
194 }
195
196 public void computeJointStatMarkovJumpsPrecompute(double[] evec,
197 double[] ievc,
198 double[] eval,
199 double[] ievcRateRegEvc,
200 double time,
201 double[] countMatrix) {
202 // Equation (37) from Minin and Suchard
203 populateAuxInt(eval,time,auxInt);
204
205 // Equation (36) from Minin and Suchard
206 // Take int.matrix*(rate.eigen$invvectors%*%rate.reg%*%rate.eigen$vectors)
207 for (int i = 0; i < stateCount2; i++) {
208 tmp2[i] = auxInt[i] * ievcRateRegEvc[i];
209 }
210
211 // Take (int.matrix*(rate.eigen$invvectors%*%rate.reg%*%rate.eigen$vectors))%*%
212 // rate.eigen$invvectors
213 matrixMultiply(tmp2, ievc, stateCount, tmp1);
214
215 // Take factorial.moments = rate.eigen$vectors%*%
216 // (int.matrix*(rate.eigen$invvectors%*%rate.reg%*%rate.eigen$vectors))%*%
217 // rate.eigen$invvectors
218 matrixMultiply(evec, tmp1, stateCount, countMatrix);
219 }
220
221 // Computes C = A %*% B for square matrices A and B
222 public static void matrixMultiply(final double[] A,
223 final double[] B,
224 final int dim,
225 final double[] C) {
226 int index = 0;
227 for (int i = 0; i < dim; i++) {
228 for (int j = 0; j < dim; j++) {
229 C[index] = 0;
230 for (int k = 0; k < dim; k++) {
231 C[index] += A[i * dim + k] * B[k * dim + j];
232 }
233 index++;
234 }
235 }
236 }
237
238 public static void fillRegistrationMatrix(double[] matrix, int dim) {
239 Arrays.fill(matrix,1.0);
240 for(int i=0; i<dim; i++) {
241 matrix[i*dim + i] = 0;
242 }
243 }
244
245 public static void fillRegistrationMatrix(double[] matrix, int from, int to, int dim) {
246 fillRegistrationMatrix(matrix,from,to,dim,1.0);
247 }
248
249 public static void fillRegistrationMatrix(double[] matrix, int from, int to, int dim, double value) {
250 Arrays.fill(matrix,0.0);
251 matrix[from*dim + to] = value;
252 }
253
254 public static void swapRows(double[] matrix, int swap1, int swap2, int dim) {
255 for(int i=0; i<dim; i++) {
256 double tmp = matrix[swap1 * dim + i];
257 matrix[swap1 * dim + i] = matrix[swap2 * dim + i];
258 matrix[swap2 * dim + i] = tmp;
259 }
260 }
261
262 public static void swapCols(double[] matrix, int swap1, int swap2, int dim) {
263 for(int i=0; i<dim; i++) {
264 double tmp = matrix[i * dim + swap1];
265 matrix[i * dim + swap1] = matrix[i * dim + swap2];
266 matrix[i * dim + swap2] = tmp;
267 }
268 }
269
270 public static void makeComparableToRPackage(double[] matrix) {
271 if (matrix.length == 16) {
272 swapRows(matrix,1,2,4);
273 swapCols(matrix,1,2,4);
274 } else if (matrix.length == 4) {
275 swapCols(matrix,1,2,1);
276 } else {
277 throw new RuntimeException("Function constructed for nucleotides");
278 }
279 }
280
281 private int stateCount;
282 private int stateCount2;
283 private double[] auxInt;
284 private double[] tmp1;
285 private double[] tmp2;
286 private double[] expEvalScalar;
287 }