| 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.distributions.GammaDistribution; |
| 34 |
import dr.math.distributions.GeneralizedIntegerGammaDistribution; |
| 35 |
import dr.math.matrixAlgebra.Vector; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public class TwoStateOccupancyMarkovReward implements MarkovReward { |
| 51 |
|
| 52 |
|
| 53 |
private static final boolean DEBUG = true; |
| 54 |
private static final boolean DEBUG2 = false; |
| 55 |
|
| 56 |
public TwoStateOccupancyMarkovReward(double[] Q) { |
| 57 |
this(Q, 1E-10); |
| 58 |
} |
| 59 |
|
| 60 |
public TwoStateOccupancyMarkovReward(double[] Q, double epsilon) { |
| 61 |
this.Q = Q; |
| 62 |
this.maxTime = 0; |
| 63 |
this.epsilon = epsilon; |
| 64 |
|
| 65 |
this.maxK = 10; |
| 66 |
|
| 67 |
eigenSystem = new DefaultEigenSystem(2); |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
} |
| 73 |
|
| 74 |
private int idx(int i, int j) { |
| 75 |
return i * 2 + j; |
| 76 |
} |
| 77 |
|
| 78 |
private double[] jumpProbabilities = null; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
private void computeCDForJumpProbabilities(double lambda1, double lambda2, double[][] C, double[][] D) { |
| 106 |
|
| 107 |
if (lambda1 == lambda2) return; |
| 108 |
|
| 109 |
for (int k = 0; k < maxK / 2; ++k) { |
| 110 |
final double l1l2k = Math.pow(lambda1 * lambda2, k); |
| 111 |
|
| 112 |
for (int i = 0; i <= k + 1; ++i) { |
| 113 |
final double sign = Math.pow(-1, i); |
| 114 |
|
| 115 |
if (k == 0 && i == 0) { |
| 116 |
C[k][i] = 1.0; |
| 117 |
} else { |
| 118 |
C[k][i] = sign * l1l2k * Binomial.choose(k + i - 1, i) |
| 119 |
/ Math.pow(lambda2 - lambda1, k + i); |
| 120 |
} |
| 121 |
D[k][i] = sign * l1l2k * Binomial.choose(k + i, i) |
| 122 |
/ Math.pow(lambda1 - lambda2, k + i + 1); |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
private void computeJumpProbabilities(double lambda1, double lambda2, double time, |
| 143 |
final double[][] C, final double[][] D, double[] jumpProbabilities) { |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
final double expLambda1Time = Math.exp(-lambda1 * time); |
| 148 |
final double expLambda2Time = Math.exp(-lambda2 * time); |
| 149 |
|
| 150 |
if (lambda1 == lambda2) { |
| 151 |
for (int m = 1; m < maxK / 2 + 1; ++m) { |
| 152 |
final int k = 2 * m; |
| 153 |
jumpProbabilities[k] = expLambda1Time * Math.pow(lambda1 * time, k) |
| 154 |
/ Math.exp(GammaFunction.lnGamma(k + 1)); |
| 155 |
} |
| 156 |
} else { |
| 157 |
|
| 158 |
for (int k = 1; k < maxK / 2; ++k) { |
| 159 |
double sum = 0.0; |
| 160 |
double multiplicativeFactor = 1.0; |
| 161 |
for (int i = 1; i <= k + 1; ++i) { |
| 162 |
|
| 163 |
if (i > 1) { |
| 164 |
multiplicativeFactor *= time / (i - 1); |
| 165 |
} |
| 166 |
|
| 167 |
sum += C[k][k - i + 1] |
| 168 |
* multiplicativeFactor |
| 169 |
* expLambda1Time; |
| 170 |
|
| 171 |
if (i <= k) { |
| 172 |
sum += D[k][k - i] |
| 173 |
* multiplicativeFactor |
| 174 |
* expLambda2Time; |
| 175 |
} |
| 176 |
} |
| 177 |
jumpProbabilities[2 * k] = sum; |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
public double[][] getC() { return C; } |
| 183 |
|
| 184 |
public double[][] getD() { return D; } |
| 185 |
|
| 186 |
public double[] getJumpProbabilities() { return jumpProbabilities; } |
| 187 |
|
| 188 |
public double computeCdf(double x, double time, int i, int j) { |
| 189 |
throw new RuntimeException("Not yet implemented"); |
| 190 |
} |
| 191 |
|
| 192 |
public double computePdf(double x, double time, int i, int j) { |
| 193 |
|
| 194 |
if (i != 0 || j != 0) throw new RuntimeException("Not yet implemented"); |
| 195 |
|
| 196 |
final double lambda0 = -Q[idx(0,0)]; |
| 197 |
final double lambda1 = -Q[idx(1,1)]; |
| 198 |
|
| 199 |
final boolean symmetric = (lambda0 == lambda1); |
| 200 |
|
| 201 |
if (!symmetric && C == null) { |
| 202 |
C = new double[(maxK / 2) + 1][(maxK / 2) + 1]; |
| 203 |
D = new double[(maxK / 2) + 1][(maxK / 2) + 1]; |
| 204 |
computeCDForJumpProbabilities(lambda0, lambda1, C, D); |
| 205 |
} |
| 206 |
|
| 207 |
if (jumpProbabilities == null) { |
| 208 |
jumpProbabilities = new double[maxK + 1]; |
| 209 |
|
| 210 |
} |
| 211 |
computeJumpProbabilities(lambda0, lambda1, time, C, D, jumpProbabilities); |
| 212 |
|
| 213 |
|
| 214 |
if (symmetric) { |
| 215 |
|
| 216 |
final double scale = 1.0 / lambda0; |
| 217 |
double sum = 0.0; |
| 218 |
|
| 219 |
|
| 220 |
for (int m = 1; m <= maxK / 2; ++m) { |
| 221 |
final int k = 2 * m; |
| 222 |
sum += jumpProbabilities[k] * |
| 223 |
Math.exp( |
| 224 |
+ GammaDistribution.logPdf(x, m, scale) |
| 225 |
+ GammaDistribution.logPdf(time - x, m + 1, scale) |
| 226 |
- GammaDistribution.logPdf(time, k + 1, scale) |
| 227 |
); |
| 228 |
} |
| 229 |
return sum; |
| 230 |
} else { |
| 231 |
|
| 232 |
double sum = 0.0; |
| 233 |
for (int m = 1; m <= maxK / 2; ++m) { |
| 234 |
final int k = 2 * m; |
| 235 |
sum += jumpProbabilities[k] * |
| 236 |
GammaDistribution.pdf(x, m, 1.0 / lambda1) * |
| 237 |
GammaDistribution.pdf(time - x, m + 1, 1.0 / lambda0) / |
| 238 |
GeneralizedIntegerGammaDistribution.pdf(time, m, m + 1, lambda1, lambda0); |
| 239 |
} |
| 240 |
|
| 241 |
return sum; |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
public double[] computePdf(double x, double time) { |
| 246 |
throw new RuntimeException("Not yet implemented"); |
| 247 |
} |
| 248 |
|
| 249 |
private double[][] squareMatrix(final double[] mat) { |
| 250 |
final int dim = 2; |
| 251 |
double[][] rtn = new double[dim][dim]; |
| 252 |
for (int i = 0; i < dim; ++i) { |
| 253 |
for (int j = 0; j < dim; ++j) { |
| 254 |
rtn[i][j] = mat[idx(i, j)]; |
| 255 |
} |
| 256 |
} |
| 257 |
return rtn; |
| 258 |
} |
| 259 |
|
| 260 |
private int determineNumberOfSteps(double time, double lambda) { |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
int i = -1; |
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
final double tolerance2 = |
| 279 |
|
| 280 |
(1.0 - epsilon); |
| 281 |
double sum2 = 0.0; |
| 282 |
|
| 283 |
while (Math.abs(sum2 - tolerance2) > epsilon && sum2 < 1.0) { |
| 284 |
|
| 285 |
i++; |
| 286 |
double logDensity = -lambda * time + i * (Math.log(lambda) + Math.log(time)) - GammaFunction.lnGamma(i + 1); |
| 287 |
sum2 += Math.exp(logDensity); |
| 288 |
|
| 289 |
if (DEBUG2) { |
| 290 |
System.err.println(sum2 + " " + tolerance2 + " " + Math.abs(sum2 - tolerance2) + " " + epsilon * 0.01); |
| 291 |
|
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
return i; |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
public String toString() { |
| 344 |
StringBuilder sb = new StringBuilder(); |
| 345 |
sb.append("Q: " + new Vector(Q) + "\n"); |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
return sb.toString(); |
| 352 |
} |
| 353 |
|
| 354 |
private EigenDecomposition getEigenDecomposition() { |
| 355 |
if (eigenDecomposition == null) { |
| 356 |
eigenDecomposition = eigenSystem.decomposeMatrix(squareMatrix(Q)); |
| 357 |
} |
| 358 |
return eigenDecomposition; |
| 359 |
} |
| 360 |
|
| 361 |
private EigenDecomposition eigenDecomposition; |
| 362 |
|
| 363 |
public double[] computeConditionalProbabilities(double distance) { |
| 364 |
|
| 365 |
double[] matrix = new double[4]; |
| 366 |
eigenSystem.computeExponential(getEigenDecomposition(), distance, matrix); |
| 367 |
|
| 368 |
return matrix; |
| 369 |
} |
| 370 |
|
| 371 |
public double computeConditionalProbability(double distance, int i, int j) { |
| 372 |
return eigenSystem.computeExponential(getEigenDecomposition(), distance, i, j); |
| 373 |
} |
| 374 |
|
| 375 |
private final double[] Q; |
| 376 |
private final int maxK; |
| 377 |
|
| 378 |
private final double epsilon; |
| 379 |
|
| 380 |
private final EigenSystem eigenSystem; |
| 381 |
|
| 382 |
private double maxTime; |
| 383 |
|
| 384 |
private double[][] C = null; |
| 385 |
private double[][] D = null; |
| 386 |
} |