# Statistics/Distributions/Gamma
### Gamma Distribution

The Gamma distribution is very important for technical reasons, since it is the parent of the exponential distribution and can explain many other distributions.
The probability distribution function is:
$f_{x}(x)={\begin{cases}{\frac {1}{a^{p}\Gamma (p)}}x^{p-1}e^{-x/a},&{\mbox{if }}x\geq 0\\0,&{\mbox{if }}x<0\end{cases}}\quad a,p>0$
Where $\Gamma (p)=\int _{0}^{\infty }t^{p-1}e^{-t}\,dt\,$ is the Gamma function. The cumulative distribution function cannot be found unless p=1, in which case the Gamma distribution becomes the exponential distribution. The Gamma distribution of the stochastic variable X is denoted as $X\in \Gamma (p,a)$.
Alternatively, the gamma distribution can be parameterized in terms of a shape parameter $\alpha =k$ and an inverse scale parameter $\beta =1/\theta$, called a rate parameter:
    $g(x;\alpha ,\beta )=Kx^{\alpha -1}e^{-\beta \,x}\ \mathrm {for} \ x>0\,\!.$
where the $K$ constant can be calculated setting the integral of the density function as 1:
    $\int _{-\infty }^{+\infty }g(x;\alpha ,\beta )\mathrm {d} t\,=\int _{0}^{+\infty }Kx^{\alpha -1}e^{-\beta \,x}\mathrm {d} x\,=1$
following:
    $K\int _{0}^{+\infty }x^{\alpha -1}e^{-\beta \,x}\mathrm {d} x\,=1$
    $K={\frac {1}{\int _{0}^{+\infty }x^{\alpha -1}e^{-\beta \,x}\mathrm {d} x}}$
and, with change of variable $y=\beta x$ :
${\begin{aligned}K&={\frac {1}{\int _{0}^{+\infty }{\frac {y^{\alpha -1}}{\beta ^{\alpha -1}}}e^{-y}{\frac {\mathrm {d} y}{\beta }}}}\\&={\frac {1}{{\frac {1}{\beta ^{\alpha }}}\int _{0}^{+\infty }y^{\alpha -1}e^{-y}\mathrm {d} y}}\\&={\frac {\beta ^{\alpha }}{\int _{0}^{+\infty }y^{\alpha -1}e^{-y}\mathrm {d} y}}\\&={\frac {\beta ^{\alpha }}{\Gamma (\alpha )}}\end{aligned}}$
following:
    $g(x;\alpha ,\beta )=x^{\alpha -1}{\frac {\beta ^{-\alpha }\,e^{-\beta \,x}}{\Gamma (\alpha )}}\ \mathrm {for} \ x>0\,\!.$

#### Probability Density Function

We first check that the total integral of the probability density function is 1.
    $\int _{-\infty }^{\infty }{\frac {1}{a^{p}\Gamma (p)}}x^{p-1}e^{-x/a}dx$
Now we let y=x/a which means that dy=dx/a
    ${\frac {1}{\Gamma (p)}}\int _{0}^{\infty }y^{p-1}e^{-y}dy$
    ${\frac {1}{\Gamma (p)}}\Gamma (p)=1$

#### Mean

    $\operatorname {E} [X]=\int _{-\infty }^{\infty }x\cdot {\frac {1}{a^{p}\Gamma (p)}}x^{p-1}e^{-x/a}dx$
Now we let y=x/a which means that dy=dx/a.
    $\operatorname {E} [X]=\int _{0}^{\infty }ay\cdot {\frac {1}{\Gamma (p)}}y^{p-1}e^{-y}dy$
    $\operatorname {E} [X]={\frac {a}{\Gamma (p)}}\int _{0}^{\infty }y^{p}e^{-y}dy$
    $\operatorname {E} [X]={\frac {a}{\Gamma (p)}}\Gamma (p+1)$
We now use the fact that $\Gamma (z+1)=z\Gamma (z)$
    $\operatorname {E} [X]={\frac {a}{\Gamma (p)}}p\Gamma (p)=ap$

#### Variance

We first calculate E[X^2]
    $\operatorname {E} [X^{2}]=\int _{-\infty }^{\infty }x^{2}\cdot {\frac {1}{a^{p}\Gamma (p)}}x^{p-1}e^{-x/a}dx$
Now we let y=x/a which means that dy=dx/a.
    $\operatorname {E} [X^{2}]=\int _{0}^{\infty }a^{2}y^{2}\cdot {\frac {1}{a\Gamma (p)}}y^{p-1}e^{-y}ady$
    $\operatorname {E} [X^{2}]={\frac {a^{2}}{\Gamma (p)}}\int _{0}^{\infty }y^{p+1}e^{-y}dy$
    $\operatorname {E} [X^{2}]={\frac {a^{2}}{\Gamma (p)}}\Gamma (p+2)=pa^{2}(p+1)$
Now we use calculate the variance
    $\operatorname {Var} (X)=\operatorname {E} [X^{2}]-(\operatorname {E} [X])^{2}$
    $\operatorname {Var} (X)=pa^{2}(p+1)-(ap)^{2}=pa^{2}$

#### External links

* Interactive Gamma Distribution Web Applet (Java)