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 */ 017package org.apache.commons.math3.distribution; 018 019import org.apache.commons.math3.exception.NotPositiveException; 020import org.apache.commons.math3.exception.OutOfRangeException; 021import org.apache.commons.math3.exception.util.LocalizedFormats; 022import org.apache.commons.math3.random.RandomGenerator; 023import org.apache.commons.math3.random.Well19937c; 024import org.apache.commons.math3.special.Beta; 025import org.apache.commons.math3.util.FastMath; 026 027/** 028 * Implementation of the binomial distribution. 029 * 030 * @see <a href="http://en.wikipedia.org/wiki/Binomial_distribution">Binomial distribution (Wikipedia)</a> 031 * @see <a href="http://mathworld.wolfram.com/BinomialDistribution.html">Binomial Distribution (MathWorld)</a> 032 */ 033public class BinomialDistribution extends AbstractIntegerDistribution { 034 /** Serializable version identifier. */ 035 private static final long serialVersionUID = 6751309484392813623L; 036 /** The number of trials. */ 037 private final int numberOfTrials; 038 /** The probability of success. */ 039 private final double probabilityOfSuccess; 040 041 /** 042 * Create a binomial distribution with the given number of trials and 043 * probability of success. 044 * <p> 045 * <b>Note:</b> this constructor will implicitly create an instance of 046 * {@link Well19937c} as random generator to be used for sampling only (see 047 * {@link #sample()} and {@link #sample(int)}). In case no sampling is 048 * needed for the created distribution, it is advised to pass {@code null} 049 * as random generator via the appropriate constructors to avoid the 050 * additional initialisation overhead. 051 * 052 * @param trials Number of trials. 053 * @param p Probability of success. 054 * @throws NotPositiveException if {@code trials < 0}. 055 * @throws OutOfRangeException if {@code p < 0} or {@code p > 1}. 056 */ 057 public BinomialDistribution(int trials, double p) { 058 this(new Well19937c(), trials, p); 059 } 060 061 /** 062 * Creates a binomial distribution. 063 * 064 * @param rng Random number generator. 065 * @param trials Number of trials. 066 * @param p Probability of success. 067 * @throws NotPositiveException if {@code trials < 0}. 068 * @throws OutOfRangeException if {@code p < 0} or {@code p > 1}. 069 * @since 3.1 070 */ 071 public BinomialDistribution(RandomGenerator rng, 072 int trials, 073 double p) { 074 super(rng); 075 076 if (trials < 0) { 077 throw new NotPositiveException(LocalizedFormats.NUMBER_OF_TRIALS, 078 trials); 079 } 080 if (p < 0 || p > 1) { 081 throw new OutOfRangeException(p, 0, 1); 082 } 083 084 probabilityOfSuccess = p; 085 numberOfTrials = trials; 086 } 087 088 /** 089 * Access the number of trials for this distribution. 090 * 091 * @return the number of trials. 092 */ 093 public int getNumberOfTrials() { 094 return numberOfTrials; 095 } 096 097 /** 098 * Access the probability of success for this distribution. 099 * 100 * @return the probability of success. 101 */ 102 public double getProbabilityOfSuccess() { 103 return probabilityOfSuccess; 104 } 105 106 /** {@inheritDoc} */ 107 public double probability(int x) { 108 final double logProbability = logProbability(x); 109 return logProbability == Double.NEGATIVE_INFINITY ? 0 : FastMath.exp(logProbability); 110 } 111 112 /** {@inheritDoc} **/ 113 @Override 114 public double logProbability(int x) { 115 if (numberOfTrials == 0) { 116 return (x == 0) ? 0. : Double.NEGATIVE_INFINITY; 117 } 118 double ret; 119 if (x < 0 || x > numberOfTrials) { 120 ret = Double.NEGATIVE_INFINITY; 121 } else { 122 ret = SaddlePointExpansion.logBinomialProbability(x, 123 numberOfTrials, probabilityOfSuccess, 124 1.0 - probabilityOfSuccess); 125 } 126 return ret; 127 } 128 129 /** {@inheritDoc} */ 130 public double cumulativeProbability(int x) { 131 double ret; 132 if (x < 0) { 133 ret = 0.0; 134 } else if (x >= numberOfTrials) { 135 ret = 1.0; 136 } else { 137 ret = 1.0 - Beta.regularizedBeta(probabilityOfSuccess, 138 x + 1.0, numberOfTrials - x); 139 } 140 return ret; 141 } 142 143 /** 144 * {@inheritDoc} 145 * 146 * For {@code n} trials and probability parameter {@code p}, the mean is 147 * {@code n * p}. 148 */ 149 public double getNumericalMean() { 150 return numberOfTrials * probabilityOfSuccess; 151 } 152 153 /** 154 * {@inheritDoc} 155 * 156 * For {@code n} trials and probability parameter {@code p}, the variance is 157 * {@code n * p * (1 - p)}. 158 */ 159 public double getNumericalVariance() { 160 final double p = probabilityOfSuccess; 161 return numberOfTrials * p * (1 - p); 162 } 163 164 /** 165 * {@inheritDoc} 166 * 167 * The lower bound of the support is always 0 except for the probability 168 * parameter {@code p = 1}. 169 * 170 * @return lower bound of the support (0 or the number of trials) 171 */ 172 public int getSupportLowerBound() { 173 return probabilityOfSuccess < 1.0 ? 0 : numberOfTrials; 174 } 175 176 /** 177 * {@inheritDoc} 178 * 179 * The upper bound of the support is the number of trials except for the 180 * probability parameter {@code p = 0}. 181 * 182 * @return upper bound of the support (number of trials or 0) 183 */ 184 public int getSupportUpperBound() { 185 return probabilityOfSuccess > 0.0 ? numberOfTrials : 0; 186 } 187 188 /** 189 * {@inheritDoc} 190 * 191 * The support of this distribution is connected. 192 * 193 * @return {@code true} 194 */ 195 public boolean isSupportConnected() { 196 return true; 197 } 198}