Lesson 13 of 15

Gamma & Beta Distributions

Two Distributions with Flexible Shapes

Gamma Distribution

XGamma(α,β)X \sim \text{Gamma}(\alpha, \beta) generalizes the exponential. It is the distribution of the waiting time for α\alpha events in a Poisson process with rate β\beta:

f(x)=βαΓ(α)xα1eβx,x>0f(x) = \frac{\beta^\alpha}{\Gamma(\alpha)} x^{\alpha-1} e^{-\beta x}, \quad x > 0

E[X]=αβ,Var(X)=αβ2E[X] = \frac{\alpha}{\beta}, \qquad \text{Var}(X) = \frac{\alpha}{\beta^2}

Special case: Gamma(1,β)=Exponential(β)\text{Gamma}(1, \beta) = \text{Exponential}(\beta).

Beta Distribution

XBeta(α,β)X \sim \text{Beta}(\alpha, \beta) lives on [0,1][0, 1] and models probabilities and proportions:

f(x)=xα1(1x)β1B(α,β),0x1f(x) = \frac{x^{\alpha-1}(1-x)^{\beta-1}}{B(\alpha,\beta)}, \quad 0 \leq x \leq 1

E[X]=αα+β,Var(X)=αβ(α+β)2(α+β+1)E[X] = \frac{\alpha}{\alpha+\beta}, \qquad \text{Var}(X) = \frac{\alpha\beta}{(\alpha+\beta)^2(\alpha+\beta+1)}

Bayesian statistics: the Beta distribution is the conjugate prior for the Bernoulli/Binomial likelihood. After observing kk successes in nn trials with a Beta(1,1)\text{Beta}(1,1) (uniform) prior, the posterior is Beta(k+1,nk+1)\text{Beta}(k+1, n-k+1).

# Beta(2, 5): modeling a conversion rate
alpha, beta = 2, 5
mean = alpha / (alpha + beta)
var  = alpha * beta / ((alpha + beta)**2 * (alpha + beta + 1))
print(round(mean, 4))  # 0.2857
print(round(var, 4))   # 0.0255

Your Task

Implement two functions:

  • gamma_stats(alpha, beta) — prints E[X]E[X] and Var(X)\text{Var}(X) of Gamma(α,β)\text{Gamma}(\alpha, \beta)
  • beta_stats(alpha, beta) — prints E[X]E[X] and Var(X)\text{Var}(X) of Beta(α,β)\text{Beta}(\alpha, \beta)

Call both in sequence: first gamma_stats(2.0, 1.0), then beta_stats(2.0, 2.0).

Pyodide loading...
Loading...
Click "Run" to execute your code.