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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package org.apache.commons.math.distribution;
19
import java.io.Serializable;
21
import org.apache.commons.math.MathException;
22
import org.apache.commons.math.special.Gamma;
25
* The default implementation of {@link GammaDistribution}.
27
* @version $Revision: 617953 $ $Date: 2008-02-02 22:54:00 -0700 (Sat, 02 Feb 2008) $
29
public class GammaDistributionImpl extends AbstractContinuousDistribution
30
implements GammaDistribution, Serializable {
32
/** Serializable version identifier */
33
private static final long serialVersionUID = -3239549463135430361L;
35
/** The shape parameter. */
38
/** The scale parameter. */
42
* Create a new gamma distribution with the given alpha and beta values.
43
* @param alpha the shape parameter.
44
* @param beta the scale parameter.
46
public GammaDistributionImpl(double alpha, double beta) {
53
* For this disbution, X, this method returns P(X < x).
55
* The implementation of this method is based on:
58
* <a href="http://mathworld.wolfram.com/Chi-SquaredDistribution.html">
59
* Chi-Squared Distribution</a>, equation (9).</li>
60
* <li>Casella, G., & Berger, R. (1990). <i>Statistical Inference</i>.
61
* Belmont, CA: Duxbury Press.</li>
64
* @param x the value at which the CDF is evaluated.
65
* @return CDF for this distribution.
66
* @throws MathException if the cumulative probability can not be
67
* computed due to convergence or other numerical errors.
69
public double cumulativeProbability(double x) throws MathException{
75
ret = Gamma.regularizedGammaP(getAlpha(), x / getBeta());
82
* For this distribution, X, this method returns the critical point x, such
83
* that P(X < x) = <code>p</code>.
85
* Returns 0 for p=0 and <code>Double.POSITIVE_INFINITY</code> for p=1.</p>
87
* @param p the desired probability
88
* @return x, such that P(X < x) = <code>p</code>
89
* @throws MathException if the inverse cumulative probability can not be
90
* computed due to convergence or other numerical errors.
91
* @throws IllegalArgumentException if <code>p</code> is not a valid
94
public double inverseCumulativeProbability(final double p)
95
throws MathException {
100
return Double.POSITIVE_INFINITY;
102
return super.inverseCumulativeProbability(p);
106
* Modify the shape parameter, alpha.
107
* @param alpha the new shape parameter.
108
* @throws IllegalArgumentException if <code>alpha</code> is not positive.
110
public void setAlpha(double alpha) {
112
throw new IllegalArgumentException("alpha must be positive");
118
* Access the shape parameter, alpha
121
public double getAlpha() {
126
* Modify the scale parameter, beta.
127
* @param beta the new scale parameter.
128
* @throws IllegalArgumentException if <code>beta</code> is not positive.
130
public void setBeta(double beta) {
132
throw new IllegalArgumentException("beta must be positive");
138
* Access the scale parameter, beta
141
public double getBeta() {
146
* Access the domain value lower bound, based on <code>p</code>, used to
147
* bracket a CDF root. This method is used by
148
* {@link #inverseCumulativeProbability(double)} to find critical values.
150
* @param p the desired probability for the critical value
151
* @return domain value lower bound, i.e.
152
* P(X < <i>lower bound</i>) < <code>p</code>
154
protected double getDomainLowerBound(double p) {
155
// TODO: try to improve on this estimate
156
return Double.MIN_VALUE;
160
* Access the domain value upper bound, based on <code>p</code>, used to
161
* bracket a CDF root. This method is used by
162
* {@link #inverseCumulativeProbability(double)} to find critical values.
164
* @param p the desired probability for the critical value
165
* @return domain value upper bound, i.e.
166
* P(X < <i>upper bound</i>) > <code>p</code>
168
protected double getDomainUpperBound(double p) {
169
// TODO: try to improve on this estimate
170
// NOTE: gamma is skewed to the left
171
// NOTE: therefore, P(X < μ) > .5
177
ret = getAlpha() * getBeta();
180
ret = Double.MAX_VALUE;
187
* Access the initial domain value, based on <code>p</code>, used to
188
* bracket a CDF root. This method is used by
189
* {@link #inverseCumulativeProbability(double)} to find critical values.
191
* @param p the desired probability for the critical value
192
* @return initial domain value
194
protected double getInitialDomain(double p) {
195
// TODO: try to improve on this estimate
196
// Gamma is skewed to the left, therefore, P(X < μ) > .5
202
ret = getAlpha() * getBeta() * .5;
205
ret = getAlpha() * getBeta();