| 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 dr.evomodel.substmodel.DefaultEigenSystem; |
| 29 |
import dr.evomodel.substmodel.EigenDecomposition; |
| 30 |
import dr.evomodel.substmodel.EigenSystem; |
| 31 |
import dr.math.Binomial; |
| 32 |
import dr.math.GammaFunction; |
| 33 |
import dr.math.matrixAlgebra.Matrix; |
| 34 |
import dr.math.matrixAlgebra.Vector; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public class SericolaSeriesMarkovReward implements MarkovReward { |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
private static final boolean DEBUG = false; |
| 47 |
|
| 48 |
public SericolaSeriesMarkovReward(double[] Q, double[] r, int dim) { |
| 49 |
this(Q, r, dim, 1E-10); |
| 50 |
} |
| 51 |
|
| 52 |
public SericolaSeriesMarkovReward(double[] Q, double[] r, int dim, double epsilon) { |
| 53 |
this.Q = Q; |
| 54 |
this.r = r; |
| 55 |
this.maxTime = 0; |
| 56 |
this.epsilon = epsilon; |
| 57 |
|
| 58 |
this.dim = dim; |
| 59 |
lambda = determineLambda(); |
| 60 |
|
| 61 |
phi = dim - 1; |
| 62 |
|
| 63 |
if (DEBUG) { |
| 64 |
System.err.println("lambda = " + lambda); |
| 65 |
} |
| 66 |
|
| 67 |
P = initializeP(Q, lambda); |
| 68 |
eigenSystem = new DefaultEigenSystem(dim); |
| 69 |
} |
| 70 |
|
| 71 |
private double[][] initializeW(int times, int dim) { |
| 72 |
return new double[times][dim * dim]; |
| 73 |
} |
| 74 |
|
| 75 |
private int getHfromX(double x, double time) { |
| 76 |
|
| 77 |
int h = 1; |
| 78 |
while (x >= r[h] * time) { |
| 79 |
h++; |
| 80 |
} |
| 81 |
return h; |
| 82 |
} |
| 83 |
|
| 84 |
private void growC(double time, int extraN) { |
| 85 |
int newN = getNfromC(); |
| 86 |
if (time > maxTime) { |
| 87 |
newN = determineNumberOfSteps(time, lambda) + extraN; |
| 88 |
maxTime = time; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
if (newN > getNfromC()) { |
| 93 |
if (DEBUG) { |
| 94 |
System.err.println("Growing C to N = " + newN + " with " + maxTime); |
| 95 |
} |
| 96 |
if (newN > 500) { |
| 97 |
System.err.println("Warning: > 500 recursion depth in SericolaSeriesMarkovReward"); |
| 98 |
} |
| 99 |
initializeSpace(phi, newN); |
| 100 |
computeChnk(); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
private double[][][][] internalC; |
| 107 |
|
| 108 |
private void initializeSpace(int phi, int N) { |
| 109 |
internalC = new double[phi + 1][N + 1][N + 1][dim * dim]; |
| 110 |
|
| 111 |
} |
| 112 |
|
| 113 |
private double[] C(int h, int n, int k) { |
| 114 |
return internalC[h][n][k]; |
| 115 |
} |
| 116 |
|
| 117 |
private int getNfromC() { |
| 118 |
return (internalC == null) ? -1 : internalC[0].length - 1; |
| 119 |
} |
| 120 |
|
| 121 |
private int idx(int i, int j) { |
| 122 |
return i * dim + j; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
private int[] getHfromX(double[] X, double time) { |
| 128 |
int[] H = new int[X.length]; |
| 129 |
for (int i = 0; i < X.length; ++i) { |
| 130 |
H[i] = getHfromX(X[i], time); |
| 131 |
} |
| 132 |
return H; |
| 133 |
|
| 134 |
} |
| 135 |
|
| 136 |
public double computePdf(double x, double time, int i, int j) { |
| 137 |
if (x == time) return 0.0; |
| 138 |
else return computePdf(x, time)[i * dim + j]; |
| 139 |
} |
| 140 |
|
| 141 |
public double[] computePdf(double x, double time) { |
| 142 |
return computePdf(new double[]{x}, time)[0]; |
| 143 |
} |
| 144 |
|
| 145 |
public double[][] computePdf(double[] X, double time) { |
| 146 |
int[] H = getHfromX(X, time); |
| 147 |
|
| 148 |
growC(time, 1); |
| 149 |
|
| 150 |
double[][] W = initializeW(X.length, dim); |
| 151 |
|
| 152 |
final int N = getNfromC() - 1; |
| 153 |
for (int n = 0; n <= N; ++n) { |
| 154 |
accumulatePdf(W, X, H, n, time); |
| 155 |
} |
| 156 |
|
| 157 |
if (DEBUG) { |
| 158 |
for (int i = 0; i < W.length; ++i) { |
| 159 |
System.err.println("W'[" + i + "]:\n" + new Matrix(squareMatrix(W[i]))); |
| 160 |
} |
| 161 |
System.err.println(""); |
| 162 |
} |
| 163 |
|
| 164 |
return W; |
| 165 |
} |
| 166 |
|
| 167 |
public double computeCdf(double x, double time, int i, int j) { |
| 168 |
return computeCdf(x, time)[i * dim + j]; |
| 169 |
} |
| 170 |
|
| 171 |
public double[] computeCdf(double x, double time) { |
| 172 |
return computeCdf(new double[]{x}, time)[0]; |
| 173 |
} |
| 174 |
|
| 175 |
public double[][] computeCdf(double[] X, double time) { |
| 176 |
|
| 177 |
int[] H = getHfromX(X, time); |
| 178 |
|
| 179 |
growC(time, 0); |
| 180 |
|
| 181 |
double[][] W = initializeW(X.length, dim); |
| 182 |
|
| 183 |
final int N = getNfromC(); |
| 184 |
for (int n = 0; n <= N; ++n) { |
| 185 |
accumulateCdf(W, X, H, n, time); |
| 186 |
} |
| 187 |
|
| 188 |
if (DEBUG) { |
| 189 |
for (int i = 0; i < W.length; ++i) { |
| 190 |
System.err.println("W[" + i + "]:\n" + new Matrix(squareMatrix(W[i]))); |
| 191 |
} |
| 192 |
System.err.println(""); |
| 193 |
} |
| 194 |
|
| 195 |
return W; |
| 196 |
} |
| 197 |
|
| 198 |
private double[] initializeP(double[] Q, double lambda) { |
| 199 |
double[] P = new double[dim * dim]; |
| 200 |
for (int i = 0; i < dim; ++i) { |
| 201 |
for (int j = 0; j < dim; ++j) { |
| 202 |
double identity = (i == j) ? 1.0 : 0.0; |
| 203 |
P[idx(i, j)] = identity + Q[idx(i, j)] / lambda; |
| 204 |
} |
| 205 |
} |
| 206 |
return P; |
| 207 |
} |
| 208 |
|
| 209 |
private void accumulateCdf(double[][] W, double[] X, int[] H, int n, double time) { |
| 210 |
|
| 211 |
final double premult = Math.exp( |
| 212 |
-lambda * time + n * (Math.log(lambda) + Math.log(time)) - GammaFunction.lnGamma(n + 1.0) |
| 213 |
); |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
for (int t = 0; t < X.length; ++t) { |
| 218 |
double x = X[t]; |
| 219 |
int h = H[t]; |
| 220 |
|
| 221 |
double xh = (x - r[h - 1] * time) / ((r[h] - r[h - 1]) * time); |
| 222 |
|
| 223 |
final int dim2 = dim * dim; |
| 224 |
double[] inc = new double[dim2]; |
| 225 |
for (int k = 0; k <= n; k++) { |
| 226 |
final double binomialCoef = Binomial.choose(n, k) * Math.pow(xh, k) * Math.pow(1.0 - xh, n - k); |
| 227 |
for (int uv = 0; uv < dim2; ++uv) { |
| 228 |
inc[uv] += binomialCoef * C(h, n, k)[uv]; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
for (int uv = 0; uv < dim2; ++uv) { |
| 233 |
W[t][uv] += premult * inc[uv]; |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
private void accumulatePdf(double[][] W, double[] X, int[] H, int n, double time) { |
| 239 |
|
| 240 |
final double premult = Math.exp( |
| 241 |
-lambda * time + n * (Math.log(lambda) + Math.log(time)) - GammaFunction.lnGamma(n + 1.0) |
| 242 |
); |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
for (int t = 0; t < X.length; ++t) { |
| 249 |
double x = X[t]; |
| 250 |
int h = H[t]; |
| 251 |
|
| 252 |
final double factor = lambda / (r[h] - r[h - 1]); |
| 253 |
|
| 254 |
double xh = (x - r[h - 1] * time) / ((r[h] - r[h - 1]) * time); |
| 255 |
|
| 256 |
final int dim2 = dim * dim; |
| 257 |
double[] inc = new double[dim2]; |
| 258 |
for (int k = 0; k <= n; k++) { |
| 259 |
final double binomialCoef = Binomial.choose(n, k) * Math.pow(xh, k) * Math.pow(1.0 - xh, n - k); |
| 260 |
for (int uv = 0; uv < dim2; ++uv) { |
| 261 |
inc[uv] += binomialCoef * (C(h, n + 1, k + 1)[uv] - C(h, n + 1, k)[uv]); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
for (int uv = 0; uv < dim2; ++uv) { |
| 266 |
W[t][uv] += factor * premult * inc[uv]; |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
private double relationTwelve(int h, int n, int k, int u, int v) { |
| 272 |
|
| 273 |
double c = (r[u] - r[h]) / (r[u] - r[h - 1]) * C(h, n, k - 1)[idx(u, v)]; |
| 274 |
|
| 275 |
double d = 0; |
| 276 |
for (int w = 0; w <= phi; ++w) { |
| 277 |
d += P[idx(u, w)] * C(h, n - 1, k - 1)[idx(w, v)]; |
| 278 |
} |
| 279 |
d *= (r[h] - r[h - 1]) / (r[u] - r[h - 1]); |
| 280 |
|
| 281 |
return c + d; |
| 282 |
} |
| 283 |
|
| 284 |
private double relationThirteen(int h, int n, int k, int u, int v) { |
| 285 |
|
| 286 |
double c = (r[h - 1] - r[u]) / (r[h] - r[u]) * C(h, n, k + 1)[idx(u, v)]; |
| 287 |
|
| 288 |
double d = 0; |
| 289 |
for (int w = 0; w <= phi; ++w) { |
| 290 |
d += P[idx(u, w)] * C(h, n - 1, k)[idx(w, v)]; |
| 291 |
} |
| 292 |
d *= (r[h] - r[h - 1]) / (r[h] - r[u]); |
| 293 |
|
| 294 |
return c + d; |
| 295 |
} |
| 296 |
|
| 297 |
private double[] product(double[] a, double[] b) { |
| 298 |
double[] c = new double[dim * dim]; |
| 299 |
for (int i = 0; i < dim; ++i) { |
| 300 |
for (int j = 0; j < dim; ++j) { |
| 301 |
final int ij = idx(i, j); |
| 302 |
for (int k = 0; k < dim; ++k) { |
| 303 |
c[ij] += a[idx(i, k)] * b[idx(k, j)]; |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
return c; |
| 308 |
} |
| 309 |
|
| 310 |
private void computeChnk() { |
| 311 |
|
| 312 |
double[] Pn = new double[dim * dim]; |
| 313 |
for (int u = 0; u < dim; ++u) { |
| 314 |
Pn[idx(u, u)] = 1.0; |
| 315 |
} |
| 316 |
|
| 317 |
for (int h = 1; h <= phi; ++h) { |
| 318 |
|
| 319 |
for (int u = 0; u <= h - 1; ++u) { |
| 320 |
C(h, 0, 0)[idx(u, u)] = 1.0; |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
final int N = getNfromC(); |
| 326 |
for (int n = 1; n <= N; ++n) { |
| 327 |
|
| 328 |
for (int h = 1; h <= phi; ++h) { |
| 329 |
for (int k = 1; k <= n; ++k) { |
| 330 |
|
| 331 |
for (int u = h; u <= phi; ++u) { |
| 332 |
for (int v = 0; v <= phi; ++v) { |
| 333 |
C(h, n, k)[idx(u, v)] = relationTwelve(h, n, k, u, v); |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
} |
| 338 |
for (int u = h + 1; u <= phi; ++u) { |
| 339 |
for (int v = 0; v <= phi; ++v) { |
| 340 |
C(h + 1, n, 0)[idx(u, v)] = C(h, n, n)[idx(u, v)]; |
| 341 |
} |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
Pn = product(Pn, P); |
| 346 |
for (int u = 0; u <= phi - 1; ++u) { |
| 347 |
for (int v = 0; v <= phi; ++v) { |
| 348 |
C(phi, n, n)[idx(u, v)] = Pn[idx(u, v)]; |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
for (int h = phi; h >= 1; --h) { |
| 353 |
for (int k = n - 1; k >= 0; --k) { |
| 354 |
|
| 355 |
for (int u = 0; u <= h - 1; u++) { |
| 356 |
for (int v = 0; v <= phi; ++v) { |
| 357 |
C(h, n, k)[idx(u, v)] = relationThirteen(h, n, k, u, v); |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
for (int u = 0; u <= h - 2; ++u) { |
| 362 |
for (int v = 0; v <= phi; ++v) { |
| 363 |
C(h - 1, n, n)[idx(u, v)] = C(h, n, 0)[idx(u, v)]; |
| 364 |
} |
| 365 |
} |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
private double determineLambda() { |
| 374 |
double lambda = Q[0]; |
| 375 |
for (int i = 1; i < dim; ++i) { |
| 376 |
int ii = idx(i, i); |
| 377 |
if (Q[ii] < lambda) { |
| 378 |
lambda = Q[ii]; |
| 379 |
} |
| 380 |
} |
| 381 |
return -lambda; |
| 382 |
} |
| 383 |
|
| 384 |
private double[][] squareMatrix(final double[] mat) { |
| 385 |
double[][] rtn = new double[dim][dim]; |
| 386 |
for (int i = 0; i < dim; ++i) { |
| 387 |
for (int j = 0; j < dim; ++j) { |
| 388 |
rtn[i][j] = mat[idx(i, j)]; |
| 389 |
} |
| 390 |
} |
| 391 |
return rtn; |
| 392 |
} |
| 393 |
|
| 394 |
private int determineNumberOfSteps(double time, double lambda) { |
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
int i = -1; |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
final double tolerance2 = |
| 413 |
|
| 414 |
(1.0 - epsilon); |
| 415 |
double sum2 = 0.0; |
| 416 |
|
| 417 |
while (Math.abs(sum2 - tolerance2) > epsilon && sum2 < 1.0) { |
| 418 |
|
| 419 |
i++; |
| 420 |
double logDensity = -lambda * time + i * (Math.log(lambda) + Math.log(time)) - GammaFunction.lnGamma(i + 1); |
| 421 |
sum2 += Math.exp(logDensity); |
| 422 |
|
| 423 |
if (DEBUG) { |
| 424 |
System.err.println(sum2 + " " + tolerance2 + " " + Math.abs(sum2 - tolerance2) + " " + epsilon * 0.01); |
| 425 |
|
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
return i; |
| 434 |
} |
| 435 |
|
| 436 |
public String toString() { |
| 437 |
StringBuilder sb = new StringBuilder(); |
| 438 |
sb.append("Q: " + new Vector(Q) + "\n"); |
| 439 |
sb.append("r: " + new Vector(r) + "\n"); |
| 440 |
sb.append("lambda: " + lambda + "\n"); |
| 441 |
sb.append("N: " + getNfromC() + "\n"); |
| 442 |
sb.append("maxTime: " + maxTime + "\n"); |
| 443 |
sb.append("cprob at maxTime: " + new Vector(computeConditionalProbabilities(maxTime)) + "\n"); |
| 444 |
return sb.toString(); |
| 445 |
} |
| 446 |
|
| 447 |
private EigenDecomposition getEigenDecomposition() { |
| 448 |
if (eigenDecomposition == null) { |
| 449 |
eigenDecomposition = eigenSystem.decomposeMatrix(squareMatrix(Q)); |
| 450 |
} |
| 451 |
return eigenDecomposition; |
| 452 |
} |
| 453 |
|
| 454 |
private EigenDecomposition eigenDecomposition; |
| 455 |
|
| 456 |
public double[] computeConditionalProbabilities(double distance) { |
| 457 |
|
| 458 |
double[] matrix = new double[dim * dim]; |
| 459 |
eigenSystem.computeExponential(getEigenDecomposition(), distance, matrix); |
| 460 |
|
| 461 |
return matrix; |
| 462 |
} |
| 463 |
|
| 464 |
public double computeConditionalProbability(double distance, int i, int j) { |
| 465 |
return eigenSystem.computeExponential(getEigenDecomposition(), distance, i, j); |
| 466 |
} |
| 467 |
|
| 468 |
private final double[] Q; |
| 469 |
private final double[] r; |
| 470 |
|
| 471 |
private final double lambda; |
| 472 |
private final double[] P; |
| 473 |
private final int phi; |
| 474 |
private final int dim; |
| 475 |
private final double epsilon; |
| 476 |
|
| 477 |
private final EigenSystem eigenSystem; |
| 478 |
|
| 479 |
private double maxTime; |
| 480 |
} |