Density, distribution function, quantile function and random
generation for the binomial distribution with parameters
This is conventionally interpreted as the number of ‘successes’
1 2 3 4
vector of quantiles.
vector of probabilities.
number of observations. If
number of trials (zero or more).
probability of success on each trial.
logical; if TRUE, probabilities p are given as log(p).
logical; if TRUE (default), probabilities are P[X ≤ x], otherwise, P[X > x].
The binomial distribution with
size = n and
prob = p has density
p(x) = choose(n, x) p^x (1-p)^(n-x)
for x = 0, …, n.
Note that binomial coefficients can be computed by
choose in R.
If an element of
x is not integer, the result of
is zero, with a warning.
p(x) is computed using Loader's algorithm, see the reference below.
The quantile is defined as the smallest value x such that F(x) ≥ p, where F is the distribution function.
dbinom gives the density,
pbinom gives the distribution
qbinom gives the quantile function and
generates random deviates.
size is not an integer,
NaN is returned.
The length of the result is determined by
rbinom, and is the maximum of the lengths of the
numerical arguments for the other functions.
The numerical arguments other than
n are recycled to the
length of the result. Only the first elements of the logical
arguments are used.
dbinom a saddle-point expansion is used: see
Catherine Loader (2000). Fast and Accurate Computation of Binomial Probabilities; available from http://www.herine.net/stat/software/dbinom.html.
qbinom uses the Cornish–Fisher Expansion to include a skewness
correction to a normal approximation, followed by a search.
size < .Machine$integer.max) is based on
Kachitvichyanukul, V. and Schmeiser, B. W. (1988) Binomial random variate generation. Communications of the ACM, 31, 216–222.
For larger values it uses inversion.
Distributions for other standard distributions, including
dnbinom for the negative binomial, and
dpois for the Poisson distribution.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
require(graphics) # Compute P(45 < X < 55) for X Binomial(100,0.5) sum(dbinom(46:54, 100, 0.5)) ## Using "log = TRUE" for an extended range : n <- 2000 k <- seq(0, n, by = 20) plot (k, dbinom(k, n, pi/10, log = TRUE), type = "l", ylab = "log density", main = "dbinom(*, log=TRUE) is better than log(dbinom(*))") lines(k, log(dbinom(k, n, pi/10)), col = "red", lwd = 2) ## extreme points are omitted since dbinom gives 0. mtext("dbinom(k, log=TRUE)", adj = 0) mtext("extended range", adj = 0, line = -1, font = 4) mtext("log(dbinom(k))", col = "red", adj = 1)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.