\Header{NegBinomial}{The Negative Binomial Distribution}
\alias{dnbinom}{NegBinomial}
\alias{pnbinom}{NegBinomial}
\alias{qnbinom}{NegBinomial}
\alias{rnbinom}{NegBinomial}
\keyword{distribution}{NegBinomial}
\begin{Usage}
\begin{verbatim}
dnbinom(x, size, prob)
pnbinom(q, size, prob)
qnbinom(p, size, prob)
rnbinom(n, size, prob)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\texttt{x,q}] vector of quantiles representing the number of failures
which occur in a sequence of Bernoulli trials before a target number of
successes is reached, or alternately the probability distribution
of a heterogeneous Poisson process whose intensity is distributed
as a gamma distribution with scale parameter \texttt{prob/(1-prob)} and
shape parameter \texttt{size} (this definition allows non-integer
values of \texttt{size}).
\item[\texttt{x}] vector of (non-negative integer) quantiles.
\item[\texttt{q}] vector of quantiles.
\item[\texttt{p}] vector of probabilities.
\item[\texttt{n}] number of observations to generate.
\item[\texttt{size}] target for number of successful trials /\\{}
shape parameter of gamma distribution.
\item[\texttt{prob}] probability of success in each trial /\\{}
determines scale of gamma distribution
(\texttt{prob} = \texttt{scale/(1+scale)}).
\end{ldescription}
\end{Arguments}
\begin{Description}
These functions provide information about the negative binomial
distribution with parameters \texttt{size} and \texttt{prob}.  \texttt{dnbinom}
gives the density, \texttt{pnbinom} gives the distribution function,
\texttt{qnbinom} gives the quantile function and \texttt{rnbinom}
generates random deviates.\end{Description}
\begin{Details}
The negative binomial distribution with \texttt{size} \eqn{= n}{} and
\texttt{prob} \eqn{= p}{} has density
\deqn{
p(x) = \frac{\Gamma(x+n)}{\Gamma(n) x!} p^n (1-p)^x}{p(x) = Gamma(x+n)/(Gamma(n) x!) p^n (1-p)^x}
for \eqn{x = 0, 1, 2, \ldots}{}

If an element of \texttt{x} is not integer, the result of \texttt{dnbinom}
is zero, with a warning.\end{Details}
\begin{SeeAlso}
\texttt{\Link{dbinom}} for the binomial, \texttt{\Link{dpois}} for the
Poisson and \texttt{\Link{dgeom}} for the geometric distribution, which
is a special case of the negative binomial.\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
x <- 0:11
dnbinom(x, size = 1, prob = 1/2) * 2^(1 + x) # == 1
126 /  dnbinom(0:8, size  = 2, prob  = 1/2) #- theoretically integer

## Cumulative ('p') = Sum of discrete prob.s ('d');  Relative error :
summary(1 - cumsum(dnbinom(x, size = 2, prob = 1/2)) /
	          pnbinom(x, size  = 2, prob = 1/2))

x <- 0:15
size <- (1:20)/4
persp(x,size, dnb <- outer(x,size,function(x,s)dnbinom(x,s, pr= 0.4)))
title(tit <- "negative binomial density(x,s, pr = 0.4)  vs.  x & s")
## if persp() only could label axes ....

image  (x,size, log10(dnb), main= paste("log [",tit,"]"))
contour(x,size, log10(dnb),add=TRUE)
\end{ExampleCode}
\end{Examples}

