The R Project SVN R

Rev

Rev 88609 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
42333 ripley 1
% File src/library/stats/man/GammaDist.Rd
68948 ripley 2
% Part of the R package, https://www.R-project.org
88402 maechler 3
% Copyright 1995-2025 R Core Team
42333 ripley 4
% Distributed under GPL 2 or later
5
 
27442 ripley 6
\name{GammaDist}
56186 murdoch 7
\alias{GammaDist}
27442 ripley 8
\alias{dgamma}
9
\alias{pgamma}
10
\alias{qgamma}
11
\alias{rgamma}
28897 maechler 12
\concept{incomplete gamma function}
27442 ripley 13
\title{The Gamma Distribution}
14
\description{
15
  Density, distribution function, quantile function and random
16
  generation for the Gamma distribution with parameters \code{shape} and
17
  \code{scale}.
18
}
19
\usage{
20
dgamma(x, shape, rate = 1, scale = 1/rate, log = FALSE)
30912 ripley 21
pgamma(q, shape, rate = 1, scale = 1/rate, lower.tail = TRUE,
22
       log.p = FALSE)
23
qgamma(p, shape, rate = 1, scale = 1/rate, lower.tail = TRUE,
24
       log.p = FALSE)
27442 ripley 25
rgamma(n, shape, rate = 1, scale = 1/rate)
26
}
27
\arguments{
28
  \item{x, q}{vector of quantiles.}
29
  \item{p}{vector of probabilities.}
30
  \item{n}{number of observations. If \code{length(n) > 1}, the length
31
    is taken to be the number required.}
32
  \item{rate}{an alternative way to specify the scale.}
43895 maechler 33
  \item{shape, scale}{shape and scale parameters.  Must be positive,
34
    \code{scale} strictly.}
88459 smeyer 35
  \item{log, log.p}{logical; if \code{TRUE}, probabilities/densities
36
    are given as logarithms.}
37
  \item{lower.tail}{logical; if \code{TRUE} (default), probabilities are
88402 maechler 38
    \eqn{P[X < x]}, otherwise, \eqn{P[X \ge x]} (exceptionally not
39
    \emph{cadlag} here, to ensure, e.g., \code{pgamma(0, shape=0)} remains zero).}
27442 ripley 40
}
41
\value{
42
  \code{dgamma} gives the density,
88459 smeyer 43
  \code{pgamma} is the cumulative distribution function, and
44
  \code{qgamma} is the quantile function of the Gamma distribution.
27442 ripley 45
  \code{rgamma} generates random deviates.
37274 ripley 46
 
47
  Invalid arguments will result in return value \code{NaN}, with a warning.
79365 maechler 48
 
61975 murdoch 49
  The length of the result is determined by \code{n} for
50
  \code{rgamma}, and is the maximum of the lengths of the
79365 maechler 51
  numerical arguments for the other functions.
52
 
65782 ripley 53
  The numerical arguments other than \code{n} are recycled to the
61975 murdoch 54
  length of the result.  Only the first elements of the logical
65782 ripley 55
  arguments are used.
27442 ripley 56
}
57
\details{
58
  If \code{scale} is omitted, it assumes the default value of \code{1}.
59
 
60
  The Gamma distribution with parameters \code{shape} \eqn{=\alpha}{= a}
61
  and \code{scale} \eqn{=\sigma}{= s} has density
62
  \deqn{
63
    f(x)= \frac{1}{{\sigma}^{\alpha}\Gamma(\alpha)} {x}^{\alpha-1} e^{-x/\sigma}%
64
  }{f(x)= 1/(s^a Gamma(a)) x^(a-1) e^-(x/s)}
50805 ripley 65
  for \eqn{x \ge 0}, \eqn{\alpha > 0}{a > 0} and \eqn{\sigma > 0}{s > 0}.
34851 ripley 66
  (Here \eqn{\Gamma(\alpha)}{Gamma(a)} is the function implemented by \R's
59979 ripley 67
  \code{\link{gamma}()} and defined in its help.  Note that \eqn{a = 0}
43899 maechler 68
  corresponds to the trivial distribution with all mass at point 0.)
34848 ripley 69
 
27442 ripley 70
  The mean and variance are
71
  \eqn{E(X) = \alpha\sigma}{E(X) = a*s} and
72
  \eqn{Var(X) = \alpha\sigma^2}{Var(X) = a*s^2}.
28231 maechler 73
 
34848 ripley 74
  The cumulative hazard \eqn{H(t) = - \log(1 - F(t))}{H(t) = - log(1 - F(t))}
61173 ripley 75
  is
88459 smeyer 76
\preformatted{
77
    -pgamma(t, ..., lower = FALSE, log = TRUE)
61173 ripley 78
}
39015 ripley 79
 
46325 ripley 80
  Note that for smallish values of \code{shape} (and moderate
88402 maechler 81
  \code{scale}) a large part of the mass of the Gamma distribution is
46325 ripley 82
  on values of \eqn{x} so near zero that they will be represented as
61177 ripley 83
  zero in computer arithmetic.  So \code{rgamma} may well return values
46325 ripley 84
  which will be represented as zero.  (This will also happen for very
85
  large values of \code{scale} since the actual generation is done for
61160 ripley 86
  \code{scale = 1}.)
50414 ripley 87
}
50399 maechler 88
% Have caught all currently known problems; hence no longer say:
89
%   Similarly, \code{qgamma} has a very hard job for
90
%   small \code{scale}, and warns of potential unreliability for
91
%   \code{scale < 1e-10}.
27442 ripley 92
\note{
88585 hornik 93
  The S \bibcitep{R:Becker+Chambers+Wilks:1988} parametrization was via \code{shape}
71883 ripley 94
  and \code{rate}: S had no \code{scale} parameter. It is an error
79365 maechler 95
  to supply both \code{scale} and \code{rate}.
28231 maechler 96
 
88557 hornik 97
  \code{pgamma} is closely related to the incomplete gamma function.
98
  As defined by \bibcitet{|R:Abramowitz+Stegun:1972|section 6.5.1}
99
  (and by \sQuote{Numerical Recipes}) this is
61177 ripley 100
  \deqn{P(a,x) = \frac{1}{\Gamma(a)} \int_0^x t^{a-1} e^{-t} dt}{P(a,x) = 1/Gamma(a) integral_0^x t^(a-1) exp(-t) dt}
28789 ripley 101
  \eqn{P(a, x)} is \code{pgamma(x, a)}.  Other authors (for example
102
  Karl Pearson in his 1922 tables) omit the normalizing factor,
61042 maechler 103
  defining the incomplete gamma function \eqn{\gamma(a,x)} as
104
  \eqn{\gamma(a,x) = \int_0^x t^{a-1} e^{-t} dt,}{gamma(a,x) =
105
    integral_0^x t^(a-1) exp(-t) dt,} i.e., \code{pgamma(x, a) * gamma(a)}.
106
  Yet other use the \sQuote{upper} incomplete gamma function,
61177 ripley 107
  \deqn{\Gamma(a,x) = \int_x^\infty t^{a-1} e^{-t} dt,}{Gamma(a,x) = integral_x^Inf t^(a-1) exp(-t) dt,}
61042 maechler 108
  which can be computed by
61160 ripley 109
  \code{pgamma(x, a, lower = FALSE) * gamma(a)}.
61042 maechler 110
 
111
  Note however that \code{pgamma(x, a, ..)} currently requires \eqn{a > 0},
112
  whereas the incomplete gamma function is also defined for negative
113
  \eqn{a}.  In that case, you can use \code{gamma_inc(a,x)} (for
62120 hornik 114
  \eqn{\Gamma(a,x)}) from package \CRANpkg{gsl}.
61042 maechler 115
 
116
  See also
68939 ripley 117
  \url{https://en.wikipedia.org/wiki/Incomplete_gamma_function}, or
78962 hornik 118
  \url{https://dlmf.nist.gov/8.2#i}.
37222 ripley 119
}
120
\source{
37226 ripley 121
  \code{dgamma} is computed via the Poisson density, using code contributed
37222 ripley 122
  by Catherine Loader (see \code{\link{dbinom}}).
43895 maechler 123
 
42987 ripley 124
  \code{pgamma} uses an unpublished (and not otherwise documented)
125
  algorithm \sQuote{mainly by Morten Welinder}.
34848 ripley 126
 
37222 ripley 127
  \code{qgamma} is based on a C translation of
88557 hornik 128
  \bibcitet{R:Best+Roberts:1975} plus a final Newton step to improve the
129
  approximation.
37222 ripley 130
 
90060 hornik 131
  \code{rgamma} for \eqn{\code{shape} \ge 1} uses
88609 hornik 132
  \bibcitet{R:Ahrens+Dieter:1982a}
90060 hornik 133
  and for \eqn{0 < \code{shape} < 1} uses
88603 hornik 134
  \bibcitet{R:Ahrens+Dieter:1974}.
27442 ripley 135
}
136
\references{
88557 hornik 137
  \bibinfo{R:Abramowitz+Stegun:1972}{footer}{Chapter 6: Gamma and Related Functions.}
138
  \bibshow{*,
139
    R:Shea:1988}
28231 maechler 140
 
61042 maechler 141
  NIST Digital Library of Mathematical Functions.
78962 hornik 142
  \url{https://dlmf.nist.gov/}, section 8.2.
27442 ripley 143
}
144
\seealso{
52770 ripley 145
  \code{\link{gamma}} for the gamma function.
146
 
147
  \link{Distributions} for other standard distributions, including
148
  \code{\link{dbeta}} for the Beta distribution and \code{\link{dchisq}}
149
  for the chi-squared distribution which is a special case of the Gamma
150
  distribution.
27442 ripley 151
}
152
\examples{
61160 ripley 153
-log(dgamma(1:4, shape = 1))
27442 ripley 154
p <- (1:9)/10
61168 ripley 155
pgamma(qgamma(p, shape = 2), shape = 2)
61160 ripley 156
1 - 1/exp(qgamma(p, shape = 1))
39015 ripley 157
 
55251 ripley 158
\donttest{# even for shape = 0.001 about half the mass is on numbers
39015 ripley 159
# that cannot be represented accurately (and most of those as zero)
160
pgamma(.Machine$double.xmin, 0.001)
52417 maechler 161
pgamma(5e-324, 0.001)  # on most machines 5e-324 is the smallest
39015 ripley 162
                       # representable non-zero number
163
table(rgamma(1e4, 0.001) == 0)/1e4
55251 ripley 164
}}
27442 ripley 165
\keyword{distribution}