Debian Codegraph @

src/dr/math/distributions/TDistribution.java

raw symbols SCIP
1 /*
2 * TDistribution.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.GammaFunction;
29 import dr.math.UnivariateFunction;
30
31 /**
32 * @author Marc A. Suchard
33 */
34 public class TDistribution implements Distribution {
35
36 public TDistribution(double center, double scale, double df) {
37 this.center = center;
38 this.scale = scale;
39 this.df = df;
40 }
41
42 public double pdf(double x) {
43 return Math.exp(logPDF(x, center, scale, df));
44 }
45
46 public double logPdf(double x) {
47 return logPDF(x, center, scale, df);
48 }
49
50 public double cdf(double x) {
51 throw new RuntimeException("Not yet implemented");
52 }
53
54 public double quantile(double y) {
55 throw new RuntimeException("Not yet implemented");
56 }
57
58 public double mean() {
59 if (df > 1)
60 return center;
61 return Double.NaN;
62 }
63
64 public double variance() {
65 if (df > 2)
66 return scale * df / (df - 2);
67 return Double.NaN;
68 }
69
70 public UnivariateFunction getProbabilityDensityFunction() {
71 throw new RuntimeException("Not yet implemented");
72 }
73
74 /*
75 * t-distribution
76 *
77 * \frac{ \Gamma(\frac{\nu+1}{2}) }{ \sqrt{\nu \pi} \Gamma(\frac{\nu}{2}) } *
78 * \left(
79 * 1 + \frac{(x-x_0)^2}{\nu}
80 * \right)^{ -\frac{\nu+1}{2} }
81 *
82 * Variance = \frac{\nu}{\nu-2}
83 *
84 */
85
86 /* Returns the log PDF of a t-distribution
87 * @param df degrees of freedom (=1 cauchy, =\infty normal)
88 * @param x0 mean for df > 1
89 * @param scale variance = scale * df / (df - 2) for df > 2
90 */
91
92 public static double logPDF(double x, double x0, double scale, double df) {
93
94 double loc = x - x0;
95
96 double logPDF = GammaFunction.lnGamma((df + 1) / 2)
97 - 0.5 * Math.log(df)
98 - logSqrtPi
99 - 0.25 * Math.log(scale)
100 - GammaFunction.lnGamma(df / 2)
101 - (df + 1) / 2 * Math.log(1 + loc * loc / df / scale);
102
103 return logPDF;
104 }
105
106 private double df;
107 private double scale;
108 private double center;
109
110 private static double logSqrtPi = Math.log(Math.sqrt(Math.PI));
111
112 }