1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.math.distribution;
18
19 import java.io.Serializable;
20
21 import org.apache.commons.math.MathException;
22 import org.apache.commons.math.special.Beta;
23
24 /**
25 * Default implementation of
26 * {@link org.apache.commons.math.distribution.TDistribution}.
27 *
28 * @version $Revision: 617953 $ $Date: 2008-02-02 22:54:00 -0700 (Sat, 02 Feb 2008) $
29 */
30 public class TDistributionImpl
31 extends AbstractContinuousDistribution
32 implements TDistribution, Serializable {
33
34 /** Serializable version identifier */
35 private static final long serialVersionUID = -5852615386664158222L;
36
37 /** The degrees of freedom*/
38 private double degreesOfFreedom;
39
40 /**
41 * Create a t distribution using the given degrees of freedom.
42 * @param degreesOfFreedom the degrees of freedom.
43 */
44 public TDistributionImpl(double degreesOfFreedom) {
45 super();
46 setDegreesOfFreedom(degreesOfFreedom);
47 }
48
49 /**
50 * Modify the degrees of freedom.
51 * @param degreesOfFreedom the new degrees of freedom.
52 */
53 public void setDegreesOfFreedom(double degreesOfFreedom) {
54 if (degreesOfFreedom <= 0.0) {
55 throw new IllegalArgumentException("degrees of freedom must be positive.");
56 }
57 this.degreesOfFreedom = degreesOfFreedom;
58 }
59
60 /**
61 * Access the degrees of freedom.
62 * @return the degrees of freedom.
63 */
64 public double getDegreesOfFreedom() {
65 return degreesOfFreedom;
66 }
67
68 /**
69 * For this disbution, X, this method returns P(X < <code>x</code>).
70 * @param x the value at which the CDF is evaluated.
71 * @return CDF evaluted at <code>x</code>.
72 * @throws MathException if the cumulative probability can not be
73 * computed due to convergence or other numerical errors.
74 */
75 public double cumulativeProbability(double x) throws MathException{
76 double ret;
77 if (x == 0.0) {
78 ret = 0.5;
79 } else {
80 double t =
81 Beta.regularizedBeta(
82 getDegreesOfFreedom() / (getDegreesOfFreedom() + (x * x)),
83 0.5 * getDegreesOfFreedom(),
84 0.5);
85 if (x < 0.0) {
86 ret = 0.5 * t;
87 } else {
88 ret = 1.0 - 0.5 * t;
89 }
90 }
91
92 return ret;
93 }
94
95 /**
96 * For this distribution, X, this method returns the critical point x, such
97 * that P(X < x) = <code>p</code>.
98 * <p>
99 * Returns <code>Double.NEGATIVE_INFINITY</code> for p=0 and
100 * <code>Double.POSITIVE_INFINITY</code> for p=1.</p>
101 *
102 * @param p the desired probability
103 * @return x, such that P(X < x) = <code>p</code>
104 * @throws MathException if the inverse cumulative probability can not be
105 * computed due to convergence or other numerical errors.
106 * @throws IllegalArgumentException if <code>p</code> is not a valid
107 * probability.
108 */
109 public double inverseCumulativeProbability(final double p)
110 throws MathException {
111 if (p == 0) {
112 return Double.NEGATIVE_INFINITY;
113 }
114 if (p == 1) {
115 return Double.POSITIVE_INFINITY;
116 }
117 return super.inverseCumulativeProbability(p);
118 }
119
120 /**
121 * Access the domain value lower bound, based on <code>p</code>, used to
122 * bracket a CDF root. This method is used by
123 * {@link #inverseCumulativeProbability(double)} to find critical values.
124 *
125 * @param p the desired probability for the critical value
126 * @return domain value lower bound, i.e.
127 * P(X < <i>lower bound</i>) < <code>p</code>
128 */
129 protected double getDomainLowerBound(double p) {
130 return -Double.MAX_VALUE;
131 }
132
133 /**
134 * Access the domain value upper bound, based on <code>p</code>, used to
135 * bracket a CDF root. This method is used by
136 * {@link #inverseCumulativeProbability(double)} to find critical values.
137 *
138 * @param p the desired probability for the critical value
139 * @return domain value upper bound, i.e.
140 * P(X < <i>upper bound</i>) > <code>p</code>
141 */
142 protected double getDomainUpperBound(double p) {
143 return Double.MAX_VALUE;
144 }
145
146 /**
147 * Access the initial domain value, based on <code>p</code>, used to
148 * bracket a CDF root. This method is used by
149 * {@link #inverseCumulativeProbability(double)} to find critical values.
150 *
151 * @param p the desired probability for the critical value
152 * @return initial domain value
153 */
154 protected double getInitialDomain(double p) {
155 return 0.0;
156 }
157 }