## 伽马函数

$$\Gamma(\alpha) = \int_{0}^{\infty} t^{\alpha-1}e^{-t}dt$$

\begin{align} \Gamma(\alpha + 1) &= \int_{0}^{\infty} t^{\alpha}e^{-t}dt \notag\\ &= t^{\alpha}(-e^{-t})|_{0}^{\infty} - \int_{0}^{\infty}(-e^{-t})(\alpha t^{\alpha-1}dt) \notag\\ &= (0-0) + \alpha \int_{0}^{\infty}t^{\alpha-1}e^{-t}dt \notag\\ &= \alpha\Gamma(\alpha) \notag \end{align} \tag{1}

$\alpha$ 趋于无穷时，根据 Stirling 公式

$$\lim_{\alpha \rightarrow \infty} \frac{\Gamma(\alpha + 1)}{\alpha^{\alpha+1}e^{-\alpha}} = \sqrt{2\pi}$$

## 尺度变换 rescale

$s = at$，则 $ds = adt$，然后有

$$\int_{0}^{\infty} t^{\alpha -1}e^{-at} dt = \int_{0}^{\infty} (s/a)^{\alpha -1}e^{s}(ds/a) = a^{-\alpha}\int_{0}^{\infty} s^{\alpha -1}e^{-s} ds = \Gamma(\alpha)/a^{\alpha}$$

## 计算

gamma(x = 0.5)
sqrt(pi)

[1] 1.772


$$\mathrm{digamma}(x) = \Psi(x) = \frac{d}{dx}\ln\Gamma(x) = \frac{\Gamma'(x)}{\Gamma(x)}$$

digamma(x = 0.5)

[1] -1.964


## 伽马分布

# 概率密度函数值
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)

x,q

p

n

rate

shape, scale

log,log.p

lower.tail

$$f(x) = \frac{1}{s^a \Gamma(a)} x^{a-1}e^{-\frac{x}{s}}$$

Note that $a = 0$ corresponds to the trivial distribution with all mass at point 0.

-pgamma(t, ..., lower = FALSE, log = TRUE)


Note that for smallish values of shape (and moderate scale) a large parts 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.)

## 参考文献

1. R 软件内置帮助文档 ?gamma
2. 靳志辉.神奇的伽玛函数 (上).2014. https://cosx.org/2014/07/gamma-function-1
3. 靳志辉.神奇的伽玛函数 (下).2014. https://cosx.org/2014/07/gamma-function-2