01: package JSci.maths.statistics;
02:
03: import JSci.maths.SpecialMath;
04:
05: /**
06: * The TDistribution class provides an object for encapsulating student's t-distributions.
07: * @version 1.0
08: * @author Jaco van Kooten
09: */
10: public final class TDistribution extends ProbabilityDistribution {
11: private int dgrFreedom;
12: private double logPdfFreedom;
13:
14: /**
15: * Constructor for student's t-distribution.
16: * @param r degrees of freedom.
17: */
18: public TDistribution(int r) {
19: if (r <= 0)
20: throw new OutOfRangeException(
21: "The degrees of freedom must be greater than zero.");
22: dgrFreedom = r;
23: logPdfFreedom = -SpecialMath.logBeta(0.5 * dgrFreedom, 0.5)
24: - 0.5 * Math.log(dgrFreedom);
25: }
26:
27: /**
28: * Returns the degrees of freedom.
29: */
30: public int getDegreesOfFreedom() {
31: return dgrFreedom;
32: }
33:
34: /**
35: * Probability density function of a student's t-distribution.
36: * @return the probability that a stochastic variable x has the value X, i.e. P(x=X).
37: */
38: public double probability(double X) {
39: double logPdf = logPdfFreedom;
40: logPdf -= (0.5 * (dgrFreedom + 1))
41: * Math.log(1.0 + (X * X) / dgrFreedom);
42: return Math.exp(logPdf);
43: }
44:
45: /**
46: * Cumulative student's t-distribution function.
47: * @return the probability that a stochastic variable x is less then X, i.e. P(x<X).
48: */
49: public double cumulative(double X) {
50: double A = 0.5 * SpecialMath.incompleteBeta((dgrFreedom)
51: / (dgrFreedom + X * X), 0.5 * dgrFreedom, 0.5);
52: return X > 0 ? 1 - A : A;
53: }
54:
55: /**
56: * Inverse of the cumulative student's t-distribution function.
57: * @return the value X for which P(x<X).
58: */
59: public double inverse(double probability) {
60: checkRange(probability);
61: if (probability == 0.0)
62: return -Double.MAX_VALUE;
63: if (probability == 1.0)
64: return Double.MAX_VALUE;
65: if (probability == 0.5)
66: return 0.0;
67: return findRoot(probability, 0.0, -0.5 * Double.MAX_VALUE,
68: 0.5 * Double.MAX_VALUE);
69: }
70: }
|