KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JSci > maths > statistics > TDistribution


1 package JSci.maths.statistics;
2
3 import JSci.maths.SpecialMath;
4
5 /**
6 * The TDistribution class provides an object for encapsulating student's t-distributions.
7 * @version 1.0
8 * @author Jaco van Kooten
9 */

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("The degrees of freedom must be greater than zero.");
21                 dgrFreedom=r;
22                 logPdfFreedom=-SpecialMath.logBeta(0.5*dgrFreedom,0.5)-0.5*Math.log(dgrFreedom);
23         }
24         /**
25         * Returns the degrees of freedom.
26         */

27         public int getDegreesOfFreedom() {
28                 return dgrFreedom;
29         }
30         /**
31         * Probability density function of a student's t-distribution.
32         * @return the probability that a stochastic variable x has the value X, i.e. P(x=X).
33         */

34         public double probability(double X) {
35                 double logPdf=logPdfFreedom;
36                 logPdf-=(0.5*(dgrFreedom+1))*Math.log(1.0+(X*X)/dgrFreedom);
37                 return Math.exp(logPdf);
38         }
39         /**
40         * Cumulative student's t-distribution function.
41     * @return the probability that a stochastic variable x is less then X, i.e. P(x&lt;X).
42         */

43         public double cumulative(double X) {
44                 double A=0.5*SpecialMath.incompleteBeta((dgrFreedom)/(dgrFreedom+X*X),0.5*dgrFreedom,0.5);
45                 return X>0 ? 1-A : A;
46         }
47         /**
48     * Inverse of the cumulative student's t-distribution function.
49         * @return the value X for which P(x&lt;X).
50         */

51         public double inverse(double probability) {
52                 checkRange(probability);
53                 if(probability==0.0)
54                         return -Double.MAX_VALUE;
55                 if(probability==1.0)
56                         return Double.MAX_VALUE;
57                 if(probability==0.5)
58                         return 0.0;
59                 return findRoot(probability, 0.0, -0.5*Double.MAX_VALUE, 0.5*Double.MAX_VALUE);
60         }
61 }
62
63
Popular Tags