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.Beta;
23
import org.apache.commons.math.util.MathUtils;
26
* The default implementation of {@link PascalDistribution}.
27
* @version $Revision: 617953 $ $Date: 2008-02-02 22:54:00 -0700 (Sat, 02 Feb 2008) $
30
public class PascalDistributionImpl extends AbstractIntegerDistribution
31
implements PascalDistribution, Serializable {
33
/** Serializable version identifier */
34
private static final long serialVersionUID = 6751309484392813623L;
36
/** The number of successes */
37
private int numberOfSuccesses;
39
/** The probability of success */
40
private double probabilityOfSuccess;
43
* Create a binomial distribution with the given number of trials and
44
* probability of success.
45
* @param r the number of successes
46
* @param p the probability of success
48
public PascalDistributionImpl(int r, double p) {
50
setNumberOfSuccesses(r);
51
setProbabilityOfSuccess(p);
55
* Access the number of successes for this distribution.
56
* @return the number of successes
58
public int getNumberOfSuccesses() {
59
return numberOfSuccesses;
63
* Access the probability of success for this distribution.
64
* @return the probability of success
66
public double getProbabilityOfSuccess() {
67
return probabilityOfSuccess;
71
* Change the number of successes for this distribution.
72
* @param successes the new number of successes
73
* @throws IllegalArgumentException if <code>successes</code> is not
76
public void setNumberOfSuccesses(int successes) {
78
throw new IllegalArgumentException(
79
"number of successes must be non-negative.");
81
numberOfSuccesses = successes;
85
* Change the probability of success for this distribution.
86
* @param p the new probability of success
87
* @throws IllegalArgumentException if <code>p</code> is not a valid
90
public void setProbabilityOfSuccess(double p) {
91
if (p < 0.0 || p > 1.0) {
92
throw new IllegalArgumentException(
93
"probability of success must be between 0.0 and 1.0, inclusive.");
95
probabilityOfSuccess = p;
99
* Access the domain value lower bound, based on <code>p</code>, used to
100
* bracket a PDF root.
101
* @param p the desired probability for the critical value
102
* @return domain value lower bound, i.e. P(X < <i>lower bound</i>) <
105
protected int getDomainLowerBound(double p) {
110
* Access the domain value upper bound, based on <code>p</code>, used to
111
* bracket a PDF root.
112
* @param p the desired probability for the critical value
113
* @return domain value upper bound, i.e. P(X < <i>upper bound</i>) >
116
protected int getDomainUpperBound(double p) {
117
// use MAX - 1 because MAX causes loop
118
return Integer.MAX_VALUE - 1;
122
* For this distribution, X, this method returns P(X ≤ x).
123
* @param x the value at which the PDF is evaluated
124
* @return PDF for this distribution
125
* @throws MathException if the cumulative probability can not be computed
126
* due to convergence or other numerical errors
128
public double cumulativeProbability(int x) throws MathException {
133
ret = Beta.regularizedBeta(getProbabilityOfSuccess(),
134
getNumberOfSuccesses(), x + 1);
140
* For this distribution, X, this method returns P(X = x).
141
* @param x the value at which the PMF is evaluated
142
* @return PMF for this distribution
144
public double probability(int x) {
149
ret = MathUtils.binomialCoefficientDouble(x +
150
getNumberOfSuccesses() - 1, getNumberOfSuccesses() - 1) *
151
Math.pow(getProbabilityOfSuccess(), getNumberOfSuccesses()) *
152
Math.pow(1.0 - getProbabilityOfSuccess(), x);
158
* For this distribution, X, this method returns the largest x, such that
159
* P(X ≤ x) ≤ <code>p</code>.
161
* Returns <code>-1</code> for p=0 and <code>Integer.MAX_VALUE</code>
163
* @param p the desired probability
164
* @return the largest x such that P(X ≤ x) <= p
165
* @throws MathException if the inverse cumulative probability can not be
166
* computed due to convergence or other numerical errors.
167
* @throws IllegalArgumentException if p < 0 or p > 1
169
public int inverseCumulativeProbability(final double p)
170
throws MathException {
173
// handle extreme values explicitly
177
ret = Integer.MAX_VALUE;
179
ret = super.inverseCumulativeProbability(p);