Debian Codegraph @

src/dr/math/distributions/BifractionalDiffusionDensity.java

raw symbols SCIP
1 /*
2 * BifractionalDiffusionDensity.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.math.distributions;
27
28 import dr.math.UnivariateFunction;
29 import cern.jet.stat.Gamma;
30
31
32 /**
33 * @author Marc Suchard
34 * <p/>
35 * **
36 * Most of the following functions come from:
37 * <p/>
38 * R Gorenflo, A Iskenderov and Y Luchko (2000) Mapping between solutions of fractional diffusion-wave equations.
39 * Fractional Calculus and Applied Analysis, 3, 75 - 86
40 * <p/>
41 * Also see:
42 * F. Mainardi and G. Pagnini (2003) The Wright functions as solutions of the time-fractional diffusion equation.
43 * Applied Mathematics and Computation, 141, pages?
44 *
45 *
46 * Need to read: http://www.hindawi.com/journals/ijde/2010/104505.html and http://www1.tfh-berlin.de/~luchko/papers/Luchko_111.pdf
47 */
48
49 public class BifractionalDiffusionDensity implements Distribution {
50
51 public BifractionalDiffusionDensity(double v, double alpha, double beta) {
52 this.v = v;
53 this.alpha = alpha;
54 this.beta = beta;
55 coefficients = constructBifractionalDiffusionCoefficients(alpha, beta);
56 }
57
58 public BifractionalDiffusionDensity(double alpha, double beta) {
59 this(1.0, alpha, beta);
60 }
61
62 /**
63 * probability density function of the distribution
64 *
65 * @param x argument
66 * @return pdf value
67 */
68 public double pdf(double x) {
69 return pdf(x, v);
70 }
71
72 public double pdf(double x, double v) {
73 return pdf(x, v, alpha, beta, coefficients);
74 }
75
76 /**
77 * the natural log of the probability density function of the distribution
78 *
79 * @param x argument
80 * @return log pdf value
81 */
82 public double logPdf(double x) {
83 return logPdf(x, v, alpha, beta, coefficients);
84 }
85
86 /**
87 * cumulative density function of the distribution
88 *
89 * @param x argument
90 * @return cdf value
91 */
92 public double cdf(double x) {
93 throw new RuntimeException("Not yet implemented");
94 }
95
96 /**
97 * quantile (inverse cumulative density function) of the distribution
98 *
99 * @param y argument
100 * @return icdf value
101 */
102 public double quantile(double y) {
103 throw new RuntimeException("Not yet implemented");
104 }
105
106 /**
107 * mean of the distribution
108 *
109 * @return mean
110 */
111 public double mean() {
112 throw new RuntimeException("Not yet implemented");
113 }
114
115 /**
116 * variance of the distribution
117 *
118 * @return variance
119 */
120 public double variance() {
121 throw new RuntimeException("Not yet implemented");
122 }
123
124 /**
125 * @return a probability density function representing this distribution
126 */
127 public UnivariateFunction getProbabilityDensityFunction() {
128 throw new RuntimeException("Not yet implemented");
129 }
130
131 public static double logPdf(double x, double v, double alpha, double beta) {
132 return Math.log(pdf(x, v, alpha, beta));
133 }
134
135 public static double logPdf(double x, double v, double alpha, double beta, double[][][] coefficients) {
136 return Math.log(pdf(x, v, alpha, beta, coefficients));
137 }
138
139 // /*
140 // * Taken from: Saichev AI and Zaslavsky GM (1997) Fractional kinetic equations: solutions and applications.
141 // * Chaos, 7, 753-764
142 // * @param x evaluation point
143 // * @param t evaluation time
144 // * @param alpha coefficient
145 // * @param beta coefficient
146 // * @return probability density
147 // */
148 //
149 // public static double logPdf(double x, double t, double alpha, double beta) {
150 // final double mu = beta / alpha;
151 // final double absX = Math.abs(x);
152 // final double absY = absX / Math.pow(t,mu);
153 // double density = 0;
154 // double incr = Double.MAX_VALUE;
155 // int m = 1; // 0th term = 0 \propto cos(pi/2)
156 // int sign = -1;
157 //
158 // while (Math.abs(incr) > eps && m < maxK) {
159 // incr = sign / Math.pow(absY, m * alpha)
160 // * Gamma.gamma(m * alpha + 1)
161 // / Gamma.gamma(m * beta + 1)
162 // * Math.cos(halfPI * (m * alpha + 1));
163 // density += incr;
164 // sign *= -1;
165 // m++;
166 // }
167 //
168 // return Math.log(density / (Math.PI * absX));
169 // }
170
171
172 static class SignedDouble {
173 double x;
174 boolean positive;
175
176 SignedDouble(double x, boolean positive) {
177 this.x = x;
178 this.positive = positive;
179 }
180 }
181
182 public static SignedDouble logGamma(double z) {
183
184 // To extend the gamma function to negative (non-integer) numbers, apply the relationship
185 // \Gamma(z) = \frac{ \Gamma(z+n) }{ z(z+1)\cdots(z+n-1),
186 // by choosing n such that z+n is positive
187 if (z > 0) {
188 return new SignedDouble(Gamma.logGamma(z), true);
189 }
190 int n = ((int) -z);
191 if (z + n == 0.0) {
192 return new SignedDouble(Double.NaN, true); // Zero and the negative integers are diverging poles
193 }
194 n++;
195 boolean positive = (n % 2 == 0);
196 return new SignedDouble(Gamma.logGamma(z + n) - Gamma.logGamma(-z + 1) + Gamma.logGamma(-z - n + 1), positive);
197 }
198
199
200 public static double gamma(double z) {
201
202 // To extend the gamma function to negative (non-integer) numbers, apply the relationship
203 // \Gamma(z) = \frac{ \Gamma(z+n) }{ z(z+1)\cdots(z+n-1),
204 // by choosing n such that z+n is positive
205 if (z > 0.0) {
206 return Gamma.gamma(z);
207 }
208 int n = ((int) -z);
209 if (z+n == 0.0) {
210 return Double.NaN; // Zero and the negative integers are diverging poles
211 }
212 n++;
213 boolean positive = (n % 2 == 0);
214 double result = Gamma.gamma(z + n) / Gamma.gamma(-z + 1) * Gamma.gamma(-z - n + 1);
215 if (!positive) result *= -1;
216 return result;
217 }
218
219
220 // The following comments out functions may be faster than using the generalized Wright function.
221 // Keep for comparison
222 //
223 // public static double infiniteSumAlphaGreaterThanBeta1(double z, double alpha, double beta) {
224 // double sum = 0;
225 // int k = 0;
226 // boolean isPositive = true;
227 //
228 // double incr = Double.MAX_VALUE;
229 // while (//(Math.abs(incr) > 1E-20) &&
230 // (k < maxK)) {
231 //
232 // double x1, x2, x3;
233 // x1 = gamma(0.5 - 0.5 * alpha - 0.5 * alpha * k);
234 // if (!Double.isNaN(x1)) {
235 // incr = x1;
236 // } else {
237 // System.err.println("Big problem!");
238 // System.exit(-1);
239 // }
240 // x2 = gamma(1.0 - beta - beta * k);
241 // if (!Double.isNaN(x2)) {
242 // incr /= x2;
243 // } else {
244 // incr = 0;
245 // }
246 // x3 = gamma(0.5 * alpha + 0.5 * alpha * k);
247 // if (!Double.isNaN(x3)) {
248 // incr /= x3;
249 // } else {
250 // incr = 0;
251 // }
252 // incr *= Math.pow(z, k);
253 // if (isPositive) {
254 // sum += incr;
255 // } else {
256 // sum -= incr;
257 // }
258 // isPositive = !isPositive;
259 // k++;
260 // }
261 // return sum;
262 // }
263 //
264 // public static double infiniteSumAlphaGreaterThanBeta2(double z, double alpha, double beta) {
265 // double sum = 0;
266 // int m = 0;
267 // boolean isPositive = true;
268 //
269 // double incr = Double.MAX_VALUE;
270 // while (// (Math.abs(incr) > 1E-20) &&
271 // (m < maxK)) {
272 //
273 // double x1, x2, x3, x4;
274 //
275 // x1 = gamma(1.0 / alpha + 2.0 / alpha * m);
276 // if (!Double.isNaN(x1)) {
277 // incr = x1;
278 // } else {
279 // System.err.println("Big problem!");
280 // System.exit(-1);
281 // }
282 // x2 = gamma(1.0 - 1.0 / alpha - 2.0 / alpha * m);
283 // if (!Double.isNaN(x2)) {
284 // incr *= x2;
285 // } else {
286 // System.err.println("Big problem!");
287 // System.exit(-1);
288 // }
289 // x3 = gamma(0.5 + m);
290 // if (!Double.isNaN(x3)) {
291 // incr /= x3;
292 // } else {
293 // incr = 0;
294 // }
295 // x4 = gamma(1.0 - beta / alpha - 2 * beta / alpha * m);
296 // if (!Double.isNaN(x4)) {
297 // incr /= x4;
298 // } else {
299 // incr = 0;
300 // }
301 // incr /= gamma(m + 1);
302 // incr *= Math.pow(z, m);
303 // if (isPositive) {
304 // sum += incr;
305 // } else {
306 // sum -= incr;
307 // }
308 // isPositive = !isPositive;
309 // m++;
310 //
311 // }
312 // return sum;
313 // }
314
315 /*
316 * Taken from Equation (17) in Gorenflo, Iskenderov and Luchko (2000)
317 */
318 private static double evaluateGreensFunctionAtZero(double t, double alpha, double beta) {
319 if (beta == 1) {
320 final double oneOverAlpha = 1.0 / alpha;
321 return gamma(oneOverAlpha) / (Math.PI * alpha * Math.pow(t, oneOverAlpha));
322 } else {
323 final double betaOverAlpha = beta / alpha;
324 return 1.0 / (alpha * Math.pow(t, betaOverAlpha) * Math.sin(Math.PI / alpha) * gamma(1.0 - betaOverAlpha));
325 }
326 }
327
328 /*
329 * Taken from Equation (20) in Gorenflo, Iskenderov and Luchko (2000)
330 */
331 private static double evaluateGreensFunctionAlphaEqualsBeta(double x, double t, double alpha) {
332
333 final double absX = Math.abs(x);
334 final double twoAlpha = 2.0 * alpha;
335 final double tPowAlpha = Math.pow(t, alpha);
336 final double piHalfAlpha = 0.5 * Math.PI * alpha;
337
338 double green = Math.pow(absX, alpha - 1.0) * tPowAlpha * Math.sin(piHalfAlpha) /
339 (Math.pow(t, twoAlpha) + 2 * Math.pow(absX, alpha) * tPowAlpha * Math.cos(piHalfAlpha)
340 + Math.pow(absX, twoAlpha));
341 return oneOverPi * green;
342 }
343
344
345 /*
346 * Taken from Equation (18) in Gorenflo, Iskenderov and Luchko (2000)
347 */
348 private static double evaluateGreensFunctionBetaGreaterThanAlpha(double x, double t, double alpha, double beta,
349 double[][][] coefficients) {
350 double z = Math.pow(2.0, alpha) * Math.pow(t, beta) / Math.pow(Math.abs(x), alpha);
351 return oneOverSqrtPi / Math.sqrt(Math.abs(x)) * generalizedWrightFunction(-z, coefficients[0], coefficients[1]);
352 }
353
354
355 /*
356 * Taken from Equation (19) in Gorenflo, Iskenderov and Luchko (2000)
357 */
358 private static double evaluateGreensFunctionAlphaGreaterThanBeta(double x, double t, double alpha, double beta,
359 double[][][] coefficients) {
360
361 double z1 = Math.pow(Math.abs(x),alpha) / (Math.pow(2,alpha) * Math.pow(t, beta));
362 double z2 = x * x / (4.0 * Math.pow(t, 2 * beta / alpha));
363
364 double green1 = oneOverSqrtPi * Math.pow(Math.abs(x), alpha - 1.0) /
365 (Math.pow(2,alpha) * Math.pow(t, beta)) *
366 generalizedWrightFunction(-z1, coefficients[2], coefficients[3]);
367
368 double green2 = oneOverSqrtPi / (alpha * Math.pow(t, beta/alpha)) *
369 generalizedWrightFunction(-z2, coefficients[4], coefficients[5]);
370
371 return green1 + green2;
372 }
373
374 public static double pdf(double x, double v, double alpha, double beta) {
375 return pdf(x, v, alpha, beta, null);
376 }
377
378 public static double pdf(double x, double v, double alpha, double beta, double[][][] coefficients) {
379 final double t = 0.5 * v;
380 if (x == 0) {
381 return evaluateGreensFunctionAtZero(t, alpha, beta);
382 }
383 if (alpha == beta) {
384 return evaluateGreensFunctionAlphaEqualsBeta(x, t, alpha);
385 }
386 if (coefficients == null) {
387 coefficients = constructBifractionalDiffusionCoefficients(alpha, beta);
388 }
389 if (alpha > beta) {
390 return evaluateGreensFunctionAlphaGreaterThanBeta(x, t, alpha, beta, coefficients);
391 } else {
392 return evaluateGreensFunctionBetaGreaterThanAlpha(x, t, alpha, beta, coefficients);
393 }
394 }
395
396 /*
397 * Helper function to construct coefficients for generalized Wright function
398 */
399 public static double[][][] constructBifractionalDiffusionCoefficients(double alpha, double beta) {
400 double[][][] coefficients = new double[6][][];
401
402 // coefficients[0:1][][] : Greens function beta > alpha
403 coefficients[0] = new double[][]{{0.5, alpha / 2.0}, {1.0, 1.0}};
404 coefficients[1] = new double[][]{{1.0, beta}, {0.0, -alpha / 2.0}};
405
406 // coefficients[2:3][][] : Greens function #1 alpha > beta
407 coefficients[2] = new double[][]{{0.5 - alpha / 2.0, -alpha / 2.0}, {1.0, 1.0}};
408 coefficients[3] = new double[][]{{1.0 - beta, -beta}, {alpha / 2.0, alpha / 2.0}};
409
410 // coefficients[4:5][][] : Greens function #2 alpha > beta
411 coefficients[4] = new double[][]{{1.0 / alpha, 2.0 / alpha}, {1.0 - 1.0 / alpha, -2.0 / alpha}};
412 coefficients[5] = new double[][]{{0.5, 1.0}, {1.0 - beta / alpha, -2.0 * beta / alpha}};
413
414 return coefficients;
415 }
416
417 public static double generalizedWrightFunction(double z, double[][] aAp, double[][] bBq) {
418 final int p = aAp.length;
419 final int q = bBq.length;
420 double sum = 0.0;
421 double incr;
422 double zPowK = 1.0;
423 int k = 0;
424
425 while (// incr > eps &&
426 k < maxK) {
427 incr = 1;
428 for (int i = 0; i < p; i++) {
429 final double[] aAi = aAp[i];
430
431 double x = gamma(aAi[0] + aAi[1] * k); // TODO Precompute these factors
432 if (!Double.isNaN(x)) {
433 incr *= x;
434 } else {
435 incr = Double.NaN;
436 }
437 }
438 for (int i = 0; i < q; i++) {
439 final double[] bBi = bBq[i];
440 double x = gamma(bBi[0] + bBi[1] * k); // TODO Precompute these factors
441 if (!Double.isNaN(x)) {
442 incr /= x;
443 } else {
444 incr = 0.0;
445 }
446 }
447 incr /= gamma(k+1); // k! TODO Precompute these factors
448 incr *= zPowK;
449 sum += incr;
450
451 // Get ready for next loop
452 zPowK *= z;
453 k++;
454 }
455 return sum;
456 }
457
458 public static void main(String[] arg) {
459
460 double alpha = 2.0;
461 double beta = 0.8;
462 double z1 = -2.34;
463 SignedDouble result = logGamma(z1);
464 System.err.println("logGamma("+z1+") = "+result.x+" "+(result.positive ? "(+)" : "(-)"));
465 System.err.println("gamma("+z1+") = "+ gamma(z1));
466 System.err.println("gamma(-2.0) = "+gamma(-2.0));
467 System.err.println("");
468
469 double var = 4.0;
470 double t = 0.5 * var;
471 double x = 1.0;
472
473 double[][][] coefficients = constructBifractionalDiffusionCoefficients(alpha, beta);
474 System.err.println("p(x = "+x+", v = "+var+") = " + evaluateGreensFunctionAlphaGreaterThanBeta(x, t, alpha,
475 beta, coefficients));
476
477
478 alpha = 0.7;
479 beta = 1.4;
480 coefficients = constructBifractionalDiffusionCoefficients(alpha, beta);
481 // System.err.println("p(x = "+x+", v = "+var+") = " + evaluateGreensFunctionBetaGreaterThanAlpha(x, t, alpha, beta));
482 System.err.println("p(x = "+x+", v = "+var+") = " + evaluateGreensFunctionBetaGreaterThanAlpha(x, t, alpha,
483 beta, coefficients));
484
485
486
487 }
488
489 private static double oneOverSqrtPi = 1.0 / Math.sqrt(Math.PI);
490 private static double oneOverPi = 1.0 / Math.PI;
491 public static final int maxK = 50;
492
493 private double alpha;
494 private double beta;
495 private double v;
496 private double[][][] coefficients;
497 }