Debian Codegraph @

src/dr/inference/markovjumps/TwoStateOccupancyMarkovReward.java

raw symbols SCIP
1 /*
2 * TwoStateOccupancyMarkovReward.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 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 * @author Marc Suchard
39 * @author Christophe Fraiser
40 * @author Andrew Rambaut
41 *
42 *
43 * https://en.wikipedia.org/wiki/Generalized_integer_gamma_distribution
44 *
45 */
46
47
48 // This class only works for 2-state models with equal rates, starting and ending in state 0
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; // TODO How to determine?
66
67 eigenSystem = new DefaultEigenSystem(2);
68
69 // if (Q[idx(0, 0)] != Q[idx(1, 1)]) {
70 // throw new IllegalArgumentException("Only currently implemented for equal rates models");
71 // }
72 }
73
74 private int idx(int i, int j) {
75 return i * 2 + j; // row-major
76 }
77
78 private double[] jumpProbabilities = null;
79
80 // private double getAki(double lambda1, double lambda2, int k, int i) {
81 //// double logA = Binomial.logChoose(k + i - 1, i) +
82 //// k * Math.log(lambda1) + (k - 1) * Math.log(lambda2)
83 //// int sign = 1;
84 //// return sign * Math.exp(logA);
85 // return Binomial.choose(k + i - 1, i) * Math.pow(-1, i) * Math.pow(lambda1, k)
86 // * Math.pow(lambda2, k - 1) / Math.pow(lambda2 - lambda1, k + i);
87 // }
88 //
89 // private double getBki(double lambda1, double lambda2, int k, int i) {
90 // return Binomial.choose(k + i - 1, i) * Math.pow(-1, i) * Math.pow(lambda1, k)
91 // * Math.pow(lambda2, k - 1) / Math.pow(lambda1 - lambda2, k + i);
92 // }
93 //
94 // private double getCki(double lambda1, double lambda2, int k, int i) {
95 // return Binomial.choose(k + i - 1, i) * Math.pow(-1, i) * Math.pow(lambda1, k)
96 // * Math.pow(lambda2, k) / Math.pow(lambda2 - lambda1, k + i);
97 // }
98 //
99 // private double getDki(double lambda1, double lambda2, int k, int i) {
100 // return Binomial.choose(k + i, i) * Math.pow(-1, i) * Math.pow(lambda1, k)
101 // * Math.pow(lambda2, k) / Math.pow(lambda1 - lambda2, k + i + 1);
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 // C[k][i] = C1(lambda1, lambda2, k, i);
125 // D[k][i] = D1(lambda1, lambda2, k, i);
126 }
127 }
128 }
129
130 // private static double C1(double lambda1, double lambda2, int k, int index) {
131 // if (k == 0 && index == 0) return 1.0;
132 //
133 // return Math.pow(-1, index) * Math.pow(lambda1 * lambda2, k) * Binomial.choose(k + index - 1, index) /
134 // Math.pow(lambda2 - lambda1, k + index);
135 // }
136 //
137 // private static double D1(double lambda1, double lambda2, int k, int index) {
138 // return Math.pow(-1, index) * Math.pow(lambda1 * lambda2, k) * Binomial.choose(k + index, index) /
139 // Math.pow(lambda1 - lambda2, k + index + 1);
140 // }
141
142 private void computeJumpProbabilities(double lambda1, double lambda2, double time,
143 final double[][] C, final double[][] D, double[] jumpProbabilities) {
144
145 // jumpProbabilities = new double[maxK];
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 // computeJumpProbabilities(lambda0, lambda1, time, C, D, jumpProbabilities); // Error: probs are function of time
210 }
211 computeJumpProbabilities(lambda0, lambda1, time, C, D, jumpProbabilities); // are function of time.
212 // TODO Could cache computeJumpProbabilities(key = time) in HashMap
213
214 if (symmetric) {
215 // Single rate (symmetric)
216 final double scale = 1.0 / lambda0;
217 double sum = 0.0;
218
219 // if time - x > 0, then there must have been at least k = 2 jumps
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 // Two rate model
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); // TODO Cache
239 }
240 // TODO Remove code duplication in if (symmetric) { } else { }
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 // final double tolerance = (1.0 - epsilon) / Math.exp(-lambda * time);
262 // final double logTolerance = Math.log(1.0 - epsilon);
263
264 int i = -1;
265 // double sum = 0.0;
266 // int factorialI = 1;
267 //
268 // while (sum < tolerance) {
269 // i++;
270 // sum += Math.pow(lambda * time, i) / factorialI;
271 // factorialI *= (i + 1); // + 1 because used on next iterate
272 // }
273 //
274 // int firstN = i;
275 //
276 // LogTricks.logSum()
277 // i = -1;
278 final double tolerance2 =
279 // -epsilon;
280 (1.0 - epsilon);
281 double sum2 = 0.0;
282 // double sum2 = Double.NEGATIVE_INFINITY;
283 while (Math.abs(sum2 - tolerance2) > epsilon && sum2 < 1.0) {
284 // while (sum2 < tolerance2) {
285 i++;
286 double logDensity = -lambda * time + i * (Math.log(lambda) + Math.log(time)) - GammaFunction.lnGamma(i + 1);
287 sum2 += Math.exp(logDensity);
288 // sum2 = LogTricks.logSum(sum2, logDensity);
289 if (DEBUG2) {
290 System.err.println(sum2 + " " + tolerance2 + " " + Math.abs(sum2 - tolerance2) + " " + epsilon * 0.01);
291 // if (i > 500) System.exit(-1);
292 }
293 }
294
295 // System.err.println("First: " + firstN);
296 // System.err.println("Second:" + i);
297 // System.exit(-1);
298
299 return i;
300 }
301
302 // private static int getKMax(int n) {
303 // if (n % 2 == 0) {
304 // return n / 2;
305 // } else {
306 // return (n + 1) / 2;
307 // }
308 // }
309
310 // private static void hypgeoF11_k_2k(final double x, double[] result, double[] work) {
311 // final int len = result.length;
312 // if (len == 1 || x <= 0.0) return;
313 //
314 // final int nmax = len - 1;
315 // final int kmax = getKMax(nmax);
316 // final double y = x / 2.0;
317 // final double lx = Math.log(0.25 * x);
318 // // TODO finish
319 //
320 //
321 //
322 // }
323
324 // private static void pclt(double t, double lambda1, double lambda2, double[] result) {
325 //
326 // final int len = result.length;
327 // final int nmax = len - 1;
328 // final int kmax = getKMax(nmax);
329 //
330 // double[] work = new double[kmax + 1];
331 //
332 // final double x = t * (lambda2 - lambda1);
333 // final double abs_x = Math.abs(x);
334 // final double log_l1 = Math.log(lambda1), log_l2 = Math.log(lambda2), log_t = Math.log(t);
335 // final double max_rate = Math.max(lambda1, lambda2);
336 // double log_gamma = 0.0;
337 //
338 // hypgeoF11_k_2k(abs_x, result, work);
339 // // TODO finish
340 //
341 // }
342
343 public String toString() {
344 StringBuilder sb = new StringBuilder();
345 sb.append("Q: " + new Vector(Q) + "\n");
346 // sb.append("r: " + new Vector(r) + "\n");
347 // sb.append("lambda: " + lambda + "\n");
348 // sb.append("N: " + getNfromC() + "\n");
349 // sb.append("maxTime: " + maxTime + "\n");
350 // sb.append("cprob at maxTime: " + new Vector(computeConditionalProbabilities(maxTime)) + "\n");
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 }