Skip to content
Tutorials Center
My learning

Topics

Lesson 22 of 68 ยท Integrals

Gamma and Beta Functions

Learn the Gamma and Beta functions: integral definitions, factorials, the half-integer values, the Beta-Gamma relation and how to evaluate tricky integrals in Python.

What is 3!? Six. What is 3.5!? The question sounds silly, since factorials are defined by multiplying whole numbers. But in physics, probability and engineering, formulas with factorials turn up where the argument is not a whole number. The Gamma function gives a smooth, natural way to extend the factorial to all positive real numbers (and beyond). Its close relative, the Beta function, packages a very common type of integral into a ratio of Gamma values.

This lesson defines both functions as integrals, derives the key identities, evaluates several "impossible-looking" integrals with them, and verifies everything numerically with SciPy. It builds on improper integrals, integration techniques and the Gaussian integral from multiple integrals. The library side is covered in SciPy special functions.

The Gamma function

For z > 0, define

Gamma(z) = integral from 0 to infinity of t**(z-1) * e**(-t) dt

This is an improper integral that converges for every z > 0: near t = 0 the integrand behaves like t**(z-1) and e**(-t) kills the growth at infinity.

Compute Gamma(1) directly: the integral of e**(-t) from 0 to infinity is 1. For other values, integration by parts gives the key recurrence

Gamma(z + 1) = z * Gamma(z)

(Take u = t**z and dv = e**(-t) dt; the boundary term vanishes and what remains is z * Gamma(z).)

Starting from Gamma(1) = 1, repeated use gives Gamma(2) = 1, Gamma(3) = 2, Gamma(4) = 6, and in general

Gamma(n + 1) = n! for non-negative integers n.

So Gamma really is the factorial, shifted by one, and for non-integers it interpolates smoothly between the factorial values. The recurrence also lets you extend the function to negative non-integers (Gamma(z) = Gamma(z + 1) / z), where it has poles at 0, -1, -2, ....

flowchart LR
    A["Gamma(z) = integral of<br/>t^(z-1) e^(-t) dt"] --> B["Gamma(z+1) = z Gamma(z)"]
    B --> C["Gamma(n+1) = n!"]
    A --> D["Gamma(1/2) = sqrt(pi)"]
    D --> E["half-integers:<br/>Gamma(3/2) = sqrt(pi)/2, ..."]

One definition gives factorials, the recurrence and the half-integer values.

The value at one half

Gamma(1/2) is special. Substitute t = u**2 to get Gamma(1/2) = 2 * integral from 0 to infinity of e**(-u**2) du, which is twice half of the Gaussian integral sqrt(pi)/2:

Gamma(1/2) = sqrt(pi), about 1.77245.

Then Gamma(3/2) = (1/2)*sqrt(pi), Gamma(5/2) = (3/4)*sqrt(pi) and so on. These values turn up whenever a Gaussian is integrated.

Python Gamma: factorials, half-integers and the recurrence

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last line shows the reflection formula Gamma(z) * Gamma(1 - z) = pi / sin(pi*z), a surprising link between Gamma and the sine.

Large factorials: Stirling

Computing 100! directly is a big number, but its logarithm is easy. Stirling's approximation says n! is close to sqrt(2*pi*n) * (n/e)**n. Libraries use gammaln, the logarithm of Gamma, to avoid overflow; see special functions.

The Beta function

For a, b > 0, the Beta function is

B(a, b) = integral from 0 to 1 of t**(a-1) * (1 - t)**(b-1) dt

It is symmetric: B(a, b) = B(b, a) (substitute t -> 1 - t). Integrals of exactly this shape come up in probability (the Beta distribution), in mechanics and in many applied problems. The remarkable fact is its link to Gamma:

B(a, b) = Gamma(a) * Gamma(b) / Gamma(a + b)

The proof multiplies two Gamma integrals into a double integral and changes variables (compare change of variables). You do not need the proof to use the result.

For integer arguments this gives the familiar fraction: B(2, 3) = Gamma(2) * Gamma(3) / Gamma(5) = 1 * 2 / 24 = 1/12. Check directly: the integral of t*(1-t)**2 over [0, 1] is 1/2 - 2/3 + 1/4 = 1/12.

Python Beta function: definition and the Gamma formula

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice B(1/2, 1/2) = pi: with Gamma(1/2)**2 = pi and Gamma(1) = 1, the formula gives it immediately.

Using them to evaluate integrals

Trigonometric integrals

Substituting t = sin(x)**2 in the Beta integral gives

integral from 0 to pi/2 of sin(x)**p * cos(x)**q dx = (1/2) * B((p+1)/2, (q+1)/2)

Take p = 4, q = 2. Then the integral is (1/2) * B(5/2, 3/2) = (1/2) * Gamma(5/2) * Gamma(3/2) / Gamma(4). With Gamma(5/2) = 3*sqrt(pi)/4, Gamma(3/2) = sqrt(pi)/2 and Gamma(4) = 6, the product is 3*pi/8, divided by 6 gives pi/16, and half of that is pi/32, about 0.09817.

Gaussian-type integrals

Substituting t = x**2 gives

integral from 0 to infinity of x**k * e**(-x**2) dx = (1/2) * Gamma((k + 1)/2)

For k = 0 this is (1/2)*sqrt(pi), the Gaussian integral. For k = 3, you get (1/2)*Gamma(2) = 1/2.

More general: integral from 0 to infinity of x**(s-1) * e**(-a*x) dx = Gamma(s) / a**s, handy for Laplace transforms of powers: the transform of t**n is n! / s**(n+1), and for non-integer powers Gamma(n+1) / s**(n+1).

Python Evaluating integrals with Gamma and Beta

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

All five numeric integrals match the formulas to the digits shown. For Stirling at n = 20, the approximation is already within about 0.4 percent.

When an integral has the shape x**(something) * e**(-x**2) or sin**p * cos**q or t**(a-1) * (1-t)**(b-1), try the substitution that turns it into Gamma or Beta before attempting integration by parts repeatedly.

Where you will meet them

  • Probability: the Gamma distribution and Beta distribution are named after these functions. Their normalization constants are exactly Gamma and B.
  • Chi-squared and Student's t tests involve Gamma(n/2).
  • Physics: the volume of a ball in n dimensions is pi**(n/2) / Gamma(n/2 + 1).
  • Combinatorics: the binomial coefficient n! / (k! (n-k)!) becomes Gamma(n+1) / (Gamma(k+1) * Gamma(n-k+1)), which defines it for non-integer arguments.

Common mistakes

  • Forgetting the shift: Gamma(n) equals (n-1)!, not n!.
  • Using the Gamma recurrence at zero or negative integers, where Gamma is infinite.
  • Applying the trigonometric Beta formula with p and q swapped in the arguments: the arguments are (p+1)/2 and (q+1)/2.
  • Losing the factor 1/2 that comes from the substitution t = x**2 or t = sin(x)**2.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is Gamma(5)?

    Gamma(n + 1) = n!, so Gamma(5) = 4! = 24.
  2. 2. What is Gamma(1/2)?

    Substituting t = u^2 turns the definition into twice the half-line Gaussian integral, which is sqrt(pi).
  3. 3. Which identity connects the Beta function with Gamma?

    The Beta integral equals the product of the two Gamma values divided by Gamma of the sum of the arguments.
  4. 4. The value of B(2, 3) is...

    B(2, 3) = Gamma(2) Gamma(3) / Gamma(5) = 1 * 2 / 24 = 1/12, which matches the integral of t (1 - t)^2 on [0, 1].
  5. 5. What does the recurrence Gamma(z + 1) = z Gamma(z) let you do?

    Applying the recurrence repeatedly shifts the argument by whole numbers, so only values between 0 and 1 are really new. It also fails at 0, where Gamma has a pole.

Summary

  • Gamma(z) = integral of t**(z-1) * e**(-t) dt satisfies Gamma(z+1) = z*Gamma(z) and Gamma(n+1) = n!.
  • Gamma(1/2) = sqrt(pi); half-integer values follow from the recurrence; reflection: Gamma(z)*Gamma(1-z) = pi / sin(pi*z).
  • B(a, b) = integral of t**(a-1) * (1-t)**(b-1) dt = Gamma(a)*Gamma(b) / Gamma(a+b), symmetric in a and b.
  • Useful forms: integral of sin**p * cos**q over [0, pi/2] = B((p+1)/2, (q+1)/2) / 2 and integral of x**k * e**(-x**2) over [0, inf) = Gamma((k+1)/2) / 2.
  • scipy.special.gamma, beta, gammaln compute them reliably.

Next: Change of Variables in Multiple Integrals, where the Jacobian does the heavy lifting.