| 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.math.distributions; |
| 27 |
|
| 28 |
import dr.math.GammaFunction; |
| 29 |
import dr.math.UnivariateFunction; |
| 30 |
|
| 31 |
|
| 32 |
|
| 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 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 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 |
} |