| GammaDist {stats} | R Documentation |
The Gamma Distribution
Description
Density, distribution function, quantile function and random
generation for the Gamma distribution with parameters shape and
scale.
Usage
dgamma(x, shape, rate = 1, scale = 1/rate, log = FALSE)
pgamma(q, shape, rate = 1, scale = 1/rate, lower.tail = TRUE,
log.p = FALSE)
qgamma(p, shape, rate = 1, scale = 1/rate, lower.tail = TRUE,
log.p = FALSE)
rgamma(n, shape, rate = 1, scale = 1/rate)
Arguments
x, q |
vector of quantiles. |
p |
vector of probabilities. |
n |
number of observations. If |
rate |
an alternative way to specify the scale. |
shape, scale |
shape and scale parameters. Must be positive,
|
log, log.p |
logical; if |
lower.tail |
logical; if |
Details
If scale is omitted, it assumes the default value of 1.
The Gamma distribution with parameters shape =\alpha
and scale =\sigma has density
f(x)= \frac{1}{{\sigma}^{\alpha}\Gamma(\alpha)} {x}^{\alpha-1} e^{-x/\sigma}%
for x \ge 0, \alpha > 0 and \sigma > 0.
(Here \Gamma(\alpha) is the function implemented by R's
gamma() and defined in its help. Note that a = 0
corresponds to the trivial distribution with all mass at point 0.)
The mean and variance are
E(X) = \alpha\sigma and
Var(X) = \alpha\sigma^2.
The cumulative hazard H(t) = - \log(1 - F(t))
is
-pgamma(t, ..., lower = FALSE, log = TRUE)
Note that for smallish values of shape (and moderate
scale) a large part of the mass of the Gamma distribution is
on values of x so near zero that they will be represented as
zero in computer arithmetic. So rgamma may well return values
which will be represented as zero. (This will also happen for very
large values of scale since the actual generation is done for
scale = 1.)
Value
dgamma gives the density,
pgamma is the cumulative distribution function, and
qgamma is the quantile function of the Gamma distribution.
rgamma generates random deviates.
Invalid arguments will result in return value NaN, with a warning.
The length of the result is determined by n for
rgamma, 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.
Note
The S (Becker, Chambers, and Wilks 1988) parametrization was via shape
and rate: S had no scale parameter. It is an error
to supply both scale and rate.
pgamma is closely related to the incomplete gamma function.
As defined by Abramowitz and Stegun (1972, section 6.5.1)
(and by ‘Numerical Recipes’) this is
P(a,x) = \frac{1}{\Gamma(a)} \int_0^x t^{a-1} e^{-t} dt
P(a, x) is pgamma(x, a). Other authors (for example
Karl Pearson in his 1922 tables) omit the normalizing factor,
defining the incomplete gamma function \gamma(a,x) as
\gamma(a,x) = \int_0^x t^{a-1} e^{-t} dt, i.e., pgamma(x, a) * gamma(a).
Yet other use the ‘upper’ incomplete gamma function,
\Gamma(a,x) = \int_x^\infty t^{a-1} e^{-t} dt,
which can be computed by
pgamma(x, a, lower = FALSE) * gamma(a).
Note however that pgamma(x, a, ..) currently requires a > 0,
whereas the incomplete gamma function is also defined for negative
a. In that case, you can use gamma_inc(a,x) (for
\Gamma(a,x)) from package gsl.
See also https://en.wikipedia.org/wiki/Incomplete_gamma_function, or https://dlmf.nist.gov/8.2#i.
Source
dgamma is computed via the Poisson density, using code contributed
by Catherine Loader (see dbinom).
pgamma uses an unpublished (and not otherwise documented)
algorithm ‘mainly by Morten Welinder’.
qgamma is based on a C translation of
Best and Roberts (1975) plus a final Newton step to improve the
approximation.
rgamma for shape >= 1 uses
Ahrens and Dieter (1982)
and for 0 < shape < 1 uses
Ahrens and Dieter (1974).
References
Abramowitz M, Stegun IA (1972).
Handbook of Mathematical Functions with Formulas, Graphs, and Mathematical Tables.
Dover, New York.
Chapter 6: Gamma and Related Functions.
Ahrens JH, Dieter U (1982). “Generating Gamma Variates by a Modified Rejection Technique.” Communications of the ACM, 25(1), 47–54. doi:10.1145/358315.358390.
Ahrens JH, Dieter U (1974). “Computer Methods for Sampling from Gamma, Beta, Poisson and Binomial Distributions.” Computing, 12(3), 223–246. doi:10.1007/bf02293108.
Becker RA, Chambers JM, Wilks AR (1988). The New S Language. Chapman and Hall/CRC, London.
Best DJ, Roberts DE (1975). “Algorithm AS 89: The Upper Tail Probabilities of Spearman’s Rho.” Applied Statistics, 24(3), 377. doi:10.2307/2347111.
Shea BL (1988). “Algorithm AS 239: Chi-Squared and Incomplete Gamma Integral.” Applied Statistics, 37(3), 466. doi:10.2307/2347328.
NIST Digital Library of Mathematical Functions. https://dlmf.nist.gov/, section 8.2.
See Also
gamma for the gamma function.
Distributions for other standard distributions, including
dbeta for the Beta distribution and dchisq
for the chi-squared distribution which is a special case of the Gamma
distribution.
Examples
-log(dgamma(1:4, shape = 1))
p <- (1:9)/10
pgamma(qgamma(p, shape = 2), shape = 2)
1 - 1/exp(qgamma(p, shape = 1))
# even for shape = 0.001 about half the mass is on numbers
# that cannot be represented accurately (and most of those as zero)
pgamma(.Machine$double.xmin, 0.001)
pgamma(5e-324, 0.001) # on most machines 5e-324 is the smallest
# representable non-zero number
table(rgamma(1e4, 0.001) == 0)/1e4