001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.math.distribution;
018
019 import java.io.Serializable;
020
021 import org.apache.commons.math.MathException;
022 import org.apache.commons.math.MathRuntimeException;
023 import org.apache.commons.math.special.Beta;
024
025 /**
026 * The default implementation of {@link BinomialDistribution}.
027 *
028 * @version $Revision: 920852 $ $Date: 2010-03-09 07:53:44 -0500 (Tue, 09 Mar 2010) $
029 */
030 public class BinomialDistributionImpl extends AbstractIntegerDistribution
031 implements BinomialDistribution, Serializable {
032
033 /** Serializable version identifier */
034 private static final long serialVersionUID = 6751309484392813623L;
035
036 /** The number of trials. */
037 private int numberOfTrials;
038
039 /** The probability of success. */
040 private double probabilityOfSuccess;
041
042 /**
043 * Create a binomial distribution with the given number of trials and
044 * probability of success.
045 *
046 * @param trials the number of trials.
047 * @param p the probability of success.
048 */
049 public BinomialDistributionImpl(int trials, double p) {
050 super();
051 setNumberOfTrialsInternal(trials);
052 setProbabilityOfSuccessInternal(p);
053 }
054
055 /**
056 * Access the number of trials for this distribution.
057 *
058 * @return the number of trials.
059 */
060 public int getNumberOfTrials() {
061 return numberOfTrials;
062 }
063
064 /**
065 * Access the probability of success for this distribution.
066 *
067 * @return the probability of success.
068 */
069 public double getProbabilityOfSuccess() {
070 return probabilityOfSuccess;
071 }
072
073 /**
074 * Change the number of trials for this distribution.
075 *
076 * @param trials the new number of trials.
077 * @throws IllegalArgumentException if <code>trials</code> is not a valid
078 * number of trials.
079 * @deprecated as of 2.1 (class will become immutable in 3.0)
080 */
081 @Deprecated
082 public void setNumberOfTrials(int trials) {
083 setNumberOfTrialsInternal(trials);
084 }
085 /**
086 * Change the number of trials for this distribution.
087 *
088 * @param trials the new number of trials.
089 * @throws IllegalArgumentException if <code>trials</code> is not a valid
090 * number of trials.
091 */
092 private void setNumberOfTrialsInternal(int trials) {
093 if (trials < 0) {
094 throw MathRuntimeException.createIllegalArgumentException(
095 "number of trials must be non-negative ({0})", trials);
096 }
097 numberOfTrials = trials;
098 }
099
100 /**
101 * Change the probability of success for this distribution.
102 *
103 * @param p the new probability of success.
104 * @throws IllegalArgumentException if <code>p</code> is not a valid
105 * probability.
106 * @deprecated as of 2.1 (class will become immutable in 3.0)
107 */
108 @Deprecated
109 public void setProbabilityOfSuccess(double p) {
110 setProbabilityOfSuccessInternal(p);
111 }
112 /**
113 * Change the probability of success for this distribution.
114 *
115 * @param p the new probability of success.
116 * @throws IllegalArgumentException if <code>p</code> is not a valid
117 * probability.
118 */
119 private void setProbabilityOfSuccessInternal(double p) {
120 if (p < 0.0 || p > 1.0) {
121 throw MathRuntimeException.createIllegalArgumentException(
122 "{0} out of [{1}, {2}] range", p, 0.0, 1.0);
123 }
124 probabilityOfSuccess = p;
125 }
126
127 /**
128 * Access the domain value lower bound, based on <code>p</code>, used to
129 * bracket a PDF root.
130 *
131 * @param p the desired probability for the critical value
132 * @return domain value lower bound, i.e. P(X < <i>lower bound</i>) <
133 * <code>p</code>
134 */
135 @Override
136 protected int getDomainLowerBound(double p) {
137 return -1;
138 }
139
140 /**
141 * Access the domain value upper bound, based on <code>p</code>, used to
142 * bracket a PDF root.
143 *
144 * @param p the desired probability for the critical value
145 * @return domain value upper bound, i.e. P(X < <i>upper bound</i>) >
146 * <code>p</code>
147 */
148 @Override
149 protected int getDomainUpperBound(double p) {
150 return numberOfTrials;
151 }
152
153 /**
154 * For this distribution, X, this method returns P(X ≤ x).
155 *
156 * @param x the value at which the PDF is evaluated.
157 * @return PDF for this distribution.
158 * @throws MathException if the cumulative probability can not be computed
159 * due to convergence or other numerical errors.
160 */
161 @Override
162 public double cumulativeProbability(int x) throws MathException {
163 double ret;
164 if (x < 0) {
165 ret = 0.0;
166 } else if (x >= numberOfTrials) {
167 ret = 1.0;
168 } else {
169 ret = 1.0 - Beta.regularizedBeta(getProbabilityOfSuccess(),
170 x + 1.0, numberOfTrials - x);
171 }
172 return ret;
173 }
174
175 /**
176 * For this distribution, X, this method returns P(X = x).
177 *
178 * @param x the value at which the PMF is evaluated.
179 * @return PMF for this distribution.
180 */
181 public double probability(int x) {
182 double ret;
183 if (x < 0 || x > numberOfTrials) {
184 ret = 0.0;
185 } else {
186 ret = Math.exp(SaddlePointExpansion.logBinomialProbability(x,
187 numberOfTrials, probabilityOfSuccess,
188 1.0 - probabilityOfSuccess));
189 }
190 return ret;
191 }
192
193 /**
194 * For this distribution, X, this method returns the largest x, such that
195 * P(X ≤ x) ≤ <code>p</code>.
196 * <p>
197 * Returns <code>-1</code> for p=0 and <code>Integer.MAX_VALUE</code> for
198 * p=1.
199 * </p>
200 *
201 * @param p the desired probability
202 * @return the largest x such that P(X ≤ x) <= p
203 * @throws MathException if the inverse cumulative probability can not be
204 * computed due to convergence or other numerical errors.
205 * @throws IllegalArgumentException if p < 0 or p > 1
206 */
207 @Override
208 public int inverseCumulativeProbability(final double p)
209 throws MathException {
210 // handle extreme values explicitly
211 if (p == 0) {
212 return -1;
213 }
214 if (p == 1) {
215 return Integer.MAX_VALUE;
216 }
217
218 // use default bisection impl
219 return super.inverseCumulativeProbability(p);
220 }
221 }