| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.inference.markovjumps; |
| 27 |
|
| 28 |
import java.util.Arrays; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 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 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 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 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public void computeJointStatMarkovJumps(double[] evec, |
| 168 |
double[] ievc, |
| 169 |
double[] eval, |
| 170 |
double[] rateReg, |
| 171 |
double time, |
| 172 |
double[] countMatrix) { |
| 173 |
|
| 174 |
populateAuxInt(eval,time,auxInt); |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
matrixMultiply(rateReg, evec, stateCount, tmp1); |
| 179 |
|
| 180 |
|
| 181 |
matrixMultiply(ievc, tmp1, stateCount, tmp2); |
| 182 |
for (int i = 0; i < stateCount2; i++) { |
| 183 |
tmp2[i] *= auxInt[i]; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
matrixMultiply(tmp2, ievc, stateCount, tmp1); |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 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 |
|
| 203 |
populateAuxInt(eval,time,auxInt); |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
for (int i = 0; i < stateCount2; i++) { |
| 208 |
tmp2[i] = auxInt[i] * ievcRateRegEvc[i]; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
matrixMultiply(tmp2, ievc, stateCount, tmp1); |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
matrixMultiply(evec, tmp1, stateCount, countMatrix); |
| 219 |
} |
| 220 |
|
| 221 |
|
| 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 |
} |